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);
62 this.wrapEl = this.el.wrap().wrap();
63 ///this.el = this.wrapEla.appendChild(document.createElement("div"));
66 if(typeof(this.tpl) == "string"){
68 this.tpl = new Roo.Template(this.tpl);
71 // support xtype ctors..
72 this.tpl = new Roo.factory(this.tpl, Roo);
85 * Fires before a click is processed. Returns false to cancel the default action.
86 * @param {Roo.View} this
87 * @param {Number} index The index of the target node
88 * @param {HTMLElement} node The target node
89 * @param {Roo.EventObject} e The raw event object
94 * Fires when a template node is clicked.
95 * @param {Roo.View} this
96 * @param {Number} index The index of the target node
97 * @param {HTMLElement} node The target node
98 * @param {Roo.EventObject} e The raw event object
103 * Fires when a template node is double clicked.
104 * @param {Roo.View} this
105 * @param {Number} index The index of the target node
106 * @param {HTMLElement} node The target node
107 * @param {Roo.EventObject} e The raw event object
112 * Fires when a template node is right clicked.
113 * @param {Roo.View} this
114 * @param {Number} index The index of the target node
115 * @param {HTMLElement} node The target node
116 * @param {Roo.EventObject} e The raw event object
118 "contextmenu" : true,
120 * @event selectionchange
121 * Fires when the selected nodes change.
122 * @param {Roo.View} this
123 * @param {Array} selections Array of the selected nodes
125 "selectionchange" : true,
128 * @event beforeselect
129 * Fires before a selection is made. If any handlers return false, the selection is cancelled.
130 * @param {Roo.View} this
131 * @param {HTMLElement} node The node to be selected
132 * @param {Array} selections Array of currently selected nodes
134 "beforeselect" : true,
137 * Fires on every row to render, to allow you to change the data.
138 * @param {Roo.View} this
139 * @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);
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(){
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]);
284 html[html.length] = Roo.util.Format.trim(
286 t.applySubtemplate(this.dataName, data, this.store.meta) :
293 el.update(html.join(""));
294 this.nodes = el.dom.childNodes;
295 this.updateIndexes(0);
300 * Function to override to reformat the data that is sent to
301 * the template for each node.
302 * DEPRICATED - use the preparedata event handler.
303 * @param {Array/Object} data The raw data (array of colData for a data model bound view or
304 * a JSON object for an UpdateManager bound view).
306 prepareData : function(data, index, record)
308 this.fireEvent("preparedata", this, data, index, record);
312 onUpdate : function(ds, record){
313 Roo.log('on update');
314 this.clearSelections();
315 var index = this.store.indexOf(record);
316 var n = this.nodes[index];
317 this.tpl.insertBefore(n, this.prepareData(record.data, index, record));
318 n.parentNode.removeChild(n);
319 this.updateIndexes(index, index);
325 onAdd : function(ds, records, index)
327 Roo.log(['on Add', ds, records, index] );
328 this.clearSelections();
329 if(this.nodes.length == 0){
333 var n = this.nodes[index];
334 for(var i = 0, len = records.length; i < len; i++){
335 var d = this.prepareData(records[i].data, i, records[i]);
337 this.tpl.insertBefore(n, d);
340 this.tpl.append(this.el, d);
343 this.updateIndexes(index);
346 onRemove : function(ds, record, index){
348 this.clearSelections();
349 var el = this.dataName ?
350 this.el.child('.roo-tpl-' + this.dataName) :
353 el.dom.removeChild(this.nodes[index]);
354 this.updateIndexes(index);
358 * Refresh an individual node.
359 * @param {Number} index
361 refreshNode : function(index){
362 this.onUpdate(this.store, this.store.getAt(index));
365 updateIndexes : function(startIndex, endIndex){
367 startIndex = startIndex || 0;
368 endIndex = endIndex || ns.length - 1;
369 for(var i = startIndex; i <= endIndex; i++){
375 * Changes the data store this view uses and refresh the view.
376 * @param {Store} store
378 setStore : function(store, initial){
379 if(!initial && this.store){
380 this.store.un("datachanged", this.refresh);
381 this.store.un("add", this.onAdd);
382 this.store.un("remove", this.onRemove);
383 this.store.un("update", this.onUpdate);
384 this.store.un("clear", this.refresh);
385 this.store.un("beforeload", this.onBeforeLoad);
386 this.store.un("load", this.onLoad);
387 this.store.un("loadexception", this.onLoad);
391 store.on("datachanged", this.refresh, this);
392 store.on("add", this.onAdd, this);
393 store.on("remove", this.onRemove, this);
394 store.on("update", this.onUpdate, this);
395 store.on("clear", this.refresh, this);
396 store.on("beforeload", this.onBeforeLoad, this);
397 store.on("load", this.onLoad, this);
398 store.on("loadexception", this.onLoad, this);
406 * onbeforeLoad - masks the loading area.
409 onBeforeLoad : function(store,opts)
411 Roo.log('onBeforeLoad');
415 this.el.mask(this.mask ? this.mask : "Loading" );
424 * Returns the template node the passed child belongs to or null if it doesn't belong to one.
425 * @param {HTMLElement} node
426 * @return {HTMLElement} The template node
428 findItemFromChild : function(node){
429 var el = this.dataName ?
430 this.el.child('.roo-tpl-' + this.dataName,true) :
433 if(!node || node.parentNode == el){
436 var p = node.parentNode;
438 if(p.parentNode == el){
447 onClick : function(e){
448 var item = this.findItemFromChild(e.getTarget());
450 var index = this.indexOf(item);
451 if(this.onItemClick(item, index, e) !== false){
452 this.fireEvent("click", this, index, item, e);
455 this.clearSelections();
460 onContextMenu : function(e){
461 var item = this.findItemFromChild(e.getTarget());
463 this.fireEvent("contextmenu", this, this.indexOf(item), item, e);
468 onDblClick : function(e){
469 var item = this.findItemFromChild(e.getTarget());
471 this.fireEvent("dblclick", this, this.indexOf(item), item, e);
475 onItemClick : function(item, index, e)
477 if(this.fireEvent("beforeclick", this, index, item, e) === false){
480 if (this.toggleSelect) {
481 var m = this.isSelected(item) ? 'unselect' : 'select';
484 _t[m](item, true, false);
487 if(this.multiSelect || this.singleSelect){
488 if(this.multiSelect && e.shiftKey && this.lastSelection){
489 this.select(this.getNodes(this.indexOf(this.lastSelection), index), false);
491 this.select(item, this.multiSelect && e.ctrlKey);
492 this.lastSelection = item;
504 * Get the number of selected nodes.
507 getSelectionCount : function(){
508 return this.selections.length;
512 * Get the currently selected nodes.
513 * @return {Array} An array of HTMLElements
515 getSelectedNodes : function(){
516 return this.selections;
520 * Get the indexes of the selected nodes.
523 getSelectedIndexes : function(){
524 var indexes = [], s = this.selections;
525 for(var i = 0, len = s.length; i < len; i++){
526 indexes.push(s[i].nodeIndex);
532 * Clear all selections
533 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange event
535 clearSelections : function(suppressEvent){
536 if(this.nodes && (this.multiSelect || this.singleSelect) && this.selections.length > 0){
537 this.cmp.elements = this.selections;
538 this.cmp.removeClass(this.selectedClass);
539 this.selections = [];
541 this.fireEvent("selectionchange", this, this.selections);
547 * Returns true if the passed node is selected
548 * @param {HTMLElement/Number} node The node or node index
551 isSelected : function(node){
552 var s = this.selections;
556 node = this.getNode(node);
557 return s.indexOf(node) !== -1;
562 * @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
563 * @param {Boolean} keepExisting (optional) true to keep existing selections
564 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
566 select : function(nodeInfo, keepExisting, suppressEvent){
567 if(nodeInfo instanceof Array){
569 this.clearSelections(true);
571 for(var i = 0, len = nodeInfo.length; i < len; i++){
572 this.select(nodeInfo[i], true, true);
576 var node = this.getNode(nodeInfo);
577 if(!node || this.isSelected(node)){
578 return; // already selected.
581 this.clearSelections(true);
583 if(this.fireEvent("beforeselect", this, node, this.selections) !== false){
584 Roo.fly(node).addClass(this.selectedClass);
585 this.selections.push(node);
587 this.fireEvent("selectionchange", this, this.selections);
595 * @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
596 * @param {Boolean} keepExisting (optional) true IGNORED (for campatibility with select)
597 * @param {Boolean} suppressEvent (optional) true to skip firing of the selectionchange vent
599 unselect : function(nodeInfo, keepExisting, suppressEvent)
601 if(nodeInfo instanceof Array){
602 Roo.each(this.selections, function(s) {
603 this.unselect(s, nodeInfo);
607 var node = this.getNode(nodeInfo);
608 if(!node || !this.isSelected(node)){
609 Roo.log("not selected");
610 return; // not selected.
614 Roo.each(this.selections, function(s) {
616 Roo.fly(node).removeClass(this.selectedClass);
624 this.fireEvent("selectionchange", this, this.selections);
628 * Gets a template node.
629 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
630 * @return {HTMLElement} The node or null if it wasn't found
632 getNode : function(nodeInfo){
633 if(typeof nodeInfo == "string"){
634 return document.getElementById(nodeInfo);
635 }else if(typeof nodeInfo == "number"){
636 return this.nodes[nodeInfo];
642 * Gets a range template nodes.
643 * @param {Number} startIndex
644 * @param {Number} endIndex
645 * @return {Array} An array of nodes
647 getNodes : function(start, end){
650 end = typeof end == "undefined" ? ns.length - 1 : end;
653 for(var i = start; i <= end; i++){
657 for(var i = start; i >= end; i--){
665 * Finds the index of the passed node
666 * @param {HTMLElement/String/Number} nodeInfo An HTMLElement template node, index of a template node or the id of a template node
667 * @return {Number} The index of the node or -1
669 indexOf : function(node){
670 node = this.getNode(node);
671 if(typeof node.nodeIndex == "number"){
672 return node.nodeIndex;
675 for(var i = 0, len = ns.length; i < len; i++){