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
228 Roo.extend(Roo.tree.MultiSelectionModel, Roo.util.Observable, {
229 init : function(tree){
231 tree.getTreeEl().on("keydown", this.onKeyDown, this);
232 tree.on("click", this.onNodeClick, this);
235 onNodeClick : function(node, e){
236 this.select(node, e, e.ctrlKey);
241 * @param {TreeNode} node The node to select
242 * @param {EventObject} e (optional) An event associated with the selection
243 * @param {Boolean} keepExisting True to retain existing selections
244 * @return {TreeNode} The selected node
246 select : function(node, e, keepExisting){
247 if(keepExisting !== true){
248 this.clearSelections(true);
250 if(this.isSelected(node)){
251 this.lastSelNode = node;
254 this.selNodes.push(node);
255 this.selMap[node.id] = node;
256 this.lastSelNode = node;
257 node.ui.onSelectedChange(true);
258 this.fireEvent("selectionchange", this, this.selNodes);
264 * @param {TreeNode} node The node to unselect
266 unselect : function(node){
267 if(this.selMap[node.id]){
268 node.ui.onSelectedChange(false);
269 var sn = this.selNodes;
272 index = sn.indexOf(node);
274 for(var i = 0, len = sn.length; i < len; i++){
282 this.selNodes.splice(index, 1);
284 delete this.selMap[node.id];
285 this.fireEvent("selectionchange", this, this.selNodes);
290 * Clear all selections
292 clearSelections : function(suppressEvent){
293 var sn = this.selNodes;
295 for(var i = 0, len = sn.length; i < len; i++){
296 sn[i].ui.onSelectedChange(false);
300 if(suppressEvent !== true){
301 this.fireEvent("selectionchange", this, this.selNodes);
307 * Returns true if the node is selected
308 * @param {TreeNode} node The node to check
311 isSelected : function(node){
312 return this.selMap[node.id] ? true : false;
316 * Returns an array of the selected nodes
319 getSelectedNodes : function(){
320 return this.selNodes;
323 onKeyDown : Roo.tree.DefaultSelectionModel.prototype.onKeyDown,
325 selectNext : Roo.tree.DefaultSelectionModel.prototype.selectNext,
327 selectPrevious : Roo.tree.DefaultSelectionModel.prototype.selectPrevious