1 /** |
|
2 * ColorButton.js |
|
3 * |
|
4 * Copyright, Moxiecode Systems AB |
|
5 * Released under LGPL License. |
|
6 * |
|
7 * License: http://www.tinymce.com/license |
|
8 * Contributing: http://www.tinymce.com/contributing |
|
9 */ |
|
10 |
|
11 /** |
|
12 * This class creates a color button control. This is a split button in which the main |
|
13 * button has a visual representation of the currently selected color. When clicked |
|
14 * the caret button displays a color picker, allowing the user to select a new color. |
|
15 * |
|
16 * @-x-less ColorButton.less |
|
17 * @class tinymce.ui.ColorButton |
|
18 * @extends tinymce.ui.PanelButton |
|
19 */ |
|
20 define("tinymce/ui/ColorButton", [ |
|
21 "tinymce/ui/PanelButton", |
|
22 "tinymce/dom/DOMUtils" |
|
23 ], function(PanelButton, DomUtils) { |
|
24 "use strict"; |
|
25 |
|
26 var DOM = DomUtils.DOM; |
|
27 |
|
28 return PanelButton.extend({ |
|
29 /** |
|
30 * Constructs a new ColorButton instance with the specified settings. |
|
31 * |
|
32 * @constructor |
|
33 * @param {Object} settings Name/value object with settings. |
|
34 */ |
|
35 init: function(settings) { |
|
36 this._super(settings); |
|
37 this.addClass('colorbutton'); |
|
38 }, |
|
39 |
|
40 /** |
|
41 * Getter/setter for the current color. |
|
42 * |
|
43 * @method color |
|
44 * @param {String} [color] Color to set. |
|
45 * @return {String|tinymce.ui.ColorButton} Current color or current instance. |
|
46 */ |
|
47 color: function(color) { |
|
48 if (color) { |
|
49 this._color = color; |
|
50 this.getEl('preview').style.backgroundColor = color; |
|
51 return this; |
|
52 } |
|
53 |
|
54 return this._color; |
|
55 }, |
|
56 |
|
57 /** |
|
58 * Resets the current color. |
|
59 * |
|
60 * @method resetColor |
|
61 * @return {tinymce.ui.ColorButton} Current instance. |
|
62 */ |
|
63 resetColor: function() { |
|
64 this._color = null; |
|
65 this.getEl('preview').style.backgroundColor = null; |
|
66 return this; |
|
67 }, |
|
68 |
|
69 /** |
|
70 * Renders the control as a HTML string. |
|
71 * |
|
72 * @method renderHtml |
|
73 * @return {String} HTML representing the control. |
|
74 */ |
|
75 renderHtml: function() { |
|
76 var self = this, id = self._id, prefix = self.classPrefix; |
|
77 var icon = self.settings.icon ? prefix + 'ico ' + prefix + 'i-' + self.settings.icon : ''; |
|
78 var image = self.settings.image ? ' style="background-image: url(\'' + self.settings.image + '\')"' : ''; |
|
79 |
|
80 return ( |
|
81 '<div id="' + id + '" class="' + self.classes() + '" role="button" tabindex="-1" aria-haspopup="true">' + |
|
82 '<button role="presentation" hidefocus="1" type="button" tabindex="-1">' + |
|
83 (icon ? '<i class="' + icon + '"' + image + '></i>' : '') + |
|
84 '<span id="' + id + '-preview" class="' + prefix + 'preview"></span>' + |
|
85 (self._text ? (icon ? ' ' : '') + (self._text) : '') + |
|
86 '</button>' + |
|
87 '<button type="button" class="' + prefix + 'open" hidefocus="1" tabindex="-1">' + |
|
88 ' <i class="' + prefix + 'caret"></i>' + |
|
89 '</button>' + |
|
90 '</div>' |
|
91 ); |
|
92 }, |
|
93 |
|
94 /** |
|
95 * Called after the control has been rendered. |
|
96 * |
|
97 * @method postRender |
|
98 */ |
|
99 postRender: function() { |
|
100 var self = this, onClickHandler = self.settings.onclick; |
|
101 |
|
102 self.on('click', function(e) { |
|
103 if (e.aria && e.aria.key == 'down') { |
|
104 return; |
|
105 } |
|
106 |
|
107 if (e.control == self && !DOM.getParent(e.target, '.' + self.classPrefix + 'open')) { |
|
108 e.stopImmediatePropagation(); |
|
109 onClickHandler.call(self, e); |
|
110 } |
|
111 }); |
|
112 |
|
113 delete self.settings.onclick; |
|
114 |
|
115 return self._super(); |
|
116 } |
|
117 }); |
|
118 }); |
|