Debian Codegraph @

dijit/_editor/plugins/TextColor.js

raw symbols SCIP
1 define([
2 "require",
3 "dojo/colors", // colors.fromRgb
4 "dojo/_base/declare", // declare
5 "dojo/_base/lang",
6 "../_Plugin",
7 "../../form/DropDownButton"
8 ], function(require, colors, declare, lang, _Plugin, DropDownButton){
9
10 // module:
11 // dijit/_editor/plugins/TextColor
12
13 var TextColor = declare("dijit._editor.plugins.TextColor", _Plugin, {
14 // summary:
15 // This plugin provides dropdown color pickers for setting text color and background color
16 // description:
17 // The commands provided by this plugin are:
18 //
19 // - foreColor - sets the text color
20 // - hiliteColor - sets the background color
21
22 // Override _Plugin.buttonClass to use DropDownButton (with ColorPalette) to control this plugin
23 buttonClass: DropDownButton,
24
25 // colorPicker: String|Constructor
26 // The color picker dijit to use, defaults to dijit/ColorPalette
27 colorPicker: "dijit/ColorPalette",
28
29 // useDefaultCommand: Boolean
30 // False as we do not use the default editor command/click behavior.
31 useDefaultCommand: false,
32
33 _initButton: function(){
34 this.command = this.name;
35
36 this.inherited(arguments);
37
38 // Setup to lazy load ColorPalette first time the button is clicked
39 var self = this;
40 this.button.loadDropDown = function(callback){
41 function onColorPaletteLoad(ColorPalette){
42 self.button.dropDown = new ColorPalette({
43 dir: self.editor.dir,
44 ownerDocument: self.editor.ownerDocument,
45 value: self.value,
46 onChange: function(color){
47 self.editor.execCommand(self.command, color);
48 },
49 onExecute: function(){
50 self.editor.execCommand(self.command, this.get("value"));
51 }
52 });
53 callback();
54 }
55 if(typeof self.colorPicker == "string"){
56 require([self.colorPicker], onColorPaletteLoad);
57 }else{
58 onColorPaletteLoad(self.colorPicker);
59 }
60 };
61 },
62
63 updateState: function(){
64 // summary:
65 // Overrides _Plugin.updateState(). This updates the ColorPalette
66 // to show the color of the currently selected text.
67 // tags:
68 // protected
69
70 var _e = this.editor;
71 var _c = this.command;
72 if(!_e || !_e.isLoaded || !_c.length){
73 return;
74 }
75
76 if(this.button){
77 var disabled = this.get("disabled");
78 this.button.set("disabled", disabled);
79 if(disabled){
80 return;
81 }
82
83 var value;
84 try{
85 value = _e.queryCommandValue(_c) || "";
86 }catch(e){
87 //Firefox may throw error above if the editor is just loaded, ignore it
88 value = "";
89 }
90 }
91
92 if(value == ""){
93 value = "#000000";
94 }
95 if(value == "transparent"){
96 value = "#ffffff";
97 }
98
99 if(typeof value == "string"){
100 //if RGB value, convert to hex value
101 if(value.indexOf("rgb") > -1){
102 value = colors.fromRgb(value).toHex();
103 }
104 }else{ //it's an integer(IE returns an MS access #)
105 value = ((value & 0x0000ff) << 16) | (value & 0x00ff00) | ((value & 0xff0000) >>> 16);
106 value = value.toString(16);
107 value = "#000000".slice(0, 7 - value.length) + value;
108
109 }
110
111 this.value = value;
112
113 var dropDown = this.button.dropDown;
114 if(dropDown && dropDown.get && value !== dropDown.get('value')){
115 dropDown.set('value', value, false);
116 }
117 }
118 });
119
120 // Register this plugin.
121 _Plugin.registry["foreColor"] = function(args){
122 return new TextColor(args);
123 };
124 _Plugin.registry["hiliteColor"] = function(args){
125 return new TextColor(args);
126 };
127
128 return TextColor;
129 });