mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #4202 from jonom/fix-treedropdown-folders
FIX: TreeDropdownField Folder expansion
This commit is contained in:
commit
25f2689833
@ -46,9 +46,6 @@
|
||||
// when JSTree auto-selects elements on first load.
|
||||
if(!origEvent) {
|
||||
return false;
|
||||
}else if($(origEvent.target).hasClass('jstree-icon') || $(origEvent.target).hasClass('jstree-pageicon')){
|
||||
// in case the click is not on the node title, ie on pageicon or dragicon,
|
||||
return false;
|
||||
}
|
||||
|
||||
// Don't allow checking disabled nodes
|
||||
|
@ -390,7 +390,6 @@ class File extends DataObject {
|
||||
|
||||
//get a tree listing with only folder, no files
|
||||
$folderTree = new TreeDropdownField("ParentID", _t('AssetTableField.FOLDER','Folder'), 'Folder');
|
||||
$folderTree->setChildrenMethod('ChildFolders');
|
||||
|
||||
$fields = new FieldList(
|
||||
new TabSet('Root',
|
||||
|
@ -467,6 +467,13 @@ class Folder extends File {
|
||||
return Folder::get()->filter('ParentID', $this->ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of children of this folder that are also folders.
|
||||
*/
|
||||
public function numChildFolders() {
|
||||
return $this->ChildFolders()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return String
|
||||
*/
|
||||
@ -479,7 +486,7 @@ class Folder extends File {
|
||||
if(!$this->canEdit())
|
||||
$classes .= " disabled";
|
||||
|
||||
$classes .= $this->markingClasses();
|
||||
$classes .= $this->markingClasses('numChildFolders');
|
||||
|
||||
return $classes;
|
||||
}
|
||||
|
@ -56,9 +56,13 @@ class TreeDropdownField extends FormField {
|
||||
protected $sourceObject, $keyField, $labelField, $filterCallback,
|
||||
$disableCallback, $searchCallback, $baseID = 0;
|
||||
/**
|
||||
* @var string default child method in Hierarcy->getChildrenAsUL
|
||||
* @var string default child method in Hierarchy->getChildrenAsUL
|
||||
*/
|
||||
protected $childrenMethod = 'AllChildrenIncludingDeleted';
|
||||
/**
|
||||
* @var string default child counting method in Hierarchy->getChildrenAsUL
|
||||
*/
|
||||
protected $numChildrenMethod = 'numChildren';
|
||||
|
||||
/**
|
||||
* Used by field search to leave only the relevant entries
|
||||
@ -97,6 +101,12 @@ class TreeDropdownField extends FormField {
|
||||
$this->labelField = $labelField;
|
||||
$this->showSearch = $showSearch;
|
||||
|
||||
//Extra settings for Folders
|
||||
if ($sourceObject == 'Folder') {
|
||||
$this->childrenMethod = 'ChildFolders';
|
||||
$this->numChildrenMethod = 'numChildFolders';
|
||||
}
|
||||
|
||||
$this->addExtraClass('single');
|
||||
|
||||
parent::__construct($name, $title);
|
||||
@ -171,9 +181,9 @@ class TreeDropdownField extends FormField {
|
||||
|
||||
/**
|
||||
* @param $method The parameter to ChildrenMethod to use when calling Hierarchy->getChildrenAsUL in
|
||||
* {@link Hierarchy}. The method specified determined the structure of the returned list. Use "ChildFolders"
|
||||
* {@link Hierarchy}. The method specified determines the structure of the returned list. Use "ChildFolders"
|
||||
* in place of the default to get a drop-down listing with only folders, i.e. not including the child elements in
|
||||
* the currently selected folder.
|
||||
* the currently selected folder. setNumChildrenMethod() should be used as well for proper functioning.
|
||||
*
|
||||
* See {@link Hierarchy} for a complete list of possible methods.
|
||||
*/
|
||||
@ -182,6 +192,16 @@ class TreeDropdownField extends FormField {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $method The parameter to numChildrenMethod to use when calling Hierarchy->getChildrenAsUL in
|
||||
* {@link Hierarchy}. Should be used in conjunction with setChildrenMethod().
|
||||
*
|
||||
*/
|
||||
public function setNumChildrenMethod($method) {
|
||||
$this->numChildrenMethod = $method;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@ -273,10 +293,11 @@ class TreeDropdownField extends FormField {
|
||||
if ( $this->search != "" )
|
||||
$this->populateIDs();
|
||||
|
||||
if ($this->filterCallback || $this->sourceObject == 'Folder' || $this->search != "" )
|
||||
if ($this->filterCallback || $this->search != "" )
|
||||
$obj->setMarkingFilterFunction(array($this, "filterMarking"));
|
||||
|
||||
$obj->markPartialTree();
|
||||
$obj->markPartialTree($nodeCountThreshold = 30, $context = null,
|
||||
$this->childrenMethod, $this->numChildrenMethod);
|
||||
|
||||
// allow to pass values to be selected within the ajax request
|
||||
if( isset($_REQUEST['forceValue']) || $this->value ) {
|
||||
@ -298,7 +319,7 @@ class TreeDropdownField extends FormField {
|
||||
Convert::raw2xml($child->$keyField),
|
||||
Convert::raw2xml($child->$keyField),
|
||||
Convert::raw2xml($child->class),
|
||||
Convert::raw2xml($child->markingClasses()),
|
||||
Convert::raw2xml($child->markingClasses($self->numChildrenMethod)),
|
||||
($self->nodeIsDisabled($child)) ? 'disabled' : '',
|
||||
(int)$child->ID,
|
||||
$child->obj($labelField)->forTemplate()
|
||||
@ -330,7 +351,7 @@ class TreeDropdownField extends FormField {
|
||||
null,
|
||||
true,
|
||||
$this->childrenMethod,
|
||||
'numChildren',
|
||||
$this->numChildrenMethod,
|
||||
true, // root call
|
||||
null,
|
||||
$nodeCountCallback
|
||||
@ -343,7 +364,7 @@ class TreeDropdownField extends FormField {
|
||||
null,
|
||||
true,
|
||||
$this->childrenMethod,
|
||||
'numChildren',
|
||||
$this->numChildrenMethod,
|
||||
true, // root call
|
||||
null,
|
||||
$nodeCountCallback
|
||||
@ -353,15 +374,14 @@ class TreeDropdownField extends FormField {
|
||||
}
|
||||
|
||||
/**
|
||||
* Marking public function for the tree, which combines different filters sensibly. If a filter function has been
|
||||
* set, that will be called. If the source is a folder, automatically filter folder. And if search text is set,
|
||||
* Marking public function for the tree, which combines different filters sensibly.
|
||||
* If a filter function has been set, that will be called. And if search text is set,
|
||||
* filter on that too. Return true if all applicable conditions are true, false otherwise.
|
||||
* @param $node
|
||||
* @return unknown_type
|
||||
*/
|
||||
public function filterMarking($node) {
|
||||
if ($this->filterCallback && !call_user_func($this->filterCallback, $node)) return false;
|
||||
if ($this->sourceObject == "Folder" && $node->ClassName != 'Folder') return false;
|
||||
if ($this->search != "") {
|
||||
return isset($this->searchIds[$node->ID]) && $this->searchIds[$node->ID] ? true : false;
|
||||
}
|
||||
|
@ -324,7 +324,7 @@ class Hierarchy extends DataExtension {
|
||||
}
|
||||
|
||||
// Set jstree open state, or mark it as a leaf (closed) if there are no children
|
||||
if(!$this->$numChildrenMethod()) {
|
||||
if(!$this->owner->$numChildrenMethod()) {
|
||||
$classes .= " jstree-leaf closed";
|
||||
} elseif($this->isTreeOpened()) {
|
||||
$classes .= " jstree-open";
|
||||
|
Loading…
x
Reference in New Issue
Block a user