Skip to content

Commit

Permalink
Merge pull request sheadawson#40 from SilbinaryWolf/feat-actionimprov…
Browse files Browse the repository at this point in the history
…ements

feat(JavaScript): Allow changing action URL and more improvements
sheadawson authored Apr 6, 2017
2 parents b7fc247 + 2335a13 commit f90a9ec
Showing 2 changed files with 74 additions and 16 deletions.
36 changes: 34 additions & 2 deletions code/extensions/QuickAddNewExtension.php
Original file line number Diff line number Diff line change
@@ -7,13 +7,16 @@
**/
class QuickAddNewExtension extends Extension
{
/**
* @var boolean
*/
protected $addNewEnabled = false;

/**
* @var FieldList
**/
protected $addNewFields;


/**
* @var string
**/
@@ -98,6 +101,7 @@ public function useAddNew(
}

$this->owner->addExtraClass('quickaddnew-field');
$this->addNewEnabled = true;

$this->sourceCallback = $sourceCallback;
$this->isFrontend = $isFrontend;
@@ -108,6 +112,33 @@ public function useAddNew(
return $this->owner;
}

/**
* @return boolean
*/
public function hasAddNewButton() {
return $this->addNewEnabled;
}

/**
*
*/
public function updateAttributes(&$attributes) {
if (!$this->addNewFields) {
// Ignore if not using QuickAddNew
return;
}
// NOTE(Jake): This below comment will be necessary if
// $this->owner->setForm($form); is needed in 'doAddNew'
/*$form = $this->owner->getForm();
if ($this->owner === $form->getController()) {
// Ignore action to avoid cyclic calls with Link() function
return;
}*/
$action = $this->owner->Link('AddNewFormHTML');
// Remove [] for ListboxSetField/CheckboxSetField
$action = preg_replace("/[\[\]']+/", "", $action);
$attributes['data-quickaddnew-action'] = $action;
}

/**
* The AddNewForm for the dialog window
@@ -179,7 +210,8 @@ public function doAddNew($data, $form)
}

$this->owner->setValue($value);
$this->owner->setForm($form);
// NOTE(Jake): Below line causes cyclic issues, I assume it's not necessary.
//$this->owner->setForm($form);
return $this->owner->FieldHolder();
}

54 changes: 40 additions & 14 deletions javascript/quickaddnew.js
Original file line number Diff line number Diff line change
@@ -43,8 +43,17 @@ jQuery.entwine("quickaddnew", function($) {
var fieldName = this.attr('name');
if (this.hasClass('checkboxset')) fieldName = this.find('input:checkbox').attr('name').replace(/\[[0-9]+\]/g, '');
var action = this.parents('form').attr('action').split('?', 2); //add support for url parameters e.g. ?locale=en_US when using Translatable
var dialogHTMLURL = action[0] + '/field/' + fieldName + '/AddNewFormHTML' + '?' + action[1];
this.setURL(dialogHTMLURL.replace(/[\[\]']+/g,''));

var dialogHTMLURL = this.data('quickaddnew-action');
if (!dialogHTMLURL) {
// Fallback to default action
dialogHTMLURL = action[0] + '/field/' + fieldName + '/AddNewFormHTML';
}
if (action[1]) {
dialogHTMLURL += '?' + action[1];
}
dialogHTMLURL = dialogHTMLURL.replace(/[\[\]']+/g,'');
this.setURL(dialogHTMLURL);

// configure the dialog
this.getDialog().data("field", this).dialog({
@@ -55,39 +64,55 @@ jQuery.entwine("quickaddnew", function($) {
position: { my: "center", at: "center", of: window }
});

// submit button loading state while form is submitting
this.getDialog().on("click", "button", function() {
$(this).addClass("loading ui-state-disabled");
});

// handle dialog form submission
this.getDialog().on("submit", "form", function() {

var dlg = self.getDialog().dialog(),
options = {};

var $submitButtons = $(this).find('input[type="submit"], button[type="submit"]');
$submitButtons.addClass("loading ui-state-disabled");

// if this is a multiselect field, send the existing values
// along with the form submission so they can be included in the
// replacement field
if(self.val() && typeof self.val() === 'object'){
options.data = {
existing : self.val().join(',')
}
};
}

options.success = function(response) {
if($(response).is(".field")) {
self.getDialog().empty().dialog("close");
self.parents('.field:first').replaceWith(response);
options.success = function(res) {
var $response = $(res);
if($response.is('.field')) {
self.getDialog().empty().dialog('close');
var $newInput = $response.find(self[0].tagName);
// Replace <select> <option>'s rather than the entire HTML block
// to avoid JS hooks being lost on the frontend.
if ($newInput[0] && $newInput[0].tagName === 'SELECT') {
self.html($newInput.children());

// Support legacy and new chosen
self.trigger('liszt:updated').trigger('chosen:updated');
// Support select2
self.trigger('change.select2');
} else {
self.parents('.field:first').replaceWith(res);
}
} else {
self.getDialog().html(response);
self.getDialog().html(res);
}
}
};
options.complete = function() {
$submitButtons.removeClass("loading ui-state-disabled");
};

$(this).ajaxSubmit(options);

return false;
});

this._super();
},

showDialog: function(url) {
@@ -102,6 +127,7 @@ jQuery.entwine("quickaddnew", function($) {
dlg.find('form :input:visible:enabled:first').focus();
});
}
this._super();
}
});
});

0 comments on commit f90a9ec

Please sign in to comment.