]>
git.wh0rd.org - tt-rss.git/blob - lib/dijit/_editor/_Plugin.js.uncompressed.js
1 define("dijit/_editor/_Plugin", [
2 "dojo/_base/connect", // connect.connect
3 "dojo/_base/declare", // declare
4 "dojo/_base/lang", // lang.mixin, lang.hitch
6 ], function(connect
, declare
, lang
, Button
){
9 // dijit/_editor/_Plugin
11 // Base class for a "plugin" to the editor, which is usually
12 // a single button on the Toolbar and some associated code
15 var _Plugin
= declare("dijit._editor._Plugin", null, {
17 // Base class for a "plugin" to the editor, which is usually
18 // a single button on the Toolbar and some associated code
20 constructor: function(args
){
24 // Initial settings for any of the attributes.
26 this.params
= args
|| {};
27 lang
.mixin(this, this.params
);
29 this._attrPairNames
= {};
32 // editor: [const] dijit.Editor
33 // Points to the parent editor
36 // iconClassPrefix: [const] String
37 // The CSS class name for the button node is formed from `iconClassPrefix` and `command`
38 iconClassPrefix
: "dijitEditorIcon",
40 // button: dijit/_WidgetBase?
41 // Pointer to `dijit/form/Button` or other widget (ex: `dijit/form/FilteringSelect`)
42 // that is added to the toolbar to control this plugin.
43 // If not specified, will be created on initialization according to `buttonClass`
47 // String like "insertUnorderedList", "outdent", "justifyCenter", etc. that represents an editor command.
48 // Passed to editor.execCommand() if `useDefaultCommand` is true.
51 // useDefaultCommand: Boolean
52 // If true, this plugin executes by calling Editor.execCommand() with the argument specified in `command`.
53 useDefaultCommand
: true,
55 // buttonClass: Widget Class
56 // Class of widget (ex: dijit.form.Button or dijit/form/FilteringSelect)
57 // that is added to the toolbar to control this plugin.
58 // This is used to instantiate the button, unless `button` itself is specified directly.
62 // Flag to indicate if this plugin has been disabled and should do nothing
63 // helps control button state, among other things. Set via the setter api.
66 getLabel: function(/*String*/key
){
68 // Returns the label to use for the button
71 return this.editor
.commands
[key
]; // String
74 _initButton: function(){
76 // Initialize the button or other widget that will control this plugin.
77 // This code only works for plugins controlling built-in commands in the editor.
79 // protected extension
80 if(this.command
.length
){
81 var label
= this.getLabel(this.command
),
83 className
= this.iconClassPrefix
+" "+this.iconClassPrefix
+ this.command
.charAt(0).toUpperCase() + this.command
.substr(1);
85 var props
= lang
.mixin({
87 ownerDocument
: editor
.ownerDocument
,
92 dropDown
: this.dropDown
,
94 }, this.params
|| {});
95 this.button
= new this.buttonClass(props
);
98 if(this.get("disabled") && this.button
){
99 this.button
.set("disabled", this.get("disabled"));
105 // Destroy this plugin
108 while(h
= this._connects
.pop()){ h
.remove(); }
110 this.dropDown
.destroyRecursive();
114 connect: function(o
, f
, tf
){
116 // Make a connect.connect() that is automatically disconnected when this plugin is destroyed.
117 // Similar to `dijit/_Widget.connect()`.
120 this._connects
.push(connect
.connect(o
, f
, this, tf
));
123 updateState: function(){
125 // Change state of the plugin to respond to events in the editor.
127 // This is called on meaningful events in the editor, such as change of selection
128 // or caret position (but not simple typing of alphanumeric keys). It gives the
129 // plugin a chance to update the CSS of its button.
131 // For example, the "bold" plugin will highlight/unhighlight the bold button depending on whether the
132 // characters next to the caret are bold or not.
134 // Only makes sense when `useDefaultCommand` is true, as it calls Editor.queryCommandEnabled(`command`).
138 if(!e
|| !e
.isLoaded
|| !c
.length
){ return; }
139 var disabled
= this.get("disabled");
142 enabled
= !disabled
&& e
.queryCommandEnabled(c
);
143 if(this.enabled
!== enabled
){
144 this.enabled
= enabled
;
145 this.button
.set('disabled', !enabled
);
148 if(typeof this.button
.checked
== 'boolean'){
149 checked
= e
.queryCommandState(c
);
150 if(this.checked
!== checked
){
151 this.checked
= checked
;
152 this.button
.set('checked', e
.queryCommandState(c
));
157 console
.log(e
); // FIXME: we shouldn't have debug statements in our code. Log as an error?
162 setEditor: function(/*dijit/Editor*/ editor){
164 // Tell the plugin which Editor it is associated with.
166 // TODO: refactor code to just pass editor to constructor.
168 // FIXME: detach from previous editor!!
169 this.editor = editor;
171 // FIXME: prevent creating this if we don't need to (i.e., editor can't handle our command)
174 // Processing for buttons that execute by calling editor.execCommand()
175 if(this.button && this.useDefaultCommand){
176 if(this.editor.queryCommandAvailable(this.command)){
177 this.connect(this.button, "onClick",
178 lang.hitch(this.editor, "execCommand", this.command, this.commandArg)
181 // hide button because editor doesn't support command (due to browser limitations)
182 this.button.domNode.style.display = "none";
186 this.connect(this.editor, "onNormalizedDisplayChanged", "updateState");
189 setToolbar: function(/*dijit/Toolbar*/ toolbar){
191 // Tell the plugin to add it's controller widget (often a button)
192 // to the toolbar. Does nothing if there is no controller widget.
194 // TODO: refactor code to just pass toolbar to constructor.
197 toolbar.addChild(this.button);
199 // console.debug("adding", this.button, "to:", toolbar);
202 set: function(/* attribute */ name
, /* anything */ value
){
204 // Set a property on a plugin
206 // The property to set.
208 // The value to set in the property.
210 // Sets named properties on a plugin which may potentially be handled by a
211 // setter in the plugin.
212 // For example, if the plugin has a properties "foo"
213 // and "bar" and a method named "_setFooAttr", calling:
214 // | plugin.set("foo", "Howdy!");
215 // would be equivalent to writing:
216 // | plugin._setFooAttr("Howdy!");
218 // | plugin.set("bar", 3);
219 // would be equivalent to writing:
222 // set() may also be called with a hash of name/value pairs, ex:
227 // This is equivalent to calling set(foo, "Howdy") and set(bar, 3)
228 if(typeof name
=== "object"){
230 this.set(x
, name
[x
]);
234 var names
= this._getAttrNames(name
);
236 // use the explicit setter
237 var result
= this[names
.s
].apply(this, Array
.prototype.slice
.call(arguments
, 1));
239 this._set(name
, value
);
241 return result
|| this;
246 // Get a property from a plugin.
248 // The property to get.
250 // Get a named property from a plugin. The property may
251 // potentially be retrieved via a getter method. If no getter is defined, this
252 // just retrieves the object's property.
253 // For example, if the plugin has a properties "foo"
254 // and "bar" and a method named "_getFooAttr", calling:
255 // | plugin.get("foo");
256 // would be equivalent to writing:
257 // | plugin._getFooAttr();
259 // | plugin.get("bar");
260 // would be equivalent to writing:
262 var names
= this._getAttrNames(name
);
263 return this[names
.g
] ? this[names
.g
]() : this[name
];
266 _setDisabledAttr: function(disabled
){
268 // Function to set the plugin state and call updateState to make sure the
269 // button is updated appropriately.
270 this.disabled
= disabled
;
274 _getAttrNames: function(name
){
276 // Helper function for get() and set().
277 // Caches attribute name values so we don't do the string ops every time.
281 var apn
= this._attrPairNames
;
282 if(apn
[name
]){ return apn
[name
]; }
283 var uc
= name
.charAt(0).toUpperCase() + name
.substr(1);
284 return (apn
[name
] = {
290 _set: function(/*String*/ name
, /*anything*/ value
){
292 // Helper function to set new value for specified attribute
297 // Hash mapping plugin name to factory, used for registering plugins
298 _Plugin
.registry
= {};