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, {
136 * The css class to add to selected nodes
137 * @type {Roo.DomHelper.Template}
139 selectedClass : "x-view-selected",
143 * Returns the element this view is bound to.
144 * @return {Roo.Element}
151 * Refreshes the view.
153 refresh : function(){
155 this.clearSelections();
158 var records = this.store.getRange();
159 if(records.length < 1){
160 this.el.update(this.emptyText);
163 for(var i = 0, len = records.length; i < len; i++){
164 var data = this.prepareData(records[i].data, i, records[i]);
165 html[html.length] = t.apply(data);
167 this.el.update(html.join(""));
168 this.nodes = this.el.dom.childNodes;
169 this.updateIndexes(0);
173 * Function to override to reformat the data that is sent to
174 * the template for each node.
175 * @param {Array/Object} data The raw data (array of colData for a data model bound view or
176 * a JSON object for an UpdateManager bound view).
178 prepareData : function(data){
182 onUpdate : function(ds, record){
183 this.clearSelections();
184 var index = this.store.indexOf(record);
185 var n = this.nodes[index];
186 this.tpl.insertBefore(n, this.prepareData(record.data));
187 n.parentNode.removeChild(n);
188 this.updateIndexes(index, index);
191 onAdd : function(ds, records, index){
192 this.clearSelections();
193 if(this.nodes.length == 0){
197 var n = this.nodes[index];
198 for(var i = 0, len = records.length; i < len; i++){
199 var d = this.prepareData(records[i].data);
201 this.tpl.insertBefore(n, d);
203 this.tpl.append(this.el, d);
206 this.updateIndexes(index);
209 onRemove : function(ds, record, index){
210 this.clearSelections();
211 this.el.dom.removeChild(this.nodes[index]);
212 this.updateIndexes(index);
216 * Refresh an individual node.
217 * @param {Number} index
219 refreshNode : function(index){
220 this.onUpdate(this.store, this.store.getAt(index));
223 updateIndexes : function(startIndex, endIndex){
225 startIndex = startIndex || 0;
226 endIndex = endIndex || ns.length - 1;
227 for(var i = startIndex; i <= endIndex; i++){
233 * Changes the data store this view uses and refresh the view.
234 * @param {Store} store
236 setStore : function(store, initial){
237 if(!initial && this.store){
238 this.store.un("datachanged", this.refresh);
239 this.store.un("add", this.onAdd);
240 this.store.un("remove", this.onRemove);
241 this.store.un("update", this.onUpdate);
242 this.store.un("clear", this.refresh);
246 store.on("datachanged", this.refresh, this);
247 store.on("add", this.onAdd, this);
248 store.on("remove", this.onRemove, this);
249 store.on("update", this.onUpdate, this);
250 store.on("clear", this.refresh, this);
259 * Returns the template node the passed child belongs to or null if it doesn't belong to one.
260 * @param {HTMLElement} node
261 * @return {HTMLElement} The template node
263 findItemFromChild : function(node){
264 var el = this.el.dom;
265 if(!node || node.parentNode == el){
268 var p = node.parentNode;
270 if(p.parentNode == el){
279 onClick : function(e){
280 var item = this.findItemFromChild(e.getTarget());
282 var index = this.indexOf(item);
283 if(this.onItemClick(item, index, e) !== false){
284 this.fireEvent("click", this, index, item, e);
287 this.clearSelections();
292 onContextMenu : function(e){
293 var item = this.findItemFromChild(e.getTarget());
295 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
300 onDblClick : function(e){
301 var item = this.findItemFromChild(e.getTarget());
303 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
307 onItemClick : function(item, index, e){
308 if(this.fireEvent("beforeclick", this, index, item, e) === false){
311 if(this.multiSelect || this.singleSelect){
312 if(this.multiSelect && e.shiftKey && this.lastSelection){
313 this.select(this.getNodes(this.indexOf(this.lastSelection), index), false);
315 this.select(item, this.multiSelect && e.ctrlKey);
316 this.lastSelection = item;
324 * Get the number of selected nodes.
327 getSelectionCount : function(){
328 return this.selections.length;
332 * Get the currently selected nodes.
333 * @return {Array} An array of HTMLElements
335 getSelectedNodes : function(){
336 return this.selections;
340 * Get the indexes of the selected nodes.
343 getSelectedIndexes : function(){
344 var indexes = [], s = this.selections;
345 for(var i = 0, len = s.length; i < len; i++){
346 indexes.push(s[i].nodeIndex);
352 * Clear all selections
353 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange event
355 clearSelections : function(suppressEvent){
356 if(this.nodes && (this.multiSelect || this.singleSelect) && this.selections.length > 0){
357 this.cmp.elements = this.selections;
358 this.cmp.removeClass(this.selectedClass);
359 this.selections = [];
361 this.fireEvent("selectionchange", this, this.selections);
367 * Returns true if the passed node is selected
368 * @param {HTMLElement/Number} node The node or node index
371 isSelected : function(node){
372 var s = this.selections;
376 node = this.getNode(node);
377 return s.indexOf(node) !== -1;
382 * @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
383 * @param {Boolean} keepExisting (optional) true to keep existing selections
384 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
386 select : function(nodeInfo, keepExisting, suppressEvent){
387 if(nodeInfo instanceof Array){
389 this.clearSelections(true);
391 for(var i = 0, len = nodeInfo.length; i < len; i++){
392 this.select(nodeInfo[i], true, true);
395 var node = this.getNode(nodeInfo);
396 if(node && !this.isSelected(node)){
398 this.clearSelections(true);
400 if(this.fireEvent("beforeselect", this, node, this.selections) !== false){
401 Roo.fly(node).addClass(this.selectedClass);
402 this.selections.push(node);
404 this.fireEvent("selectionchange", this, this.selections);
412 * Gets a template node.
413 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
414 * @return {HTMLElement} The node or null if it wasn't found
416 getNode : function(nodeInfo){
417 if(typeof nodeInfo == "string"){
418 return document.getElementById(nodeInfo);
419 }else if(typeof nodeInfo == "number"){
420 return this.nodes[nodeInfo];
426 * Gets a range template nodes.
427 * @param {Number} startIndex
428 * @param {Number} endIndex
429 * @return {Array} An array of nodes
431 getNodes : function(start, end){
434 end = typeof end == "undefined" ? ns.length - 1 : end;
437 for(var i = start; i <= end; i++){
441 for(var i = start; i >= end; i--){
449 * Finds the index of the passed node
450 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
451 * @return {Number} The index of the node or -1
453 indexOf : function(node){
454 node = this.getNode(node);
455 if(typeof node.nodeIndex == "number"){
456 return node.nodeIndex;
459 for(var i = 0, len = ns.length; i < len; i++){