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 // support xtype ctors..
68 this.tpl = new Roo.factory(this.tpl, Roo);
73 if ( this.footer && this.footer.xtype) {
77 this.wrapEl = this.el.wrap();
79 this.footer.dataSource = this.store
80 this.footer.container = this.wrapEl;
81 this.footer = Roo.factory(this.footer, Roo);
89 * Fires before a click is processed. Returns false to cancel the default action.
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 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 double 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
116 * Fires when a template node is right clicked.
117 * @param {Roo.View} this
118 * @param {Number} index The index of the target node
119 * @param {HTMLElement} node The target node
120 * @param {Roo.EventObject} e The raw event object
122 "contextmenu" : true,
124 * @event selectionchange
125 * Fires when the selected nodes change.
126 * @param {Roo.View} this
127 * @param {Array} selections Array of the selected nodes
129 "selectionchange" : true,
132 * @event beforeselect
133 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
134 * @param {Roo.View} this
135 * @param {HTMLElement} node The node to be selected
136 * @param {Array} selections Array of currently selected nodes
138 "beforeselect" : true,
141 * Fires on every row to render, to allow you to change the data.
142 * @param {Roo.View} this
143 * @param {Object} data to be rendered (change this)
149 "click": this.onClick,
150 "dblclick": this.onDblClick,
151 "contextmenu": this.onContextMenu,
155 this.selections = [];
157 this.cmp = new Roo.CompositeElementLite([]);
159 this.store = Roo.factory(this.store, Roo.data);
160 this.setStore(this.store, true);
162 Roo.View.superclass.constructor.call(this);
169 Roo.extend(Roo.View, Roo.util.Observable, {
172 * @cfg {Roo.data.Store} store Data store to load data from.
177 * @cfg {String|Roo.Element} el The container element.
182 * @cfg {String|Roo.Template} tpl The template used by this View
186 * @cfg {String} dataName the named area of the template to use as the data area
187 * Works with domtemplates roo-name="name"
191 * @cfg {String} selectedClass The css class to add to selected nodes
193 selectedClass : "x-view-selected",
195 * @cfg {String} emptyText The empty text to show when nothing is loaded.
200 * @cfg {String} text to display on mask (default Loading)
204 * @cfg {Boolean} multiSelect Allow multiple selection
208 * @cfg {Boolean} singleSelect Allow single selection
213 * @cfg {Boolean} toggleSelect - selecting
215 toggleSelect : false,
218 * Returns the element this view is bound to.
219 * @return {Roo.Element}
225 render : function(a,b, panel)
234 * Refreshes the view. - called by datachanged on the store. - do not call directly.
236 refresh : function(){
239 // if we are using something like 'domtemplate', then
240 // the what gets used is:
241 // t.applySubtemplate(NAME, data, wrapping data..)
242 // the outer template then get' applied with
243 // the store 'extra data'
244 // and the body get's added to the
245 // roo-name="data" node?
246 // <span class='roo-tpl-{name}'></span> ?????
250 this.clearSelections();
253 var records = this.store.getRange();
254 if(records.length < 1) {
256 // is this valid?? = should it render a template??
258 this.el.update(this.emptyText);
263 this.el.update(t.apply(this.store.meta)); //????
264 el = this.el.child('.roo-tpl-' + this.dataName);
267 for(var i = 0, len = records.length; i < len; i++){
268 var data = this.prepareData(records[i].data, i, records[i]);
269 this.fireEvent("preparedata", this, data, i, records[i]);
270 html[html.length] = Roo.util.Format.trim(
272 t.applySubtemplate(this.dataName, data, this.store.meta) :
279 el.update(html.join(""));
280 this.nodes = el.dom.childNodes;
281 this.updateIndexes(0);
285 * Function to override to reformat the data that is sent to
286 * the template for each node.
287 * DEPRICATED - use the preparedata event handler.
288 * @param {Array/Object} data The raw data (array of colData for a data model bound view or
289 * a JSON object for an UpdateManager bound view).
291 prepareData : function(data, index, record)
293 this.fireEvent("preparedata", this, data, index, record);
297 onUpdate : function(ds, record){
298 this.clearSelections();
299 var index = this.store.indexOf(record);
300 var n = this.nodes[index];
301 this.tpl.insertBefore(n, this.prepareData(record.data, index, record));
302 n.parentNode.removeChild(n);
303 this.updateIndexes(index, index);
309 onAdd : function(ds, records, index)
311 this.clearSelections();
312 if(this.nodes.length == 0){
316 var n = this.nodes[index];
317 for(var i = 0, len = records.length; i < len; i++){
318 var d = this.prepareData(records[i].data, i, records[i]);
320 this.tpl.insertBefore(n, d);
323 this.tpl.append(this.el, d);
326 this.updateIndexes(index);
329 onRemove : function(ds, record, index){
330 this.clearSelections();
331 var el = this.dataName ?
332 this.el.child('.roo-tpl-' + this.dataName) :
334 el.dom.removeChild(this.nodes[index]);
335 this.updateIndexes(index);
339 * Refresh an individual node.
340 * @param {Number} index
342 refreshNode : function(index){
343 this.onUpdate(this.store, this.store.getAt(index));
346 updateIndexes : function(startIndex, endIndex){
348 startIndex = startIndex || 0;
349 endIndex = endIndex || ns.length - 1;
350 for(var i = startIndex; i <= endIndex; i++){
356 * Changes the data store this view uses and refresh the view.
357 * @param {Store} store
359 setStore : function(store, initial){
360 if(!initial && this.store){
361 this.store.un("datachanged", this.refresh);
362 this.store.un("add", this.onAdd);
363 this.store.un("remove", this.onRemove);
364 this.store.un("update", this.onUpdate);
365 this.store.un("clear", this.refresh);
366 this.store.un("beforeload", this.onBeforeLoad);
367 this.store.un("load", this.onLoad);
368 this.store.un("loadexception", this.onLoad);
372 store.on("datachanged", this.refresh, this);
373 store.on("add", this.onAdd, this);
374 store.on("remove", this.onRemove, this);
375 store.on("update", this.onUpdate, this);
376 store.on("clear", this.refresh, this);
377 store.on("beforeload", this.onBeforeLoad, this);
378 store.on("load", this.onLoad, this);
379 store.on("loadexception", this.onLoad, this);
387 * onbeforeLoad - masks the loading area.
390 onBeforeLoad : function()
393 this.el.mask(this.mask ? this.mask : "Loading" );
402 * Returns the template node the passed child belongs to or null if it doesn't belong to one.
403 * @param {HTMLElement} node
404 * @return {HTMLElement} The template node
406 findItemFromChild : function(node){
407 var el = this.dataName ?
408 this.el.child('.roo-tpl-' + this.dataName,true) :
411 if(!node || node.parentNode == el){
414 var p = node.parentNode;
416 if(p.parentNode == el){
425 onClick : function(e){
426 var item = this.findItemFromChild(e.getTarget());
428 var index = this.indexOf(item);
429 if(this.onItemClick(item, index, e) !== false){
430 this.fireEvent("click", this, index, item, e);
433 this.clearSelections();
438 onContextMenu : function(e){
439 var item = this.findItemFromChild(e.getTarget());
441 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
446 onDblClick : function(e){
447 var item = this.findItemFromChild(e.getTarget());
449 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
453 onItemClick : function(item, index, e)
455 if(this.fireEvent("beforeclick", this, index, item, e) === false){
458 if (this.toggleSelect) {
459 var m = this.isSelected(item) ? 'unselect' : 'select';
462 _t[m](item, true, false);
465 if(this.multiSelect || this.singleSelect){
466 if(this.multiSelect && e.shiftKey && this.lastSelection){
467 this.select(this.getNodes(this.indexOf(this.lastSelection), index), false);
469 this.select(item, this.multiSelect && e.ctrlKey);
470 this.lastSelection = item;
478 * Get the number of selected nodes.
481 getSelectionCount : function(){
482 return this.selections.length;
486 * Get the currently selected nodes.
487 * @return {Array} An array of HTMLElements
489 getSelectedNodes : function(){
490 return this.selections;
494 * Get the indexes of the selected nodes.
497 getSelectedIndexes : function(){
498 var indexes = [], s = this.selections;
499 for(var i = 0, len = s.length; i < len; i++){
500 indexes.push(s[i].nodeIndex);
506 * Clear all selections
507 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange event
509 clearSelections : function(suppressEvent){
510 if(this.nodes && (this.multiSelect || this.singleSelect) && this.selections.length > 0){
511 this.cmp.elements = this.selections;
512 this.cmp.removeClass(this.selectedClass);
513 this.selections = [];
515 this.fireEvent("selectionchange", this, this.selections);
521 * Returns true if the passed node is selected
522 * @param {HTMLElement/Number} node The node or node index
525 isSelected : function(node){
526 var s = this.selections;
530 node = this.getNode(node);
531 return s.indexOf(node) !== -1;
536 * @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
537 * @param {Boolean} keepExisting (optional) true to keep existing selections
538 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
540 select : function(nodeInfo, keepExisting, suppressEvent){
541 if(nodeInfo instanceof Array){
543 this.clearSelections(true);
545 for(var i = 0, len = nodeInfo.length; i < len; i++){
546 this.select(nodeInfo[i], true, true);
550 var node = this.getNode(nodeInfo);
551 if(!node || this.isSelected(node)){
552 return; // already selected.
555 this.clearSelections(true);
557 if(this.fireEvent("beforeselect", this, node, this.selections) !== false){
558 Roo.fly(node).addClass(this.selectedClass);
559 this.selections.push(node);
561 this.fireEvent("selectionchange", this, this.selections);
569 * @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
570 * @param {Boolean} keepExisting (optional) true IGNORED (for campatibility with select)
571 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
573 unselect : function(nodeInfo, keepExisting, suppressEvent)
575 if(nodeInfo instanceof Array){
576 Roo.each(this.selections, function(s) {
577 this.unselect(s, nodeInfo);
581 var node = this.getNode(nodeInfo);
582 if(!node || !this.isSelected(node)){
583 Roo.log("not selected");
584 return; // not selected.
588 Roo.each(this.selections, function(s) {
590 Roo.fly(node).removeClass(this.selectedClass);
598 this.fireEvent("selectionchange", this, this.selections);
602 * Gets a template node.
603 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
604 * @return {HTMLElement} The node or null if it wasn't found
606 getNode : function(nodeInfo){
607 if(typeof nodeInfo == "string"){
608 return document.getElementById(nodeInfo);
609 }else if(typeof nodeInfo == "number"){
610 return this.nodes[nodeInfo];
616 * Gets a range template nodes.
617 * @param {Number} startIndex
618 * @param {Number} endIndex
619 * @return {Array} An array of nodes
621 getNodes : function(start, end){
624 end = typeof end == "undefined" ? ns.length - 1 : end;
627 for(var i = start; i <= end; i++){
631 for(var i = start; i >= end; i--){
639 * Finds the index of the passed node
640 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
641 * @return {Number} The index of the node or -1
643 indexOf : function(node){
644 node = this.getNode(node);
645 if(typeof node.nodeIndex == "number"){
646 return node.nodeIndex;
649 for(var i = 0, len = ns.length; i < len; i++){