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 template : '<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);
77 * Fires before a click is processed. Returns false to cancel the default action.
78 * @param {Roo.View} this
79 * @param {Number} index The index of the target node
80 * @param {HTMLElement} node The target node
81 * @param {Roo.EventObject} e The raw event object
86 * Fires when a template node is clicked.
87 * @param {Roo.View} this
88 * @param {Number} index The index of the target node
89 * @param {HTMLElement} node The target node
90 * @param {Roo.EventObject} e The raw event object
95 * Fires when a template node is double clicked.
96 * @param {Roo.View} this
97 * @param {Number} index The index of the target node
98 * @param {HTMLElement} node The target node
99 * @param {Roo.EventObject} e The raw event object
104 * Fires when a template node is right clicked.
105 * @param {Roo.View} this
106 * @param {Number} index The index of the target node
107 * @param {HTMLElement} node The target node
108 * @param {Roo.EventObject} e The raw event object
110 "contextmenu" : true,
112 * @event selectionchange
113 * Fires when the selected nodes change.
114 * @param {Roo.View} this
115 * @param {Array} selections Array of the selected nodes
117 "selectionchange" : true,
120 * @event beforeselect
121 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
122 * @param {Roo.View} this
123 * @param {HTMLElement} node The node to be selected
124 * @param {Array} selections Array of currently selected nodes
126 "beforeselect" : true
130 "click": this.onClick,
131 "dblclick": this.onDblClick,
132 "contextmenu": this.onContextMenu,
136 this.selections = [];
138 this.cmp = new Roo.CompositeElementLite([]);
140 this.store = Roo.factory(this.store, Roo.data);
141 this.setStore(this.store, true);
143 Roo.View.superclass.constructor.call(this);
146 Roo.extend(Roo.View, Roo.util.Observable, {
149 * @cfg {String|Roo.Element} The container element.
154 * @cfg {String|Roo.DomHelper.Template} The template used by this View
159 * @cfg {Roo.DomHelper.Template} The css class to add to selected nodes
161 selectedClass : "x-view-selected",
163 * @cfg {String} The empty text to show when nothing is loaded.
167 * Returns the element this view is bound to.
168 * @return {Roo.Element}
175 * Refreshes the view.
177 refresh : function(){
179 this.clearSelections();
182 var records = this.store.getRange();
183 if(records.length < 1){
184 this.el.update(this.emptyText);
187 for(var i = 0, len = records.length; i < len; i++){
188 var data = this.prepareData(records[i].data, i, records[i]);
189 html[html.length] = t.apply(data);
191 this.el.update(html.join(""));
192 this.nodes = this.el.dom.childNodes;
193 this.updateIndexes(0);
197 * Function to override to reformat the data that is sent to
198 * the template for each node.
199 * @param {Array/Object} data The raw data (array of colData for a data model bound view or
200 * a JSON object for an UpdateManager bound view).
202 prepareData : function(data){
206 onUpdate : function(ds, record){
207 this.clearSelections();
208 var index = this.store.indexOf(record);
209 var n = this.nodes[index];
210 this.tpl.insertBefore(n, this.prepareData(record.data));
211 n.parentNode.removeChild(n);
212 this.updateIndexes(index, index);
215 onAdd : function(ds, records, index){
216 this.clearSelections();
217 if(this.nodes.length == 0){
221 var n = this.nodes[index];
222 for(var i = 0, len = records.length; i < len; i++){
223 var d = this.prepareData(records[i].data);
225 this.tpl.insertBefore(n, d);
227 this.tpl.append(this.el, d);
230 this.updateIndexes(index);
233 onRemove : function(ds, record, index){
234 this.clearSelections();
235 this.el.dom.removeChild(this.nodes[index]);
236 this.updateIndexes(index);
240 * Refresh an individual node.
241 * @param {Number} index
243 refreshNode : function(index){
244 this.onUpdate(this.store, this.store.getAt(index));
247 updateIndexes : function(startIndex, endIndex){
249 startIndex = startIndex || 0;
250 endIndex = endIndex || ns.length - 1;
251 for(var i = startIndex; i <= endIndex; i++){
257 * Changes the data store this view uses and refresh the view.
258 * @param {Store} store
260 setStore : function(store, initial){
261 if(!initial && this.store){
262 this.store.un("datachanged", this.refresh);
263 this.store.un("add", this.onAdd);
264 this.store.un("remove", this.onRemove);
265 this.store.un("update", this.onUpdate);
266 this.store.un("clear", this.refresh);
270 store.on("datachanged", this.refresh, this);
271 store.on("add", this.onAdd, this);
272 store.on("remove", this.onRemove, this);
273 store.on("update", this.onUpdate, this);
274 store.on("clear", this.refresh, this);
283 * Returns the template node the passed child belongs to or null if it doesn't belong to one.
284 * @param {HTMLElement} node
285 * @return {HTMLElement} The template node
287 findItemFromChild : function(node){
288 var el = this.el.dom;
289 if(!node || node.parentNode == el){
292 var p = node.parentNode;
294 if(p.parentNode == el){
303 onClick : function(e){
304 var item = this.findItemFromChild(e.getTarget());
306 var index = this.indexOf(item);
307 if(this.onItemClick(item, index, e) !== false){
308 this.fireEvent("click", this, index, item, e);
311 this.clearSelections();
316 onContextMenu : function(e){
317 var item = this.findItemFromChild(e.getTarget());
319 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
324 onDblClick : function(e){
325 var item = this.findItemFromChild(e.getTarget());
327 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
331 onItemClick : function(item, index, e){
332 if(this.fireEvent("beforeclick", this, index, item, e) === false){
335 if(this.multiSelect || this.singleSelect){
336 if(this.multiSelect && e.shiftKey && this.lastSelection){
337 this.select(this.getNodes(this.indexOf(this.lastSelection), index), false);
339 this.select(item, this.multiSelect && e.ctrlKey);
340 this.lastSelection = item;
348 * Get the number of selected nodes.
351 getSelectionCount : function(){
352 return this.selections.length;
356 * Get the currently selected nodes.
357 * @return {Array} An array of HTMLElements
359 getSelectedNodes : function(){
360 return this.selections;
364 * Get the indexes of the selected nodes.
367 getSelectedIndexes : function(){
368 var indexes = [], s = this.selections;
369 for(var i = 0, len = s.length; i < len; i++){
370 indexes.push(s[i].nodeIndex);
376 * Clear all selections
377 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange event
379 clearSelections : function(suppressEvent){
380 if(this.nodes && (this.multiSelect || this.singleSelect) && this.selections.length > 0){
381 this.cmp.elements = this.selections;
382 this.cmp.removeClass(this.selectedClass);
383 this.selections = [];
385 this.fireEvent("selectionchange", this, this.selections);
391 * Returns true if the passed node is selected
392 * @param {HTMLElement/Number} node The node or node index
395 isSelected : function(node){
396 var s = this.selections;
400 node = this.getNode(node);
401 return s.indexOf(node) !== -1;
406 * @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
407 * @param {Boolean} keepExisting (optional) true to keep existing selections
408 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
410 select : function(nodeInfo, keepExisting, suppressEvent){
411 if(nodeInfo instanceof Array){
413 this.clearSelections(true);
415 for(var i = 0, len = nodeInfo.length; i < len; i++){
416 this.select(nodeInfo[i], true, true);
419 var node = this.getNode(nodeInfo);
420 if(node && !this.isSelected(node)){
422 this.clearSelections(true);
424 if(this.fireEvent("beforeselect", this, node, this.selections) !== false){
425 Roo.fly(node).addClass(this.selectedClass);
426 this.selections.push(node);
428 this.fireEvent("selectionchange", this, this.selections);
436 * Gets a template node.
437 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
438 * @return {HTMLElement} The node or null if it wasn't found
440 getNode : function(nodeInfo){
441 if(typeof nodeInfo == "string"){
442 return document.getElementById(nodeInfo);
443 }else if(typeof nodeInfo == "number"){
444 return this.nodes[nodeInfo];
450 * Gets a range template nodes.
451 * @param {Number} startIndex
452 * @param {Number} endIndex
453 * @return {Array} An array of nodes
455 getNodes : function(start, end){
458 end = typeof end == "undefined" ? ns.length - 1 : end;
461 for(var i = start; i <= end; i++){
465 for(var i = start; i >= end; i--){
473 * Finds the index of the passed node
474 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
475 * @return {Number} The index of the node or -1
477 indexOf : function(node){
478 node = this.getNode(node);
479 if(typeof node.nodeIndex == "number"){
480 return node.nodeIndex;
483 for(var i = 0, len = ns.length; i < len; i++){