Skip to content

Commit

Permalink
MDL-62514 behat: Inplace editable pendingjs wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jan 30, 2019
1 parent aac98ca commit bebb976
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/amd/build/inplace_editable.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 47 additions & 35 deletions lib/amd/src/inplace_editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

});

Expand Down

0 comments on commit bebb976

Please sign in to comment.