aurelia-materialize-bridge
Version:
Aurelia interface to http://materializecss.com/
95 lines (77 loc) • 2.46 kB
text/typescript
import * as au from "../aurelia";
.customAttribute("md-modal")
.autoinject
export class MdModal {
constructor(private element: Element) {
this.log = au.getLogger("md-modal");
this.attributeManager = new au.AttributeManager(this.element);
}
log: au.Logger;
attributeManager: au.AttributeManager;
.ato.bindable.numberMd
opacity: number; // Opacity of modal background
.ato.bindable.numberMd
inDuration: number; // Transition in duration
.ato.bindable.numberMd
outDuration: number; // Transition out duration
.ato.bindable.booleanMd
preventScrolling: boolean;
.ato.bindable.booleanMd
dismissible: boolean;
.ato.bindable.stringMd
startingTop: string; // Starting top style attribute
.ato.bindable.stringMd
endingTop: string; // Ending top style attribute
.ato.bindable.booleanMd
fixedFooter: boolean;
fixedFooterChanged() {
if (this.element) {
this.element.classList.toggle("modal-fixed-footer", this.fixedFooter);
}
}
.ato.bindable.booleanMd
bottomSheet: boolean;
instance: M.Modal;
defaultActionElement: HTMLElement;
attached() {
const options: Partial<M.ModalOptions> = {
opacity: this.opacity,
inDuration: this.inDuration,
outDuration: this.outDuration,
preventScrolling: this.preventScrolling,
dismissible: this.dismissible,
startingTop: this.startingTop,
endingTop: this.endingTop,
onOpenStart: () => au.fireMaterializeEvent(this.element, "open-start"),
onOpenEnd: () => au.fireMaterializeEvent(this.element, "open-end"),
onCloseStart: () => au.fireMaterializeEvent(this.element, "close-start"),
onCloseEnd: () => au.fireMaterializeEvent(this.element, "close-end")
};
this.log.debug("modal options: ", options);
au.cleanOptions(options);
this.attributeManager.addClasses("modal");
this.instance = new M.Modal(this.element, options);
this.defaultActionElement = this.element.querySelector(".modal-action.default");
if (this.defaultActionElement) {
(this.element as HTMLElement).addEventListener("keydown", this.keydown);
}
}
keydown = (e: KeyboardEvent) => {
if (e.keyCode === 13) {
this.defaultActionElement.click();
}
}
detached() {
this.instance.destroy();
this.attributeManager.removeClasses("modal");
if (this.defaultActionElement) {
(this.element as HTMLElement).removeEventListener("keydown", this.keydown);
}
}
open() {
this.instance.open();
}
close() {
this.instance.close();
}
}