Action3.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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-grid-column-Action'>/**
  19. </span> * A Grid header type which renders an icon, or a series of icons in a grid cell, and offers a scoped click
  20. * handler for each icon.
  21. *
  22. * @example
  23. * Ext.create('Ext.data.Store', {
  24. * storeId:'employeeStore',
  25. * fields:['firstname', 'lastname', 'seniority', 'dep', 'hired'],
  26. * data:[
  27. * {firstname:&quot;Michael&quot;, lastname:&quot;Scott&quot;},
  28. * {firstname:&quot;Dwight&quot;, lastname:&quot;Schrute&quot;},
  29. * {firstname:&quot;Jim&quot;, lastname:&quot;Halpert&quot;},
  30. * {firstname:&quot;Kevin&quot;, lastname:&quot;Malone&quot;},
  31. * {firstname:&quot;Angela&quot;, lastname:&quot;Martin&quot;}
  32. * ]
  33. * });
  34. *
  35. * Ext.create('Ext.grid.Panel', {
  36. * title: 'Action Column Demo',
  37. * store: Ext.data.StoreManager.lookup('employeeStore'),
  38. * columns: [
  39. * {text: 'First Name', dataIndex:'firstname'},
  40. * {text: 'Last Name', dataIndex:'lastname'},
  41. * {
  42. * xtype:'actioncolumn',
  43. * width:50,
  44. * items: [{
  45. * icon: 'extjs/examples/shared/icons/fam/cog_edit.png', // Use a URL in the icon config
  46. * tooltip: 'Edit',
  47. * handler: function(grid, rowIndex, colIndex) {
  48. * var rec = grid.getStore().getAt(rowIndex);
  49. * alert(&quot;Edit &quot; + rec.get('firstname'));
  50. * }
  51. * },{
  52. * icon: 'extjs/examples/restful/images/delete.png',
  53. * tooltip: 'Delete',
  54. * handler: function(grid, rowIndex, colIndex) {
  55. * var rec = grid.getStore().getAt(rowIndex);
  56. * alert(&quot;Terminate &quot; + rec.get('firstname'));
  57. * }
  58. * }]
  59. * }
  60. * ],
  61. * width: 250,
  62. * renderTo: Ext.getBody()
  63. * });
  64. *
  65. * The action column can be at any index in the columns array, and a grid can have any number of
  66. * action columns.
  67. */
  68. Ext.define('Ext.grid.column.Action', {
  69. extend: 'Ext.grid.column.Column',
  70. alias: ['widget.actioncolumn'],
  71. alternateClassName: 'Ext.grid.ActionColumn',
  72. <span id='Ext-grid-column-Action-cfg-icon'> /**
  73. </span> * @cfg {String} icon
  74. * The URL of an image to display as the clickable element in the column.
  75. *
  76. * Defaults to `{@link Ext#BLANK_IMAGE_URL}`.
  77. */
  78. <span id='Ext-grid-column-Action-cfg-iconCls'> /**
  79. </span> * @cfg {String} iconCls
  80. * A CSS class to apply to the icon image. To determine the class dynamically, configure the Column with
  81. * a `{@link #getClass}` function.
  82. */
  83. <span id='Ext-grid-column-Action-cfg-handler'> /**
  84. </span> * @cfg {Function} handler
  85. * A function called when the icon is clicked.
  86. * @cfg {Ext.view.Table} handler.view The owning TableView.
  87. * @cfg {Number} handler.rowIndex The row index clicked on.
  88. * @cfg {Number} handler.colIndex The column index clicked on.
  89. * @cfg {Object} handler.item The clicked item (or this Column if multiple {@link #cfg-items} were not configured).
  90. * @cfg {Event} handler.e The click event.
  91. * @cfg {Ext.data.Model} handler.record The Record underlying the clicked row.
  92. * @cfg {HtmlElement} row The table row clicked upon.
  93. */
  94. <span id='Ext-grid-column-Action-cfg-scope'> /**
  95. </span> * @cfg {Object} scope
  96. * The scope (**this** reference) in which the `{@link #handler}` and `{@link #getClass}` fuctions are executed.
  97. * Defaults to this Column.
  98. */
  99. <span id='Ext-grid-column-Action-cfg-tooltip'> /**
  100. </span> * @cfg {String} tooltip
  101. * A tooltip message to be displayed on hover. {@link Ext.tip.QuickTipManager#init Ext.tip.QuickTipManager} must
  102. * have been initialized.
  103. */
  104. <span id='Ext-grid-column-Action-cfg-disabled'> /**
  105. </span> * @cfg {Boolean} disabled
  106. * If true, the action will not respond to click events, and will be displayed semi-opaque.
  107. */
  108. <span id='Ext-grid-column-Action-cfg-stopSelection'> /**
  109. </span> * @cfg {Boolean} [stopSelection=true]
  110. * Prevent grid selection upon mousedown.
  111. */
  112. <span id='Ext-grid-column-Action-cfg-getClass'> /**
  113. </span> * @cfg {Function} getClass
  114. * A function which returns the CSS class to apply to the icon image.
  115. *
  116. * @cfg {Object} getClass.v The value of the column's configured field (if any).
  117. *
  118. * @cfg {Object} getClass.metadata An object in which you may set the following attributes:
  119. * @cfg {String} getClass.metadata.css A CSS class name to add to the cell's TD element.
  120. * @cfg {String} getClass.metadata.attr An HTML attribute definition string to apply to the data container
  121. * element *within* the table cell (e.g. 'style=&quot;color:red;&quot;').
  122. *
  123. * @cfg {Ext.data.Model} getClass.r The Record providing the data.
  124. *
  125. * @cfg {Number} getClass.rowIndex The row index..
  126. *
  127. * @cfg {Number} getClass.colIndex The column index.
  128. *
  129. * @cfg {Ext.data.Store} getClass.store The Store which is providing the data Model.
  130. */
  131. <span id='Ext-grid-column-Action-cfg-items'> /**
  132. </span> * @cfg {Object[]} items
  133. * An Array which may contain multiple icon definitions, each element of which may contain:
  134. *
  135. * @cfg {String} items.icon The url of an image to display as the clickable element in the column.
  136. *
  137. * @cfg {String} items.iconCls A CSS class to apply to the icon image. To determine the class dynamically,
  138. * configure the item with a `getClass` function.
  139. *
  140. * @cfg {Function} items.getClass A function which returns the CSS class to apply to the icon image.
  141. * @cfg {Object} items.getClass.v The value of the column's configured field (if any).
  142. * @cfg {Object} items.getClass.metadata An object in which you may set the following attributes:
  143. * @cfg {String} items.getClass.metadata.css A CSS class name to add to the cell's TD element.
  144. * @cfg {String} items.getClass.metadata.attr An HTML attribute definition string to apply to the data
  145. * container element _within_ the table cell (e.g. 'style=&quot;color:red;&quot;').
  146. * @cfg {Ext.data.Model} items.getClass.r The Record providing the data.
  147. * @cfg {Number} items.getClass.rowIndex The row index..
  148. * @cfg {Number} items.getClass.colIndex The column index.
  149. * @cfg {Ext.data.Store} items.getClass.store The Store which is providing the data Model.
  150. *
  151. * @cfg {Function} items.handler A function called when the icon is clicked.
  152. *
  153. * @cfg {Object} items.scope The scope (`this` reference) in which the `handler` and `getClass` functions
  154. * are executed. Fallback defaults are this Column's configured scope, then this Column.
  155. *
  156. * @cfg {String} items.tooltip A tooltip message to be displayed on hover.
  157. * @cfg {Boolean} items.disabled If true, the action will not respond to click events, and will be displayed semi-opaque.
  158. * {@link Ext.tip.QuickTipManager#init Ext.tip.QuickTipManager} must have been initialized.
  159. */
  160. <span id='Ext-grid-column-Action-property-items'> /**
  161. </span> * @property {Array} items
  162. * An array of action items copied from the configured {@link #cfg-items items} configuration. Each will have
  163. * an `enable` and `disable` method added which will enable and disable the associated action, and
  164. * update the displayed icon accordingly.
  165. */
  166. actionIdRe: new RegExp(Ext.baseCSSPrefix + 'action-col-(\\d+)'),
  167. <span id='Ext-grid-column-Action-cfg-altText'> /**
  168. </span> * @cfg {String} altText
  169. * The alt text to use for the image element.
  170. */
  171. altText: '',
  172. <span id='Ext-grid-column-Action-cfg-menuText'> /**
  173. </span> * @cfg {String} menuText=[&lt;i&gt;Actions&lt;/i&gt;]
  174. * Text to display in this column's menu item if no {@link #text} was specified as a header.
  175. */
  176. menuText: '&lt;i&gt;Actions&lt;/i&gt;',
  177. sortable: false,
  178. constructor: function(config) {
  179. var me = this,
  180. cfg = Ext.apply({}, config),
  181. items = cfg.items || [me],
  182. hasGetClass,
  183. i,
  184. len;
  185. me.origRenderer = cfg.renderer || me.renderer;
  186. me.origScope = cfg.scope || me.scope;
  187. delete me.renderer;
  188. delete me.scope;
  189. delete cfg.renderer;
  190. delete cfg.scope;
  191. // This is a Container. Delete the items config to be reinstated after construction.
  192. delete cfg.items;
  193. me.callParent([cfg]);
  194. // Items is an array property of ActionColumns
  195. me.items = items;
  196. for (i = 0, len = items.length; i &lt; len; ++i) {
  197. if (items[i].getClass) {
  198. hasGetClass = true;
  199. break;
  200. }
  201. }
  202. // Also need to check for getClass, since it changes how the cell renders
  203. if (me.origRenderer || hasGetClass) {
  204. me.hasCustomRenderer = true;
  205. }
  206. },
  207. // Renderer closure iterates through items creating an &lt;img&gt; element for each and tagging with an identifying
  208. // class name x-action-col-{n}
  209. defaultRenderer: function(v, meta){
  210. var me = this,
  211. prefix = Ext.baseCSSPrefix,
  212. scope = me.origScope || me,
  213. items = me.items,
  214. len = items.length,
  215. i = 0,
  216. item;
  217. // Allow a configured renderer to create initial value (And set the other values in the &quot;metadata&quot; argument!)
  218. v = Ext.isFunction(me.origRenderer) ? me.origRenderer.apply(scope, arguments) || '' : '';
  219. meta.tdCls += ' ' + Ext.baseCSSPrefix + 'action-col-cell';
  220. for (; i &lt; len; i++) {
  221. item = items[i];
  222. // Only process the item action setup once.
  223. if (!item.hasActionConfiguration) {
  224. // Apply our documented default to all items
  225. item.stopSelection = me.stopSelection;
  226. item.disable = Ext.Function.bind(me.disableAction, me, [i], 0);
  227. item.enable = Ext.Function.bind(me.enableAction, me, [i], 0);
  228. item.hasActionConfiguration = true;
  229. }
  230. v += '&lt;img alt=&quot;' + (item.altText || me.altText) + '&quot; src=&quot;' + (item.icon || Ext.BLANK_IMAGE_URL) +
  231. '&quot; class=&quot;' + prefix + 'action-col-icon ' + prefix + 'action-col-' + String(i) + ' ' + (item.disabled ? prefix + 'item-disabled' : ' ') +
  232. ' ' + (Ext.isFunction(item.getClass) ? item.getClass.apply(item.scope || scope, arguments) : (item.iconCls || me.iconCls || '')) + '&quot;' +
  233. ((item.tooltip) ? ' data-qtip=&quot;' + item.tooltip + '&quot;' : '') + ' /&gt;';
  234. }
  235. return v;
  236. },
  237. <span id='Ext-grid-column-Action-method-enableAction'> /**
  238. </span> * Enables this ActionColumn's action at the specified index.
  239. * @param {Number/Ext.grid.column.Action} index
  240. * @param {Boolean} [silent=false]
  241. */
  242. enableAction: function(index, silent) {
  243. var me = this;
  244. if (!index) {
  245. index = 0;
  246. } else if (!Ext.isNumber(index)) {
  247. index = Ext.Array.indexOf(me.items, index);
  248. }
  249. me.items[index].disabled = false;
  250. me.up('tablepanel').el.select('.' + Ext.baseCSSPrefix + 'action-col-' + index).removeCls(me.disabledCls);
  251. if (!silent) {
  252. me.fireEvent('enable', me);
  253. }
  254. },
  255. <span id='Ext-grid-column-Action-method-disableAction'> /**
  256. </span> * Disables this ActionColumn's action at the specified index.
  257. * @param {Number/Ext.grid.column.Action} index
  258. * @param {Boolean} [silent=false]
  259. */
  260. disableAction: function(index, silent) {
  261. var me = this;
  262. if (!index) {
  263. index = 0;
  264. } else if (!Ext.isNumber(index)) {
  265. index = Ext.Array.indexOf(me.items, index);
  266. }
  267. me.items[index].disabled = true;
  268. me.up('tablepanel').el.select('.' + Ext.baseCSSPrefix + 'action-col-' + index).addCls(me.disabledCls);
  269. if (!silent) {
  270. me.fireEvent('disable', me);
  271. }
  272. },
  273. destroy: function() {
  274. delete this.items;
  275. delete this.renderer;
  276. return this.callParent(arguments);
  277. },
  278. <span id='Ext-grid-column-Action-method-processEvent'> /**
  279. </span> * @private
  280. * Process and refire events routed from the GridView's processEvent method.
  281. * Also fires any configured click handlers. By default, cancels the mousedown event to prevent selection.
  282. * Returns the event handler's status to allow canceling of GridView's bubbling process.
  283. */
  284. processEvent : function(type, view, cell, recordIndex, cellIndex, e, record, row){
  285. var me = this,
  286. target = e.getTarget(),
  287. match,
  288. item, fn,
  289. key = type == 'keydown' &amp;&amp; e.getKey();
  290. // If the target was not within a cell (ie it's a keydown event from the View), then
  291. // rely on the selection data injected by View.processUIEvent to grab the
  292. // first action icon from the selected cell.
  293. if (key &amp;&amp; !Ext.fly(target).findParent(view.cellSelector)) {
  294. target = Ext.fly(cell).down('.' + Ext.baseCSSPrefix + 'action-col-icon', true);
  295. }
  296. // NOTE: The statement below tests the truthiness of an assignment.
  297. if (target &amp;&amp; (match = target.className.match(me.actionIdRe))) {
  298. item = me.items[parseInt(match[1], 10)];
  299. if (item) {
  300. if (type == 'click' || (key == e.ENTER || key == e.SPACE)) {
  301. fn = item.handler || me.handler;
  302. if (fn &amp;&amp; !item.disabled) {
  303. fn.call(item.scope || me.origScope || me, view, recordIndex, cellIndex, item, e, record, row);
  304. }
  305. } else if (type == 'mousedown' &amp;&amp; item.stopSelection !== false) {
  306. return false;
  307. }
  308. }
  309. }
  310. return me.callParent(arguments);
  311. },
  312. cascade: function(fn, scope) {
  313. fn.call(scope||this, this);
  314. },
  315. // Private override because this cannot function as a Container, and it has an items property which is an Array, NOT a MixedCollection.
  316. getRefItems: function() {
  317. return [];
  318. }
  319. });
  320. </pre>
  321. </body>
  322. </html>