MDL-67917 core: Return newly added nodes from template helpers
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 25 Mar 2020 07:11:06 +0000 (15:11 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 27 May 2020 02:49:43 +0000 (10:49 +0800)
Part of MDL-67743

lib/amd/build/templates.min.js
lib/amd/build/templates.min.js.map
lib/amd/src/templates.js

index 24665b1..bcc63b6 100644 (file)
Binary files a/lib/amd/build/templates.min.js and b/lib/amd/build/templates.min.js differ
index 119e917..b75e37f 100644 (file)
Binary files a/lib/amd/build/templates.min.js.map and b/lib/amd/build/templates.min.js.map differ
index c145173..8ba0e8b 100644 (file)
@@ -876,6 +876,7 @@ define([
      * @param {String} newHTML - HTML to insert / replace.
      * @param {String} newJS - Javascript to run after the insertion.
      * @param {Boolean} replaceChildNodes - Replace only the childnodes, alternative is to replace the entire node.
+     * @return {Array} The list of new DOM Nodes
      */
     var domReplace = function(element, newHTML, newJS, replaceChildNodes) {
         var replaceNode = $(element);
@@ -904,7 +905,11 @@ define([
             runTemplateJS(newJS);
             // Notify all filters about the new content.
             event.notifyFilterContentUpdated(newNodes);
+
+            return newNodes.get();
         }
+
+        return [];
     };
 
     /**
@@ -1043,17 +1048,23 @@ define([
      * @param {jQuery|String} element - Element or selector to prepend HTML to
      * @param {String} html - HTML to prepend
      * @param {String} js - Javascript to run after we prepend the html
+     * @return {Array} The list of new DOM Nodes
      */
     var domPrepend = function(element, html, js) {
         var node = $(element);
         if (node.length) {
             // Prepend the html.
-            node.prepend(html);
+            var newContent = $(html);
+            node.prepend(newContent);
             // Run any javascript associated with the new HTML.
             runTemplateJS(js);
             // Notify all filters about the new content.
             event.notifyFilterContentUpdated(node);
+
+            return newContent.get();
         }
+
+        return [];
     };
 
     /**
@@ -1064,17 +1075,23 @@ define([
      * @param {jQuery|String} element - Element or selector to append HTML to
      * @param {String} html - HTML to append
      * @param {String} js - Javascript to run after we append the html
+     * @return {Array} The list of new DOM Nodes
      */
     var domAppend = function(element, html, js) {
         var node = $(element);
         if (node.length) {
             // Append the html.
-            node.append(html);
+            var newContent = $(html);
+            node.append(newContent);
             // Run any javascript associated with the new HTML.
             runTemplateJS(js);
             // Notify all filters about the new content.
             event.notifyFilterContentUpdated(node);
+
+            return newContent.get();
         }
+
+        return [];
     };
 
     return /** @alias module:core/templates */ {
@@ -1175,9 +1192,10 @@ define([
          * @param {JQuery} element - Element or selector to replace.
          * @param {String} newHTML - HTML to insert / replace.
          * @param {String} newJS - Javascript to run after the insertion.
+         * @return {Array} The list of new DOM Nodes
          */
         replaceNodeContents: function(element, newHTML, newJS) {
-            domReplace(element, newHTML, newJS, true);
+            return domReplace(element, newHTML, newJS, true);
         },
 
         /**
@@ -1187,9 +1205,10 @@ define([
          * @param {JQuery} element - Element or selector to replace.
          * @param {String} newHTML - HTML to insert / replace.
          * @param {String} newJS - Javascript to run after the insertion.
+         * @return {Array} The list of new DOM Nodes
          */
         replaceNode: function(element, newHTML, newJS) {
-            domReplace(element, newHTML, newJS, false);
+            return domReplace(element, newHTML, newJS, false);
         },
 
         /**
@@ -1199,9 +1218,10 @@ define([
          * @param {jQuery|String} element - Element or selector to prepend HTML to
          * @param {String} html - HTML to prepend
          * @param {String} js - Javascript to run after we prepend the html
+         * @return {Array} The list of new DOM Nodes
          */
         prependNodeContents: function(element, html, js) {
-            domPrepend(element, html, js);
+            return domPrepend(element, html, js);
         },
 
         /**
@@ -1211,9 +1231,10 @@ define([
          * @param {jQuery|String} element - Element or selector to append HTML to
          * @param {String} html - HTML to append
          * @param {String} js - Javascript to run after we append the html
+         * @return {Array} The list of new DOM Nodes
          */
         appendNodeContents: function(element, html, js) {
-            domAppend(element, html, js);
+            return domAppend(element, html, js);
         },
     };
 });