ListMenu.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-ux-grid-menu-ListMenu'>/**
  19. </span> * @class Ext.ux.grid.menu.ListMenu
  20. * @extends Ext.menu.Menu
  21. * This is a supporting class for {@link Ext.ux.grid.filter.ListFilter}.
  22. * Although not listed as configuration options for this class, this class
  23. * also accepts all configuration options from {@link Ext.ux.grid.filter.ListFilter}.
  24. */
  25. Ext.define('Ext.ux.grid.menu.ListMenu', {
  26. extend: 'Ext.menu.Menu',
  27. <span id='Ext-ux-grid-menu-ListMenu-cfg-labelField'> /**
  28. </span> * @cfg {String} labelField
  29. * Defaults to 'text'.
  30. */
  31. labelField : 'text',
  32. <span id='Ext-ux-grid-menu-ListMenu-cfg-paramPrefix'> /**
  33. </span> * @cfg {String} paramPrefix
  34. * Defaults to 'Loading...'.
  35. */
  36. loadingText : 'Loading...',
  37. <span id='Ext-ux-grid-menu-ListMenu-cfg-loadOnShow'> /**
  38. </span> * @cfg {Boolean} loadOnShow
  39. * Defaults to true.
  40. */
  41. loadOnShow : true,
  42. <span id='Ext-ux-grid-menu-ListMenu-cfg-single'> /**
  43. </span> * @cfg {Boolean} single
  44. * Specify true to group all items in this list into a single-select
  45. * radio button group. Defaults to false.
  46. */
  47. single : false,
  48. constructor : function (cfg) {
  49. var me = this,
  50. options,
  51. i,
  52. len,
  53. value;
  54. me.selected = [];
  55. me.addEvents(
  56. <span id='Ext-ux-grid-menu-ListMenu-event-checkchange'> /**
  57. </span> * @event checkchange
  58. * Fires when there is a change in checked items from this list
  59. * @param {Object} item Ext.menu.CheckItem
  60. * @param {Object} checked The checked value that was set
  61. */
  62. 'checkchange'
  63. );
  64. me.callParent([cfg = cfg || {}]);
  65. if(!cfg.store &amp;&amp; cfg.options) {
  66. options = [];
  67. for(i = 0, len = cfg.options.length; i &lt; len; i++){
  68. value = cfg.options[i];
  69. switch(Ext.type(value)){
  70. case 'array': options.push(value); break;
  71. case 'object': options.push([value.id, value[me.labelField]]); break;
  72. case 'string': options.push([value, value]); break;
  73. }
  74. }
  75. me.store = Ext.create('Ext.data.ArrayStore', {
  76. fields: ['id', me.labelField],
  77. data: options,
  78. listeners: {
  79. load: me.onLoad,
  80. scope: me
  81. }
  82. });
  83. me.loaded = true;
  84. me.autoStore = true;
  85. } else {
  86. me.add({
  87. text: me.loadingText,
  88. iconCls: 'loading-indicator'
  89. });
  90. me.store.on('load', me.onLoad, me);
  91. }
  92. },
  93. destroy : function () {
  94. var me = this,
  95. store = me.store;
  96. if (store) {
  97. if (me.autoStore) {
  98. store.destroyStore();
  99. } else {
  100. store.un('unload', me.onLoad, me);
  101. }
  102. }
  103. me.callParent();
  104. },
  105. <span id='Ext-ux-grid-menu-ListMenu-method-show'> /**
  106. </span> * Lists will initially show a 'loading' item while the data is retrieved from the store.
  107. * In some cases the loaded data will result in a list that goes off the screen to the
  108. * right (as placement calculations were done with the loading item). This adapter will
  109. * allow show to be called with no arguments to show with the previous arguments and
  110. * thus recalculate the width and potentially hang the menu from the left.
  111. */
  112. show : function () {
  113. if (this.loadOnShow &amp;&amp; !this.loaded &amp;&amp; !this.store.loading) {
  114. this.store.load();
  115. }
  116. this.callParent();
  117. },
  118. <span id='Ext-ux-grid-menu-ListMenu-method-onLoad'> /** @private */
  119. </span> onLoad : function (store, records) {
  120. var me = this,
  121. gid, itemValue, i, len,
  122. listeners = {
  123. checkchange: me.checkChange,
  124. scope: me
  125. };
  126. Ext.suspendLayouts();
  127. me.removeAll(true);
  128. gid = me.single ? Ext.id() : null;
  129. for (i = 0, len = records.length; i &lt; len; i++) {
  130. itemValue = records[i].get('id');
  131. me.add(Ext.create('Ext.menu.CheckItem', {
  132. text: records[i].get(me.labelField),
  133. group: gid,
  134. checked: Ext.Array.contains(me.selected, itemValue),
  135. hideOnClick: false,
  136. value: itemValue,
  137. listeners: listeners
  138. }));
  139. }
  140. me.loaded = true;
  141. Ext.resumeLayouts(true);
  142. me.fireEvent('load', me, records);
  143. },
  144. <span id='Ext-ux-grid-menu-ListMenu-method-getSelected'> /**
  145. </span> * Get the selected items.
  146. * @return {Array} selected
  147. */
  148. getSelected : function () {
  149. return this.selected;
  150. },
  151. <span id='Ext-ux-grid-menu-ListMenu-method-setSelected'> /** @private */
  152. </span> setSelected : function (value) {
  153. value = this.selected = [].concat(value);
  154. if (this.loaded) {
  155. this.items.each(function(item){
  156. item.setChecked(false, true);
  157. for (var i = 0, len = value.length; i &lt; len; i++) {
  158. if (item.value == value[i]) {
  159. item.setChecked(true, true);
  160. }
  161. }
  162. }, this);
  163. }
  164. },
  165. <span id='Ext-ux-grid-menu-ListMenu-method-checkChange'> /**
  166. </span> * Handler for the 'checkchange' event from an check item in this menu
  167. * @param {Object} item Ext.menu.CheckItem
  168. * @param {Object} checked The checked value that was set
  169. */
  170. checkChange : function (item, checked) {
  171. var value = [];
  172. this.items.each(function(item){
  173. if (item.checked) {
  174. value.push(item.value);
  175. }
  176. },this);
  177. this.selected = value;
  178. this.fireEvent('checkchange', item, checked);
  179. }
  180. });
  181. </pre>
  182. </body>
  183. </html>