Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(JavaScript): Allow changing action URL and more improvements #40

Merged
merged 7 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions code/extensions/QuickAddNewExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
**/
class QuickAddNewExtension extends Extension
{
/**
* @var boolean
*/
protected $addNewEnabled = false;

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


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

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

$this->sourceCallback = $sourceCallback;
$this->isFrontend = $isFrontend;
Expand All @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
54 changes: 40 additions & 14 deletions javascript/quickaddnew.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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) {
Expand All @@ -102,6 +127,7 @@ jQuery.entwine("quickaddnew", function($) {
dlg.find('form :input:visible:enabled:first').focus();
});
}
this._super();
}
});
});
Expand Down