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>
42 * Note: old style constructor is still suported (container, template, config)
46 * @param {Object} config The config object
49 Roo.View = function(config, depreciated_tpl, depreciated_config){
51 if (typeof(depreciated_tpl) == 'undefined') {
52 config = depreciated_container;
53 depreciated_tpl = = config.template;
55 this.el = Roo.get(config);
56 this.template = depreciated_tpl;
62 if(typeof(this.template) == "string"){
63 this.tpl = new Roo.Template(this.template);
65 this.tpl = this.template;
69 * The template used by this View
70 * @type {Roo.DomHelper.Template}
74 Roo.apply(this, config);
80 * Fires before a click is processed. Returns false to cancel the default action.
81 * @param {Roo.View} this
82 * @param {Number} index The index of the target node
83 * @param {HTMLElement} node The target node
84 * @param {Roo.EventObject} e The raw event object
89 * Fires when a template node is clicked.
90 * @param {Roo.View} this
91 * @param {Number} index The index of the target node
92 * @param {HTMLElement} node The target node
93 * @param {Roo.EventObject} e The raw event object
98 * Fires when a template node is double clicked.
99 * @param {Roo.View} this
100 * @param {Number} index The index of the target node
101 * @param {HTMLElement} node The target node
102 * @param {Roo.EventObject} e The raw event object
107 * Fires when a template node is right clicked.
108 * @param {Roo.View} this
109 * @param {Number} index The index of the target node
110 * @param {HTMLElement} node The target node
111 * @param {Roo.EventObject} e The raw event object
113 "contextmenu" : true,
115 * @event selectionchange
116 * Fires when the selected nodes change.
117 * @param {Roo.View} this
118 * @param {Array} selections Array of the selected nodes
120 "selectionchange" : true,
123 * @event beforeselect
124 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
125 * @param {Roo.View} this
126 * @param {HTMLElement} node The node to be selected
127 * @param {Array} selections Array of currently selected nodes
129 "beforeselect" : true
133 "click": this.onClick,
134 "dblclick": this.onDblClick,
135 "contextmenu": this.onContextMenu,
139 this.selections = [];
141 this.cmp = new Roo.CompositeElementLite([]);
143 this.store = Roo.factory(this.store, Roo.data);
144 this.setStore(this.store, true);
146 Roo.View.superclass.constructor.call(this);
149 Roo.extend(Roo.View, Roo.util.Observable, {
152 * The container element.
153 * @cfg {String|Roo.Element}
157 * The template used by this View
158 * @cfg {String|Roo.DomHelper.Template}
163 * The template used by this View (after contruction)
164 * @type {Roo.DomHelper.Template}
169 * The css class to add to selected nodes
170 * @type {Roo.DomHelper.Template}
172 selectedClass : "x-view-selected",
176 * Returns the element this view is bound to.
177 * @return {Roo.Element}
184 * Refreshes the view.
186 refresh : function(){
188 this.clearSelections();
191 var records = this.store.getRange();
192 if(records.length < 1){
193 this.el.update(this.emptyText);
196 for(var i = 0, len = records.length; i < len; i++){
197 var data = this.prepareData(records[i].data, i, records[i]);
198 html[html.length] = t.apply(data);
200 this.el.update(html.join(""));
201 this.nodes = this.el.dom.childNodes;
202 this.updateIndexes(0);
206 * Function to override to reformat the data that is sent to
207 * the template for each node.
208 * @param {Array/Object} data The raw data (array of colData for a data model bound view or
209 * a JSON object for an UpdateManager bound view).
211 prepareData : function(data){
215 onUpdate : function(ds, record){
216 this.clearSelections();
217 var index = this.store.indexOf(record);
218 var n = this.nodes[index];
219 this.tpl.insertBefore(n, this.prepareData(record.data));
220 n.parentNode.removeChild(n);
221 this.updateIndexes(index, index);
224 onAdd : function(ds, records, index){
225 this.clearSelections();
226 if(this.nodes.length == 0){
230 var n = this.nodes[index];
231 for(var i = 0, len = records.length; i < len; i++){
232 var d = this.prepareData(records[i].data);
234 this.tpl.insertBefore(n, d);
236 this.tpl.append(this.el, d);
239 this.updateIndexes(index);
242 onRemove : function(ds, record, index){
243 this.clearSelections();
244 this.el.dom.removeChild(this.nodes[index]);
245 this.updateIndexes(index);
249 * Refresh an individual node.
250 * @param {Number} index
252 refreshNode : function(index){
253 this.onUpdate(this.store, this.store.getAt(index));
256 updateIndexes : function(startIndex, endIndex){
258 startIndex = startIndex || 0;
259 endIndex = endIndex || ns.length - 1;
260 for(var i = startIndex; i <= endIndex; i++){
266 * Changes the data store this view uses and refresh the view.
267 * @param {Store} store
269 setStore : function(store, initial){
270 if(!initial && this.store){
271 this.store.un("datachanged", this.refresh);
272 this.store.un("add", this.onAdd);
273 this.store.un("remove", this.onRemove);
274 this.store.un("update", this.onUpdate);
275 this.store.un("clear", this.refresh);
279 store.on("datachanged", this.refresh, this);
280 store.on("add", this.onAdd, this);
281 store.on("remove", this.onRemove, this);
282 store.on("update", this.onUpdate, this);
283 store.on("clear", this.refresh, this);
292 * Returns the template node the passed child belongs to or null if it doesn't belong to one.
293 * @param {HTMLElement} node
294 * @return {HTMLElement} The template node
296 findItemFromChild : function(node){
297 var el = this.el.dom;
298 if(!node || node.parentNode == el){
301 var p = node.parentNode;
303 if(p.parentNode == el){
312 onClick : function(e){
313 var item = this.findItemFromChild(e.getTarget());
315 var index = this.indexOf(item);
316 if(this.onItemClick(item, index, e) !== false){
317 this.fireEvent("click", this, index, item, e);
320 this.clearSelections();
325 onContextMenu : function(e){
326 var item = this.findItemFromChild(e.getTarget());
328 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
333 onDblClick : function(e){
334 var item = this.findItemFromChild(e.getTarget());
336 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
340 onItemClick : function(item, index, e){
341 if(this.fireEvent("beforeclick", this, index, item, e) === false){
344 if(this.multiSelect || this.singleSelect){
345 if(this.multiSelect && e.shiftKey && this.lastSelection){
346 this.select(this.getNodes(this.indexOf(this.lastSelection), index), false);
348 this.select(item, this.multiSelect && e.ctrlKey);
349 this.lastSelection = item;
357 * Get the number of selected nodes.
360 getSelectionCount : function(){
361 return this.selections.length;
365 * Get the currently selected nodes.
366 * @return {Array} An array of HTMLElements
368 getSelectedNodes : function(){
369 return this.selections;
373 * Get the indexes of the selected nodes.
376 getSelectedIndexes : function(){
377 var indexes = [], s = this.selections;
378 for(var i = 0, len = s.length; i < len; i++){
379 indexes.push(s[i].nodeIndex);
385 * Clear all selections
386 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange event
388 clearSelections : function(suppressEvent){
389 if(this.nodes && (this.multiSelect || this.singleSelect) && this.selections.length > 0){
390 this.cmp.elements = this.selections;
391 this.cmp.removeClass(this.selectedClass);
392 this.selections = [];
394 this.fireEvent("selectionchange", this, this.selections);
400 * Returns true if the passed node is selected
401 * @param {HTMLElement/Number} node The node or node index
404 isSelected : function(node){
405 var s = this.selections;
409 node = this.getNode(node);
410 return s.indexOf(node) !== -1;
415 * @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
416 * @param {Boolean} keepExisting (optional) true to keep existing selections
417 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
419 select : function(nodeInfo, keepExisting, suppressEvent){
420 if(nodeInfo instanceof Array){
422 this.clearSelections(true);
424 for(var i = 0, len = nodeInfo.length; i < len; i++){
425 this.select(nodeInfo[i], true, true);
428 var node = this.getNode(nodeInfo);
429 if(node && !this.isSelected(node)){
431 this.clearSelections(true);
433 if(this.fireEvent("beforeselect", this, node, this.selections) !== false){
434 Roo.fly(node).addClass(this.selectedClass);
435 this.selections.push(node);
437 this.fireEvent("selectionchange", this, this.selections);
445 * Gets a template node.
446 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
447 * @return {HTMLElement} The node or null if it wasn't found
449 getNode : function(nodeInfo){
450 if(typeof nodeInfo == "string"){
451 return document.getElementById(nodeInfo);
452 }else if(typeof nodeInfo == "number"){
453 return this.nodes[nodeInfo];
459 * Gets a range template nodes.
460 * @param {Number} startIndex
461 * @param {Number} endIndex
462 * @return {Array} An array of nodes
464 getNodes : function(start, end){
467 end = typeof end == "undefined" ? ns.length - 1 : end;
470 for(var i = start; i <= end; i++){
474 for(var i = start; i >= end; i--){
482 * Finds the index of the passed node
483 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
484 * @return {Number} The index of the node or -1
486 indexOf : function(node){
487 node = this.getNode(node);
488 if(typeof node.nodeIndex == "number"){
489 return node.nodeIndex;
492 for(var i = 0, len = ns.length; i < len; i++){