Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENHANCEMENT Support for record/selection-specific metadata in TreeDro…

…pdownField (e.g. useful for limiting available page type option based on a parent node selection)
  • Loading branch information...
commit 7a70b29b9da91e677320d37a89225488a56d6911 1 parent 4feae59
@chillu chillu authored
Showing with 32 additions and 4 deletions.
  1. +12 −2 forms/TreeDropdownField.php
  2. +20 −2 javascript/TreeDropdownField.js
View
14 forms/TreeDropdownField.php
@@ -124,12 +124,20 @@ public function Field() {
Requirements::css(SAPPHIRE_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
Requirements::css(SAPPHIRE_DIR . '/css/TreeDropdownField.css');
- if($this->Value() && $record = $this->objectForKey($this->Value())) {
+ $record = $this->Value() ? $this->objectForKey($this->Value()) : null;
+ if($record) {
$title = $record->{$this->labelField};
} else {
$title = _t('DropdownField.CHOOSE', '(Choose)', PR_MEDIUM, 'start value of a dropdown');
}
+ // TODO Implement for TreeMultiSelectField
+ if($record) {
+ $metadata = array('id' => $record->ID, 'metadata' => array('ClassName' => $record->ClassName));
+ } else {
+ $metadata = null;
+ }
+
return $this->createTag (
'div',
array (
@@ -137,6 +145,8 @@ public function Field() {
'class' => 'TreeDropdownField single' . ($this->extraClass() ? " {$this->extraClass()}" : '') . ($this->showSearch ? " searchable" : ''),
'data-url-tree' => $this->form ? $this->Link('tree') : "",
'data-title' => $title,
+ // Any additional data from the selected record
+ 'data-metadata' => ($metadata) ? Convert::raw2json($metadata) : null
),
$this->createTag (
'input',
@@ -209,7 +219,7 @@ public function tree(SS_HTTPRequest $request) {
}
}
- $eval = '"<li id=\"selector-' . $this->Name() . '-{$child->' . $this->keyField . '}\" data-id=\"$child->' . $this->keyField . '\" class=\"$child->class"' .
+ $eval = '"<li id=\"selector-' . $this->Name() . '-{$child->' . $this->keyField . '}\" data-id=\"$child->' . $this->keyField . '\" class=\"class-$child->class"' .
' . $child->markingClasses() . "\"><a rel=\"$child->ID\">" . $child->' . $this->labelField . ' . "</a>"';
if($isSubTree) {
View
22 javascript/TreeDropdownField.js
@@ -77,11 +77,13 @@
.bind('select_node.jstree', function(e, data) {
var node = data.rslt.obj, id = $(node).data('id');
if(self.getValue() == id) {
- self.setValue(null);
+ self.data('metadata', null);
self.setTitle(null);
+ self.setValue(null);
} else {
- self.setValue(id);
+ self.data('metadata', [$.extend({id: id}, $(node).getMetaData())]);
self.setTitle(data.inst.get_text(node));
+ self.setValue(id);
}
// Avoid auto-closing panel on first load
@@ -132,6 +134,19 @@
return [];
}
});
+ $('.TreeDropdownField .tree-holder li').entwine({
+ /**
+ * Overload to return more data. The same data should be set on initial
+ * value through PHP as well (see TreeDropdownField->Field()).
+ *
+ * @return {object}
+ */
+ getMetaData: function() {
+ var matches = this.attr('class').match(/class-([^\s]*)/i);
+ var klass = matches ? matches[1] : '';
+ return {ClassName: klass};
+ }
+ });
$('.TreeDropdownField *').entwine({
getField: function() {
return this.parents('.TreeDropdownField:first');
@@ -219,6 +234,9 @@
self.setTitle($.map(nodes, function(el, i) {
return data.inst.get_text(el);
}));
+ self.data('metadata', $.map(nodes, function(el, i) {
+ return {id: $(el).data('id'), metadata: $(el).getMetaData()};
+ }));
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.