3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.tree.DefaultSelectionModel"></div>/**
\r
15 * @class Ext.tree.DefaultSelectionModel
\r
16 * @extends Ext.util.Observable
\r
17 * The default single selection for a TreePanel.
\r
19 Ext.tree.DefaultSelectionModel = function(config){
\r
20 this.selNode = null;
\r
23 <div id="event-Ext.tree.DefaultSelectionModel-selectionchange"></div>/**
\r
24 * @event selectionchange
\r
25 * Fires when the selected node changes
\r
26 * @param {DefaultSelectionModel} this
\r
27 * @param {TreeNode} node the new selection
\r
31 <div id="event-Ext.tree.DefaultSelectionModel-beforeselect"></div>/**
\r
32 * @event beforeselect
\r
33 * Fires before the selected node changes, return false to cancel the change
\r
34 * @param {DefaultSelectionModel} this
\r
35 * @param {TreeNode} node the new selection
\r
36 * @param {TreeNode} node the old selection
\r
41 Ext.apply(this, config);
\r
42 Ext.tree.DefaultSelectionModel.superclass.constructor.call(this);
\r
45 Ext.extend(Ext.tree.DefaultSelectionModel, Ext.util.Observable, {
\r
46 init : function(tree){
\r
48 tree.mon(tree.getTreeEl(), 'keydown', this.onKeyDown, this);
\r
49 tree.on("click", this.onNodeClick, this);
\r
52 onNodeClick : function(node, e){
\r
56 <div id="method-Ext.tree.DefaultSelectionModel-select"></div>/**
\r
58 * @param {TreeNode} node The node to select
\r
59 * @return {TreeNode} The selected node
\r
61 select : function(node, /* private*/ selectNextNode){
\r
62 // If node is hidden, select the next node in whatever direction was being moved in.
\r
63 if (!Ext.fly(node.ui.wrap).isVisible() && selectNextNode) {
\r
64 return selectNextNode.call(this, node);
\r
66 var last = this.selNode;
\r
68 node.ui.onSelectedChange(true);
\r
69 }else if(this.fireEvent('beforeselect', this, node, last) !== false){
\r
71 last.ui.onSelectedChange(false);
\r
73 this.selNode = node;
\r
74 node.ui.onSelectedChange(true);
\r
75 this.fireEvent("selectionchange", this, node, last);
\r
80 <div id="method-Ext.tree.DefaultSelectionModel-unselect"></div>/**
\r
82 * @param {TreeNode} node The node to unselect
\r
84 unselect : function(node){
\r
85 if(this.selNode == node){
\r
86 this.clearSelections();
\r
90 <div id="method-Ext.tree.DefaultSelectionModel-clearSelections"></div>/**
\r
91 * Clear all selections
\r
93 clearSelections : function(){
\r
94 var n = this.selNode;
\r
96 n.ui.onSelectedChange(false);
\r
97 this.selNode = null;
\r
98 this.fireEvent("selectionchange", this, null);
\r
103 <div id="method-Ext.tree.DefaultSelectionModel-getSelectedNode"></div>/**
\r
104 * Get the selected node
\r
105 * @return {TreeNode} The selected node
\r
107 getSelectedNode : function(){
\r
108 return this.selNode;
\r
111 <div id="method-Ext.tree.DefaultSelectionModel-isSelected"></div>/**
\r
112 * Returns true if the node is selected
\r
113 * @param {TreeNode} node The node to check
\r
114 * @return {Boolean}
\r
116 isSelected : function(node){
\r
117 return this.selNode == node;
\r
120 <div id="method-Ext.tree.DefaultSelectionModel-selectPrevious"></div>/**
\r
121 * Selects the node above the selected node in the tree, intelligently walking the nodes
\r
122 * @return TreeNode The new selection
\r
124 selectPrevious : function(/* private */ s){
\r
125 if(!(s = s || this.selNode || this.lastSelNode)){
\r
128 // Here we pass in the current function to select to indicate the direction we're moving
\r
129 var ps = s.previousSibling;
\r
131 if(!ps.isExpanded() || ps.childNodes.length < 1){
\r
132 return this.select(ps, this.selectPrevious);
\r
134 var lc = ps.lastChild;
\r
135 while(lc && lc.isExpanded() && Ext.fly(lc.ui.wrap).isVisible() && lc.childNodes.length > 0){
\r
138 return this.select(lc, this.selectPrevious);
\r
140 } else if(s.parentNode && (this.tree.rootVisible || !s.parentNode.isRoot)){
\r
141 return this.select(s.parentNode, this.selectPrevious);
\r
146 <div id="method-Ext.tree.DefaultSelectionModel-selectNext"></div>/**
\r
147 * Selects the node above the selected node in the tree, intelligently walking the nodes
\r
148 * @return TreeNode The new selection
\r
150 selectNext : function(/* private */ s){
\r
151 if(!(s = s || this.selNode || this.lastSelNode)){
\r
154 // Here we pass in the current function to select to indicate the direction we're moving
\r
155 if(s.firstChild && s.isExpanded() && Ext.fly(s.ui.wrap).isVisible()){
\r
156 return this.select(s.firstChild, this.selectNext);
\r
157 }else if(s.nextSibling){
\r
158 return this.select(s.nextSibling, this.selectNext);
\r
159 }else if(s.parentNode){
\r
161 s.parentNode.bubble(function(){
\r
162 if(this.nextSibling){
\r
163 newS = this.getOwnerTree().selModel.select(this.nextSibling, this.selectNext);
\r
172 onKeyDown : function(e){
\r
173 var s = this.selNode || this.lastSelNode;
\r
174 // undesirable, but required
\r
179 var k = e.getKey();
\r
187 this.selectPrevious();
\r
190 e.preventDefault();
\r
191 if(s.hasChildNodes()){
\r
192 if(!s.isExpanded()){
\r
194 }else if(s.firstChild){
\r
195 this.select(s.firstChild, e);
\r
200 e.preventDefault();
\r
201 if(s.hasChildNodes() && s.isExpanded()){
\r
203 }else if(s.parentNode && (this.tree.rootVisible || s.parentNode != this.tree.getRootNode())){
\r
204 this.select(s.parentNode, e);
\r
211 <div id="cls-Ext.tree.MultiSelectionModel"></div>/**
\r
212 * @class Ext.tree.MultiSelectionModel
\r
213 * @extends Ext.util.Observable
\r
214 * Multi selection for a TreePanel.
\r
216 Ext.tree.MultiSelectionModel = function(config){
\r
217 this.selNodes = [];
\r
220 <div id="event-Ext.tree.MultiSelectionModel-selectionchange"></div>/**
\r
221 * @event selectionchange
\r
222 * Fires when the selected nodes change
\r
223 * @param {MultiSelectionModel} this
\r
224 * @param {Array} nodes Array of the selected nodes
\r
228 Ext.apply(this, config);
\r
229 Ext.tree.MultiSelectionModel.superclass.constructor.call(this);
\r
232 Ext.extend(Ext.tree.MultiSelectionModel, Ext.util.Observable, {
\r
233 init : function(tree){
\r
235 tree.mon(tree.getTreeEl(), 'keydown', this.onKeyDown, this);
\r
236 tree.on("click", this.onNodeClick, this);
\r
239 onNodeClick : function(node, e){
\r
240 if(e.ctrlKey && this.isSelected(node)){
\r
241 this.unselect(node);
\r
243 this.select(node, e, e.ctrlKey);
\r
247 <div id="method-Ext.tree.MultiSelectionModel-select"></div>/**
\r
249 * @param {TreeNode} node The node to select
\r
250 * @param {EventObject} e (optional) An event associated with the selection
\r
251 * @param {Boolean} keepExisting True to retain existing selections
\r
252 * @return {TreeNode} The selected node
\r
254 select : function(node, e, keepExisting){
\r
255 if(keepExisting !== true){
\r
256 this.clearSelections(true);
\r
258 if(this.isSelected(node)){
\r
259 this.lastSelNode = node;
\r
262 this.selNodes.push(node);
\r
263 this.selMap[node.id] = node;
\r
264 this.lastSelNode = node;
\r
265 node.ui.onSelectedChange(true);
\r
266 this.fireEvent("selectionchange", this, this.selNodes);
\r
270 <div id="method-Ext.tree.MultiSelectionModel-unselect"></div>/**
\r
272 * @param {TreeNode} node The node to unselect
\r
274 unselect : function(node){
\r
275 if(this.selMap[node.id]){
\r
276 node.ui.onSelectedChange(false);
\r
277 var sn = this.selNodes;
\r
278 var index = sn.indexOf(node);
\r
280 this.selNodes.splice(index, 1);
\r
282 delete this.selMap[node.id];
\r
283 this.fireEvent("selectionchange", this, this.selNodes);
\r
287 <div id="method-Ext.tree.MultiSelectionModel-clearSelections"></div>/**
\r
288 * Clear all selections
\r
290 clearSelections : function(suppressEvent){
\r
291 var sn = this.selNodes;
\r
293 for(var i = 0, len = sn.length; i < len; i++){
\r
294 sn[i].ui.onSelectedChange(false);
\r
296 this.selNodes = [];
\r
298 if(suppressEvent !== true){
\r
299 this.fireEvent("selectionchange", this, this.selNodes);
\r
304 <div id="method-Ext.tree.MultiSelectionModel-isSelected"></div>/**
\r
305 * Returns true if the node is selected
\r
306 * @param {TreeNode} node The node to check
\r
307 * @return {Boolean}
\r
309 isSelected : function(node){
\r
310 return this.selMap[node.id] ? true : false;
\r
313 <div id="method-Ext.tree.MultiSelectionModel-getSelectedNodes"></div>/**
\r
314 * Returns an array of the selected nodes
\r
317 getSelectedNodes : function(){
\r
318 return this.selNodes;
\r
321 onKeyDown : Ext.tree.DefaultSelectionModel.prototype.onKeyDown,
\r
323 selectNext : Ext.tree.DefaultSelectionModel.prototype.selectNext,
\r
325 selectPrevious : Ext.tree.DefaultSelectionModel.prototype.selectPrevious
\r