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.
17 * @param {Object} cfg Configuration
19 Roo.tree.DefaultSelectionModel = function(cfg){
26 * @event selectionchange
27 * Fires when the selected node changes
28 * @param {DefaultSelectionModel} this
29 * @param {TreeNode} node the new selection
31 "selectionchange" : true,
35 * Fires before the selected node changes, return false to cancel the change
36 * @param {DefaultSelectionModel} this
37 * @param {TreeNode} node the new selection
38 * @param {TreeNode} node the old selection
43 Roo.tree.DefaultSelectionModel.superclass.constructor.call(this,cfg);
46 Roo.extend(Roo.tree.DefaultSelectionModel, Roo.util.Observable, {
47 init : function(tree){
49 tree.getTreeEl().on("keydown", this.onKeyDown, this);
50 tree.on("click", this.onNodeClick, this);
53 onNodeClick : function(node, e){
54 if (e.ctrlKey && this.selNode == node) {
63 * @param {TreeNode} node The node to select
64 * @return {TreeNode} The selected node
66 select : function(node){
67 var last = this.selNode;
68 if(last != node && this.fireEvent('beforeselect', this, node, last) !== false){
70 last.ui.onSelectedChange(false);
73 node.ui.onSelectedChange(true);
74 this.fireEvent("selectionchange", this, node, last);
81 * @param {TreeNode} node The node to unselect
83 unselect : function(node){
84 if(this.selNode == node){
85 this.clearSelections();
90 * Clear all selections
92 clearSelections : function(){
95 n.ui.onSelectedChange(false);
97 this.fireEvent("selectionchange", this, null);
103 * Get the selected node
104 * @return {TreeNode} The selected node
106 getSelectedNode : function(){
111 * Returns true if the node is selected
112 * @param {TreeNode} node The node to check
115 isSelected : function(node){
116 return this.selNode == node;
120 * Selects the node above the selected node in the tree, intelligently walking the nodes
121 * @return TreeNode The new selection
123 selectPrevious : function(){
124 var s = this.selNode || this.lastSelNode;
128 var ps = s.previousSibling;
130 if(!ps.isExpanded() || ps.childNodes.length < 1){
131 return this.select(ps);
133 var lc = ps.lastChild;
134 while(lc && lc.isExpanded() && lc.childNodes.length > 0){
137 return this.select(lc);
139 } else if(s.parentNode && (this.tree.rootVisible || !s.parentNode.isRoot)){
140 return this.select(s.parentNode);
146 * Selects the node above the selected node in the tree, intelligently walking the nodes
147 * @return TreeNode The new selection
149 selectNext : function(){
150 var s = this.selNode || this.lastSelNode;
154 if(s.firstChild && s.isExpanded()){
155 return this.select(s.firstChild);
156 }else if(s.nextSibling){
157 return this.select(s.nextSibling);
158 }else if(s.parentNode){
160 s.parentNode.bubble(function(){
161 if(this.nextSibling){
162 newS = this.getOwnerTree().selModel.select(this.nextSibling);
171 onKeyDown : function(e){
172 var s = this.selNode || this.lastSelNode;
173 // undesirable, but required
186 this.selectPrevious();
190 if(s.hasChildNodes()){
193 }else if(s.firstChild){
194 this.select(s.firstChild, e);
200 if(s.hasChildNodes() && s.isExpanded()){
202 }else if(s.parentNode && (this.tree.rootVisible || s.parentNode != this.tree.getRootNode())){
203 this.select(s.parentNode, e);
211 * @class Roo.tree.MultiSelectionModel
212 * @extends Roo.util.Observable
213 * Multi selection for a TreePanel.
214 * @param {Object} cfg Configuration
216 Roo.tree.MultiSelectionModel = function(){
221 * @event selectionchange
222 * Fires when the selected nodes change
223 * @param {MultiSelectionModel} this
224 * @param {Array} nodes Array of the selected nodes
226 "selectionchange" : true
228 Roo.tree.MultiSelectionModel.superclass.constructor.call(this,cfg);
232 Roo.extend(Roo.tree.MultiSelectionModel, Roo.util.Observable, {
233 init : function(tree){
235 tree.getTreeEl().on("keydown", this.onKeyDown, this);
236 tree.on("click", this.onNodeClick, this);
239 onNodeClick : function(node, e){
240 this.select(node, e, e.ctrlKey);
245 * @param {TreeNode} node The node to select
246 * @param {EventObject} e (optional) An event associated with the selection
247 * @param {Boolean} keepExisting True to retain existing selections
248 * @return {TreeNode} The selected node
250 select : function(node, e, keepExisting){
251 if(keepExisting !== true){
252 this.clearSelections(true);
254 if(this.isSelected(node)){
255 this.lastSelNode = node;
258 this.selNodes.push(node);
259 this.selMap[node.id] = node;
260 this.lastSelNode = node;
261 node.ui.onSelectedChange(true);
262 this.fireEvent("selectionchange", this, this.selNodes);
268 * @param {TreeNode} node The node to unselect
270 unselect : function(node){
271 if(this.selMap[node.id]){
272 node.ui.onSelectedChange(false);
273 var sn = this.selNodes;
276 index = sn.indexOf(node);
278 for(var i = 0, len = sn.length; i < len; i++){
286 this.selNodes.splice(index, 1);
288 delete this.selMap[node.id];
289 this.fireEvent("selectionchange", this, this.selNodes);
294 * Clear all selections
296 clearSelections : function(suppressEvent){
297 var sn = this.selNodes;
299 for(var i = 0, len = sn.length; i < len; i++){
300 sn[i].ui.onSelectedChange(false);
304 if(suppressEvent !== true){
305 this.fireEvent("selectionchange", this, this.selNodes);
311 * Returns true if the node is selected
312 * @param {TreeNode} node The node to check
315 isSelected : function(node){
316 return this.selMap[node.id] ? true : false;
320 * Returns an array of the selected nodes
323 getSelectedNodes : function(){
324 return this.selNodes;
327 onKeyDown : Roo.tree.DefaultSelectionModel.prototype.onKeyDown,
329 selectNext : Roo.tree.DefaultSelectionModel.prototype.selectNext,
331 selectPrevious : Roo.tree.DefaultSelectionModel.prototype.selectPrevious