@progress/kendo-base-components-vue-wrapper
Version:
Kendo UI Base Component wrapper for Vue.js
555 lines (491 loc) • 22.9 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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; } }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var allVue = aVue;
var gh = allVue.h;
var isV3 = allVue.version && allVue.version[0] === '3';
var createApp = allVue.createApp;
exports.default = {
name: 'baseComponent',
props: {
disabled: {
type: Boolean,
default: undefined
},
readonly: {
type: Boolean,
default: undefined
}
},
render: function render(createElement) {
var h = gh || createElement;
return h('div');
},
created: function created() {
this.createdMethod();
},
beforeDestroy: gh ? undefined : function () {
this.destroyKendoWidget();
},
beforeUnmount: function beforeUnmount() {
this.destroyKendoWidget();
},
setup: function setup() {
var v3 = isV3;
return {
v3: v3
};
},
beforeMount: function beforeMount() {
if (this.v3) {
this.createdMethod();
var defaultSlot = this.v3 && this.$slots.default && typeof this.$slots.default === 'function' ? this.$slots.default() : this.$slots.default;
var hasDataSource = !this.$props.dataSource;
if (hasDataSource && defaultSlot) {
var innerDatasource = defaultSlot.find(function (slot) {
return 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: function updated() {
if (this.modelValue) {
this.changeValue(this.modelValue);
}
},
mounted: function mounted() {
if (this.$el.classList && this.$el.classList.length > 0) {
this.nativeClasses = [].concat(_toConsumableArray(this.$el.classList));
} else {
this.nativeClasses = [];
}
var 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: function $attrs() {
this.toggleClasses();
}
},
methods: {
createdMethod: function createdMethod() {
this.resolveWidgetOptions();
this.$_nativeTemplates = [];
if (!this.v3) {
this.$on('kendowidgetready', this.ready);
}
},
destroyKendoWidget: function 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: function 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 = [].concat(_toConsumableArray(that.$el.classList)).filter(function (item) {
return that.kendoClasses.indexOf(item) < 0;
});
} else {
that.nativeClasses = [].concat(_toConsumableArray(that.$el.classList));
}
that.nativeClasses.forEach(function (item) {
$wrapper.addClass(item);
});
}
if (that.kendoClasses) {
that.kendoClasses.forEach(function (item) {
$element.addClass(item);
});
}
},
updateWidget: function 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 transformTemplate(key, val) {
var that = this;
var object;
var 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 data() {
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: function resolveChildren(prop, name) {
var defaultSlot = this.v3 && this.$slots.default && typeof this.$slots.default === 'function' ? this.$slots.default() : this.$slots.default;
if (!this.widgetOptions[prop] && defaultSlot) {
var items = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = defaultSlot[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var childSlot = _step.value;
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);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
if (items.length) {
this.widgetOptions[prop] = items;
}
}
},
handleWatcher: function handleWatcher(childInstance) {
var that = this;
childInstance.stashedPropsData = JSON.stringify(that.v3 ? childInstance.$props : childInstance.$options.propsData);
if (!that.v3 && !childInstance._isWatchAttached) {
childInstance.$watch('$props', function () {
var 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: function makeDisabled(toDisable) {
if (this.kendoWidget().enable) {
this.kendoWidget().enable(!toDisable);
}
},
makeReadonly: function makeReadonly(value) {
if (this.kendoWidget().readonly) {
this.kendoWidget().readonly(value);
}
},
changeValue: function changeValue(newValue) {
if (this.kendoWidget().value) {
this.kendoWidget().value(newValue);
}
},
resolveWidgetOptions: function resolveWidgetOptions() {
this.widgetOptions = this.parseOptions();
},
getListeners: function getListeners() {
if (this.v3) {
var listeners = {};
for (var key in this.$attrs) {
if (key.startsWith('on')) {
listeners[key] = this.$attrs[key];
}
}
return listeners;
} else {
return this.$listeners;
}
},
parseOptions: function parseOptions(component, defaultMixins) {
var _this = this;
var that = component || this;
var options = {};
var props = this.v3 ? component ? that.props : that.$props : that.$options.propsData;
var _loop = function _loop(_key) {
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];
val = props[_key];
if (_this.v3 && _key.indexOf('-') !== -1) {
_key = _this.camelize(_key);
}
compositeProps = propOptions ? propOptions.kComposite : undefined;
if (that.v3 || _this.v3) {
var 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);
}
}
key = _key;
};
for (var key in props) {
var propOptions;
var val;
var compositeProps;
_loop(key);
}
if (that.getListeners) {
if (this.v3) {
Object.keys(that.$attrs).forEach(function (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();
}
var kendoEventProp = Object.keys(that.$props).find(function (e) {
return e.toLowerCase() === _this.removeOn(event);
});
options[kendoEvent || kendoEventProp || _this.removeOn(event)] = listeners[eventName];
}
});
} else {
Object.keys(that.$props).forEach(function (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: function addCompositeProperty(obj, keys, val) {
var lastKey = keys.pop();
var 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: function resolveInnerTags(tagName) {
var items = [];
var defaultSlot = this.v3 && this.$slots.default && typeof this.$slots.default === 'function' ? this.$slots.default() : this.$slots.default;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = defaultSlot[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var childSlot = _step2.value;
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;
var item = this.v3 ? childInstance.$props || childInstance.props : childOptions.propsData;
if (this.v3) {
var itemKeys = Object.keys(item);
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = itemKeys[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var propKey = _step3.value;
if (propKey.indexOf('-') !== -1) {
item[this.camelize(propKey)] = item[propKey];
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
if (!item.items) {
item.items = childInstance.subitems;
}
items.push(item);
this.handleWatcher(childInstance);
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return items;
},
ready: function ready() {
var that = this;
if (this.$el.classList.length > 0) {
that.kendoClasses = [].concat(_toConsumableArray(that.$el.classList)).filter(function (item) {
return that.nativeClasses.indexOf(item) < 0;
});
}
var props = that.v3 ? that.$props : that.$options.propsData;
if (props && props.disabled) {
that.makeDisabled(true);
}
if (props && props.readonly) {
that.makeReadonly(true);
}
},
camelize: function camelize(str) {
var arr = str.split('-');
var capital = arr.map(function (item, index) {
return index ? item.charAt(0).toUpperCase() + item.slice(1).toLowerCase() : item;
});
var capitalString = capital.join('');
return capitalString;
},
removeOn: function removeOn(str) {
if (str.indexOf('onKendo') !== -1) {
return str.replace('onKendo', '').toLowerCase();
}
return str.replace('on', '').toLowerCase();
}
}
};