From 26151f2e4c8f8d667f8e6e7625c47cedaffac189 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 8 May 2011 23:36:55 +1200 Subject: [PATCH] ENHANCEMENT Limiting page types based on SiteTree::$allowed_children in CMSMain.AddForm.js --- code/controller/CMSMain.php | 3 ++ javascript/CMSMain.AddForm.js | 52 ++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/code/controller/CMSMain.php b/code/controller/CMSMain.php index 3d59203e..8293ec21 100755 --- a/code/controller/CMSMain.php +++ b/code/controller/CMSMain.php @@ -1297,6 +1297,8 @@ JS; $fields = new FieldSet( // new HiddenField("ParentID", false, ($this->parentRecord) ? $this->parentRecord->ID : null), + // TODO Should be part of the form attribute, but not possible in current form API + $hintsField = new LiteralField('Hints', sprintf('', $this->SiteTreeHints())), $parentField = new TreeDropdownField("ParentID", _t('CMSMain.AddFormParentLabel', 'Parent page'), 'SiteTree'), new OptionsetField("PageType", "", $pageTypes, 'Page') ); @@ -1312,6 +1314,7 @@ JS; $this->extend('updatePageOptions', $fields); $form = new Form($this, "AddForm", $fields, $actions); + $form->addExtraClass('cms-add-form'); return $form; } diff --git a/javascript/CMSMain.AddForm.js b/javascript/CMSMain.AddForm.js index 1e636f30..765309ec 100644 --- a/javascript/CMSMain.AddForm.js +++ b/javascript/CMSMain.AddForm.js @@ -20,7 +20,57 @@ this._super(); }, onclick: function() { - this.parents('li:first').addClass('selected').siblings().removeClass('selected'); + var el = this.parents('li:first'); + el.setSelected(true); + el.siblings().setSelected(false); + } + }); + + $(".cms-add-form").entwine({ + onmatch: function() { + var self = this; + this.find('#ParentID .TreeDropdownField').bind('change', function() { + self.updateTypeList(); + }); + }, + + /** + * Limit page type selection based on parent class. + * Similar implementation to LeftAndMain.Tree.js. + */ + updateTypeList: function() { + var hints = this.find('.hints').data('hints'), + metadata = this.find('#ParentID .TreeDropdownField').data('metadata'), + id = this.find('#ParentID .TreeDropdownField').getValue(), + newClassName = metadata[0].ClassName, + disallowedChildren = hints[newClassName ? newClassName : 'Root'].disallowedChildren || [], + defaultChildClass = hints[newClassName ? newClassName : 'Root'].defaultChild || null; + + // Limit selection + this.find('#PageType li').each(function() { + var className = $(this).find('input').val(), isAllowed = ($.inArray(className, disallowedChildren) == -1); + $(this).setEnabled(isAllowed); + }); + + // Set default child selection, or fall back to first available option + if(defaultChildClass) { + var selectedEl = this.find('#PageType li input[value=' + defaultChildClass + ']').parents('li:first'); + } else { + var selectedEl = this.find('#PageType li:not(.disabled):first'); + } + selectedEl.setSelected(true); + selectedEl.siblings().setSelected(false); + } + }); + + $(".cms-add-form #PageType li").entwine({ + setSelected: function(bool) { + this.toggleClass('selected', bool); + this.find('input').attr('checked', bool ? 'checked' : null); + }, + setEnabled: function(bool) { + $(this).toggleClass('disabled', bool); + $(this).find('input').attr('disabled', bool ? '' : 'disabled'); } });