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){
54 if (typeof(depreciated_tpl) == 'undefined') {
55 // new way.. - universal constructor.
56 Roo.apply(this, config);
57 this.el = Roo.get(this.el);
60 this.el = Roo.get(config);
61 this.tpl = depreciated_tpl;
62 Roo.apply(this, depreciated_config);
64 this.wrapEl = this.el.wrap().wrap();
65 ///this.el = this.wrapEla.appendChild(document.createElement("div"));
68 if(typeof(this.tpl) == "string"){
69 this.tpl = new Roo.Template(this.tpl);
71 // support xtype ctors..
72 this.tpl = new Roo.factory(this.tpl, Roo);
82 * Fires before a click is processed. Returns false to cancel the default action.
83 * @param {Roo.View} this
84 * @param {Number} index The index of the target node
85 * @param {HTMLElement} node The target node
86 * @param {Roo.EventObject} e The raw event object
91 * Fires when a template node is clicked.
92 * @param {Roo.View} this
93 * @param {Number} index The index of the target node
94 * @param {HTMLElement} node The target node
95 * @param {Roo.EventObject} e The raw event object
100 * Fires when a template node is double clicked.
101 * @param {Roo.View} this
102 * @param {Number} index The index of the target node
103 * @param {HTMLElement} node The target node
104 * @param {Roo.EventObject} e The raw event object
109 * Fires when a template node is right clicked.
110 * @param {Roo.View} this
111 * @param {Number} index The index of the target node
112 * @param {HTMLElement} node The target node
113 * @param {Roo.EventObject} e The raw event object
115 "contextmenu" : true,
117 * @event selectionchange
118 * Fires when the selected nodes change.
119 * @param {Roo.View} this
120 * @param {Array} selections Array of the selected nodes
122 "selectionchange" : true,
125 * @event beforeselect
126 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
127 * @param {Roo.View} this
128 * @param {HTMLElement} node The node to be selected
129 * @param {Array} selections Array of currently selected nodes
131 "beforeselect" : true,
134 * Fires on every row to render, to allow you to change the data.
135 * @param {Roo.View} this
136 * @param {Object} data to be rendered (change this)
146 "click": this.onClick,
147 "dblclick": this.onDblClick,
148 "contextmenu": this.onContextMenu,
152 this.selections = [];
154 this.cmp = new Roo.CompositeElementLite([]);
159 this.store = Roo.factory(this.store, Roo.data);
160 this.setStore(this.store, true);
163 if ( this.footer && this.footer.xtype) {
165 var fctr = this.wrapEl.appendChild(document.createElement("div"));
167 this.footer.dataSource = this.store;
168 this.footer.container = fctr;
169 this.footer = Roo.factory(this.footer, Roo);
170 fctr.insertFirst(this.el);
172 // this is a bit insane - as the paging toolbar seems to detach the el..
173 // dom.parentNode.parentNode.parentNode
174 // they get detached?
178 Roo.View.superclass.constructor.call(this);
183 Roo.extend(Roo.View, Roo.util.Observable, {
186 * @cfg {Roo.data.Store} store Data store to load data from.
191 * @cfg {String|Roo.Element} el The container element.
196 * @cfg {String|Roo.Template} tpl The template used by this View
200 * @cfg {String} dataName the named area of the template to use as the data area
201 * Works with domtemplates roo-name="name"
205 * @cfg {String} selectedClass The css class to add to selected nodes
207 selectedClass : "x-view-selected",
209 * @cfg {String} emptyText The empty text to show when nothing is loaded.
214 * @cfg {String} text to display on mask (default Loading)
218 * @cfg {Boolean} multiSelect Allow multiple selection
222 * @cfg {Boolean} singleSelect Allow single selection
227 * @cfg {Boolean} toggleSelect - selecting
229 toggleSelect : false,
232 * @cfg {Boolean} tickable - selecting
237 * Returns the element this view is bound to.
238 * @return {Roo.Element}
247 * Refreshes the view. - called by datachanged on the store. - do not call directly.
249 refresh : function(){
250 //Roo.log('refresh');
253 // if we are using something like 'domtemplate', then
254 // the what gets used is:
255 // t.applySubtemplate(NAME, data, wrapping data..)
256 // the outer template then get' applied with
257 // the store 'extra data'
258 // and the body get's added to the
259 // roo-name="data" node?
260 // <span class='roo-tpl-{name}'></span> ?????
264 this.clearSelections();
267 var records = this.store.getRange();
268 if(records.length < 1) {
270 // is this valid?? = should it render a template??
272 this.el.update(this.emptyText);
277 this.el.update(t.apply(this.store.meta)); //????
278 el = this.el.child('.roo-tpl-' + this.dataName);
281 for(var i = 0, len = records.length; i < len; i++){
282 var data = this.prepareData(records[i].data, i, records[i]);
283 this.fireEvent("preparedata", this, data, i, records[i]);
285 var d = Roo.apply({}, data);
288 Roo.apply(d, {'roo-id' : Roo.id()});
292 Roo.each(this.parent.item, function(item){
293 if(item[_this.parent.valueField] != data[_this.parent.valueField]){
296 Roo.apply(d, {'roo-data-checked' : 'checked'});
300 html[html.length] = Roo.util.Format.trim(
302 t.applySubtemplate(this.dataName, d, this.store.meta) :
309 el.update(html.join(""));
310 this.nodes = el.dom.childNodes;
311 this.updateIndexes(0);
316 * Function to override to reformat the data that is sent to
317 * the template for each node.
318 * DEPRICATED - use the preparedata event handler.
319 * @param {Array/Object} data The raw data (array of colData for a data model bound view or
320 * a JSON object for an UpdateManager bound view).
322 prepareData : function(data, index, record)
324 this.fireEvent("preparedata", this, data, index, record);
328 onUpdate : function(ds, record){
329 // Roo.log('on update');
330 this.clearSelections();
331 var index = this.store.indexOf(record);
332 var n = this.nodes[index];
333 this.tpl.insertBefore(n, this.prepareData(record.data, index, record));
334 n.parentNode.removeChild(n);
335 this.updateIndexes(index, index);
341 onAdd : function(ds, records, index)
343 //Roo.log(['on Add', ds, records, index] );
344 this.clearSelections();
345 if(this.nodes.length == 0){
349 var n = this.nodes[index];
350 for(var i = 0, len = records.length; i < len; i++){
351 var d = this.prepareData(records[i].data, i, records[i]);
353 this.tpl.insertBefore(n, d);
356 this.tpl.append(this.el, d);
359 this.updateIndexes(index);
362 onRemove : function(ds, record, index){
363 // Roo.log('onRemove');
364 this.clearSelections();
365 var el = this.dataName ?
366 this.el.child('.roo-tpl-' + this.dataName) :
369 el.dom.removeChild(this.nodes[index]);
370 this.updateIndexes(index);
374 * Refresh an individual node.
375 * @param {Number} index
377 refreshNode : function(index){
378 this.onUpdate(this.store, this.store.getAt(index));
381 updateIndexes : function(startIndex, endIndex){
383 startIndex = startIndex || 0;
384 endIndex = endIndex || ns.length - 1;
385 for(var i = startIndex; i <= endIndex; i++){
391 * Changes the data store this view uses and refresh the view.
392 * @param {Store} store
394 setStore : function(store, initial){
395 if(!initial && this.store){
396 this.store.un("datachanged", this.refresh);
397 this.store.un("add", this.onAdd);
398 this.store.un("remove", this.onRemove);
399 this.store.un("update", this.onUpdate);
400 this.store.un("clear", this.refresh);
401 this.store.un("beforeload", this.onBeforeLoad);
402 this.store.un("load", this.onLoad);
403 this.store.un("loadexception", this.onLoad);
407 store.on("datachanged", this.refresh, this);
408 store.on("add", this.onAdd, this);
409 store.on("remove", this.onRemove, this);
410 store.on("update", this.onUpdate, this);
411 store.on("clear", this.refresh, this);
412 store.on("beforeload", this.onBeforeLoad, this);
413 store.on("load", this.onLoad, this);
414 store.on("loadexception", this.onLoad, this);
422 * onbeforeLoad - masks the loading area.
425 onBeforeLoad : function(store,opts)
427 //Roo.log('onBeforeLoad');
431 this.el.mask(this.mask ? this.mask : "Loading" );
440 * Returns the template node the passed child belongs to or null if it doesn't belong to one.
441 * @param {HTMLElement} node
442 * @return {HTMLElement} The template node
444 findItemFromChild : function(node){
445 var el = this.dataName ?
446 this.el.child('.roo-tpl-' + this.dataName,true) :
449 if(!node || node.parentNode == el){
452 var p = node.parentNode;
454 if(p.parentNode == el){
463 onClick : function(e){
464 var item = this.findItemFromChild(e.getTarget());
466 var index = this.indexOf(item);
467 if(this.onItemClick(item, index, e) !== false){
468 this.fireEvent("click", this, index, item, e);
471 this.clearSelections();
476 onContextMenu : function(e){
477 var item = this.findItemFromChild(e.getTarget());
479 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
484 onDblClick : function(e){
485 var item = this.findItemFromChild(e.getTarget());
487 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
491 onItemClick : function(item, index, e)
493 if(this.fireEvent("beforeclick", this, index, item, e) === false){
496 if (this.toggleSelect) {
497 var m = this.isSelected(item) ? 'unselect' : 'select';
500 _t[m](item, true, false);
503 if(this.multiSelect || this.singleSelect){
504 if(this.multiSelect && e.shiftKey && this.lastSelection){
505 this.select(this.getNodes(this.indexOf(this.lastSelection), index), false);
507 this.select(item, this.multiSelect && e.ctrlKey);
508 this.lastSelection = item;
520 * Get the number of selected nodes.
523 getSelectionCount : function(){
524 return this.selections.length;
528 * Get the currently selected nodes.
529 * @return {Array} An array of HTMLElements
531 getSelectedNodes : function(){
532 return this.selections;
536 * Get the indexes of the selected nodes.
539 getSelectedIndexes : function(){
540 var indexes = [], s = this.selections;
541 for(var i = 0, len = s.length; i < len; i++){
542 indexes.push(s[i].nodeIndex);
548 * Clear all selections
549 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange event
551 clearSelections : function(suppressEvent){
552 if(this.nodes && (this.multiSelect || this.singleSelect) && this.selections.length > 0){
553 this.cmp.elements = this.selections;
554 this.cmp.removeClass(this.selectedClass);
555 this.selections = [];
557 this.fireEvent("selectionchange", this, this.selections);
563 * Returns true if the passed node is selected
564 * @param {HTMLElement/Number} node The node or node index
567 isSelected : function(node){
568 var s = this.selections;
572 node = this.getNode(node);
573 return s.indexOf(node) !== -1;
578 * @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
579 * @param {Boolean} keepExisting (optional) true to keep existing selections
580 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
582 select : function(nodeInfo, keepExisting, suppressEvent){
583 if(nodeInfo instanceof Array){
585 this.clearSelections(true);
587 for(var i = 0, len = nodeInfo.length; i < len; i++){
588 this.select(nodeInfo[i], true, true);
592 var node = this.getNode(nodeInfo);
593 if(!node || this.isSelected(node)){
594 return; // already selected.
597 this.clearSelections(true);
600 if(this.fireEvent("beforeselect", this, node, this.selections) !== false){
601 Roo.fly(node).addClass(this.selectedClass);
602 this.selections.push(node);
604 this.fireEvent("selectionchange", this, this.selections);
612 * @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
613 * @param {Boolean} keepExisting (optional) true IGNORED (for campatibility with select)
614 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
616 unselect : function(nodeInfo, keepExisting, suppressEvent)
618 if(nodeInfo instanceof Array){
619 Roo.each(this.selections, function(s) {
620 this.unselect(s, nodeInfo);
624 var node = this.getNode(nodeInfo);
625 if(!node || !this.isSelected(node)){
626 //Roo.log("not selected");
627 return; // not selected.
631 Roo.each(this.selections, function(s) {
633 Roo.fly(node).removeClass(this.selectedClass);
641 this.fireEvent("selectionchange", this, this.selections);
645 * Gets a template node.
646 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
647 * @return {HTMLElement} The node or null if it wasn't found
649 getNode : function(nodeInfo){
650 if(typeof nodeInfo == "string"){
651 return document.getElementById(nodeInfo);
652 }else if(typeof nodeInfo == "number"){
653 return this.nodes[nodeInfo];
659 * Gets a range template nodes.
660 * @param {Number} startIndex
661 * @param {Number} endIndex
662 * @return {Array} An array of nodes
664 getNodes : function(start, end){
667 end = typeof end == "undefined" ? ns.length - 1 : end;
670 for(var i = start; i <= end; i++){
674 for(var i = start; i >= end; i--){
682 * Finds the index of the passed node
683 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
684 * @return {Number} The index of the node or -1
686 indexOf : function(node){
687 node = this.getNode(node);
688 if(typeof node.nodeIndex == "number"){
689 return node.nodeIndex;
692 for(var i = 0, len = ns.length; i < len; i++){