@progress/kendo-base-components-vue-wrapper
Version:
Kendo UI Base Component wrapper for Vue.js
464 lines (418 loc) • 18.4 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _vue = require('vue');
var aVue = _interopRequireWildcard(_vue);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
const allVue = aVue;
const gh = allVue.h;
const isV3 = allVue.version && allVue.version[0] === '3';
const createApp = allVue.createApp;
exports.default = {
name: 'baseComponent',
props: {
disabled: {
type: Boolean,
default: undefined
},
readonly: {
type: Boolean,
default: undefined
}
},
render(createElement) {
const h = gh || createElement;
return h('div');
},
created() {
this.createdMethod();
},
beforeDestroy: gh ? undefined : function () {
this.destroyKendoWidget();
},
beforeUnmount() {
this.destroyKendoWidget();
},
setup() {
const v3 = isV3;
return {
v3
};
},
beforeMount: function beforeMount() {
if (this.v3) {
this.createdMethod();
const defaultSlot = this.v3 && this.$slots.default && typeof this.$slots.default === 'function' ? this.$slots.default() : this.$slots.default;
const hasDataSource = !this.$props.dataSource;
if (hasDataSource && defaultSlot) {
var innerDatasource = defaultSlot.find(slot => slot.type && slot.type.name && slot.type.name.indexOf('datasource') !== -1);
if (innerDatasource) {
var dataSourceOptions = this.parseOptions(innerDatasource, innerDatasource.type.mixins[0].mixins);
this.widgetOptions.dataSource = dataSourceOptions;
}
}
}
},
updated() {
if (this.modelValue) {
this.changeValue(this.modelValue);
}
},
mounted() {
if (this.$el.classList && this.$el.classList.length > 0) {
this.nativeClasses = [...this.$el.classList];
} else {
this.nativeClasses = [];
}
const props = this.v3 ? this.$props : this.$options.propsData;
for (var key in props) {
var that = this;
if (key.toLowerCase().indexOf('template') !== -1) {
var isFunction = this.widgetOptions[key] instanceof Function;
if (isFunction) {
this.widgetOptions[key] = this.transformTemplate(key);
}
}
if (key === 'value') {
that.$watch(key, function (newValue) {
that.changeValue(newValue);
});
} else if (key === 'disabled') {
that.$watch(key, function (newValue) {
that.makeDisabled(newValue);
});
} else if (key === 'readonly') {
that.$watch(key, function (newValue) {
that.makeReadonly(newValue);
});
} else {
that.$watch(key, function (newValue, oldValue) {
// Vue always dispatches a change when inline complex objects are bound
// https://github.com/telerik/kendo-ui-core/issues/3952
if (JSON.stringify(oldValue) !== JSON.stringify(newValue)) {
that.updateWidget();
}
});
}
}
},
watch: {
$attrs() {
this.toggleClasses();
}
},
methods: {
createdMethod() {
this.resolveWidgetOptions();
this.$_nativeTemplates = [];
if (!this.v3) {
this.$on('kendowidgetready', this.ready);
}
},
destroyKendoWidget() {
if (this.kendoWidget() && this.kendoWidget().destroy) {
this.kendoWidget().destroy();
if (this.$_nativeTemplates.length) {
for (var i = 0; i < this.$_nativeTemplates.length; i++) {
var currentTemplate = this.$_nativeTemplates[i];
if (currentTemplate.unmount) {
currentTemplate.unmount();
} else {
currentTemplate.$destroy();
}
}
}
}
},
toggleClasses() {
var that = this;
var $element = kendo.jQuery(that.$el);
var $wrapper = that.kendoWidget().wrapper;
if ($wrapper && $wrapper[0] !== $element[0]) {
that.nativeClasses.forEach(function (item) {
$wrapper.removeClass(item);
});
// Add to wrapper only the custom classes without the default Kendo ones
if (that.kendoClasses) {
that.nativeClasses = [...that.$el.classList].filter(item => {
return that.kendoClasses.indexOf(item) < 0;
});
} else {
that.nativeClasses = [...that.$el.classList];
}
that.nativeClasses.forEach(function (item) {
$wrapper.addClass(item);
});
}
if (that.kendoClasses) {
that.kendoClasses.forEach(function (item) {
$element.addClass(item);
});
}
},
updateWidget() {
var that = this;
that.resolveWidgetOptions();
if (that._resolveChildren) {
that._resolveChildren();
}
var currentWidget = that.kendoWidget();
if (currentWidget && currentWidget.setOptions) {
currentWidget.setOptions(that.widgetOptions);
}
},
transformTemplate: function (key, val) {
var that = this;
var object;
const props = that.v3 ? that.$props : that.$options.propsData;
var templateDefinition = val || props[key];
try {
object = templateDefinition.call(that, {});
} catch (e) {
return templateDefinition;
}
if (!object.template) {
return templateDefinition;
}
return function () {
object = templateDefinition.apply(this, arguments);
var kendoguid;
if (that.v3) {
var newTemplateArgs = typeof object.templateArgs === 'string' ? object.templateArgs : Object.assign({}, object.templateArgs);
var tempComponent = (0, createApp)(Object.assign({}, object.template, {
data: function () {
return Object.assign({}, object.template.data ? object.template.data() : {}, {
templateArgs: newTemplateArgs
});
}
}));
kendoguid = 'kendo' + kendo.guid();
that.$nextTick(function () {
if (document.getElementById(kendoguid)) {
tempComponent.mount('#' + kendoguid);
that.$_nativeTemplates.push(tempComponent);
}
});
} else {
var newVue = allVue.default.extend(object);
var vueObject = new newVue(object.template);
vueObject.$data.templateArgs = object.templateArgs;
kendoguid = 'kendo' + kendo.guid();
that.$nextTick(function () {
if (document.getElementById(kendoguid)) {
vueObject.$mount('#' + kendoguid);
that.$_nativeTemplates.push(vueObject);
}
});
}
return '<div id="' + kendoguid + '"></div>';
};
},
resolveChildren(prop, name) {
const defaultSlot = this.v3 && this.$slots.default && typeof this.$slots.default === 'function' ? this.$slots.default() : this.$slots.default;
if (!this.widgetOptions[prop] && defaultSlot) {
var items = [];
for (const childSlot of defaultSlot) {
if (childSlot.tag && childSlot.tag.indexOf(name) !== -1 || childSlot.type && childSlot.type.name && childSlot.type.name.toLowerCase().indexOf(name) !== -1) {
var childInstance = this.v3 ? childSlot : childSlot.componentInstance;
var itemOptions = this.parseOptions(childInstance);
if (itemOptions.dataSourceRef) {
this.setInnerDataSource('dataSourceRef', 'dataSource', itemOptions);
}
items.push(itemOptions);
this.handleWatcher(childInstance);
}
}
if (items.length) {
this.widgetOptions[prop] = items;
}
}
},
handleWatcher(childInstance) {
var that = this;
childInstance.stashedPropsData = JSON.stringify(that.v3 ? childInstance.$props : childInstance.$options.propsData);
if (!that.v3 && !childInstance._isWatchAttached) {
childInstance.$watch('$props', () => {
const props = that.v3 ? childInstance.$props : childInstance.$options.propsData;
var propsAsJson = JSON.stringify(props);
var needsUpdate = childInstance.stashedPropsData !== propsAsJson;
if (that.updateWidget && needsUpdate) {
that.updateWidget();
} else if (that._resolveInnerChildren) {
that._resolveInnerChildren();
}
childInstance.stashedPropsData = propsAsJson;
}, { deep: true });
childInstance._isWatchAttached = true;
}
},
makeDisabled(toDisable) {
if (this.kendoWidget().enable) {
this.kendoWidget().enable(!toDisable);
}
},
makeReadonly(value) {
if (this.kendoWidget().readonly) {
this.kendoWidget().readonly(value);
}
},
changeValue(newValue) {
if (this.kendoWidget().value) {
this.kendoWidget().value(newValue);
}
},
resolveWidgetOptions() {
this.widgetOptions = this.parseOptions();
},
getListeners() {
if (this.v3) {
let listeners = {};
for (let key in this.$attrs) {
if (key.startsWith('on')) {
listeners[key] = this.$attrs[key];
}
}
return listeners;
} else {
return this.$listeners;
}
},
parseOptions(component, defaultMixins) {
var that = component || this;
var options = {};
const props = this.v3 ? component ? that.props : that.$props : that.$options.propsData;
for (let key in props) {
var propOptions = this.v3 ? component ? undefined : (that.$options.__props ? that.$options.__props[0][key] : that.$options.props[key]) || that.$options.__props[0][0][key] : that.$options.props[key];
var val = props[key];
if (this.v3 && key.indexOf('-') !== -1) {
key = this.camelize(key);
}
var compositeProps = propOptions ? propOptions.kComposite : undefined;
if (that.v3 || this.v3) {
const mixins = component ? defaultMixins || component.type.mixins : that.$options.mixins;
if (mixins) {
mixins.forEach(function (mixin) {
if (mixin.props && mixin.name !== 'baseComponent' && mixin.name !== 'baseDataSourceComponent' && mixin.props[key] !== undefined && (that.$props ? that.$props[key] !== undefined : true)) {
compositeProps = mixin.props[key].kComposite;
}
});
}
}
if (!compositeProps) {
if (key.toLowerCase().indexOf('template') !== -1 && val instanceof Function) {
options[key] = this.transformTemplate(key, val);
} else {
if (val !== undefined) {
options[key] = val;
}
}
} else {
if (val !== undefined) {
this.addCompositeProperty(options, compositeProps.split('.'), val);
}
}
}
if (that.getListeners) {
if (this.v3) {
Object.keys(that.$attrs).forEach(event => {
var listeners = that.getListeners();
var eventName = this.v3 ? event : event.toLowerCase();
if (listeners && listeners[eventName]) {
var kendoIndex = event.lastIndexOf('kendo');
var kendoEvent;
if (kendoIndex !== -1) {
kendoEvent = event.replace('kendo', '').toLowerCase();
}
if (this.v3 && event.indexOf('onKendo') !== -1) {
kendoEvent = event.replace('onKendo', '').toLowerCase();
}
const kendoEventProp = Object.keys(that.$props).find(e => e.toLowerCase() === this.removeOn(event));
options[kendoEvent || kendoEventProp || this.removeOn(event)] = listeners[eventName];
}
});
} else {
Object.keys(that.$props).forEach(event => {
var listeners = that.getListeners();
if (listeners && listeners[event.toLowerCase()]) {
var kendoIndex = event.lastIndexOf('kendo');
var kendoEvent;
if (kendoIndex !== -1) {
kendoEvent = event.replace('kendo', '').toLowerCase();
}
options[kendoEvent || event] = listeners[event.toLowerCase()];
}
});
}
}
if (this.v3 && this.$props.modelValue) {
options.value = this.$props.modelValue;
}
return options;
},
addCompositeProperty(obj, keys, val) {
const lastKey = keys.pop();
const lastObj = keys.reduce(function (obj, key) {
obj[key] = typeof obj[key] === 'object' ? obj[key] || {} : {};
return obj[key];
}, obj);
if (lastKey.toLowerCase().indexOf('template') !== -1 && val instanceof Function) {
lastObj[lastKey] = this.transformTemplate(lastKey, val);
} else {
lastObj[lastKey] = val;
}
},
resolveInnerTags(tagName) {
var items = [];
const defaultSlot = this.v3 && this.$slots.default && typeof this.$slots.default === 'function' ? this.$slots.default() : this.$slots.default;
for (const childSlot of defaultSlot) {
if (childSlot.tag && childSlot.tag.indexOf(tagName) !== -1 || childSlot.type && childSlot.type.name && childSlot.type.name.toLowerCase().indexOf(tagName) !== -1) {
var childOptions = childSlot.componentOptions;
var childInstance = this.v3 ? childSlot : childSlot.componentInstance;
const item = this.v3 ? childInstance.$props || childInstance.props : childOptions.propsData;
if (this.v3) {
const itemKeys = Object.keys(item);
for (const propKey of itemKeys) {
if (propKey.indexOf('-') !== -1) {
item[this.camelize(propKey)] = item[propKey];
}
}
}
if (!item.items) {
item.items = childInstance.subitems;
}
items.push(item);
this.handleWatcher(childInstance);
}
}
return items;
},
ready() {
var that = this;
if (this.$el.classList.length > 0) {
that.kendoClasses = [...that.$el.classList].filter(item => {
return that.nativeClasses.indexOf(item) < 0;
});
}
const props = that.v3 ? that.$props : that.$options.propsData;
if (props && props.disabled) {
that.makeDisabled(true);
}
if (props && props.readonly) {
that.makeReadonly(true);
}
},
camelize(str) {
const arr = str.split('-');
const capital = arr.map((item, index) => index ? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() : item);
const capitalString = capital.join('');
return capitalString;
},
removeOn(str) {
if (str.indexOf('onKendo') !== -1) {
return str.replace('onKendo', '').toLowerCase();
}
return str.replace('on', '').toLowerCase();
}
}
};