This is an automated email from the ASF dual-hosted git repository. jeb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git
commit 6e240f3bdaadd47dcdaa846768bfcaff8980f968 Author: JE Bailey <[email protected]> AuthorDate: Thu Nov 1 14:32:15 2018 -0400 updates to nomnom to support YUI compression --- ui/src/main/frontend/src/js/nomnom.js | 320 ++++++++++++++++------------------ 1 file changed, 148 insertions(+), 172 deletions(-) diff --git a/ui/src/main/frontend/src/js/nomnom.js b/ui/src/main/frontend/src/js/nomnom.js index 07c587b..135d728 100644 --- a/ui/src/main/frontend/src/js/nomnom.js +++ b/ui/src/main/frontend/src/js/nomnom.js @@ -19,189 +19,165 @@ /* eslint-env es6, browser */ (function(nomnom) { - if (!Element.prototype.matches) { - Element.prototype.matches = Element.prototype.msMatchesSelector; - } - - if (typeof NodeList.prototype.forEach !== "function" && typeof Array.prototype.forEach === "function") { - NodeList.prototype.forEach = Array.prototype.forEach; - } - - var tagSelectors = {}; - var debug = false; - var elementMap = new WeakMap(); - var topics = {}; - -// public -nomnom.decorate = function(selector, config) { - if (debug) { - console.log("storing selector" + selector); + if (!Element.prototype.matches) { + Element.prototype.matches = Element.prototype.msMatchesSelector; } - tagSelectors[selector] = config; - let foundItems = document.querySelectorAll(selector); - if (foundItems) { - foundItems.forEach(function(node) { - wrap(node, config); - }); + + if (typeof NodeList.prototype.forEach !== "function" && typeof Array.prototype.forEach === "function") { + NodeList.prototype.forEach = Array.prototype.forEach; } -}; -nomnom.subscribe = function(topic, listener) { - if (!topics[topic]){ - topics[topic] = new Map(); - } - var caller = this; - topics[topic].set(caller,listener); - return { - remove: function() { - topics[topic].delete(caller); + var tagSelectors = {}; + var debug = false; + var elementMap = new WeakMap(); + + // public + nomnom.decorate = function(selector, config) { + if (debug) { + console.log("storing selector" + selector); } - } -}; + tagSelectors[selector] = config; + var foundItems = document.querySelectorAll(selector); + if (foundItems) { + foundItems.forEach(function(node) { + wrap(node, config); + }); + } + }; -nomnom.publish = function(topic) { - if (!topics[topic]) { - return; - } - var index = topics[topic].push(listener) - 1; - topics[topic].forEach(function(caller, func) { - func.apply(caller,arguments); + new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + mutation.addedNodes.forEach(function(node) { + checkAll(node); + }); + }); + }).observe(document.body, { + attributes : false, + childList : true, + subtree : true, + characterData : false }); -}; -new MutationObserver(function(mutations) { - mutations.forEach(function(mutation) { - mutation.addedNodes.forEach(function(node) { - checkAll(node); - }); - }); -}).observe(document.body, { - attributes : false, - childList : true, - subtree : true, - characterData : false -}); - -var wrap = function(node, config) { - if (debug) { - console.log("decorating element " + node + node.name); - } - var configSet = elementMap.get(node); - if (!configSet) { - configSet = new Set(); - elementMap.set(node, configSet); - } - if (configSet.has(config)) { - return; - } - configSet.add(config); - var names = Object.getOwnPropertyNames(config); - var keys = Object.keys(config); - var data = config.data; - names.forEach(function(name) { - if (name === "constructor") { - return; - } - if (debug) { - console.log(" decorating " + name); - } - if (name === "events") { - registerEventHandlers(node, data, config[name]); - } - if (name === "methods") { - handleMethods(node, config[name]); - } - }); - keys.forEach(function(key) { - if (debug) { - console.log(" decorating " + key); - } - node[key] = config[key]; - }); - if (config.callbacks) { - if (config.callbacks.created){ - config.callbacks.created.call(node); - } - } -}; - -// generic function to wrap the event handler in the case that -// we only want it to fire for a specific child event -var targetedEventHandler = function(fn, correctTarget, data) { - return function(event) { - if (!event.target.matches(correctTarget)) { - return; - } - fn.call(this, event, data); - }; -}; + var wrap = function(node, config) { + if (debug) { + console.log("decorating element " + node + node.name); + } + var configSet = elementMap.get(node); + if (!configSet) { + configSet = new Set(); + elementMap.set(node, configSet); + } + if (configSet.has(config)) { + return; + } + configSet.add(config); + var names = Object.getOwnPropertyNames(config); + var keys = Object.keys(config); + var data = config.data; + names.forEach(function(name) { + if (name === "constructor") { + return; + } + if (debug) { + console.log(" decorating " + name); + } + if (name === "events") { + registerEventHandlers(node, data, config[name]); + } + if (name === "methods") { + handleMethods(node, config[name]); + } + }); + keys.forEach(function(key) { + if (debug) { + console.log(" decorating " + key); + } + node[key] = config[key]; + }); + if (config.callbacks) { + if (config.callbacks.created){ + config.callbacks.created.call(node); + } + } + }; -var handleMethods = function(node, funcs) { - for ( var funcName in funcs) { - node[funcName] = funcs[funcName]; - } -}; + // generic function to wrap the event handler in the case that + // we only want it to fire for a specific child event + var targetedEventHandler = function(fn, correctTarget, data) { + return function(event) { + if (!event.target.matches(correctTarget)) { + return; + } + fn.call(this, event, data); + }; + }; + + var handleMethods = function(node, funcs) { + for ( var funcName in funcs) { + node[funcName] = funcs[funcName]; + } + }; -var registerEventHandlers = function(node, data, events) { - for ( var eventName in events) { - let possibleFunc = events[eventName]; - let targetNode = node; - if (typeof possibleFunc !== "object") { - targetNode.addEventListener(eventName, function(event) { - possibleFunc.call(node, event, data); - }); - } else { - let selector = eventName; - if (selector === "document") { - targetNode = document; - } - for ( var childEventName in possibleFunc) { - let func = possibleFunc[childEventName]; - if (selector !== "document"){ - func = targetedEventHandler(func, selector, data); - } - targetNode.addEventListener(childEventName, - function(event) { - func.call(node, event, data); - }); - } - } - } -}; + var registerEventHandlers = function(node, data, events) { + for ( var eventName in events) { + var possibleFunc = events[eventName]; + var targetNode = node; + if (typeof possibleFunc !== "object") { + targetNode.addEventListener(eventName, function(event) { + possibleFunc.call(node, event, data); + }); + } else { + var selector = eventName; + if (selector === "document") { + targetNode = document; + } + for ( var childEventName in possibleFunc) { + var func = possibleFunc[childEventName]; + if (selector !== "document"){ + func = targetedEventHandler(func, selector, data); + } + targetNode.addEventListener(childEventName, + function(event) { + func.call(node, event, data); + }); + } + } + } + }; -var checkAll = function(node) { - var checkSet = new Set(); - checkSet.add(node); - checkSet.forEach(function(element){ - if (element.querySelectorAll) { - check(element); - } - let elements = element.children; - if (elements){ - for (let i = 0; i < elements.length; i++) { - checkSet.add(elements[i]); - } - } - checkSet.delete(element); - }); -} + var checkAll = function(node) { + var checkSet = new Set(); + checkSet.add(node); + checkSet.forEach(function(foundElement){ + if (foundElement.querySelectorAll) { + check(foundElement); + } + var foundElements = foundElement.children; + if (foundElements){ + for (var i = 0; i < foundElements.length; i++) { + checkSet.add(foundElements[i]); + } + } + checkSet["delete"](foundElement); + }); + } -var check = function(node) { - for ( var selector in tagSelectors) { - let found = false; - if (debug) { - console.log("checking nodes for " + selector); - } - if (node.matches(selector)) { - found = true; - wrap(node, tagSelectors[selector]); - } - if (found && debug) { - console.log("node found for " + selector); - } - } -}; + var check = function(node) { + for ( var selector in tagSelectors) { + var found = false; + if (debug) { + console.log("checking nodes for " + selector); + } + if (node.matches(selector)) { + found = true; + wrap(node, tagSelectors[selector]); + } + if (found && debug) { + console.log("node found for " + selector); + } + } + }; -return nomnom; + return nomnom; })(window.nomnom = window.nomnom || {}); \ No newline at end of file
