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