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({
23 tpl : '<div id="{0}">{2} - {1}</div>', // auto create template
26 selectedClass: "ydataview-selected",
30 // listen for node click?
31 view.on("click", function(vw, index, node, e){
32 alert('Node "' + node.id + '" at index: ' + index + " was clicked.");
36 dataModel.load("foobar.xml");
38 For an example of creating a JSON/UpdateManager view, see {@link Roo.JsonView}.
40 * <b>Note: The root of your template must be a single node. Table/row implementations may work but are not supported due to
41 * IE"s limited insertion support with tables and Opera"s faulty event bubbling.</b>
43 * Note: old style constructor is still suported (container, template, config)
47 * @param {Object} config The config object
50 Roo.View = function(config, depreciated_tpl, depreciated_config){
52 if (typeof(depreciated_tpl) == 'undefined') {
53 // new way.. - universal constructor.
54 Roo.apply(this, config);
55 this.el = Roo.get(this.el);
58 this.el = Roo.get(config);
59 this.tpl = depreciated_tpl;
60 Roo.apply(this, depreciated_config);
64 if(typeof(this.tpl) == "string"){
65 this.tpl = new Roo.Template(this.tpl);
67 this.tpl = new Roo.Factory(this.tpl, Roo);
79 * Fires before a click is processed. Returns false to cancel the default action.
80 * @param {Roo.View} this
81 * @param {Number} index The index of the target node
82 * @param {HTMLElement} node The target node
83 * @param {Roo.EventObject} e The raw event object
88 * Fires when a template node is clicked.
89 * @param {Roo.View} this
90 * @param {Number} index The index of the target node
91 * @param {HTMLElement} node The target node
92 * @param {Roo.EventObject} e The raw event object
97 * Fires when a template node is double clicked.
98 * @param {Roo.View} this
99 * @param {Number} index The index of the target node
100 * @param {HTMLElement} node The target node
101 * @param {Roo.EventObject} e The raw event object
106 * Fires when a template node is right clicked.
107 * @param {Roo.View} this
108 * @param {Number} index The index of the target node
109 * @param {HTMLElement} node The target node
110 * @param {Roo.EventObject} e The raw event object
112 "contextmenu" : true,
114 * @event selectionchange
115 * Fires when the selected nodes change.
116 * @param {Roo.View} this
117 * @param {Array} selections Array of the selected nodes
119 "selectionchange" : true,
122 * @event beforeselect
123 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
124 * @param {Roo.View} this
125 * @param {HTMLElement} node The node to be selected
126 * @param {Array} selections Array of currently selected nodes
128 "beforeselect" : true
132 "click": this.onClick,
133 "dblclick": this.onDblClick,
134 "contextmenu": this.onContextMenu,
138 this.selections = [];
140 this.cmp = new Roo.CompositeElementLite([]);
142 this.store = Roo.factory(this.store, Roo.data);
143 this.setStore(this.store, true);
145 Roo.View.superclass.constructor.call(this);
148 Roo.extend(Roo.View, Roo.util.Observable, {
151 * @cfg {Roo.data.Store} store Data store to load data from.
156 * @cfg {String|Roo.Element} el The container element.
161 * @cfg {String|Roo.Template} tpl The template used by this View
166 * @cfg {String} selectedClass The css class to add to selected nodes
168 selectedClass : "x-view-selected",
170 * @cfg {String} emptyText The empty text to show when nothing is loaded.
174 * Returns the element this view is bound to.
175 * @return {Roo.Element}
182 * Refreshes the view.
184 refresh : function(){
186 this.clearSelections();
189 var records = this.store.getRange();
190 if(records.length < 1){
191 this.el.update(this.emptyText);
194 for(var i = 0, len = records.length; i < len; i++){
195 var data = this.prepareData(records[i].data, i, records[i]);
196 html[html.length] = t.apply(data);
198 this.el.update(html.join(""));
199 this.nodes = this.el.dom.childNodes;
200 this.updateIndexes(0);
204 * Function to override to reformat the data that is sent to
205 * the template for each node.
206 * @param {Array/Object} data The raw data (array of colData for a data model bound view or
207 * a JSON object for an UpdateManager bound view).
209 prepareData : function(data){
213 onUpdate : function(ds, record){
214 this.clearSelections();
215 var index = this.store.indexOf(record);
216 var n = this.nodes[index];
217 this.tpl.insertBefore(n, this.prepareData(record.data));
218 n.parentNode.removeChild(n);
219 this.updateIndexes(index, index);
222 onAdd : function(ds, records, index){
223 this.clearSelections();
224 if(this.nodes.length == 0){
228 var n = this.nodes[index];
229 for(var i = 0, len = records.length; i < len; i++){
230 var d = this.prepareData(records[i].data);
232 this.tpl.insertBefore(n, d);
234 this.tpl.append(this.el, d);
237 this.updateIndexes(index);
240 onRemove : function(ds, record, index){
241 this.clearSelections();
242 this.el.dom.removeChild(this.nodes[index]);
243 this.updateIndexes(index);
247 * Refresh an individual node.
248 * @param {Number} index
250 refreshNode : function(index){
251 this.onUpdate(this.store, this.store.getAt(index));
254 updateIndexes : function(startIndex, endIndex){
256 startIndex = startIndex || 0;
257 endIndex = endIndex || ns.length - 1;
258 for(var i = startIndex; i <= endIndex; i++){
264 * Changes the data store this view uses and refresh the view.
265 * @param {Store} store
267 setStore : function(store, initial){
268 if(!initial && this.store){
269 this.store.un("datachanged", this.refresh);
270 this.store.un("add", this.onAdd);
271 this.store.un("remove", this.onRemove);
272 this.store.un("update", this.onUpdate);
273 this.store.un("clear", this.refresh);
277 store.on("datachanged", this.refresh, this);
278 store.on("add", this.onAdd, this);
279 store.on("remove", this.onRemove, this);
280 store.on("update", this.onUpdate, this);
281 store.on("clear", this.refresh, this);
290 * Returns the template node the passed child belongs to or null if it doesn't belong to one.
291 * @param {HTMLElement} node
292 * @return {HTMLElement} The template node
294 findItemFromChild : function(node){
295 var el = this.el.dom;
296 if(!node || node.parentNode == el){
299 var p = node.parentNode;
301 if(p.parentNode == el){
310 onClick : function(e){
311 var item = this.findItemFromChild(e.getTarget());
313 var index = this.indexOf(item);
314 if(this.onItemClick(item, index, e) !== false){
315 this.fireEvent("click", this, index, item, e);
318 this.clearSelections();
323 onContextMenu : function(e){
324 var item = this.findItemFromChild(e.getTarget());
326 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
331 onDblClick : function(e){
332 var item = this.findItemFromChild(e.getTarget());
334 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
338 onItemClick : function(item, index, e){
339 if(this.fireEvent("beforeclick", this, index, item, e) === false){
342 if(this.multiSelect || this.singleSelect){
343 if(this.multiSelect && e.shiftKey && this.lastSelection){
344 this.select(this.getNodes(this.indexOf(this.lastSelection), index), false);
346 this.select(item, this.multiSelect && e.ctrlKey);
347 this.lastSelection = item;
355 * Get the number of selected nodes.
358 getSelectionCount : function(){
359 return this.selections.length;
363 * Get the currently selected nodes.
364 * @return {Array} An array of HTMLElements
366 getSelectedNodes : function(){
367 return this.selections;
371 * Get the indexes of the selected nodes.
374 getSelectedIndexes : function(){
375 var indexes = [], s = this.selections;
376 for(var i = 0, len = s.length; i < len; i++){
377 indexes.push(s[i].nodeIndex);
383 * Clear all selections
384 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange event
386 clearSelections : function(suppressEvent){
387 if(this.nodes && (this.multiSelect || this.singleSelect) && this.selections.length > 0){
388 this.cmp.elements = this.selections;
389 this.cmp.removeClass(this.selectedClass);
390 this.selections = [];
392 this.fireEvent("selectionchange", this, this.selections);
398 * Returns true if the passed node is selected
399 * @param {HTMLElement/Number} node The node or node index
402 isSelected : function(node){
403 var s = this.selections;
407 node = this.getNode(node);
408 return s.indexOf(node) !== -1;
413 * @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
414 * @param {Boolean} keepExisting (optional) true to keep existing selections
415 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
417 select : function(nodeInfo, keepExisting, suppressEvent){
418 if(nodeInfo instanceof Array){
420 this.clearSelections(true);
422 for(var i = 0, len = nodeInfo.length; i < len; i++){
423 this.select(nodeInfo[i], true, true);
426 var node = this.getNode(nodeInfo);
427 if(node && !this.isSelected(node)){
429 this.clearSelections(true);
431 if(this.fireEvent("beforeselect", this, node, this.selections) !== false){
432 Roo.fly(node).addClass(this.selectedClass);
433 this.selections.push(node);
435 this.fireEvent("selectionchange", this, this.selections);
443 * Gets a template node.
444 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
445 * @return {HTMLElement} The node or null if it wasn't found
447 getNode : function(nodeInfo){
448 if(typeof nodeInfo == "string"){
449 return document.getElementById(nodeInfo);
450 }else if(typeof nodeInfo == "number"){
451 return this.nodes[nodeInfo];
457 * Gets a range template nodes.
458 * @param {Number} startIndex
459 * @param {Number} endIndex
460 * @return {Array} An array of nodes
462 getNodes : function(start, end){
465 end = typeof end == "undefined" ? ns.length - 1 : end;
468 for(var i = start; i <= end; i++){
472 for(var i = start; i >= end; i--){
480 * Finds the index of the passed node
481 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
482 * @return {Number} The index of the node or -1
484 indexOf : function(node){
485 node = this.getNode(node);
486 if(typeof node.nodeIndex == "number"){
487 return node.nodeIndex;
490 for(var i = 0, len = ns.length; i < len; i++){