4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
13 var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each, extend = tinymce.extend;
16 * This class is responsible for managing UI control instances. It's both a factory and a collection for the controls.
17 * @class tinymce.ControlManager
19 tinymce.create('tinymce.ControlManager', {
21 * Constructs a new control manager instance.
22 * Consult the Wiki for more details on this class.
25 * @method ControlManager
26 * @param {tinymce.Editor} ed TinyMCE editor instance to add the control to.
27 * @param {Object} s Optional settings object for the control manager.
29 ControlManager : function(ed, s) {
35 t.onAdd = new tinymce.util.Dispatcher(t);
36 t.onPostRender = new tinymce.util.Dispatcher(t);
37 t.prefix = s.prefix || ed.id + '_';
40 t.onPostRender.add(function() {
41 each(t.controls, function(c) {
48 * Returns a control by id or undefined it it wasn't found.
51 * @param {String} id Control instance name.
52 * @return {tinymce.ui.Control} Control instance or undefined.
55 return this.controls[this.prefix + id] || this.controls[id];
59 * Sets the active state of a control by id.
62 * @param {String} id Control id to set state on.
63 * @param {Boolean} s Active state true/false.
64 * @return {tinymce.ui.Control} Control instance that got activated or null if it wasn't found.
66 setActive : function(id, s) {
76 * Sets the dsiabled state of a control by id.
79 * @param {String} id Control id to set state on.
80 * @param {Boolean} s Active state true/false.
81 * @return {tinymce.ui.Control} Control instance that got disabled or null if it wasn't found.
83 setDisabled : function(id, s) {
93 * Adds a control to the control collection inside the manager.
96 * @param {tinymce.ui.Control} Control instance to add to collection.
97 * @return {tinymce.ui.Control} Control instance that got passed in.
103 t.controls[c.id] = c;
104 t.onAdd.dispatch(c, t);
111 * Creates a control by name, when a control is created it will automatically add it to the control collection.
112 * It first ask all plugins for the specified control if the plugins didn't return a control then the default behavior
115 * @method createControl
116 * @param {String} n Control name to create for example "separator".
117 * @return {tinymce.ui.Control} Control instance that got created and added.
119 createControl : function(n) {
120 var c, t = this, ed = t.editor;
122 each(ed.plugins, function(p) {
123 if (p.createControl) {
124 c = p.createControl(n, t);
134 return t.createSeparator();
137 if (!c && ed.buttons && (c = ed.buttons[n]))
138 return t.createButton(n, c);
144 * Creates a drop menu control instance by id.
146 * @method createDropMenu
147 * @param {String} id Unique id for the new dropdown instance. For example "some menu".
148 * @param {Object} s Optional settings object for the control.
149 * @param {Object} cc Optional control class to use instead of the default one.
150 * @return {tinymce.ui.Control} Control instance that got created and added.
152 createDropMenu : function(id, s, cc) {
153 var t = this, ed = t.editor, c, bm, v, cls;
156 'class' : 'mceDropDown',
157 constrain : ed.settings.constrain_menus
160 s['class'] = s['class'] + ' ' + ed.getParam('skin') + 'Skin';
161 if (v = ed.getParam('skin_variant'))
162 s['class'] += ' ' + ed.getParam('skin') + 'Skin' + v.substring(0, 1).toUpperCase() + v.substring(1);
165 cls = cc || t._cls.dropmenu || tinymce.ui.DropMenu;
166 c = t.controls[id] = new cls(id, s);
167 c.onAddItem.add(function(c, o) {
170 s.title = ed.getLang(s.title, s.title);
173 s.onclick = function(v) {
175 ed.execCommand(s.cmd, s.ui || false, s.value);
180 ed.onRemove.add(function() {
184 // Fix for bug #1897785, #1898007
186 c.onShowMenu.add(function() {
187 // IE 8 needs focus in order to store away a range with the current collapsed caret location
190 bm = ed.selection.getBookmark(1);
193 c.onHideMenu.add(function() {
195 ed.selection.moveToBookmark(bm);
205 * Creates a list box control instance by id. A list box is either a native select element or a DOM/JS based list box control. This
206 * depends on the use_native_selects settings state.
208 * @method createListBox
209 * @param {String} id Unique id for the new listbox instance. For example "styles".
210 * @param {Object} s Optional settings object for the control.
211 * @param {Object} cc Optional control class to use instead of the default one.
212 * @return {tinymce.ui.Control} Control instance that got created and added.
214 createListBox : function(id, s, cc) {
215 var t = this, ed = t.editor, cmd, c, cls;
220 s.title = ed.translate(s.title);
221 s.scope = s.scope || ed;
224 s.onselect = function(v) {
225 ed.execCommand(s.cmd, s.ui || false, v || s.value);
231 'class' : 'mce_' + id,
238 if (ed.settings.use_native_selects)
239 c = new tinymce.ui.NativeListBox(id, s);
241 cls = cc || t._cls.listbox || tinymce.ui.ListBox;
242 c = new cls(id, s, ed);
247 // Fix focus problem in Safari
248 if (tinymce.isWebKit) {
249 c.onPostRender.add(function(c, n) {
250 // Store bookmark on mousedown
251 Event.add(n, 'mousedown', function() {
252 ed.bookmark = ed.selection.getBookmark(1);
255 // Restore on focus, since it might be lost
256 Event.add(n, 'focus', function() {
257 ed.selection.moveToBookmark(ed.bookmark);
264 ed.onMouseDown.add(c.hideMenu, c);
270 * Creates a button control instance by id.
272 * @method createButton
273 * @param {String} id Unique id for the new button instance. For example "bold".
274 * @param {Object} s Optional settings object for the control.
275 * @param {Object} cc Optional control class to use instead of the default one.
276 * @return {tinymce.ui.Control} Control instance that got created and added.
278 createButton : function(id, s, cc) {
279 var t = this, ed = t.editor, o, c, cls;
284 s.title = ed.translate(s.title);
285 s.label = ed.translate(s.label);
286 s.scope = s.scope || ed;
288 if (!s.onclick && !s.menu_button) {
289 s.onclick = function() {
290 ed.execCommand(s.cmd, s.ui || false, s.value);
296 'class' : 'mce_' + id,
297 unavailable_prefix : ed.getLang('unavailable', ''),
305 cls = cc || t._cls.menubutton || tinymce.ui.MenuButton;
306 c = new cls(id, s, ed);
307 ed.onMouseDown.add(c.hideMenu, c);
309 cls = t._cls.button || tinymce.ui.Button;
310 c = new cls(id, s, ed);
317 * Creates a menu button control instance by id.
319 * @method createMenuButton
320 * @param {String} id Unique id for the new menu button instance. For example "menu1".
321 * @param {Object} s Optional settings object for the control.
322 * @param {Object} cc Optional control class to use instead of the default one.
323 * @return {tinymce.ui.Control} Control instance that got created and added.
325 createMenuButton : function(id, s, cc) {
329 return this.createButton(id, s, cc);
333 * Creates a split button control instance by id.
335 * @method createSplitButton
336 * @param {String} id Unique id for the new split button instance. For example "spellchecker".
337 * @param {Object} s Optional settings object for the control.
338 * @param {Object} cc Optional control class to use instead of the default one.
339 * @return {tinymce.ui.Control} Control instance that got created and added.
341 createSplitButton : function(id, s, cc) {
342 var t = this, ed = t.editor, cmd, c, cls;
347 s.title = ed.translate(s.title);
348 s.scope = s.scope || ed;
351 s.onclick = function(v) {
352 ed.execCommand(s.cmd, s.ui || false, v || s.value);
357 s.onselect = function(v) {
358 ed.execCommand(s.cmd, s.ui || false, v || s.value);
364 'class' : 'mce_' + id,
370 cls = cc || t._cls.splitbutton || tinymce.ui.SplitButton;
371 c = t.add(new cls(id, s, ed));
372 ed.onMouseDown.add(c.hideMenu, c);
378 * Creates a color split button control instance by id.
380 * @method createColorSplitButton
381 * @param {String} id Unique id for the new color split button instance. For example "forecolor".
382 * @param {Object} s Optional settings object for the control.
383 * @param {Object} cc Optional control class to use instead of the default one.
384 * @return {tinymce.ui.Control} Control instance that got created and added.
386 createColorSplitButton : function(id, s, cc) {
387 var t = this, ed = t.editor, cmd, c, cls, bm;
392 s.title = ed.translate(s.title);
393 s.scope = s.scope || ed;
396 s.onclick = function(v) {
398 bm = ed.selection.getBookmark(1);
400 ed.execCommand(s.cmd, s.ui || false, v || s.value);
405 s.onselect = function(v) {
406 ed.execCommand(s.cmd, s.ui || false, v || s.value);
412 'class' : 'mce_' + id,
413 'menu_class' : ed.getParam('skin') + 'Skin',
415 more_colors_title : ed.getLang('more_colors')
419 cls = cc || t._cls.colorsplitbutton || tinymce.ui.ColorSplitButton;
420 c = new cls(id, s, ed);
421 ed.onMouseDown.add(c.hideMenu, c);
423 // Remove the menu element when the editor is removed
424 ed.onRemove.add(function() {
428 // Fix for bug #1897785, #1898007
430 c.onShowMenu.add(function() {
431 // IE 8 needs focus in order to store away a range with the current collapsed caret location
433 bm = ed.selection.getBookmark(1);
436 c.onHideMenu.add(function() {
438 ed.selection.moveToBookmark(bm);
448 * Creates a toolbar container control instance by id.
450 * @method createToolbar
451 * @param {String} id Unique id for the new toolbar container control instance. For example "toolbar1".
452 * @param {Object} s Optional settings object for the control.
453 * @param {Object} cc Optional control class to use instead of the default one.
454 * @return {tinymce.ui.Control} Control instance that got created and added.
456 createToolbar : function(id, s, cc) {
457 var c, t = this, cls;
460 cls = cc || t._cls.toolbar || tinymce.ui.Toolbar;
461 c = new cls(id, s, t.editor);
469 createToolbarGroup : function(id, s, cc) {
470 var c, t = this, cls;
472 cls = cc || this._cls.toolbarGroup || tinymce.ui.ToolbarGroup;
473 c = new cls(id, s, t.editor);
482 * Creates a separator control instance.
484 * @method createSeparator
485 * @param {Object} cc Optional control class to use instead of the default one.
486 * @return {tinymce.ui.Control} Control instance that got created and added.
488 createSeparator : function(cc) {
489 var cls = cc || this._cls.separator || tinymce.ui.Separator;
495 * Overrides a specific control type with a custom class.
497 * @method setControlType
498 * @param {string} n Name of the control to override for example button or dropmenu.
499 * @param {function} c Class reference to use instead of the default one.
500 * @return {function} Same as the class reference.
502 setControlType : function(n, c) {
503 return this._cls[n.toLowerCase()] = c;
511 destroy : function() {
512 each(this.controls, function(c) {
516 this.controls = null;