1/** 2 * Copyright © Magento, Inc. All rights reserved. 3 * See COPYING.txt for license details. 4 */ 5define([ 6 'ko', 7 'jquery', 8 'underscore' 9], function (ko, $, _) { 10 'use strict'; 11 12 /** 13 * Checks if provided value is a dom element. 14 * 15 * @param {*} node - Value to be checked. 16 * @returns {Boolean} 17 */ 18 function isDomElement(node) { 19 return typeof node === 'object' && node.tagName && node.nodeType; 20 } 21 22 /** 23 * Removes from the provided array all non-root nodes located inside 24 * of the comment element as long as the closing comment tags. 25 * 26 * @param {(Array|ArrayLike)} nodes - An array of nodes to be processed. 27 * @returns {Array} 28 */ 29 function normalize(nodes) { 30 var result; 31 32 nodes = _.toArray(nodes); 33 result = nodes.slice(); 34 35 nodes.forEach(function (node) { 36 if (node.nodeType === 8) { 37 result = !ko.virtualElements.hasBindingValue(node) ? 38 _.without(result, node) : 39 _.difference(result, ko.virtualElements.childNodes(node)); 40 } 41 }); 42 43 return result; 44 } 45 46 /** 47 * Extends binding context of each item in the collection. 48 * 49 * @param {...Object} extenders - Multiple extender objects to be applied to the context. 50 * @returns {jQueryCollection} Chainable. 51 */ 52 $.fn.extendCtx = function () { 53 var nodes = normalize(this), 54 extenders = _.toArray(arguments); 55 56 nodes.forEach(function (node) { 57 var ctx = ko.contextFor(node), 58 data = [ctx].concat(extenders); 59 60 _.extend.apply(_, data); 61 }); 62 63 return this; 64 }; 65 66 /** 67 * Evaluates bindings specified in each DOM element of collection. 68 * 69 * @param {(HTMLElement|Object)} [ctx] - Context to use for bindings evaluation. 70 * If not specified then current context of a collections' item will be used. 71 * @returns {jQueryCollection} Chainable. 72 */ 73 $.fn.applyBindings = function (ctx) { 74 var nodes = normalize(this), 75 nodeCtx; 76 77 if (isDomElement(ctx)) { 78 ctx = ko.contextFor(ctx); 79 } 80 81 nodes.forEach(function (node) { 82 nodeCtx = ctx || ko.contextFor(node); 83 84 ko.applyBindings(nodeCtx, node); 85 }); 86 87 return this; 88 }; 89 90 /** 91 * Adds specified bindings to each DOM element in 92 * collection and evaluates them with provided context. 93 * 94 * @param {(Object|Function)} data - Either bindings object or a function 95 * which returns bindings data for each element in collection. 96 * @param {(HTMLElement|Object)} [ctx] - Context to use for bindings evaluation. 97 * If not specified then current context of a collections' item will be used. 98 * @returns {jQueryCollection} Chainable. 99 */ 100 $.fn.bindings = function (data, ctx) { 101 var nodes = normalize(this), 102 bindings = data, 103 nodeCtx; 104 105 if (isDomElement(ctx)) { 106 ctx = ko.contextFor(ctx); 107 } 108 109 nodes.forEach(function (node) { 110 nodeCtx = ctx || ko.contextFor(node); 111 112 if (_.isFunction(data)) { 113 bindings = data(nodeCtx, node); 114 } 115 116 ko.applyBindingsToNode(node, bindings, nodeCtx); 117 }); 118 119 return this; 120 }; 121});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.