3a8024440c3af04571a756ce90b5bb6de267feb8.svn-base 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.Action
  8. * <p>
  9. * An Action is a piece of reusable functionality that can be abstracted
  10. * out of any particular component so that it can be usefully shared
  11. * among multiple components. Actions let you share handlers,
  12. * configuration options and UI updates across any components that
  13. * support the Action interface (primarily {@link Ext.Toolbar},
  14. * {@link Ext.Button} and {@link Ext.menu.Menu} components).
  15. * </p>
  16. * <p>
  17. * Aside from supporting the config object interface, any component that
  18. * needs to use Actions must also support the following method list, as
  19. * these will be called as needed by the Action class: setText(string),
  20. * setIconCls(string), setDisabled(boolean), setVisible(boolean) and
  21. * setHandler(function).
  22. * </p>
  23. * Example usage:<br>
  24. *
  25. * <pre><code>
  26. * // Define the shared action. Each component below will have the same
  27. * // display text and icon, and will display the same message on click.
  28. * var action = new Ext.Action({
  29. * text : 'Do something',
  30. * handler : function() {
  31. * Ext.Msg.alert('Click', 'You did something.');
  32. * },
  33. * iconCls : 'do-something'
  34. * });
  35. *
  36. * var panel = new Ext.Panel({
  37. * title : 'Actions',
  38. * width : 500,
  39. * height : 300,
  40. * tbar : [
  41. * // Add the action directly to a toolbar as a menu button
  42. * action, {
  43. * text : 'Action Menu',
  44. * // Add the action to a menu as a text item
  45. * menu : [action]
  46. * }],
  47. * items : [
  48. * // Add the action to the panel body as a standard button
  49. * new Ext.Button(action)],
  50. * renderTo : Ext.getBody()
  51. * });
  52. *
  53. * // Change the text for all components using the action
  54. * action.setText('Something else');
  55. * </code></pre>
  56. *
  57. * @constructor
  58. * @param {Object}
  59. * config The configuration options
  60. */
  61. Ext.Action = function(config) {
  62. this.initialConfig = config;
  63. this.items = [];
  64. }
  65. Ext.Action.prototype = {
  66. /**
  67. * @cfg {String} text The text to set for all components using this action
  68. * (defaults to '').
  69. */
  70. /**
  71. * @cfg {String} iconCls The icon CSS class for all components using this
  72. * action (defaults to ''). The class should supply a background image
  73. * that will be used as the icon image.
  74. */
  75. /**
  76. * @cfg {Boolean} disabled True to disable all components using this action,
  77. * false to enable them (defaults to false).
  78. */
  79. /**
  80. * @cfg {Boolean} hidden True to hide all components using this action,
  81. * false to show them (defaults to false).
  82. */
  83. /**
  84. * @cfg {Function} handler The function that will be invoked by each
  85. * component tied to this action when the component's primary event is
  86. * triggered (defaults to undefined).
  87. */
  88. /**
  89. * @cfg {Object} scope The scope in which the {@link #handler} function will
  90. * execute.
  91. */
  92. // private
  93. isAction : true,
  94. /**
  95. * Sets the text to be displayed by all components using this action.
  96. *
  97. * @param {String}
  98. * text The text to display
  99. */
  100. setText : function(text) {
  101. this.initialConfig.text = text;
  102. this.callEach('setText', [text]);
  103. },
  104. /**
  105. * Gets the text currently displayed by all components using this action.
  106. */
  107. getText : function() {
  108. return this.initialConfig.text;
  109. },
  110. /**
  111. * Sets the icon CSS class for all components using this action. The class
  112. * should supply a background image that will be used as the icon image.
  113. *
  114. * @param {String}
  115. * cls The CSS class supplying the icon image
  116. */
  117. setIconClass : function(cls) {
  118. this.initialConfig.iconCls = cls;
  119. this.callEach('setIconClass', [cls]);
  120. },
  121. /**
  122. * Gets the icon CSS class currently used by all components using this
  123. * action.
  124. */
  125. getIconClass : function() {
  126. return this.initialConfig.iconCls;
  127. },
  128. /**
  129. * Sets the disabled state of all components using this action. Shortcut
  130. * method for {@link #enable} and {@link #disable}.
  131. *
  132. * @param {Boolean}
  133. * disabled True to disable the component, false to enable it
  134. */
  135. setDisabled : function(v) {
  136. this.initialConfig.disabled = v;
  137. this.callEach('setDisabled', [v]);
  138. },
  139. /**
  140. * Enables all components using this action.
  141. */
  142. enable : function() {
  143. this.setDisabled(false);
  144. },
  145. /**
  146. * Disables all components using this action.
  147. */
  148. disable : function() {
  149. this.setDisabled(true);
  150. },
  151. /**
  152. * Returns true if the components using this action are currently disabled,
  153. * else returns false. Read-only.
  154. *
  155. * @property
  156. */
  157. isDisabled : function() {
  158. return this.initialConfig.disabled;
  159. },
  160. /**
  161. * Sets the hidden state of all components using this action. Shortcut
  162. * method for {@link #hide} and {@link #show}.
  163. *
  164. * @param {Boolean}
  165. * hidden True to hide the component, false to show it
  166. */
  167. setHidden : function(v) {
  168. this.initialConfig.hidden = v;
  169. this.callEach('setVisible', [!v]);
  170. },
  171. /**
  172. * Shows all components using this action.
  173. */
  174. show : function() {
  175. this.setHidden(false);
  176. },
  177. /**
  178. * Hides all components using this action.
  179. */
  180. hide : function() {
  181. this.setHidden(true);
  182. },
  183. /**
  184. * Returns true if the components using this action are currently hidden,
  185. * else returns false. Read-only.
  186. *
  187. * @property
  188. */
  189. isHidden : function() {
  190. return this.initialConfig.hidden;
  191. },
  192. /**
  193. * Sets the function that will be called by each component using this action
  194. * when its primary event is triggered.
  195. *
  196. * @param {Function}
  197. * fn The function that will be invoked by the action's
  198. * components. The function will be called with no arguments.
  199. * @param {Object}
  200. * scope The scope in which the function will execute
  201. */
  202. setHandler : function(fn, scope) {
  203. this.initialConfig.handler = fn;
  204. this.initialConfig.scope = scope;
  205. this.callEach('setHandler', [fn, scope]);
  206. },
  207. /**
  208. * Executes the specified function once for each component currently tied to
  209. * this action. The function passed in should accept a single argument that
  210. * will be an object that supports the basic Action config/method interface.
  211. *
  212. * @param {Function}
  213. * fn The function to execute for each component
  214. * @param {Object}
  215. * scope The scope in which the function will execute
  216. */
  217. each : function(fn, scope) {
  218. Ext.each(this.items, fn, scope);
  219. },
  220. // private
  221. callEach : function(fnName, args) {
  222. var cs = this.items;
  223. for (var i = 0, len = cs.length; i < len; i++) {
  224. cs[i][fnName].apply(cs[i], args);
  225. }
  226. },
  227. // private
  228. addComponent : function(comp) {
  229. this.items.push(comp);
  230. comp.on('destroy', this.removeComponent, this);
  231. },
  232. // private
  233. removeComponent : function(comp) {
  234. this.items.remove(comp);
  235. }
  236. };