mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
KnockoutJS remove unused beforeRemove
This commit is contained in:
parent
9e05aa4a50
commit
513fc949d7
4 changed files with 14 additions and 35 deletions
|
|
@ -17,8 +17,7 @@ ko.bindingHandlers['foreach'] = {
|
||||||
ko.utils.unwrapObservable(modelValue);
|
ko.utils.unwrapObservable(modelValue);
|
||||||
return {
|
return {
|
||||||
'foreach': unwrappedValue['data'],
|
'foreach': unwrappedValue['data'],
|
||||||
'as': unwrappedValue['as'],
|
'as': unwrappedValue['as']
|
||||||
'beforeRemove': unwrappedValue['beforeRemove']
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
'init': (element, valueAccessor) =>
|
'init': (element, valueAccessor) =>
|
||||||
|
|
|
||||||
|
|
@ -107,10 +107,6 @@ ko.bindingHandlers['options'] = {
|
||||||
return [option];
|
return [option];
|
||||||
};
|
};
|
||||||
|
|
||||||
// By using a beforeRemove callback, we delay the removal until after new items are added. This fixes a selection
|
|
||||||
// problem in IE<=8 and Firefox. See https://github.com/knockout/knockout/issues/1208
|
|
||||||
arrayToDomNodeChildrenOptions['beforeRemove'] = option => element.removeChild(option);
|
|
||||||
|
|
||||||
var callback = setSelectionCallback;
|
var callback = setSelectionCallback;
|
||||||
if (allBindings['has']('optionsAfterRender') && typeof allBindings.get('optionsAfterRender') == "function") {
|
if (allBindings['has']('optionsAfterRender') && typeof allBindings.get('optionsAfterRender') == "function") {
|
||||||
callback = (arrayEntry, newOptions) => {
|
callback = (arrayEntry, newOptions) => {
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,14 @@
|
||||||
// of which nodes would be deleted if valueToMap was itself later removed
|
// of which nodes would be deleted if valueToMap was itself later removed
|
||||||
mappedNodes.length = 0;
|
mappedNodes.length = 0;
|
||||||
mappedNodes.push(...newMappedNodes);
|
mappedNodes.push(...newMappedNodes);
|
||||||
}, { disposeWhenNodeIsRemoved: containerNode, disposeWhen: ()=>!!mappedNodes.find(ko.utils.domNodeIsAttachedToDocument) });
|
}, {
|
||||||
return { mappedNodes : mappedNodes, dependentObservable : (dependentObservable.isActive() ? dependentObservable : undefined) };
|
disposeWhenNodeIsRemoved: containerNode,
|
||||||
|
disposeWhen: ()=>!!mappedNodes.find(ko.utils.domNodeIsAttachedToDocument)
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
mappedNodes : mappedNodes,
|
||||||
|
dependentObservable : (dependentObservable.isActive() ? dependentObservable : undefined)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var lastMappingResultDomDataKey = ko.utils.domData.nextKey(),
|
var lastMappingResultDomDataKey = ko.utils.domData.nextKey(),
|
||||||
|
|
@ -70,12 +76,6 @@
|
||||||
mapData.indexObservable(currentArrayIndex++);
|
mapData.indexObservable(currentArrayIndex++);
|
||||||
ko.utils.fixUpContinuousNodeArray(mapData.mappedNodes, domNode);
|
ko.utils.fixUpContinuousNodeArray(mapData.mappedNodes, domNode);
|
||||||
newMappingResult.push(mapData);
|
newMappingResult.push(mapData);
|
||||||
},
|
|
||||||
|
|
||||||
callCallback = (callback, items) => {
|
|
||||||
if (callback) {
|
|
||||||
items.forEach(item => item?.mappedNodes.forEach(node => callback(node, i, item.arrayEntry)));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isFirstExecution) {
|
if (isFirstExecution) {
|
||||||
|
|
@ -112,15 +112,6 @@
|
||||||
|
|
||||||
// Queue these nodes for later removal
|
// Queue these nodes for later removal
|
||||||
if (ko.utils.fixUpContinuousNodeArray(mapData.mappedNodes, domNode).length) {
|
if (ko.utils.fixUpContinuousNodeArray(mapData.mappedNodes, domNode).length) {
|
||||||
if (options['beforeRemove']) {
|
|
||||||
newMappingResult.push(mapData);
|
|
||||||
countWaitingForRemove++;
|
|
||||||
if (mapData.arrayEntry === deletedItemDummyValue) {
|
|
||||||
mapData = null;
|
|
||||||
} else {
|
|
||||||
itemsForBeforeRemoveCallbacks[mapData.indexObservable.peek()] = mapData;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (mapData) {
|
if (mapData) {
|
||||||
nodesToDelete.push.apply(nodesToDelete, mapData.mappedNodes);
|
nodesToDelete.push.apply(nodesToDelete, mapData.mappedNodes);
|
||||||
}
|
}
|
||||||
|
|
@ -155,8 +146,8 @@
|
||||||
// Store a copy of the array items we just considered so we can difference it next time
|
// Store a copy of the array items we just considered so we can difference it next time
|
||||||
ko.utils.domData.set(domNode, lastMappingResultDomDataKey, newMappingResult);
|
ko.utils.domData.set(domNode, lastMappingResultDomDataKey, newMappingResult);
|
||||||
|
|
||||||
// Next remove nodes for deleted items (or just clean if there's a beforeRemove callback)
|
// Next remove nodes for deleted items
|
||||||
nodesToDelete.forEach(options['beforeRemove'] ? ko.cleanNode : ko.removeNode);
|
nodesToDelete.forEach(ko.removeNode);
|
||||||
|
|
||||||
var i, lastNode, mappedNodes, activeElement,
|
var i, lastNode, mappedNodes, activeElement,
|
||||||
insertNode = nodeToInsert => {
|
insertNode = nodeToInsert => {
|
||||||
|
|
@ -183,7 +174,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next add/reorder the remaining items (will include deleted items if there's a beforeRemove callback)
|
// Next add/reorder the remaining items
|
||||||
newMappingResult.forEach(mapData => {
|
newMappingResult.forEach(mapData => {
|
||||||
// Get nodes for newly added items
|
// Get nodes for newly added items
|
||||||
mapData.mappedNodes
|
mapData.mappedNodes
|
||||||
|
|
@ -205,15 +196,8 @@
|
||||||
activeElement?.focus();
|
activeElement?.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there's a beforeRemove callback, call it after reordering.
|
|
||||||
// Note that we assume that the beforeRemove callback will usually be used to remove the nodes using
|
|
||||||
// some sort of animation, which is why we first reorder the nodes that will be removed. If the
|
|
||||||
// callback instead removes the nodes right away, it would be more efficient to skip reordering them.
|
|
||||||
// Perhaps we'll make that change in the future if this scenario becomes more common.
|
|
||||||
callCallback(options['beforeRemove'], itemsForBeforeRemoveCallbacks);
|
|
||||||
|
|
||||||
// Replace the stored values of deleted items with a dummy value. This provides two benefits: it marks this item
|
// Replace the stored values of deleted items with a dummy value. This provides two benefits: it marks this item
|
||||||
// as already "removed" so we won't call beforeRemove for it again, and it ensures that the item won't match up
|
// as already "removed", and it ensures that the item won't match up
|
||||||
// with an actual item in the array and appear as "retained" or "moved".
|
// with an actual item in the array and appear as "retained" or "moved".
|
||||||
itemsForBeforeRemoveCallbacks.forEach(callback => callback && (callback.arrayEntry = deletedItemDummyValue));
|
itemsForBeforeRemoveCallbacks.forEach(callback => callback && (callback.arrayEntry = deletedItemDummyValue));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -165,7 +165,7 @@
|
||||||
ko.bindingEvent.notify(targetNode, ko.bindingEvent.childrenComplete);
|
ko.bindingEvent.notify(targetNode, ko.bindingEvent.childrenComplete);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!options['beforeRemove'] && ko['isObservableArray'](arrayOrObservableArray)) {
|
if (ko['isObservableArray'](arrayOrObservableArray)) {
|
||||||
setDomNodeChildrenFromArrayMapping(arrayOrObservableArray.peek());
|
setDomNodeChildrenFromArrayMapping(arrayOrObservableArray.peek());
|
||||||
|
|
||||||
var subscription = arrayOrObservableArray['subscribe'](changeList => {
|
var subscription = arrayOrObservableArray['subscribe'](changeList => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue