define(["dojo/Evented", "dojo/_base/declare", "dojo/_base/lang", "dijit/_WidgetBase", "dojo/on", "dojo/dom", "dojo/dom-class", "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/Deferred", "dojo/window"], function (
Evented, declare, lang, _WidgetBase, on, dom, domClass, BorderContainer, ContentPane, Deferred, win) {
var Widget = declare("application.Drawer", [_WidgetBase, Evented], {
options: {
showDrawerSize: 850,
borderContainer: null,
contentPaneCenter: null,
contentPaneSide: null,
toggleButton: null,
mapResizeTimeout: 300,
mapResizeStepTimeout: 25
},
constructor: function (options) {
var defaults = lang.mixin({}, this.options, options);
this.set("showDrawerSize", defaults.showDrawerSize);
this.set("borderContainer", defaults.borderContainer);
this.set("contentPaneCenter", defaults.contentPaneCenter);
this.set("contentPaneSide", defaults.contentPaneSide);
this.set("toggleButton", defaults.toggleButton);
this.set("mapResizeTimeout", defaults.mapResizeTimeout);
this.set("mapResizeStepTimeout", defaults.mapResizeStepTimeout);
this.css = {
toggleButton: "toggle-grey",
toggleButtonSelected: "toggle-grey-on",
drawerOpen: "drawer-open",
drawerOpenComplete: "drawer-open-complete"
};
},
startup: function () {
this._init();
},
destroy: function () {
this._removeEvents();
this.inherited(arguments);
},
resize: function () {
if (this._borderContainer) {
this._borderContainer.layout();
}
this.emit("resize", {});
},
toggle: function (add) {
var def = new Deferred();
var currentlyOpen = domClass.contains(document.body, this.css.drawerOpen);
if ((currentlyOpen && add === true) || (!currentlyOpen && add === false)) {
return def.promise;
}
var nowOpen;
if (typeof add !== "undefined") {
nowOpen = domClass.toggle(document.body, this.css.drawerOpen, add);
} else {
nowOpen = domClass.toggle(document.body, this.css.drawerOpen, !currentlyOpen);
}
domClass.remove(document.body, this.css.drawerOpenComplete);
if (this._animationSteps) {
clearInterval(this._animationSteps);
this._animationSteps = null;
}
this._animationSteps = setInterval(lang.hitch(this, function () {
this.resize();
}), this.get("mapResizeStepTimeout"));
if (this._animationTimeout) {
clearTimeout(this._animationTimeout);
this._animationTimeout = null;
}
this._animationTimeout = setTimeout(lang.hitch(this, function () {
this._checkDrawerStatus();
clearInterval(this._animationSteps);
this._animationSteps = null;
if (nowOpen) {
domClass.add(document.body, this.css.drawerOpenComplete);
}
def.resolve();
}), this.get("mapResizeTimeout"));
return def.promise;
},
_removeEvents: function () {
if (this._events && this._events.length) {
for (var i = 0; i < this._events.length; i++) {
this._events[i].remove();
}
}
this._events = [];
if (this._contentPaneCenter) {
this._contentPaneCenter.destroy();
}
if (this._contentPaneSide) {
this._contentPaneSide.destroy();
}
if (this._borderContainer) {
this._borderContainer.destroy();
}
},
_init: function () {
this._removeEvents();
this._borderContainerNode = dom.byId(this.get("borderContainer"));
this._contentPaneCenterNode = dom.byId(this.get("contentPaneCenter"));
this._contentPaneSideNode = dom.byId(this.get("contentPaneSide"));
this._toggleNode = dom.byId(this.get("toggleButton"));
if (this._borderContainerNode && this._contentPaneCenterNode && this._contentPaneSideNode && this._toggleNode) {
this._borderContainer = new BorderContainer({
design: "sidebar",
gutters: false
}, this._borderContainerNode);
this._contentPaneCenter = new ContentPane({
region: "center",
style: {
padding: 0
}
}, this._contentPaneCenterNode);
this._borderContainer.addChild(this._contentPaneCenter);
this._contentPaneSide = new ContentPane({
region: "leading",
style: {
padding: 0
}
}, this._contentPaneSideNode);
this._borderContainer.addChild(this._contentPaneSide);
this._borderContainer.startup();
var toggleClick = on(this._toggleNode, "click", lang.hitch(this, function () {
this.toggle();
}));
this._events.push(toggleClick);
var w = win.get(document);
var winResize = on(w, "resize", lang.hitch(this, function () {
this._windowResized();
}));
this._events.push(winResize);
var winFocus = on(w, "focus", lang.hitch(this, function () {
setTimeout(lang.hitch(this, function () {
this.resize();
}), 250);
}));
this._events.push(winFocus);
this._windowResized();
this.resize();
this.set("loaded", true);
this.emit("load", {});
} else {
console.log("Drawer::Missing required node");
}
},
_windowResized: function () {
var vs = win.getBox(),
add;
if (vs.w < this.get("showDrawerSize")) {
add = false;
} else {
add = true;
}
this.toggle(add).always(lang.hitch(this, function () {
this._checkDrawerStatus();
}));
},
_checkDrawerStatus: function () {
this.resize();
this._toggleButton();
},
_toggleButton: function () {
var search = dom.byId("search");
if (domClass.contains(document.body, this.css.drawerOpen)) {
if (domClass.contains(this._toggleNode, this.css.toggleButton)) {
domClass.replace(this._toggleNode, this.css.toggleButtonSelected, this.css.toggleButton);
}
if (search) {
domClass.add(search, "open-drawer");
}
} else {
if (domClass.contains(this._toggleNode, this.css.toggleButtonSelected)) {
domClass.replace(this._toggleNode, this.css.toggleButton, this.css.toggleButtonSelected);
}
if (search) {
domClass.remove(search, "open-drawer");
}
}
}
});
return Widget;
});