Merge pull request #209 from chillu/pulls/subsites-dropdown

BUG Subsites selection on SubsitesVirtualPage (fixes #45 and #47)
This commit is contained in:
Damian Mooyman 2015-11-13 18:52:23 +13:00
commit e9678221fc
2 changed files with 34 additions and 26 deletions

View File

@ -22,15 +22,13 @@ class SubsitesVirtualPage extends VirtualPage {
$subsites->push(new ArrayData(array('Title' => 'Main site', 'ID' => 0)));
$subsiteSelectionField = new DropdownField(
"CopyContentFromID_SubsiteID",
_t('SubsitesVirtualPage.SubsiteField',"Subsite"),
$subsites->map('ID', 'Title'),
($this->CopyContentFromID) ? $this->CopyContentFrom()->SubsiteID : Session::get('SubsiteID')
);
$fields->addFieldToTab(
'Root.Main',
$subsiteSelectionField,
DropdownField::create(
"CopyContentFromID_SubsiteID",
_t('SubsitesVirtualPage.SubsiteField',"Subsite"),
$subsites->map('ID', 'Title')
)->addExtraClass('subsitestreedropdownfield-chooser no-change-track'),
'CopyContentFromID'
);
@ -44,7 +42,7 @@ class SubsitesVirtualPage extends VirtualPage {
);
if(Controller::has_curr() && Controller::curr()->getRequest()) {
$subsiteID = Controller::curr()->getRequest()->postVar('CopyContentFromID_SubsiteID');
$subsiteID = Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID');
$pageSelectionField->setSubsiteID($subsiteID);
}
$fields->replaceField('CopyContentFromID', $pageSelectionField);
@ -111,6 +109,10 @@ class SubsitesVirtualPage extends VirtualPage {
return $labels;
}
public function getCopyContentFromID_SubsiteID() {
return ($this->CopyContentFromID) ? (int)$this->CopyContentFrom()->SubsiteID : (int)Session::get('SubsiteID');
}
public function getVirtualFields() {
$fields = parent::getVirtualFields();

View File

@ -1,24 +1,30 @@
(function($) {
$.entwine('ss', function($) {
$('.TreeDropdownField').entwine({
subsiteID: function() {
var subsiteSel = $('#CopyContentFromID_SubsiteID select')[0];
if(!subsiteSel) return;
subsiteSel.onchange = (function() {
this.createTreeNode(true);
this.ajaxGetTree((function(response) {
this.newTreeReady(response, true);
this.updateTreeLabel();
}).bind(this));
}).bind(this);
return subsiteSel.options[subsiteSel.selectedIndex].value;
},
/**
* Choose a subsite from which to select pages.
* Needs to clear tree dropdowns in case selection is changed.
*/
$('.subsitestreedropdownfield-chooser').entwine({
onchange: function() {
// TODO Data binding between two fields
// TODO create resetField method on API instead
var fields = $('.SubsitesTreeDropdownField');
fields.setValue(null);
fields.setTitle(null);
fields.find('.tree-holder').empty();
}
});
/**
* Add selected subsite from separate dropdown to the request parameters
* before asking for the tree.
*/
$('.TreeDropdownField.SubsitesTreeDropdownField').entwine({
getRequestParams: function() {
var name = this.find(':input:hidden').attr('name'), obj = {};
obj[name + '_SubsiteID'] = parseInt(this.subsiteID());
return obj;
var name = this.find(':input[type=hidden]:first').attr('name') + '_SubsiteID',
source = $('[name=' + name + ']'), params = {};
params[name] = source.length ? source.val() : null;
return params;
}
});
});