4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.tree.DefaultSelectionModel
15 * @extends Roo.util.Observable
16 * The default single selection for a TreePanel.
18 Roo.tree.DefaultSelectionModel = function(){
23 * @event selectionchange
24 * Fires when the selected node changes
25 * @param {DefaultSelectionModel} this
26 * @param {TreeNode} node the new selection
28 "selectionchange" : true,
32 * Fires before the selected node changes, return false to cancel the change
33 * @param {DefaultSelectionModel} this
34 * @param {TreeNode} node the new selection
35 * @param {TreeNode} node the old selection
41 Roo.extend(Roo.tree.DefaultSelectionModel, Roo.util.Observable, {
42 init : function(tree){
44 tree.getTreeEl().on("keydown", this.onKeyDown, this);
45 tree.on("click", this.onNodeClick, this);
48 onNodeClick : function(node, e){
49 if (e.ctrlKey && this.selNode == node) {
58 * @param {TreeNode} node The node to select
59 * @return {TreeNode} The selected node
61 select : function(node){
62 var last = this.selNode;
63 if(last != node && this.fireEvent('beforeselect', this, node, last) !== false){
65 last.ui.onSelectedChange(false);
68 node.ui.onSelectedChange(true);
69 this.fireEvent("selectionchange", this, node, last);
76 * @param {TreeNode} node The node to unselect
78 unselect : function(node){
79 if(this.selNode == node){
80 this.clearSelections();
85 * Clear all selections
87 clearSelections : function(){
90 n.ui.onSelectedChange(false);
92 this.fireEvent("selectionchange", this, null);
98 * Get the selected node
99 * @return {TreeNode} The selected node
101 getSelectedNode : function(){
106 * Returns true if the node is selected
107 * @param {TreeNode} node The node to check
110 isSelected : function(node){
111 return this.selNode == node;
115 * Selects the node above the selected node in the tree, intelligently walking the nodes
116 * @return TreeNode The new selection
118 selectPrevious : function(){
119 var s = this.selNode || this.lastSelNode;
123 var ps = s.previousSibling;
125 if(!ps.isExpanded() || ps.childNodes.length < 1){
126 return this.select(ps);
128 var lc = ps.lastChild;
129 while(lc && lc.isExpanded() && lc.childNodes.length > 0){
132 return this.select(lc);
134 } else if(s.parentNode && (this.tree.rootVisible || !s.parentNode.isRoot)){
135 return this.select(s.parentNode);
141 * Selects the node above the selected node in the tree, intelligently walking the nodes
142 * @return TreeNode The new selection
144 selectNext : function(){
145 var s = this.selNode || this.lastSelNode;
149 if(s.firstChild && s.isExpanded()){
150 return this.select(s.firstChild);
151 }else if(s.nextSibling){
152 return this.select(s.nextSibling);
153 }else if(s.parentNode){
155 s.parentNode.bubble(function(){
156 if(this.nextSibling){
157 newS = this.getOwnerTree().selModel.select(this.nextSibling);
166 onKeyDown : function(e){
167 var s = this.selNode || this.lastSelNode;
168 // undesirable, but required
181 this.selectPrevious();
185 if(s.hasChildNodes()){
188 }else if(s.firstChild){
189 this.select(s.firstChild, e);
195 if(s.hasChildNodes() && s.isExpanded()){
197 }else if(s.parentNode && (this.tree.rootVisible || s.parentNode != this.tree.getRootNode())){
198 this.select(s.parentNode, e);
206 * @class Roo.tree.MultiSelectionModel
207 * @extends Roo.util.Observable
208 * Multi selection for a TreePanel.
210 Roo.tree.MultiSelectionModel = function(){
215 * @event selectionchange
216 * Fires when the selected nodes change
217 * @param {MultiSelectionModel} this
218 * @param {Array} nodes Array of the selected nodes
220 "selectionchange" : true
224 Roo.extend(Roo.tree.MultiSelectionModel, Roo.util.Observable, {
225 init : function(tree){
227 tree.getTreeEl().on("keydown", this.onKeyDown, this);
228 tree.on("click", this.onNodeClick, this);
231 onNodeClick : function(node, e){
232 this.select(node, e, e.ctrlKey);
237 * @param {TreeNode} node The node to select
238 * @param {EventObject} e (optional) An event associated with the selection
239 * @param {Boolean} keepExisting True to retain existing selections
240 * @return {TreeNode} The selected node
242 select : function(node, e, keepExisting){
243 if(keepExisting !== true){
244 this.clearSelections(true);
246 if(this.isSelected(node)){
247 this.lastSelNode = node;
250 this.selNodes.push(node);
251 this.selMap[node.id] = node;
252 this.lastSelNode = node;
253 node.ui.onSelectedChange(true);
254 this.fireEvent("selectionchange", this, this.selNodes);
260 * @param {TreeNode} node The node to unselect
262 unselect : function(node){
263 if(this.selMap[node.id]){
264 node.ui.onSelectedChange(false);
265 var sn = this.selNodes;
268 index = sn.indexOf(node);
270 for(var i = 0, len = sn.length; i < len; i++){
278 this.selNodes.splice(index, 1);
280 delete this.selMap[node.id];
281 this.fireEvent("selectionchange", this, this.selNodes);
286 * Clear all selections
288 clearSelections : function(suppressEvent){
289 var sn = this.selNodes;
291 for(var i = 0, len = sn.length; i < len; i++){
292 sn[i].ui.onSelectedChange(false);
296 if(suppressEvent !== true){
297 this.fireEvent("selectionchange", this, this.selNodes);
303 * Returns true if the node is selected
304 * @param {TreeNode} node The node to check
307 isSelected : function(node){
308 return this.selMap[node.id] ? true : false;
312 * Returns an array of the selected nodes
315 getSelectedNodes : function(){
316 return this.selNodes;
319 onKeyDown : Roo.tree.DefaultSelectionModel.prototype.onKeyDown,
321 selectNext : Roo.tree.DefaultSelectionModel.prototype.selectNext,
323 selectPrevious : Roo.tree.DefaultSelectionModel.prototype.selectPrevious