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 * @extends Roo.util.Observable
15 * Create a "View" for an element based on a data model or UpdateManager and the supplied DomHelper template.
16 * This class also supports single and multi selection modes. <br>
17 * Create a data model bound view:
19 var store = new Roo.data.Store(...);
21 var view = new Roo.View("my-element",
22 '<div id="{0}">{2} - {1}</div>', // auto create template
25 selectedClass: "ydataview-selected",
29 // listen for node click?
30 view.on("click", function(vw, index, node, e){
31 alert('Node "' + node.id + '" at index: ' + index + " was clicked.");
35 dataModel.load("foobar.xml");
37 For an example of creating a JSON/UpdateManager view, see {@link Roo.JsonView}.
39 * <b>Note: The root of your template must be a single node. Table/row implementations may work but are not supported due to
40 * IE"s limited insertion support with tables and Opera"s faulty event bubbling.</b>
43 * @param {String/HTMLElement/Element} container The container element where the view is to be rendered.
44 * @param {String/DomHelper.Template} tpl The rendering template or a string to create a template with
45 * @param {Object} config The config object
47 Roo.View = function(container, tpl, config){
48 this.el = Roo.get(container);
49 if(typeof tpl == "string"){
50 tpl = new Roo.Template(tpl);
54 * The template used by this View
55 * @type {Roo.DomHelper.Template}
59 Roo.apply(this, config);
65 * Fires before a click is processed. Returns false to cancel the default action.
66 * @param {Roo.View} this
67 * @param {Number} index The index of the target node
68 * @param {HTMLElement} node The target node
69 * @param {Roo.EventObject} e The raw event object
74 * Fires when a template node is clicked.
75 * @param {Roo.View} this
76 * @param {Number} index The index of the target node
77 * @param {HTMLElement} node The target node
78 * @param {Roo.EventObject} e The raw event object
83 * Fires when a template node is double clicked.
84 * @param {Roo.View} this
85 * @param {Number} index The index of the target node
86 * @param {HTMLElement} node The target node
87 * @param {Roo.EventObject} e The raw event object
92 * Fires when a template node is right clicked.
93 * @param {Roo.View} this
94 * @param {Number} index The index of the target node
95 * @param {HTMLElement} node The target node
96 * @param {Roo.EventObject} e The raw event object
100 * @event selectionchange
101 * Fires when the selected nodes change.
102 * @param {Roo.View} this
103 * @param {Array} selections Array of the selected nodes
105 "selectionchange" : true,
108 * @event beforeselect
109 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
110 * @param {Roo.View} this
111 * @param {HTMLElement} node The node to be selected
112 * @param {Array} selections Array of currently selected nodes
114 "beforeselect" : true
118 "click": this.onClick,
119 "dblclick": this.onDblClick,
120 "contextmenu": this.onContextMenu,
124 this.selections = [];
126 this.cmp = new Roo.CompositeElementLite([]);
128 this.store = Roo.factory(this.store, Roo.data);
129 this.setStore(this.store, true);
131 Roo.View.superclass.constructor.call(this);
134 Roo.extend(Roo.View, Roo.util.Observable, {
137 * The template used by this View
138 * @cfg {String|Roo.DomHelper.Template}
142 * The css class to add to selected nodes
143 * @type {Roo.DomHelper.Template}
145 selectedClass : "x-view-selected",
149 * Returns the element this view is bound to.
150 * @return {Roo.Element}
157 * Refreshes the view.
159 refresh : function(){
161 this.clearSelections();
164 var records = this.store.getRange();
165 if(records.length < 1){
166 this.el.update(this.emptyText);
169 for(var i = 0, len = records.length; i < len; i++){
170 var data = this.prepareData(records[i].data, i, records[i]);
171 html[html.length] = t.apply(data);
173 this.el.update(html.join(""));
174 this.nodes = this.el.dom.childNodes;
175 this.updateIndexes(0);
179 * Function to override to reformat the data that is sent to
180 * the template for each node.
181 * @param {Array/Object} data The raw data (array of colData for a data model bound view or
182 * a JSON object for an UpdateManager bound view).
184 prepareData : function(data){
188 onUpdate : function(ds, record){
189 this.clearSelections();
190 var index = this.store.indexOf(record);
191 var n = this.nodes[index];
192 this.tpl.insertBefore(n, this.prepareData(record.data));
193 n.parentNode.removeChild(n);
194 this.updateIndexes(index, index);
197 onAdd : function(ds, records, index){
198 this.clearSelections();
199 if(this.nodes.length == 0){
203 var n = this.nodes[index];
204 for(var i = 0, len = records.length; i < len; i++){
205 var d = this.prepareData(records[i].data);
207 this.tpl.insertBefore(n, d);
209 this.tpl.append(this.el, d);
212 this.updateIndexes(index);
215 onRemove : function(ds, record, index){
216 this.clearSelections();
217 this.el.dom.removeChild(this.nodes[index]);
218 this.updateIndexes(index);
222 * Refresh an individual node.
223 * @param {Number} index
225 refreshNode : function(index){
226 this.onUpdate(this.store, this.store.getAt(index));
229 updateIndexes : function(startIndex, endIndex){
231 startIndex = startIndex || 0;
232 endIndex = endIndex || ns.length - 1;
233 for(var i = startIndex; i <= endIndex; i++){
239 * Changes the data store this view uses and refresh the view.
240 * @param {Store} store
242 setStore : function(store, initial){
243 if(!initial && this.store){
244 this.store.un("datachanged", this.refresh);
245 this.store.un("add", this.onAdd);
246 this.store.un("remove", this.onRemove);
247 this.store.un("update", this.onUpdate);
248 this.store.un("clear", this.refresh);
252 store.on("datachanged", this.refresh, this);
253 store.on("add", this.onAdd, this);
254 store.on("remove", this.onRemove, this);
255 store.on("update", this.onUpdate, this);
256 store.on("clear", this.refresh, this);
265 * Returns the template node the passed child belongs to or null if it doesn't belong to one.
266 * @param {HTMLElement} node
267 * @return {HTMLElement} The template node
269 findItemFromChild : function(node){
270 var el = this.el.dom;
271 if(!node || node.parentNode == el){
274 var p = node.parentNode;
276 if(p.parentNode == el){
285 onClick : function(e){
286 var item = this.findItemFromChild(e.getTarget());
288 var index = this.indexOf(item);
289 if(this.onItemClick(item, index, e) !== false){
290 this.fireEvent("click", this, index, item, e);
293 this.clearSelections();
298 onContextMenu : function(e){
299 var item = this.findItemFromChild(e.getTarget());
301 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
306 onDblClick : function(e){
307 var item = this.findItemFromChild(e.getTarget());
309 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
313 onItemClick : function(item, index, e){
314 if(this.fireEvent("beforeclick", this, index, item, e) === false){
317 if(this.multiSelect || this.singleSelect){
318 if(this.multiSelect && e.shiftKey && this.lastSelection){
319 this.select(this.getNodes(this.indexOf(this.lastSelection), index), false);
321 this.select(item, this.multiSelect && e.ctrlKey);
322 this.lastSelection = item;
330 * Get the number of selected nodes.
333 getSelectionCount : function(){
334 return this.selections.length;
338 * Get the currently selected nodes.
339 * @return {Array} An array of HTMLElements
341 getSelectedNodes : function(){
342 return this.selections;
346 * Get the indexes of the selected nodes.
349 getSelectedIndexes : function(){
350 var indexes = [], s = this.selections;
351 for(var i = 0, len = s.length; i < len; i++){
352 indexes.push(s[i].nodeIndex);
358 * Clear all selections
359 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange event
361 clearSelections : function(suppressEvent){
362 if(this.nodes && (this.multiSelect || this.singleSelect) && this.selections.length > 0){
363 this.cmp.elements = this.selections;
364 this.cmp.removeClass(this.selectedClass);
365 this.selections = [];
367 this.fireEvent("selectionchange", this, this.selections);
373 * Returns true if the passed node is selected
374 * @param {HTMLElement/Number} node The node or node index
377 isSelected : function(node){
378 var s = this.selections;
382 node = this.getNode(node);
383 return s.indexOf(node) !== -1;
388 * @param {Array/HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node, id of a template node or an array of any of those to select
389 * @param {Boolean} keepExisting (optional) true to keep existing selections
390 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
392 select : function(nodeInfo, keepExisting, suppressEvent){
393 if(nodeInfo instanceof Array){
395 this.clearSelections(true);
397 for(var i = 0, len = nodeInfo.length; i < len; i++){
398 this.select(nodeInfo[i], true, true);
401 var node = this.getNode(nodeInfo);
402 if(node && !this.isSelected(node)){
404 this.clearSelections(true);
406 if(this.fireEvent("beforeselect", this, node, this.selections) !== false){
407 Roo.fly(node).addClass(this.selectedClass);
408 this.selections.push(node);
410 this.fireEvent("selectionchange", this, this.selections);
418 * Gets a template node.
419 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
420 * @return {HTMLElement} The node or null if it wasn't found
422 getNode : function(nodeInfo){
423 if(typeof nodeInfo == "string"){
424 return document.getElementById(nodeInfo);
425 }else if(typeof nodeInfo == "number"){
426 return this.nodes[nodeInfo];
432 * Gets a range template nodes.
433 * @param {Number} startIndex
434 * @param {Number} endIndex
435 * @return {Array} An array of nodes
437 getNodes : function(start, end){
440 end = typeof end == "undefined" ? ns.length - 1 : end;
443 for(var i = start; i <= end; i++){
447 for(var i = start; i >= end; i--){
455 * Finds the index of the passed node
456 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
457 * @return {Number} The index of the node or -1
459 indexOf : function(node){
460 node = this.getNode(node);
461 if(typeof node.nodeIndex == "number"){
462 return node.nodeIndex;
465 for(var i = 0, len = ns.length; i < len; i++){