mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
ENHANCEMENT adapt the page dropdown based off the allowedChildren values (from r97745)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@102746 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
29292b865d
commit
c0707cb562
@ -13,21 +13,36 @@
|
|||||||
*/
|
*/
|
||||||
Tree: null,
|
Tree: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type Array Map of <option> values to an object of "title" and "value"
|
||||||
|
*/
|
||||||
|
OrigOptions: null,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type Array Internal counter to create unique page identifiers prior to ajax saving
|
* @type Array Internal counter to create unique page identifiers prior to ajax saving
|
||||||
*/
|
*/
|
||||||
NewPages: [],
|
NewPages: [],
|
||||||
|
|
||||||
onmatch: function() {
|
onmatch: function() {
|
||||||
var self = this;
|
var self = this, typeDropdown = this.find(':input[name=PageType]');
|
||||||
|
|
||||||
Observable.applyTo(this[0]);
|
Observable.applyTo(this[0]);
|
||||||
|
|
||||||
var tree = jQuery('#sitetree')[0];
|
var tree = jQuery('#sitetree')[0];
|
||||||
this.setTree(tree);
|
this.setTree(tree);
|
||||||
jQuery(tree).bind('selectionchanged', function(e, data) {self.treeSelectionChanged(e, data);});
|
|
||||||
|
|
||||||
this.find(':input[name=PageType]').bind('change', this.typeDropdownChanged);
|
// Event bindings
|
||||||
|
jQuery(tree).bind('selectionchanged', function(e, data) {self.refresh(data.node);});
|
||||||
|
typeDropdown.bind('change', function(e) {self.refresh();});
|
||||||
|
// TODO Bind on tree initialization to set dropdown for selected node
|
||||||
|
|
||||||
|
// Store original page type options (they might get filtered to "allowed_children") later on
|
||||||
|
// TODO Better DOM element serialization (jQuery 1.4?)
|
||||||
|
var opts = {};
|
||||||
|
typeDropdown.find('option').each(function(el) {
|
||||||
|
opts[$(this).val()] = {html:$(this).html(), value: $(this).val()};
|
||||||
|
});
|
||||||
|
this.setOrigOptions(opts);
|
||||||
|
|
||||||
this._super();
|
this._super();
|
||||||
},
|
},
|
||||||
@ -71,34 +86,53 @@
|
|||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
treeSelectionChanged : function(e, data) {
|
refresh: function(selectedNode) {
|
||||||
var selectedNode = data.node;
|
// Note: Uses siteTreeHints global
|
||||||
|
var tree = this.getTree(),
|
||||||
|
origOptions = this.getOrigOptions(),
|
||||||
|
dropdown = this.find(':select[name=PageType]');
|
||||||
|
if(!selectedNode) selectedNode = tree.firstSelected();
|
||||||
|
|
||||||
if(selectedNode.hints && selectedNode.hints.defaultChild) {
|
// Clear all existing <option> elements
|
||||||
this.find(':input[name=PageType]').val(selectedNode.hints.defaultChild);
|
// (IE doesn't allow setting display:none on these elements)
|
||||||
|
dropdown.find('option').remove();
|
||||||
|
|
||||||
|
// Find allowed children through preferences on node or globally
|
||||||
|
var allowed = [];
|
||||||
|
if(selectedNode) {
|
||||||
|
if(selectedNode.hints && selectedNode.hints.allowedChildren) {
|
||||||
|
allowed = selectedNode.hints.allowedChildren;
|
||||||
|
} else {
|
||||||
|
// Fallback to globals
|
||||||
|
allowed = siteTreeHints['Root'].allowedChildren;
|
||||||
}
|
}
|
||||||
|
|
||||||
var parentID = this.getTree().getIdxOf(selectedNode);
|
// Re-add all allowed <option> to the dropdown
|
||||||
this.find(':input[name=ParentID]').val(parentID ? parentID : 0);
|
|
||||||
},
|
|
||||||
|
|
||||||
typeDropdownChanged : function() {
|
|
||||||
var tree = this.getTree();
|
|
||||||
|
|
||||||
// Don't do anything if we're already on an appropriate node
|
|
||||||
var sel = tree.firstSelected();
|
|
||||||
if(sel && sel.hints && sel.hints.allowedChildren) {
|
|
||||||
var allowed = sel.hints.allowedChildren;
|
|
||||||
for(i=0;i<allowed.length;i++) {
|
for(i=0;i<allowed.length;i++) {
|
||||||
if(allowed[i] == this.value) return;
|
var optProps = origOptions[allowed[i]];
|
||||||
|
if(optProps) dropdown.append($('<option value="' + optProps.value + '">' + optProps.html + '</option>'));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// No tree node selected, reset to original elements
|
||||||
|
$.each(origOptions, function(i, optProps) {
|
||||||
|
if(optProps) dropdown.append($('<option value="' + optProps.value + '">' + optProps.html + '</option>'));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise move to the default parent for that.
|
// TODO Re-select the currently selected element
|
||||||
if(siteTreeHints && siteTreeHints[this.value] ) {
|
|
||||||
var newNode = tree.getTreeNodeByIdx(siteTreeHints[this.value].defaultParent);
|
// Disable dropdown if no elements are selectable
|
||||||
if(newNode) tree.changeCurrentTo(newNode);
|
if(allowed) dropdown.removeAttr('disabled');
|
||||||
|
else dropdown.attr('disabled', 'disabled');
|
||||||
|
|
||||||
|
// Set default child (optional)
|
||||||
|
if(selectedNode.hints && selectedNode.hints.defaultChild) {
|
||||||
|
dropdown.val(selectedNode.hints.defaultChild);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set parent node (fallback to root)
|
||||||
|
var parentID = tree.getIdxOf(selectedNode);
|
||||||
|
this.find(':input[name=ParentID]').val(parentID ? parentID : 0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -413,10 +413,10 @@ SiteTreeNode = Class.extend('TreeNode').extend('TreeNodeAPI');
|
|||||||
SiteTreeNode.prototype = {
|
SiteTreeNode.prototype = {
|
||||||
initialize: function(options) {
|
initialize: function(options) {
|
||||||
this.TreeNode.initialize(options);
|
this.TreeNode.initialize(options);
|
||||||
|
if(this.className && this.className.match(/class\-([^\s]*)/)) {
|
||||||
if(this.className && this.className.match(/ *([^ ]+)( +|$)/)) {
|
var klass = RegExp.$1;
|
||||||
if((typeof siteTreeHints != 'undefined') && siteTreeHints[ RegExp.$1 ]) {
|
if(siteTreeHints && siteTreeHints[klass]) {
|
||||||
this.hints = siteTreeHints[ RegExp.$1 ];
|
this.hints = siteTreeHints[klass];
|
||||||
this.dropperOptions = {
|
this.dropperOptions = {
|
||||||
accept : (this.hints.allowedChildren && (this.className.indexOf('nochildren') == -1))
|
accept : (this.hints.allowedChildren && (this.className.indexOf('nochildren') == -1))
|
||||||
? this.hints.allowedChildren : 'none'
|
? this.hints.allowedChildren : 'none'
|
||||||
|
Loading…
Reference in New Issue
Block a user