_Plugin.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. define("dijit/_editor/_Plugin", [
  2. "dojo/_base/connect", // connect.connect
  3. "dojo/_base/declare", // declare
  4. "dojo/_base/lang", // lang.mixin, lang.hitch
  5. "../form/Button"
  6. ], function(connect, declare, lang, Button){
  7. // module:
  8. // dijit/_editor/_Plugin
  9. // summary:
  10. // Base class for a "plugin" to the editor, which is usually
  11. // a single button on the Toolbar and some associated code
  12. var _Plugin = declare("dijit._editor._Plugin", null, {
  13. // summary:
  14. // Base class for a "plugin" to the editor, which is usually
  15. // a single button on the Toolbar and some associated code
  16. constructor: function(/*Object?*/args){
  17. this.params = args || {};
  18. lang.mixin(this, this.params);
  19. this._connects=[];
  20. this._attrPairNames = {};
  21. },
  22. // editor: [const] dijit.Editor
  23. // Points to the parent editor
  24. editor: null,
  25. // iconClassPrefix: [const] String
  26. // The CSS class name for the button node is formed from `iconClassPrefix` and `command`
  27. iconClassPrefix: "dijitEditorIcon",
  28. // button: dijit._Widget?
  29. // Pointer to `dijit.form.Button` or other widget (ex: `dijit.form.FilteringSelect`)
  30. // that is added to the toolbar to control this plugin.
  31. // If not specified, will be created on initialization according to `buttonClass`
  32. button: null,
  33. // command: String
  34. // String like "insertUnorderedList", "outdent", "justifyCenter", etc. that represents an editor command.
  35. // Passed to editor.execCommand() if `useDefaultCommand` is true.
  36. command: "",
  37. // useDefaultCommand: Boolean
  38. // If true, this plugin executes by calling Editor.execCommand() with the argument specified in `command`.
  39. useDefaultCommand: true,
  40. // buttonClass: Widget Class
  41. // Class of widget (ex: dijit.form.Button or dijit.form.FilteringSelect)
  42. // that is added to the toolbar to control this plugin.
  43. // This is used to instantiate the button, unless `button` itself is specified directly.
  44. buttonClass: Button,
  45. // disabled: Boolean
  46. // Flag to indicate if this plugin has been disabled and should do nothing
  47. // helps control button state, among other things. Set via the setter api.
  48. disabled: false,
  49. getLabel: function(/*String*/key){
  50. // summary:
  51. // Returns the label to use for the button
  52. // tags:
  53. // private
  54. return this.editor.commands[key]; // String
  55. },
  56. _initButton: function(){
  57. // summary:
  58. // Initialize the button or other widget that will control this plugin.
  59. // This code only works for plugins controlling built-in commands in the editor.
  60. // tags:
  61. // protected extension
  62. if(this.command.length){
  63. var label = this.getLabel(this.command),
  64. editor = this.editor,
  65. className = this.iconClassPrefix+" "+this.iconClassPrefix + this.command.charAt(0).toUpperCase() + this.command.substr(1);
  66. if(!this.button){
  67. var props = lang.mixin({
  68. label: label,
  69. dir: editor.dir,
  70. lang: editor.lang,
  71. showLabel: false,
  72. iconClass: className,
  73. dropDown: this.dropDown,
  74. tabIndex: "-1"
  75. }, this.params || {});
  76. this.button = new this.buttonClass(props);
  77. }
  78. }
  79. if(this.get("disabled") && this.button){
  80. this.button.set("disabled", this.get("disabled"));
  81. }
  82. },
  83. destroy: function(){
  84. // summary:
  85. // Destroy this plugin
  86. var h;
  87. while(h = this._connects.pop()){ h.remove(); }
  88. if(this.dropDown){
  89. this.dropDown.destroyRecursive();
  90. }
  91. },
  92. connect: function(o, f, tf){
  93. // summary:
  94. // Make a connect.connect() that is automatically disconnected when this plugin is destroyed.
  95. // Similar to `dijit._Widget.connect`.
  96. // tags:
  97. // protected
  98. this._connects.push(connect.connect(o, f, this, tf));
  99. },
  100. updateState: function(){
  101. // summary:
  102. // Change state of the plugin to respond to events in the editor.
  103. // description:
  104. // This is called on meaningful events in the editor, such as change of selection
  105. // or caret position (but not simple typing of alphanumeric keys). It gives the
  106. // plugin a chance to update the CSS of its button.
  107. //
  108. // For example, the "bold" plugin will highlight/unhighlight the bold button depending on whether the
  109. // characters next to the caret are bold or not.
  110. //
  111. // Only makes sense when `useDefaultCommand` is true, as it calls Editor.queryCommandEnabled(`command`).
  112. var e = this.editor,
  113. c = this.command,
  114. checked, enabled;
  115. if(!e || !e.isLoaded || !c.length){ return; }
  116. var disabled = this.get("disabled");
  117. if(this.button){
  118. try{
  119. enabled = !disabled && e.queryCommandEnabled(c);
  120. if(this.enabled !== enabled){
  121. this.enabled = enabled;
  122. this.button.set('disabled', !enabled);
  123. }
  124. if(typeof this.button.checked == 'boolean'){
  125. checked = e.queryCommandState(c);
  126. if(this.checked !== checked){
  127. this.checked = checked;
  128. this.button.set('checked', e.queryCommandState(c));
  129. }
  130. }
  131. }catch(e){
  132. console.log(e); // FIXME: we shouldn't have debug statements in our code. Log as an error?
  133. }
  134. }
  135. },
  136. setEditor: function(/*dijit.Editor*/ editor){
  137. // summary:
  138. // Tell the plugin which Editor it is associated with.
  139. // TODO: refactor code to just pass editor to constructor.
  140. // FIXME: detach from previous editor!!
  141. this.editor = editor;
  142. // FIXME: prevent creating this if we don't need to (i.e., editor can't handle our command)
  143. this._initButton();
  144. // Processing for buttons that execute by calling editor.execCommand()
  145. if(this.button && this.useDefaultCommand){
  146. if(this.editor.queryCommandAvailable(this.command)){
  147. this.connect(this.button, "onClick",
  148. lang.hitch(this.editor, "execCommand", this.command, this.commandArg)
  149. );
  150. }else{
  151. // hide button because editor doesn't support command (due to browser limitations)
  152. this.button.domNode.style.display = "none";
  153. }
  154. }
  155. this.connect(this.editor, "onNormalizedDisplayChanged", "updateState");
  156. },
  157. setToolbar: function(/*dijit.Toolbar*/ toolbar){
  158. // summary:
  159. // Tell the plugin to add it's controller widget (often a button)
  160. // to the toolbar. Does nothing if there is no controller widget.
  161. // TODO: refactor code to just pass toolbar to constructor.
  162. if(this.button){
  163. toolbar.addChild(this.button);
  164. }
  165. // console.debug("adding", this.button, "to:", toolbar);
  166. },
  167. set: function(/* attribute */ name, /* anything */ value){
  168. // summary:
  169. // Set a property on a plugin
  170. // name:
  171. // The property to set.
  172. // value:
  173. // The value to set in the property.
  174. // description:
  175. // Sets named properties on a plugin which may potentially be handled by a
  176. // setter in the plugin.
  177. // For example, if the plugin has a properties "foo"
  178. // and "bar" and a method named "_setFooAttr", calling:
  179. // | plugin.set("foo", "Howdy!");
  180. // would be equivalent to writing:
  181. // | plugin._setFooAttr("Howdy!");
  182. // and:
  183. // | plugin.set("bar", 3);
  184. // would be equivalent to writing:
  185. // | plugin.bar = 3;
  186. //
  187. // set() may also be called with a hash of name/value pairs, ex:
  188. // | plugin.set({
  189. // | foo: "Howdy",
  190. // | bar: 3
  191. // | })
  192. // This is equivalent to calling set(foo, "Howdy") and set(bar, 3)
  193. if(typeof name === "object"){
  194. for(var x in name){
  195. this.set(x, name[x]);
  196. }
  197. return this;
  198. }
  199. var names = this._getAttrNames(name);
  200. if(this[names.s]){
  201. // use the explicit setter
  202. var result = this[names.s].apply(this, Array.prototype.slice.call(arguments, 1));
  203. }else{
  204. this._set(name, value);
  205. }
  206. return result || this;
  207. },
  208. get: function(name){
  209. // summary:
  210. // Get a property from a plugin.
  211. // name:
  212. // The property to get.
  213. // description:
  214. // Get a named property from a plugin. The property may
  215. // potentially be retrieved via a getter method. If no getter is defined, this
  216. // just retrieves the object's property.
  217. // For example, if the plugin has a properties "foo"
  218. // and "bar" and a method named "_getFooAttr", calling:
  219. // | plugin.get("foo");
  220. // would be equivalent to writing:
  221. // | plugin._getFooAttr();
  222. // and:
  223. // | plugin.get("bar");
  224. // would be equivalent to writing:
  225. // | plugin.bar;
  226. var names = this._getAttrNames(name);
  227. return this[names.g] ? this[names.g]() : this[name];
  228. },
  229. _setDisabledAttr: function(disabled){
  230. // summary:
  231. // Function to set the plugin state and call updateState to make sure the
  232. // button is updated appropriately.
  233. this.disabled = disabled;
  234. this.updateState();
  235. },
  236. _getAttrNames: function(name){
  237. // summary:
  238. // Helper function for get() and set().
  239. // Caches attribute name values so we don't do the string ops every time.
  240. // tags:
  241. // private
  242. var apn = this._attrPairNames;
  243. if(apn[name]){ return apn[name]; }
  244. var uc = name.charAt(0).toUpperCase() + name.substr(1);
  245. return (apn[name] = {
  246. s: "_set"+uc+"Attr",
  247. g: "_get"+uc+"Attr"
  248. });
  249. },
  250. _set: function(/*String*/ name, /*anything*/ value){
  251. // summary:
  252. // Helper function to set new value for specified attribute
  253. this[name] = value;
  254. }
  255. });
  256. // Hash mapping plugin name to factory, used for registering plugins
  257. _Plugin.registry = {};
  258. return _Plugin;
  259. });