Created
February 12, 2015 04:44
-
-
Save yelvert/0c66cb34f1d1ff6c3ea2 to your computer and use it in GitHub Desktop.
AuClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<import from="./class" as="au-class"></import> | |
<section> | |
<div au-class.bind="myClasses"> | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo | |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse | |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
</div> | |
<div au-class.bind="myOtherClasses"> | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo | |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse | |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
</div> | |
<div au-class.bind="{active: active, inactive: !active}"> | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo | |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse | |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
</div> | |
<button click.delegate="toggleActive()">Toggle active</button> | |
</section> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class App { | |
constructor() { | |
this.active = true; | |
} | |
toggleActive () { | |
this.active = !this.active; | |
} | |
get myClasses () { | |
return { | |
active: this.active, | |
inactive: !this.active | |
} | |
} | |
get myOtherClasses () { | |
this._myOtherClasses || (this._myOtherClasses = {}); | |
this._myOtherClasses.active = this.active; | |
this._myOtherClasses.inactive = !this.active; | |
return this._myOtherClasses; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Behavior} from 'aurelia-templating'; | |
export class AuClass { | |
static metadata(){ | |
return Behavior | |
.attachedBehavior('au-class') | |
.withProperty('value', 'valueChanged', 'au-class') | |
; | |
} | |
static inject() { return [Element]; } | |
constructor (element) { | |
this.element = element; | |
} | |
bind () { | |
this.setupObserver(); | |
this.updateClasses(); | |
} | |
updateClasses () { | |
Object.keys(this.value).forEach(className => { | |
this.element.classList[this.value[className] ? 'add' : 'remove'](className); | |
}); | |
} | |
valueChanged (newValue, oldValue) { | |
this.updateClasses() | |
if (!Object.is(newValue, oldValue)) { | |
this.setupObserver() | |
} | |
} | |
setupObserver () { | |
var self = this; | |
Object.observe(this.value, () => { | |
self.updateClasses() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment