mdb-ui-kit/js/baseLayout.js

125 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-08-11 17:59:31 +03:00
import Base from "./base";
import Util from "./util";
2017-08-11 17:59:31 +03:00
const BaseLayout = ($ => {
const ClassName = {
2017-08-11 17:59:31 +03:00
CANVAS: "bmd-layout-canvas",
CONTAINER: "bmd-layout-container",
BACKDROP: `bmd-layout-backdrop`
2017-08-11 17:59:31 +03:00
};
const Selector = {
CANVAS: `.${ClassName.CANVAS}`,
CONTAINER: `.${ClassName.CONTAINER}`,
BACKDROP: `.${ClassName.BACKDROP}`
2017-08-11 17:59:31 +03:00
};
const Default = {
canvas: {
create: true,
required: true,
template: `<div class="${ClassName.CANVAS}"></div>`
},
backdrop: {
create: true,
required: true,
template: `<div class="${ClassName.BACKDROP}"></div>`
}
2017-08-11 17:59:31 +03:00
};
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class BaseLayout extends Base {
constructor($element, config, properties = {}) {
2017-08-11 17:59:31 +03:00
super($element, $.extend(true, {}, Default, config), properties);
2017-08-11 17:59:31 +03:00
this.$container = this.findContainer(true);
this.$backdrop = this.resolveBackdrop();
this.resolveCanvas();
}
dispose(dataKey) {
2017-08-11 17:59:31 +03:00
super.dispose(dataKey);
this.$container = null;
this.$backdrop = null;
}
// ------------------------------------------------------------------------
// protected
// Will wrap container in bmd-layout-canvas if necessary
resolveCanvas() {
2017-08-11 17:59:31 +03:00
let bd = this.findCanvas(false);
if (bd === undefined || bd.length === 0) {
if (this.config.canvas.create) {
2017-08-11 17:59:31 +03:00
this.$container.wrap(this.config.canvas.template);
}
2017-08-11 17:59:31 +03:00
bd = this.findCanvas(this.config.canvas.required);
}
2017-08-11 17:59:31 +03:00
return bd;
}
// Find closest bmd-layout-container based on the given context
findCanvas(raiseError = true, context = this.$container) {
2017-08-11 17:59:31 +03:00
let canvas = context.closest(Selector.CANVAS);
if (canvas.length === 0 && raiseError) {
2017-08-11 17:59:31 +03:00
$.error(
`Failed to find ${Selector.CANVAS} for ${Util.describe(context)}`
);
}
2017-08-11 17:59:31 +03:00
return canvas;
}
// Will add bmd-layout-backdrop to bmd-layout-container if necessary
resolveBackdrop() {
2017-08-11 17:59:31 +03:00
let bd = this.findBackdrop(false);
if (bd === undefined || bd.length === 0) {
if (this.config.backdrop.create) {
2017-08-11 17:59:31 +03:00
this.$container.append(this.config.backdrop.template);
}
2017-08-11 17:59:31 +03:00
bd = this.findBackdrop(this.config.backdrop.required);
}
2017-08-11 17:59:31 +03:00
return bd;
}
// Find closest bmd-layout-container based on the given context
findBackdrop(raiseError = true, context = this.$container) {
2017-08-11 17:59:31 +03:00
let backdrop = context.find(`> ${Selector.BACKDROP}`);
if (backdrop.length === 0 && raiseError) {
2017-08-11 17:59:31 +03:00
$.error(
`Failed to find ${Selector.BACKDROP} for ${Util.describe(context)}`
);
}
2017-08-11 17:59:31 +03:00
return backdrop;
}
// Find closest bmd-layout-container based on the given context
findContainer(raiseError = true, context = this.$element) {
2017-08-11 17:59:31 +03:00
let container = context.closest(Selector.CONTAINER);
if (container.length === 0 && raiseError) {
2017-08-11 17:59:31 +03:00
$.error(
`Failed to find ${Selector.CONTAINER} for ${Util.describe(context)}`
);
}
2017-08-11 17:59:31 +03:00
return container;
}
// ------------------------------------------------------------------------
// private
// ------------------------------------------------------------------------
// static
}
2017-08-11 17:59:31 +03:00
return BaseLayout;
})(jQuery);
2017-08-11 17:59:31 +03:00
export default BaseLayout;