You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by je...@apache.org on 2018/10/25 17:23:18 UTC
[sling-org-apache-sling-app-cms] branch master updated: added pub
sub functionality
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
The following commit(s) were added to refs/heads/master by this push:
new 73979be added pub sub functionality
73979be is described below
commit 73979be4928804254f4ead7b0c019041f2cf69c5
Author: JE Bailey <ja...@sas.com>
AuthorDate: Thu Oct 25 13:22:17 2018 -0400
added pub sub functionality
---
ui/src/main/frontend/src/js/nomnom.js | 320 ++++++++++++++++++----------------
1 file changed, 172 insertions(+), 148 deletions(-)
diff --git a/ui/src/main/frontend/src/js/nomnom.js b/ui/src/main/frontend/src/js/nomnom.js
index 8a032bf..07c587b 100644
--- a/ui/src/main/frontend/src/js/nomnom.js
+++ b/ui/src/main/frontend/src/js/nomnom.js
@@ -19,165 +19,189 @@
/* eslint-env es6, browser */
(function(nomnom) {
- if (!Element.prototype.matches) {
- Element.prototype.matches = Element.prototype.msMatchesSelector;
+ 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 (typeof NodeList.prototype.forEach !== "function" && typeof Array.prototype.forEach === "function") {
- NodeList.prototype.forEach = Array.prototype.forEach;
+ tagSelectors[selector] = config;
+ let foundItems = document.querySelectorAll(selector);
+ if (foundItems) {
+ foundItems.forEach(function(node) {
+ wrap(node, config);
+ });
}
+};
- 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;
- let foundItems = document.querySelectorAll(selector);
- if (foundItems) {
- foundItems.forEach(function(node) {
- wrap(node, config);
- });
+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);
}
- };
+ }
+};
- 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
+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);
});
+};
- 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);
- }
- }
- };
+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
+});
- // 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 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 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);
- });
- }
- }
- }
- };
+// 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 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 handleMethods = function(node, funcs) {
+ for ( var funcName in funcs) {
+ node[funcName] = funcs[funcName];
+ }
+};
- 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 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 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 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);
+ }
+ }
+};
- return nomnom;
+return nomnom;
})(window.nomnom = window.nomnom || {});
\ No newline at end of file