Skip to content

Commit

Permalink
4.0.0-pre.5
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenlehara committed Jan 2, 2018
1 parent 74be704 commit 68fea0a
Show file tree
Hide file tree
Showing 2 changed files with 3,272 additions and 0 deletions.
205 changes: 205 additions & 0 deletions dist/amd/can-view-nodelist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*can-view-nodelist@4.0.0-pre.4#can-view-nodelist*/
define([
'require',
'exports',
'module',
'can-namespace',
'can-dom-mutate/node'
], function (require, exports, module) {
var namespace = require('can-namespace');
var domMutate = require('can-dom-mutate/node');
var nodeMap = new Map(), splice = [].splice, push = [].push, itemsInChildListTree = function (list) {
var count = 0;
for (var i = 0, len = list.length; i < len; i++) {
var item = list[i];
if (item.nodeType) {
count++;
} else {
count += itemsInChildListTree(item);
}
}
return count;
}, replacementMap = function (replacements) {
var map = new Map();
for (var i = 0, len = replacements.length; i < len; i++) {
var node = nodeLists.first(replacements[i]);
map.set(node, replacements[i]);
}
return map;
}, addUnfoundAsDeepChildren = function (list, rMap) {
rMap.forEach(function (replacement) {
list.newDeepChildren.push(replacement);
});
};
var nodeLists = {
update: function (nodeList, newNodes) {
var oldNodes = nodeLists.unregisterChildren(nodeList);
var arr = [];
for (var i = 0, ref = arr.length = newNodes.length; i < ref; i++) {
arr[i] = newNodes[i];
}
newNodes = arr;
var oldListLength = nodeList.length;
splice.apply(nodeList, [
0,
oldListLength
].concat(newNodes));
if (nodeList.replacements) {
nodeLists.nestReplacements(nodeList);
nodeList.deepChildren = nodeList.newDeepChildren;
nodeList.newDeepChildren = [];
} else {
nodeLists.nestList(nodeList);
}
return oldNodes;
},
nestReplacements: function (list) {
var index = 0, rMap = replacementMap(list.replacements), rCount = list.replacements.length;
while (index < list.length && rCount) {
var node = list[index], replacement = rMap.get(node);
if (replacement) {
rMap['delete'](node);
list.splice(index, itemsInChildListTree(replacement), replacement);
rCount--;
}
index++;
}
if (rCount) {
addUnfoundAsDeepChildren(list, rMap);
}
list.replacements = [];
},
nestList: function (list) {
var index = 0;
while (index < list.length) {
var node = list[index], childNodeList = nodeMap.get(node);
if (childNodeList) {
if (childNodeList !== list) {
list.splice(index, itemsInChildListTree(childNodeList), childNodeList);
}
} else {
nodeMap.set(node, list);
}
index++;
}
},
last: function (nodeList) {
var last = nodeList[nodeList.length - 1];
if (last.nodeType) {
return last;
} else {
return nodeLists.last(last);
}
},
first: function (nodeList) {
var first = nodeList[0];
if (first.nodeType) {
return first;
} else {
return nodeLists.first(first);
}
},
flatten: function (nodeList) {
var items = [];
for (var i = 0; i < nodeList.length; i++) {
var item = nodeList[i];
if (item.nodeType) {
items.push(item);
} else {
items.push.apply(items, nodeLists.flatten(item));
}
}
return items;
},
register: function (nodeList, unregistered, parent, directlyNested) {
nodeList.unregistered = unregistered;
nodeList.parentList = parent;
nodeList.nesting = parent && typeof parent.nesting !== 'undefined' ? parent.nesting + 1 : 0;
if (parent) {
nodeList.deepChildren = [];
nodeList.newDeepChildren = [];
nodeList.replacements = [];
if (parent !== true) {
if (directlyNested) {
parent.replacements.push(nodeList);
} else {
parent.newDeepChildren.push(nodeList);
}
}
} else {
nodeLists.nestList(nodeList);
}
return nodeList;
},
unregisterChildren: function (nodeList) {
var nodes = [];
for (var n = 0; n < nodeList.length; n++) {
var node = nodeList[n];
if (node.nodeType) {
if (!nodeList.replacements) {
nodeMap['delete'](node);
}
nodes.push(node);
} else {
push.apply(nodes, nodeLists.unregister(node, true));
}
}
var deepChildren = nodeList.deepChildren;
if (deepChildren) {
for (var l = 0; l < deepChildren.length; l++) {
nodeLists.unregister(deepChildren[l], true);
}
}
return nodes;
},
unregister: function (nodeList, isChild) {
var nodes = nodeLists.unregisterChildren(nodeList, true);
if (nodeList.unregistered) {
var unregisteredCallback = nodeList.unregistered;
nodeList.replacements = nodeList.unregistered = null;
if (!isChild) {
var deepChildren = nodeList.parentList && nodeList.parentList.deepChildren;
if (deepChildren) {
var index = deepChildren.indexOf(nodeList);
if (index !== -1) {
deepChildren.splice(index, 1);
}
}
}
unregisteredCallback();
}
return nodes;
},
after: function (oldElements, newFrag) {
var last = oldElements[oldElements.length - 1];
if (last.nextSibling) {
domMutate.insertBefore.call(last.parentNode, newFrag, last.nextSibling);
} else {
domMutate.appendChild.call(last.parentNode, newFrag);
}
},
replace: function (oldElements, newFrag) {
var selectedValue, parentNode = oldElements[0].parentNode;
if (parentNode.nodeName.toUpperCase() === 'SELECT' && parentNode.selectedIndex >= 0) {
selectedValue = parentNode.value;
}
if (oldElements.length === 1) {
domMutate.replaceChild.call(parentNode, newFrag, oldElements[0]);
} else {
nodeLists.after(oldElements, newFrag);
nodeLists.remove(oldElements);
}
if (selectedValue !== undefined) {
parentNode.value = selectedValue;
}
},
remove: function (elementsToBeRemoved) {
var parent = elementsToBeRemoved[0] && elementsToBeRemoved[0].parentNode;
for (var i = 0; i < elementsToBeRemoved.length; i++) {
domMutate.removeChild.call(parent, elementsToBeRemoved[i]);
}
},
nodeMap: nodeMap
};
module.exports = namespace.nodeLists = nodeLists;
});
Loading

0 comments on commit 68fea0a

Please sign in to comment.