ENHANCEMENT Support for record/selection-specific metadata in TreeDropdownField (e.g. useful for limiting available page type option based on a parent node selection)

This commit is contained in:
Ingo Schommer 2011-05-08 23:36:13 +12:00
parent 4feae59391
commit 7a70b29b9d
2 changed files with 32 additions and 4 deletions

View File

@ -124,12 +124,20 @@ class TreeDropdownField extends FormField {
Requirements::css(SAPPHIRE_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css'); Requirements::css(SAPPHIRE_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
Requirements::css(SAPPHIRE_DIR . '/css/TreeDropdownField.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}; $title = $record->{$this->labelField};
} else { } else {
$title = _t('DropdownField.CHOOSE', '(Choose)', PR_MEDIUM, 'start value of a dropdown'); $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 ( return $this->createTag (
'div', 'div',
array ( array (
@ -137,6 +145,8 @@ class TreeDropdownField extends FormField {
'class' => 'TreeDropdownField single' . ($this->extraClass() ? " {$this->extraClass()}" : '') . ($this->showSearch ? " searchable" : ''), 'class' => 'TreeDropdownField single' . ($this->extraClass() ? " {$this->extraClass()}" : '') . ($this->showSearch ? " searchable" : ''),
'data-url-tree' => $this->form ? $this->Link('tree') : "", 'data-url-tree' => $this->form ? $this->Link('tree') : "",
'data-title' => $title, 'data-title' => $title,
// Any additional data from the selected record
'data-metadata' => ($metadata) ? Convert::raw2json($metadata) : null
), ),
$this->createTag ( $this->createTag (
'input', 'input',
@ -209,7 +219,7 @@ class TreeDropdownField extends FormField {
} }
} }
$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>"'; ' . $child->markingClasses() . "\"><a rel=\"$child->ID\">" . $child->' . $this->labelField . ' . "</a>"';
if($isSubTree) { if($isSubTree) {

View File

@ -77,11 +77,13 @@
.bind('select_node.jstree', function(e, data) { .bind('select_node.jstree', function(e, data) {
var node = data.rslt.obj, id = $(node).data('id'); var node = data.rslt.obj, id = $(node).data('id');
if(self.getValue() == id) { if(self.getValue() == id) {
self.setValue(null); self.data('metadata', null);
self.setTitle(null); self.setTitle(null);
self.setValue(null);
} else { } else {
self.setValue(id); self.data('metadata', [$.extend({id: id}, $(node).getMetaData())]);
self.setTitle(data.inst.get_text(node)); self.setTitle(data.inst.get_text(node));
self.setValue(id);
} }
// Avoid auto-closing panel on first load // Avoid auto-closing panel on first load
@ -132,6 +134,19 @@
return []; 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({ $('.TreeDropdownField *').entwine({
getField: function() { getField: function() {
return this.parents('.TreeDropdownField:first'); return this.parents('.TreeDropdownField:first');
@ -219,6 +234,9 @@
self.setTitle($.map(nodes, function(el, i) { self.setTitle($.map(nodes, function(el, i) {
return data.inst.get_text(el); return data.inst.get_text(el);
})); }));
self.data('metadata', $.map(nodes, function(el, i) {
return {id: $(el).data('id'), metadata: $(el).getMetaData()};
}));
}); });
} }