Debian Codegraph @

dojox/editor/plugins/TextColor.js

raw symbols SCIP
1 define([
2 "dojo",
3 "dijit",
4 "dojox",
5 "dijit/_base/popup",
6 "dijit/_Widget",
7 "dijit/_TemplatedMixin",
8 "dijit/_WidgetsInTemplateMixin",
9 "dijit/_editor/_Plugin",
10 "dijit/TooltipDialog",
11 "dijit/form/Button",
12 "dijit/form/DropDownButton",
13 "dojox/widget/ColorPicker",
14 "dojo/_base/connect",
15 "dojo/_base/declare",
16 "dojo/i18n",
17 "dojo/i18n!dojox/editor/plugins/nls/TextColor"
18 ], function(dojo, dijit, dojox, popup, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin, _Plugin) {
19
20 dojo.experimental("dojox.editor.plugins.TextColor");
21
22 var TextColorDropDown = dojo.declare("dojox.editor.plugins._TextColorDropDown", [_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], {
23 // summary:
24 // A sample widget that uses/creates a dropdown with a dojox.widget.ColorPicker. Also provides
25 // passthroughs to the value of the color picker and convenient hook points.
26 // tags:
27 // private
28
29 // templateString: String
30 // The template used to create the ColorPicker.
31 templateString: "<div style='display: none; position: absolute; top: -10000; z-index: -10000'>" +
32 "<div dojoType='dijit.TooltipDialog' dojoAttachPoint='dialog' class='dojoxEditorColorPicker'>" +
33 "<div dojoType='dojox.widget.ColorPicker' dojoAttachPoint='_colorPicker'></div>" +
34 "<br>" +
35 "<center>" +
36 "<button dojoType='dijit.form.Button' type='button' dojoAttachPoint='_setButton'>${setButtonText}</button>" +
37 "&nbsp;" +
38 "<button dojoType='dijit.form.Button' type='button' dojoAttachPoint='_cancelButton'>${cancelButtonText}</button>" +
39 "</center>" +
40 "</div>" +
41 "</div>",
42
43 // widgetsInTemplate: Boolean
44 // Flag denoting widgets are contained in the template.
45 widgetsInTemplate: true,
46
47 constructor: function(){
48 // summary:
49 // Constructor over-ride so that the translated strings are mixsed in so
50 // the template fills out.
51 var strings = dojo.i18n.getLocalization("dojox.editor.plugins", "TextColor");
52 dojo.mixin(this, strings);
53 },
54
55 startup: function(){
56 // summary:
57 // Over-ride of startup to do the basic connect setups and such.
58 if(!this._started){
59 this.inherited(arguments);
60 this.connect(this._setButton, "onClick", dojo.hitch(this, function(){
61 this.onChange(this.get("value"));
62 }));
63 this.connect(this._cancelButton, "onClick", dojo.hitch(this, function(){
64 dijit.popup.close(this.dialog);
65 this.onCancel();
66 }));
67 // Fully statred, so go ahead and remove the hide.
68 dojo.style(this.domNode, "display", "block");
69 }
70 },
71
72 _setValueAttr: function(value, priorityChange){
73 // summary:
74 // Passthrough function for the color picker value.
75 // value: String
76 // The value to set in the color picker
77 // priorityChange:
78 // Value to indicate whether or not to trigger an onChange event.
79 this._colorPicker.set("value", value, priorityChange);
80 },
81
82 _getValueAttr: function(){
83 // summary:
84 // Passthrough function for the color picker value.
85 return this._colorPicker.get("value");
86 },
87
88 onChange: function(value){
89 // summary:
90 // Hook point to get the value when the color picker value is selected.
91 // value: String
92 // The value from the color picker.
93 },
94
95 onCancel: function(){
96 // summary:
97 // Hook point to get when the dialog is canceled.
98 }
99 });
100
101
102 var TextColor = dojo.declare("dojox.editor.plugins.TextColor", _Plugin, {
103 // summary:
104 // This plugin provides dropdown color pickers for setting text color and background color
105 // and makes use of the nicer-looking (though not entirely accessible), dojox.widget.ColorPicker.
106 //
107 // description:
108 // The commands provided by this plugin are:
109 //
110 // - foreColor - sets the text color
111 // - hiliteColor - sets the background color
112
113 // Override _Plugin.buttonClass to use DropDownButton (with ColorPalette) to control this plugin
114 buttonClass: dijit.form.DropDownButton,
115
116 // False as we do not use the default editor command/click behavior.
117 useDefaultCommand: false,
118
119 constructor: function(){
120 this._picker = new TextColorDropDown();
121 dojo.body().appendChild(this._picker.domNode);
122 this._picker.startup();
123 this.dropDown = this._picker.dialog;
124 this.connect(this._picker, "onChange", function(color){
125 this.editor.execCommand(this.command, color);
126 });
127 this.connect(this._picker, "onCancel", function(){
128 this.editor.focus();
129 });
130 },
131
132 updateState: function(){
133 // summary:
134 // Overrides _Plugin.updateState(). This updates the ColorPalette
135 // to show the color of the currently selected text.
136 // tags:
137 // protected
138 var _e = this.editor;
139 var _c = this.command;
140 if(!_e || !_e.isLoaded || !_c.length){
141 return;
142 }
143
144 var disabled = this.get("disabled");
145
146 var value;
147 if(this.button){
148 this.button.set("disabled", disabled);
149 if(disabled){
150 return;
151 }
152 try{
153 value = _e.queryCommandValue(_c)|| "";
154 }catch(e){
155 //Firefox may throw error above if the editor is just loaded, ignore it
156 value = "";
157 }
158 }
159
160 if(value == ""){
161 value = "#000000";
162 }
163 if(value == "transparent"){
164 value = "#ffffff";
165 }
166
167 if(typeof value == "string"){
168 //if RGB value, convert to hex value
169 if(value.indexOf("rgb")> -1){
170 value = dojo.colorFromRgb(value).toHex();
171 }
172 }else{ //it's an integer(IE returns an MS access #)
173 value =((value & 0x0000ff)<< 16)|(value & 0x00ff00)|((value & 0xff0000)>>> 16);
174 value = value.toString(16);
175 value = "#000000".slice(0, 7 - value.length)+ value;
176
177 }
178
179 if(value !== this._picker.get('value')){
180 this._picker.set('value', value, false);
181 }
182 },
183
184 destroy: function(){
185 // summary:
186 // Over-ride cleanup function.
187 this.inherited(arguments);
188 this._picker.destroyRecursive();
189 delete this._picker;
190 }
191 });
192
193 // For monkey-patching
194 TextColor._TextColorDropDown = TextColorDropDown;
195
196 // Register this plugin. Uses the same name as the dijit one, so you
197 // use one or the other, not both.
198 dojo.subscribe(dijit._scopeName + ".Editor.getPlugin", null, function(o){
199 if(o.plugin){
200 return;
201 }
202 switch(o.args.name){
203 case "foreColor":
204 case "hiliteColor":
205 o.plugin = new TextColor({
206 command: o.args.name
207 });
208 }
209 });
210
211 return TextColor;
212
213 });