MDL-62514 behat: Inplace editable pendingjs wrapper
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 11 Jan 2019 00:41:27 +0000 (08:41 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 30 Jan 2019 00:24:32 +0000 (08:24 +0800)
lib/amd/build/inplace_editable.min.js
lib/amd/src/inplace_editable.js

index 84f656b..ad0ff95 100644 (file)
Binary files a/lib/amd/build/inplace_editable.min.js and b/lib/amd/build/inplace_editable.min.js differ
index 90ddf2d..1021c33 100644 (file)
@@ -35,13 +35,16 @@ define(['jquery',
         'core/str',
         'core/config',
         'core/url',
-        'core/form-autocomplete'],
-        function($, ajax, templates, notification, str, cfg, url, autocomplete) {
+        'core/form-autocomplete',
+        'core/pending',
+    ],
+    function($, ajax, templates, notification, str, cfg, url, autocomplete, Pending) {
 
     $('body').on('click keypress', '[data-inplaceeditable] [data-inplaceeditablelink]', function(e) {
         if (e.type === 'keypress' && e.keyCode !== 13) {
             return;
         }
+        var editingEnabledPromise = new Pending('autocomplete-start-editing');
         e.stopImmediatePropagation();
         e.preventDefault();
         var target = $(this),
@@ -72,41 +75,49 @@ define(['jquery',
                     mainelement.attr('data-component'),
                     mainelement.attr('data-itemtype'),
                 ].join('-');
-            M.util.js_pending(pendingId);
+            var pendingPromise = new Pending(pendingId);
 
             addSpinner(mainelement);
-            ajax
-                .call([{
-                    methodname: 'core_update_inplace_editable',
-                    args: {
-                        itemid: mainelement.attr('data-itemid'),
-                        component: mainelement.attr('data-component'),
-                        itemtype: mainelement.attr('data-itemtype'),
-                        value: value
-                    },
-                    done: function(data) {
-                        var oldvalue = mainelement.attr('data-value');
-                        templates.render('core/inplace_editable', data).done(function(html, js) {
-                            var newelement = $(html);
-                            templates.replaceNode(mainelement, newelement, js);
-                            newelement.find('[data-inplaceeditablelink]').focus();
-                            newelement.trigger({type: 'updated', ajaxreturn: data, oldvalue: oldvalue});
-                            M.util.js_complete(pendingId);
-                        });
-                    },
-                    fail: function(ex) {
-                        var e = $.Event('updatefailed', {
-                                exception: ex,
-                                newvalue: value
-                            });
-                        removeSpinner(mainelement);
-                        M.util.js_complete(pendingId);
-                        mainelement.trigger(e);
-                        if (!e.isDefaultPrevented()) {
-                            notification.exception(ex);
-                        }
-                    }
-                }], true);
+            ajax.call([{
+                methodname: 'core_update_inplace_editable',
+                args: {
+                    itemid: mainelement.attr('data-itemid'),
+                    component: mainelement.attr('data-component'),
+                    itemtype: mainelement.attr('data-itemtype'),
+                    value: value,
+                },
+            }])[0]
+            .then(function(data) {
+                return templates.render('core/inplace_editable', data)
+                .then(function(html, js) {
+                    var oldvalue = mainelement.attr('data-value');
+                    var newelement = $(html);
+                    templates.replaceNode(mainelement, newelement, js);
+                    newelement.find('[data-inplaceeditablelink]').focus();
+                    newelement.trigger({
+                        type: 'updated',
+                        ajaxreturn: data,
+                        oldvalue: oldvalue,
+                    });
+
+                    return;
+                });
+            })
+            .then(function() {
+                return pendingPromise.resolve();
+            })
+            .fail(function(ex) {
+                var e = $.Event('updatefailed', {
+                        exception: ex,
+                        newvalue: value
+                    });
+                removeSpinner(mainelement);
+                M.util.js_complete(pendingId);
+                mainelement.trigger(e);
+                if (!e.isDefaultPrevented()) {
+                    notification.exception(ex);
+                }
+            });
         };
 
         var turnEditingOff = function(el) {
@@ -319,6 +330,7 @@ define(['jquery',
         // Turn editing on for the current element and register handler for Enter/Esc keys.
         turnEditingOffEverywhere();
         turnEditingOn(mainelement);
+        editingEnabledPromise.resolve();
 
     });