3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
7 Ext.namespace('Ext.ux.menu');
10 * @class Ext.ux.menu.ListMenu
11 * @extends Ext.menu.Menu
12 * This is a supporting class for {@link Ext.ux.grid.filter.ListFilter}.
13 * Although not listed as configuration options for this class, this class
14 * also accepts all configuration options from {@link Ext.ux.grid.filter.ListFilter}.
16 Ext.ux.menu.ListMenu = Ext.extend(Ext.menu.Menu, {
18 * @cfg {String} labelField
23 * @cfg {String} paramPrefix
24 * Defaults to 'Loading...'.
26 loadingText : 'Loading...',
28 * @cfg {Boolean} loadOnShow
33 * @cfg {Boolean} single
34 * Specify true to group all items in this list into a single-select
35 * radio button group. Defaults to false.
39 constructor : function (cfg) {
44 * Fires when there is a change in checked items from this list
45 * @param {Object} item Ext.menu.CheckItem
46 * @param {Object} checked The checked value that was set
51 Ext.ux.menu.ListMenu.superclass.constructor.call(this, cfg = cfg || {});
53 if(!cfg.store && cfg.options){
55 for(var i=0, len=cfg.options.length; i<len; i++){
56 var value = cfg.options[i];
57 switch(Ext.type(value)){
58 case 'array': options.push(value); break;
59 case 'object': options.push([value.id, value[this.labelField]]); break;
60 case 'string': options.push([value, value]); break;
64 this.store = new Ext.data.Store({
65 reader: new Ext.data.ArrayReader({id: 0}, ['id', this.labelField]),
74 this.add({text: this.loadingText, iconCls: 'loading-indicator'});
75 this.store.on('load', this.onLoad, this);
79 destroy : function () {
83 Ext.ux.menu.ListMenu.superclass.destroy.call(this);
87 * Lists will initially show a 'loading' item while the data is retrieved from the store.
88 * In some cases the loaded data will result in a list that goes off the screen to the
89 * right (as placement calculations were done with the loading item). This adapter will
90 * allow show to be called with no arguments to show with the previous arguments and
91 * thus recalculate the width and potentially hang the menu from the left.
96 if(arguments.length === 0){
97 Ext.ux.menu.ListMenu.superclass.show.apply(this, lastArgs);
100 if (this.loadOnShow && !this.loaded) {
103 Ext.ux.menu.ListMenu.superclass.show.apply(this, arguments);
109 onLoad : function (store, records) {
110 var visible = this.isVisible();
113 this.removeAll(true);
115 var gid = this.single ? Ext.id() : null;
116 for(var i=0, len=records.length; i<len; i++){
117 var item = new Ext.menu.CheckItem({
118 text: records[i].get(this.labelField),
120 checked: this.selected.indexOf(records[i].id) > -1,
121 hideOnClick: false});
123 item.itemId = records[i].id;
124 item.on('checkchange', this.checkChange, this);
134 this.fireEvent('load', this, records);
138 * Get the selected items.
139 * @return {Array} selected
141 getSelected : function () {
142 return this.selected;
146 setSelected : function (value) {
147 value = this.selected = [].concat(value);
150 this.items.each(function(item){
151 item.setChecked(false, true);
152 for (var i = 0, len = value.length; i < len; i++) {
153 if (item.itemId == value[i]) {
154 item.setChecked(true, true);
162 * Handler for the 'checkchange' event from an check item in this menu
163 * @param {Object} item Ext.menu.CheckItem
164 * @param {Object} checked The checked value that was set
166 checkChange : function (item, checked) {
168 this.items.each(function(item){
170 value.push(item.itemId);
173 this.selected = value;
175 this.fireEvent('checkchange', item, checked);