MDL-63714 javascript: Add new core/pending module
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 24 Oct 2018 00:39:38 +0000 (08:39 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 24 Oct 2018 04:49:09 +0000 (12:49 +0800)
lib/amd/build/pending.min.js [new file with mode: 0644]
lib/amd/build/templates.min.js
lib/amd/src/pending.js [new file with mode: 0644]
lib/amd/src/templates.js
theme/boost/amd/build/aria.min.js
theme/boost/amd/src/aria.js

diff --git a/lib/amd/build/pending.min.js b/lib/amd/build/pending.min.js
new file mode 100644 (file)
index 0000000..19e748f
Binary files /dev/null and b/lib/amd/build/pending.min.js differ
index 7df3db3..fedd135 100644 (file)
Binary files a/lib/amd/build/templates.min.js and b/lib/amd/build/templates.min.js differ
diff --git a/lib/amd/src/pending.js b/lib/amd/src/pending.js
new file mode 100644 (file)
index 0000000..5b85886
--- /dev/null
@@ -0,0 +1,52 @@
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * A helper to manage pendingJS checks.
+ *
+ * @module     core/pending
+ * @package    core
+ * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @since      3.6
+ */
+define(['jquery'], function($) {
+
+   /**
+    * Request a new pendingPromise to be resolved.
+    *
+    * When the action you are performing is complete, simply call resolve on the returned Promise.
+    *
+    * @param    {Object}    pendingKey An optional key value to use
+    * @return   {Promise}
+    */
+    var request = function(pendingKey) {
+        var pendingPromise = $.Deferred();
+
+        pendingKey = pendingKey || {};
+        M.util.js_pending(pendingKey);
+
+        pendingPromise.then(function() {
+            return M.util.js_complete(pendingKey);
+        })
+        .catch();
+
+        return pendingPromise;
+    };
+
+    request.prototype.constructor = request;
+
+    return request;
+});
index 21fbed0..0c8d929 100644 (file)
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  * @since      2.9
  */
-define(['core/mustache',
-         'jquery',
-         'core/ajax',
-         'core/str',
-         'core/notification',
-         'core/url',
-         'core/config',
-         'core/localstorage',
-         'core/icon_system',
-         'core/event',
-         'core/yui',
-         'core/log',
-         'core/truncate',
-         'core/user_date'
-       ],
-       function(mustache, $, ajax, str, notification, coreurl, config, storage, IconSystem, event, Y, Log, Truncate, UserDate) {
+define([
+        'core/mustache',
+        'jquery',
+        'core/ajax',
+        'core/str',
+        'core/notification',
+        'core/url',
+        'core/config',
+        'core/localstorage',
+        'core/icon_system',
+        'core/event',
+        'core/yui',
+        'core/log',
+        'core/truncate',
+        'core/user_date',
+        'core/pending',
+    ],
+    function(mustache, $, ajax, str, notification, coreurl, config, storage, IconSystem, event, Y, Log, Truncate, UserDate,
+        Pending) {
 
     // Module variables.
     /** @var {Number} uniqInstances Count of times this constructor has been called. */
@@ -509,7 +512,7 @@ define(['core/mustache',
         this.currentThemeName = themeName;
         var iconTemplate = iconSystem.getTemplateName();
 
-        M.util.js_pending('core/templates:doRender');
+        var pendingPromise = new Pending('core/templates:doRender');
         return this.getTemplate(iconTemplate).then(function() {
             this.addHelpers(context, themeName);
             var result = mustache.render(templateSource, context, this.partialHelper.bind(this));
@@ -556,7 +559,7 @@ define(['core/mustache',
             return $.Deferred().resolve(html, js).promise();
         }.bind(this))
         .then(function(html, js) {
-            M.util.js_complete('core/templates:doRender');
+            pendingPromise.resolve();
             return $.Deferred().resolve(html, js).promise();
         });
     };
index bd07aec..fb85516 100644 (file)
Binary files a/theme/boost/amd/build/aria.min.js and b/theme/boost/amd/build/aria.min.js differ
index 60651c9..3bcff10 100644 (file)
@@ -20,7 +20,7 @@
  * @copyright  2018 Damyon Wiese <damyon@moodle.com>
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-define(['jquery'], function($) {
+define(['jquery', 'core/pending'], function($, Pending) {
     return {
         init: function() {
             // Drop downs from bootstrap don't support keyboard accessibility by default.
@@ -65,12 +65,11 @@ define(['jquery'], function($) {
 
             // Special handling for navigation keys when menu is open.
             var shiftFocus = function(element) {
-                M.util.pending_js('core/aria:delayed-focus');
-                var delayedFocus = function() {
+                var delayedFocus = function(pendingPromise) {
                     $(this).focus();
-                    M.util.complete_js('core/aria:delayed-focus');
+                    pendingPromise.resolve();
                 }.bind(element);
-                setTimeout(delayedFocus, 50);
+                setTimeout(delayedFocus, 50, new Pending('core/aria:delayed-focus'));
             };
 
             $('.dropdown').on('shown.bs.dropdown', function(e) {
@@ -188,15 +187,14 @@ define(['jquery'], function($) {
 
             // After page load, focus on any element with special autofocus attribute.
             $(function() {
-                M.util.pending_js('core/aria:delayed-focus');
-                window.setTimeout(function() {
+                window.setTimeout(function(pendingPromise) {
                     var alerts = $('[role="alert"][data-aria-autofocus="true"]');
                     if (alerts.length > 0) {
                         $(alerts[0]).attr('tabindex', '0');
                         $(alerts[0]).focus();
                     }
-                    M.util.complete_js('core/aria:delayed-focus');
-                }, 300);
+                    pendingPromise.resolve();
+                }, 300, new Pending('core/aria:delayed-focus'));
             });
         }
     };