Skip to content

Commit

Permalink
Fix quickaddnew sometimes getting entwined twice
Browse files Browse the repository at this point in the history
Issue stems from jQuery plugins like Select2 re-writing the dom and
causing entwine to match quickaddnew to the field a second time.
This then causes issues with doubled up buttons and dialogs.
The other potential fix is making sure quickaddnew fires last in
the page build but this can't be guaranteed.
  • Loading branch information
Stephen McMahon committed Jul 6, 2015
1 parent 8d3bb2e commit 75d1ad6
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions javascript/quickaddnew.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ jQuery.entwine("quickaddnew", function($) {
URL: null,
onmatch: function() {
var self = this;


//Check to see if quickaddnew has been bound to this field before, sometimes jQuery plugins like Select2
//will trigger a binding a second time that we don't want.
if($(this).parents().children('.quickaddnew-button').length > 0) {
return;
}
// create add new button
var button = $("<button />")
.addClass()
.text(ss.i18n._t('QUICKADDNEW.AddNew'))
.attr('href', '#')
.addClass("quickaddnew-button ss-ui-button ss-ui-button-small")
.appendTo(self.parents('div:first'));

// create dialog
var dialog = $("<div />")
.addClass("quickaddnew-dialog")
.appendTo(self.parents('div:first'));

.appendTo(self.parents('div:first'));
this.setDialog(dialog);

// set URL
Expand Down Expand Up @@ -87,14 +92,16 @@ jQuery.entwine("quickaddnew", function($) {

showDialog: function(url) {
var dlg = this.getDialog();

dlg.empty().dialog("open").parent().addClass("loading");

dlg.load(this.getURL(), function(){
dlg.parent().removeClass("loading");
// set focus to first input element
dlg.find('form :input:visible:enabled:first').focus();
});
// Check to see we have a dialog, other jquery plugins like Select2 can get bound to by accident
if (dlg !== null) {
dlg.empty().dialog("open").parent().addClass("loading");

dlg.load(this.getURL(), function(){
dlg.parent().removeClass("loading");
// set focus to first input element
dlg.find('form :input:visible:enabled:first').focus();
});
}
}
});
});
Expand Down

0 comments on commit 75d1ad6

Please sign in to comment.