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(cfg){
25 * @event selectionchange
26 * Fires when the selected node changes
27 * @param {DefaultSelectionModel} this
28 * @param {TreeNode} node the new selection
30 "selectionchange" : true,
34 * Fires before the selected node changes, return false to cancel the change
35 * @param {DefaultSelectionModel} this
36 * @param {TreeNode} node the new selection
37 * @param {TreeNode} node the old selection
42 Roo.tree.DefaultSelectionModel.superclass.constructor.call(this,cfg);
45 Roo.extend(Roo.tree.DefaultSelectionModel, Roo.util.Observable, {
46 init : function(tree){
48 tree.getTreeEl().on("keydown", this.onKeyDown, this);
49 tree.on("click", this.onNodeClick, this);
52 onNodeClick : function(node, e){
53 if (e.ctrlKey && this.selNode == node) {
62 * @param {TreeNode} node The node to select
63 * @return {TreeNode} The selected node
65 select : function(node){
66 var last = this.selNode;
67 if(last != node && this.fireEvent('beforeselect', this, node, last) !== false){
69 last.ui.onSelectedChange(false);
72 node.ui.onSelectedChange(true);
73 this.fireEvent("selectionchange", this, node, last);
80 * @param {TreeNode} node The node to unselect
82 unselect : function(node){
83 if(this.selNode == node){
84 this.clearSelections();
89 * Clear all selections
91 clearSelections : function(){
94 n.ui.onSelectedChange(false);
96 this.fireEvent("selectionchange", this, null);
102 * Get the selected node
103 * @return {TreeNode} The selected node
105 getSelectedNode : function(){
110 * Returns true if the node is selected
111 * @param {TreeNode} node The node to check
114 isSelected : function(node){
115 return this.selNode == node;
119 * Selects the node above the selected node in the tree, intelligently walking the nodes
120 * @return TreeNode The new selection
122 selectPrevious : function(){
123 var s = this.selNode || this.lastSelNode;
127 var ps = s.previousSibling;
129 if(!ps.isExpanded() || ps.childNodes.length < 1){
130 return this.select(ps);
132 var lc = ps.lastChild;
133 while(lc && lc.isExpanded() && lc.childNodes.length > 0){
136 return this.select(lc);
138 } else if(s.parentNode && (this.tree.rootVisible || !s.parentNode.isRoot)){
139 return this.select(s.parentNode);
145 * Selects the node above the selected node in the tree, intelligently walking the nodes
146 * @return TreeNode The new selection
148 selectNext : function(){
149 var s = this.selNode || this.lastSelNode;
153 if(s.firstChild && s.isExpanded()){
154 return this.select(s.firstChild);
155 }else if(s.nextSibling){
156 return this.select(s.nextSibling);
157 }else if(s.parentNode){
159 s.parentNode.bubble(function(){
160 if(this.nextSibling){
161 newS = this.getOwnerTree().selModel.select(this.nextSibling);
170 onKeyDown : function(e){
171 var s = this.selNode || this.lastSelNode;
172 // undesirable, but required
185 this.selectPrevious();
189 if(s.hasChildNodes()){
192 }else if(s.firstChild){
193 this.select(s.firstChild, e);
199 if(s.hasChildNodes() && s.isExpanded()){
201 }else if(s.parentNode && (this.tree.rootVisible || s.parentNode != this.tree.getRootNode())){
202 this.select(s.parentNode, e);
210 * @class Roo.tree.MultiSelectionModel
211 * @extends Roo.util.Observable
212 * Multi selection for a TreePanel.
214 Roo.tree.MultiSelectionModel = function(){
219 * @event selectionchange
220 * Fires when the selected nodes change
221 * @param {MultiSelectionModel} this
222 * @param {Array} nodes Array of the selected nodes
224 "selectionchange" : true
226 Roo.tree.MultiSelectionModel.superclass.constructor.call(this,cfg);
230 Roo.extend(Roo.tree.MultiSelectionModel, Roo.util.Observable, {
231 init : function(tree){
233 tree.getTreeEl().on("keydown", this.onKeyDown, this);
234 tree.on("click", this.onNodeClick, this);
237 onNodeClick : function(node, e){
238 this.select(node, e, e.ctrlKey);
243 * @param {TreeNode} node The node to select
244 * @param {EventObject} e (optional) An event associated with the selection
245 * @param {Boolean} keepExisting True to retain existing selections
246 * @return {TreeNode} The selected node
248 select : function(node, e, keepExisting){
249 if(keepExisting !== true){
250 this.clearSelections(true);
252 if(this.isSelected(node)){
253 this.lastSelNode = node;
256 this.selNodes.push(node);
257 this.selMap[node.id] = node;
258 this.lastSelNode = node;
259 node.ui.onSelectedChange(true);
260 this.fireEvent("selectionchange", this, this.selNodes);
266 * @param {TreeNode} node The node to unselect
268 unselect : function(node){
269 if(this.selMap[node.id]){
270 node.ui.onSelectedChange(false);
271 var sn = this.selNodes;
274 index = sn.indexOf(node);
276 for(var i = 0, len = sn.length; i < len; i++){
284 this.selNodes.splice(index, 1);
286 delete this.selMap[node.id];
287 this.fireEvent("selectionchange", this, this.selNodes);
292 * Clear all selections
294 clearSelections : function(suppressEvent){
295 var sn = this.selNodes;
297 for(var i = 0, len = sn.length; i < len; i++){
298 sn[i].ui.onSelectedChange(false);
302 if(suppressEvent !== true){
303 this.fireEvent("selectionchange", this, this.selNodes);
309 * Returns true if the node is selected
310 * @param {TreeNode} node The node to check
313 isSelected : function(node){
314 return this.selMap[node.id] ? true : false;
318 * Returns an array of the selected nodes
321 getSelectedNodes : function(){
322 return this.selNodes;
325 onKeyDown : Roo.tree.DefaultSelectionModel.prototype.onKeyDown,
327 selectNext : Roo.tree.DefaultSelectionModel.prototype.selectNext,
329 selectPrevious : Roo.tree.DefaultSelectionModel.prototype.selectPrevious