aurelia-kendoui-bridge
Version:
A set of Telerik KendoUI wrappers for Aurelia allowing developers to easily use KendoUI components in their Aurelia application.
104 lines (88 loc) • 3.53 kB
JavaScript
import {inject, Container} from 'aurelia-dependency-injection';
import {customElement} from 'aurelia-templating';
import {WidgetBase} from '../common/widget-base';
import {generateBindables} from '../common/decorators';
import {constants} from '../common/constants';
import {OptionsBuilder} from '../common/options-builder';
import {TemplateGatherer} from '../common/template-gatherer';
import {PDF} from '../pdf/pdf'; //eslint-disable-line no-unused-vars
export class Grid {
constructor(element, widgetBase, container, optionsBuilder, templateGatherer) {
this.element = element;
this.templateGatherer = templateGatherer;
this.optionsBuilder = optionsBuilder;
this.widgetBase = widgetBase
.control('kendoGrid')
.useRootElement(this.element)
.beforeInitialize(options => this._beforeInitialize(options))
.linkViewModel(this)
.useContainer(container);
}
subscribe(event, callback) {
return this.widgetBase.subscribe(event, callback);
}
bind(ctx, overrideCtx) {
this.widgetBase.useParentCtx(overrideCtx);
}
attached() {
// if <table> exists, initialize on that
// if <div> exists, initialize on that
// if neither exists, create <div>
// grid needs to be initialized on a div https://github.com/aurelia-ui-toolkits/aurelia-kendoui-bridge/issues/358
if (isInitFromDiv(this.element)) {
this.widgetBase.useElement(this.element.querySelectorAll('div')[0]);
} else if (isInitFromTable(this.element)) {
this.widgetBase.useElement(this.element.children[0]);
} else {
let target = document.createElement('div');
this.element.appendChild(target);
this.widgetBase.useElement(target);
}
if (!this.kNoInit) {
this.recreate();
}
}
recreate() {
let templates = this.widgetBase.util.getChildrenVMs(this.element, `${constants.elementPrefix}template`);
this.templateGatherer.useTemplates(this, 'kendoGrid', templates);
this.kWidget = this.widgetBase.recreate();
}
_beforeInitialize(options) {
let columns = this.widgetBase.util.getChildrenVMs(this.element, `${constants.elementPrefix}col`);
let gridToolbars = this.widgetBase.util.getChildrenVMs(this.element, `${constants.elementPrefix}grid-toolbar`);
// allow for both column definitions via HTML and via an array of columns
if (columns && columns.length > 0) {
options.columns = [];
columns.forEach(column => {
options.columns.push(this.optionsBuilder.getOptions(column, 'GridColumn'));
});
}
if (gridToolbars && gridToolbars.length > 0) {
let toolbar = gridToolbars[0];
let o = this.optionsBuilder.getOptions(toolbar, 'GridToolbarItem');
if (o.template) {
options.toolbar = o.template;
} else {
options.toolbar = o;
}
}
}
destroy() {
this.widgetBase.destroy(this.kWidget);
}
detached() {
this.destroy();
}
}
// if the first child node is a table tag
// then the user wants to initialize the Kendo Grid from an
// existing table
function isInitFromTable(element) {
return element.children.length > 0 && element.children[0].nodeName === 'TABLE';
}
function isInitFromDiv(element) {
return element.querySelectorAll('div').length > 0;
}