@progress/kendo-ui
Version:
This package is part of the [Kendo UI for jQuery](http://www.telerik.com/kendo-ui) suite.
80 lines (66 loc) • 2.21 kB
JavaScript
;
const listViewMessageProps = ['loadMoreText', 'pullTemplate', 'releaseTemplate', 'refreshTemplate'];
const scrollerMessageProps = ['pullTemplate', 'releaseTemplate', 'refreshTemplate'];
function transformMessagesNesting(root, j, widgetName, messageProps) {
let changed = false;
root.find(j.CallExpression)
.filter(path => {
const callee = path.value.callee;
return (
j.MemberExpression.check(callee) &&
callee.property.name === widgetName
);
})
.forEach(path => {
const args = path.value.arguments;
args.forEach(arg => {
if (!j.ObjectExpression.check(arg)) {
return;
}
const propsToMove = [];
const propsToKeep = [];
arg.properties.forEach(prop => {
if (!j.Property.check(prop)) {
propsToKeep.push(prop);
return;
}
const key = prop.key.name || prop.key.value;
if (messageProps.includes(key)) {
propsToMove.push(prop);
} else {
propsToKeep.push(prop);
}
});
if (propsToMove.length === 0) {
return;
}
const existingMessages = propsToKeep.find(p =>
j.Property.check(p) &&
(p.key.name === 'messages' || p.key.value === 'messages')
);
if (existingMessages && j.ObjectExpression.check(existingMessages.value)) {
propsToMove.forEach(p => existingMessages.value.properties.push(p));
} else {
const messagesObj = j.objectExpression(propsToMove);
propsToKeep.push(
j.property('init', j.identifier('messages'), messagesObj)
);
}
arg.properties = propsToKeep;
changed = true;
});
});
return changed;
}
function transformListViewMessages(root, j) {
return transformMessagesNesting(root, j, 'kendoMobileListView', listViewMessageProps);
}
function transformScrollerMessages(root, j) {
return transformMessagesNesting(root, j, 'kendoMobileScroller', scrollerMessageProps);
}
module.exports = {
listViewMessageProps,
scrollerMessageProps,
transformListViewMessages,
transformScrollerMessages,
};