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/17 13:51:53 UTC

[sling-org-apache-sling-app-cms] branch master updated: updated nomnom to have a methods configuration and a callbacks configuration.

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 8485cbe  updated nomnom to have a methods configuration and a callbacks configuration.
8485cbe is described below

commit 8485cbeac0d7535912a213f5212a279a68713dad
Author: JE Bailey <ja...@sas.com>
AuthorDate: Wed Oct 17 09:51:47 2018 -0400

    updated nomnom to have a methods configuration and a callbacks
    configuration.
---
 ui/src/main/frontend/src/js/cms.draggable.js |  13 +-
 ui/src/main/frontend/src/js/cms.js           | 116 +++++------
 ui/src/main/frontend/src/js/cms.modal.js     |  27 +--
 ui/src/main/frontend/src/js/cms.pathfield.js |  12 +-
 ui/src/main/frontend/src/js/cms.table.js     |  40 ++--
 ui/src/main/frontend/src/js/cms.toggle.js    |  24 +--
 ui/src/main/frontend/src/js/nomnom.js        | 277 ++++++++++++++-------------
 7 files changed, 269 insertions(+), 240 deletions(-)

diff --git a/ui/src/main/frontend/src/js/cms.draggable.js b/ui/src/main/frontend/src/js/cms.draggable.js
index 036fa22..dac27ff 100644
--- a/ui/src/main/frontend/src/js/cms.draggable.js
+++ b/ui/src/main/frontend/src/js/cms.draggable.js
@@ -28,12 +28,15 @@
             elementX : 0,
             elementY : 0
         },
-        moveComplete: function (data) {
-            data.mouseDown = false;
-            data.elementX = parseInt(this.style.left, 10) || 0;
-            data.elementY = parseInt(this.style.top, 10) || 0;
-            return false;
+        methods : {
+            moveComplete: function (data) {
+                data.mouseDown = false;
+                data.elementX = parseInt(this.style.left, 10) || 0;
+                data.elementY = parseInt(this.style.top, 10) || 0;
+                return false;
+            }
         },
+
         events : {
             mousedown: function (event, data) {
                 if (event.target.matches('.modal-card-body *')) {
diff --git a/ui/src/main/frontend/src/js/cms.js b/ui/src/main/frontend/src/js/cms.js
index 6cc9d25..c87cec4 100644
--- a/ui/src/main/frontend/src/js/cms.js
+++ b/ui/src/main/frontend/src/js/cms.js
@@ -111,75 +111,83 @@ nomnom.decorate('.suffix-form', {
 });
     
 nomnom.decorate('.richtext', {
-    initCallback: function(){
-        $(this).summernote({
-            toolbar: [
-                ['style', ['bold', 'italic', 'clear','strikethrough', 'superscript', 'subscript']],
-                ['insert', ['picture', 'link', 'table', 'hr']],
-                ['para', ['style','ul', 'ol', 'paragraph']],
-                ['misc', ['codeview', 'undo','redo','help']]
-            ],
-            followingToolbar: false,
-            dialogsInBody: true,
-            height: 200,
-            onCreateLink: function (url) {
-                return url;
-            },
-            callbacks: {
-                onDialogShown: function(){
-                    Sling.CMS.ui.suggest($('.note-link-url')[0], 'content', '/content');
-                    Sling.CMS.ui.suggest($('.note-image-url')[0], 'content', '/content');
+    callbacks : {
+        created : function(){
+            $(this).summernote({
+                toolbar: [
+                    ['style', ['bold', 'italic', 'clear','strikethrough', 'superscript', 'subscript']],
+                    ['insert', ['picture', 'link', 'table', 'hr']],
+                    ['para', ['style','ul', 'ol', 'paragraph']],
+                    ['misc', ['codeview', 'undo','redo','help']]
+                ],
+                followingToolbar: false,
+                dialogsInBody: true,
+                height: 200,
+                onCreateLink: function (url) {
+                    return url;
+                },
+                callbacks: {
+                    onDialogShown: function(){
+                        Sling.CMS.ui.suggest($('.note-link-url')[0], 'content', '/content');
+                        Sling.CMS.ui.suggest($('.note-image-url')[0], 'content', '/content');
+                    }
                 }
-            }
-        });
+            });
+        }
     }
 });
     
 nomnom.decorate('.page-properties-container', {
-    initCallback: function(){
-        var $ctr = $(this);
-        var $wrapper = $ctr.closest('.form-wrapper');
-        $($ctr.data('source')).change(function(){
-            var config = $(this).val();
-            $ctr.load($ctr.data('path')+config, function(){
-                var source   = $('#content-template').html();
-                var template = Handlebars.compile(source);
-                var updateContent = function(){
-                    if(!$wrapper.is(':disabled')){
-                        var data = Sling.CMS.utils.form2Obj($ctr.parents('form'));
-                        $('input[name=":content"]').val(template(data));
+    callbacks : {
+        created :  function(){
+            var $ctr = $(this);
+            var $wrapper = $ctr.closest('.form-wrapper');
+            $($ctr.data('source')).change(function(){
+                var config = $(this).val();
+                $ctr.load($ctr.data('path')+config, function(){
+                    var source   = $('#content-template').html();
+                    var template = Handlebars.compile(source);
+                    var updateContent = function(){
+                        if(!$wrapper.is(':disabled')){
+                            var data = Sling.CMS.utils.form2Obj($ctr.parents('form'));
+                            $('input[name=":content"]').val(template(data));
+                        }
                     }
-                }
-                $ctr.find('input,textarea,select').change(updateContent);
-                $ctr.parents('form').submit(updateContent);
+                    $ctr.find('input,textarea,select').change(updateContent);
+                    $ctr.parents('form').submit(updateContent);
+                });
             });
-        });
+        }
     }
 });
     
 nomnom.decorate(".namehint", {
-    initCallback: function(){
-        var $nh = $(this);
-        $nh.parents('.Form-Ajax').find('select[name="sling:resourceType"]').change(function(){
-            var resourceType = $(this).val().split("\/");
-            $nh.val(resourceType[resourceType.length - 1]);
-        });
-     }
+    callbacks : {
+        created : function(){
+            var $nh = $(this);
+            $nh.parents('.Form-Ajax').find('select[name="sling:resourceType"]').change(function(){
+                var resourceType = $(this).val().split("\/");
+                $nh.val(resourceType[resourceType.length - 1]);
+            });
+         }
+    }
 });
 
 
 nomnom.decorate('.sling-cms-include-config', {
-    initCallback: function() {
-        var $ctr = $(this);
-        var load = function(){
-            var config = $($ctr.data('source')).find('option:selected').data('config');
-            
-            if(config){
-                $ctr.load(config + $ctr.parents('form').attr('action'));
-            }
-        };
-        $($ctr.data('source')).change(load);
-        load();
+    callbacks : {
+        created :  function() {
+            var $ctr = $(this);
+            var load = function(){
+                var config = $($ctr.data('source')).find('option:selected').data('config');
+                
+                if(config){
+                    $ctr.load(config + $ctr.parents('form').attr('action'));
+                }
+            };
+            $($ctr.data('source')).change(load);
+            load();
+        }
     }
 });
 
diff --git a/ui/src/main/frontend/src/js/cms.modal.js b/ui/src/main/frontend/src/js/cms.modal.js
index d276d0c..5de626e 100644
--- a/ui/src/main/frontend/src/js/cms.modal.js
+++ b/ui/src/main/frontend/src/js/cms.modal.js
@@ -26,18 +26,21 @@ nomnom.decorate("a.Fetch-Modal", {
         }
     },
     
-    getModal: function(title, link, path, complete) {
-        var button = this;
-        var $modal = $('<div class="modal"><div class="modal-background"></div><div class="modal-card is-draggable"><header class="modal-card-head"><p class="modal-card-title">'+title+'</p><button class="delete" aria-label="close"></button></header><section class="modal-card-body"></section><footer class="modal-card-foot"></footer></div>');
-        $('body').append($modal);
-            $modal.find('.modal-card-body').load(link,function(){
-            $modal.addClass('is-active');
-            $modal.find('.delete,.close-modal').click(function(){
-                $modal.css('display','none').remove();
-                return false;
+    methods : {
+        getModal: function(title, link, path, complete) {
+            var button = this;
+            var $modal = $('<div class="modal"><div class="modal-background"></div><div class="modal-card is-draggable"><header class="modal-card-head"><p class="modal-card-title">'+title+'</p><button class="delete" aria-label="close"></button></header><section class="modal-card-body"></section><footer class="modal-card-foot"></footer></div>');
+            $('body').append($modal);
+                $modal.find('.modal-card-body').load(link,function(){
+                $modal.addClass('is-active');
+                $modal.find('.delete,.close-modal').click(function(){
+                    $modal.css('display','none').remove();
+                    return false;
+                });
+                button.removeAttribute("disabled");
             });
-            button.removeAttribute("disabled");
-        });
-        return $modal;
+            return $modal;
+        }
     }
+
 });
diff --git a/ui/src/main/frontend/src/js/cms.pathfield.js b/ui/src/main/frontend/src/js/cms.pathfield.js
index f428113..e3e2531 100644
--- a/ui/src/main/frontend/src/js/cms.pathfield.js
+++ b/ui/src/main/frontend/src/js/cms.pathfield.js
@@ -21,11 +21,13 @@
     'use strict';
     var pathfield = null;
     nomnom.decorate("input.pathfield", {
-        initCallback: function(){
-            var type = this.dataset.type;
-            var base = this.dataset.base;
-            Sling.CMS.ui.suggest(this, type, base);
-        } 
+        callbacks :{
+            created : function(){
+                var type = this.dataset.type;
+                var base = this.dataset.base;
+                Sling.CMS.ui.suggest(this, type, base);
+            }
+        }
     });
     nomnom.decorate('.search-button', {
         events: {
diff --git a/ui/src/main/frontend/src/js/cms.table.js b/ui/src/main/frontend/src/js/cms.table.js
index 887ede6..6f5a3b4 100644
--- a/ui/src/main/frontend/src/js/cms.table.js
+++ b/ui/src/main/frontend/src/js/cms.table.js
@@ -17,29 +17,31 @@
  * under the License.
  */ 
 nomnom.decorate(".table .load-versions", {
-   
-    initCallback: function() {
-        var $ctr = $(this);
-        var $table = $ctr.closest('.table');
-        $.getJSON($ctr.data('url'),function(res){
-            $table.dataTable().api().destroy();
-            var source   = $('#'+$ctr.data('template')).html();
-            var template = Handlebars.compile(source);
-            $ctr.append(template(res));
-        });
+    callbacks : {
+        created : function() {
+            var $ctr = $(this);
+            var $table = $ctr.closest('.table');
+            $.getJSON($ctr.data('url'),function(res){
+                $table.dataTable().api().destroy();
+                var source   = $('#'+$ctr.data('template')).html();
+                var template = Handlebars.compile(source);
+                $ctr.append(template(res));
+            });
+        }
     }
-    
 });
 
 nomnom.decorate(".table", {
-    initCallback: function(){
-        var $table = $(this);
-        var sort = $table.data('sort') !== 'false';
-        var paginate = $table.data('paginate') !== 'false';
-        $table.DataTable({
-            sort: sort,
-            paginate: paginate
-        });
+    callbacks : {
+        created :function(){
+            var $table = $(this);
+            var sort = $table.data('sort') !== 'false';
+            var paginate = $table.data('paginate') !== 'false';
+            $table.DataTable({
+                sort: sort,
+                paginate: paginate
+            });
+        }
     }
 });
 
diff --git a/ui/src/main/frontend/src/js/cms.toggle.js b/ui/src/main/frontend/src/js/cms.toggle.js
index 6f226e3..3ac07be 100644
--- a/ui/src/main/frontend/src/js/cms.toggle.js
+++ b/ui/src/main/frontend/src/js/cms.toggle.js
@@ -26,16 +26,18 @@ nomnom.decorate('.toggle-hidden' , {
 });
     
 nomnom.decorate('.toggle-value', {
-    initCallback: function(){
-        var source = this.getAttribute('data-toggle-source');
-        var selector = 'input[name="'+ source +'"], select[name="'+ source +'"]';
-        var $tog = $(this);
-        $(selector).change(function(){
-            if($(this).val() !== $tog.data('toggle-value')){
-                $tog.addClass('is-hidden');
-            } else {
-                $tog.removeClass('is-hidden');
-            }
-        });
+    callbacks : {
+        created :function(){
+            var source = this.getAttribute('data-toggle-source');
+            var selector = 'input[name="'+ source +'"], select[name="'+ source +'"]';
+            var $tog = $(this);
+            $(selector).change(function(){
+                if($(this).val() !== $tog.data('toggle-value')){
+                    $tog.addClass('is-hidden');
+                } else {
+                    $tog.removeClass('is-hidden');
+                }
+            });
+        }
     }
 });
diff --git a/ui/src/main/frontend/src/js/nomnom.js b/ui/src/main/frontend/src/js/nomnom.js
index 280ebc6..5e53195 100644
--- a/ui/src/main/frontend/src/js/nomnom.js
+++ b/ui/src/main/frontend/src/js/nomnom.js
@@ -19,146 +19,155 @@
 /* eslint-env es6, browser */
 (function(nomnom) {
 
-    // public
-    nomnom.decorate = function(selector, config) {
-        if (debug) {
-            console.log("storing selector" + selector);
-        }
-        tagSelectors[selector] = config;
-        document.querySelectorAll(selector).forEach(function(node) {
-            wrap(node, config);
-        });
-    };
+  // public
+  nomnom.decorate = function(selector, config) {
+      if (debug) {
+          console.log("storing selector" + selector);
+      }
+      tagSelectors[selector] = config;
+      document.querySelectorAll(selector).forEach(function(node) {
+          wrap(node, config);
+      });
+  };
 
-    var tagSelectors = {};
-    var debug = false;
-    var elementMap = new WeakMap();
+  var tagSelectors = {};
+  var debug = false;
+  var elementMap = new WeakMap();
 
-    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;
-        if (elementMap.has(node)) {
-            configSet = elementMap.get(node);
-        } else {
-            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]);
-            } else {
-                node[name] = config[name];
-            }
-        });
-        keys.forEach(function(key) {
-            if (debug) {
-                console.log("   decorating " + key);
-            }
-            node[key] = config[key];
-        });
-        if (node.initCallback) {
-            node.initCallback.call(node);
-        }
-    };
+  var wrap = function(node, config) {
+      if (debug) {
+          console.log("decorating element " + node + node.name);
+      }
+      var configSet;
+      if (elementMap.has(node)) {
+          configSet = elementMap.get(node);
+      } else {
+          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);
-        };
-    };
+  // 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) {
+          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([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);
-            }
-        }
-    };
+  var checkAll = function(node) {
+      var checkSet = new Set([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