Permalink
Browse files

ENHANCEMENT Limiting page types based on SiteTree::$allowed_children …

…in CMSMain.AddForm.js
  • Loading branch information...
1 parent b4366c9 commit 26151f2e4c8f8d667f8e6e7625c47cedaffac189 @chillu chillu committed May 8, 2011
Showing with 54 additions and 1 deletion.
  1. +3 −0 code/controller/CMSMain.php
  2. +51 −1 javascript/CMSMain.AddForm.js
@@ -1297,6 +1297,8 @@ function AddForm() {
$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('<span class="hints" data-hints="%s"></span>', $this->SiteTreeHints())),
$parentField = new TreeDropdownField("ParentID", _t('CMSMain.AddFormParentLabel', 'Parent page'), 'SiteTree'),
new OptionsetField("PageType", "", $pageTypes, 'Page')
);
@@ -1312,6 +1314,7 @@ function AddForm() {
$this->extend('updatePageOptions', $fields);
$form = new Form($this, "AddForm", $fields, $actions);
+ $form->addExtraClass('cms-add-form');
return $form;
}
@@ -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');
}
});

0 comments on commit 26151f2

Please sign in to comment.