1 if(!dojo._hasResource["dijit._editor.plugins.FontChoice"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit._editor.plugins.FontChoice"] = true;
3 dojo.provide("dijit._editor.plugins.FontChoice");
5 dojo.require("dijit._editor._Plugin");
6 dojo.require("dijit.form.FilteringSelect");
7 dojo.require("dojo.data.ItemFileReadStore");
8 dojo.require("dojo.i18n");
10 dojo.requireLocalization("dijit._editor", "FontChoice", null, "zh,pt,da,tr,ru,de,sv,ja,he,ROOT,fi,nb,el,ar,pt-pt,cs,fr,es,ko,nl,zh-tw,pl,it,hu");
12 dojo.declare("dijit._editor.plugins.FontChoice",
13 dijit._editor._Plugin,
16 // This plugin provides three dropdowns for setting font information in the editor
19 // The commands provided by this plugin are:
22 // | Provides a dropdown to select from a list of generic font names
24 // | Provides a dropdown to select from a list of pre-defined font sizes
26 // | Provides a dropdown to select from a list of styles
29 // which can easily be added to an editor by including one or more of the above commands
30 // in the `plugins` attribute as follows:
32 // | plugins="['fontName','fontSize',...]"
34 // It is possible to override the default dropdown list by providing an Array for the `custom` property when
35 // instantiating this plugin, e.g.
37 // | plugins="[{name:'dijit._editor.plugins.FontChoice', command:'fontName', custom:['Verdana','Myriad','Garamond']},...]"
39 // Alternatively, for `fontName` only, `generic:true` may be specified to provide a dropdown with
40 // [CSS generic font families](http://www.w3.org/TR/REC-CSS2/fonts.html#generic-font-families)
42 // Note that the editor is often unable to properly handle font styling information defined outside
43 // the context of the current editor instance, such as pre-populated HTML.
47 buttonClass: dijit.form.FilteringSelect,
49 _initButton: function(){
50 //TODO: would be nice to be able to handle comma-separated font lists and search within them
51 var cmd = this.command;
52 var names = this.custom ||
54 fontName: this.generic ? ["serif", "sans-serif", "monospace", "cursive", "fantasy"] : // CSS font-family generics
55 ["Arial", "Times New Roman", "Comic Sans MS", "Courier New"],
56 fontSize: [1,2,3,4,5,6,7], // sizes according to the old HTML FONT SIZE
57 formatBlock: ["p", "h1", "h2", "h3", "pre"]
59 var strings = dojo.i18n.getLocalization("dijit._editor", "FontChoice");
60 var items = dojo.map(names, function(value){
61 var name = strings[value] || value;
65 label = "<div style='font-family: "+value+"'>" + name + "</div>";
68 // we're stuck using the deprecated FONT tag to correspond with the size measurements used by the editor
69 label = "<font size="+value+"'>"+name+"</font>";
72 label = "<" + value + ">" + name + "</" + value + ">";
74 return { label: label, name: name, value: value };
76 items.push({label: "", name:"", value:""}); // FilteringSelect doesn't like unmatched blank strings
78 dijit._editor.plugins.FontChoice.superclass._initButton.apply(this,
79 [{ labelType: "html", labelAttr: "label", searchAttr: "name", store: new dojo.data.ItemFileReadStore(
80 { data: { identifier: "value", items: items } })}]);
82 this.button.setValue("");
84 this.connect(this.button, "onChange", function(choice){
85 if(this.updating){ return; }
86 // FIXME: IE is really messed up here!!
87 if(dojo.isIE && "_savedSelection" in this){
88 var b = this._savedSelection;
89 delete this._savedSelection;
91 this.editor._moveToBookmark(b);
93 // this.editor.focus();
94 dijit.focus(this._focusHandle);
96 if(this.command == "fontName" && choice.indexOf(" ") != -1){ choice = "'" + choice + "'"; }
97 this.editor.execCommand(this.editor._normalizeCommand(this.command), choice);
101 updateState: function(){
102 this.inherited(arguments);
103 var _e = this.editor;
104 var _c = this.command;
105 if(!_e || !_e.isLoaded || !_c.length){ return; }
107 var value = _e.queryCommandValue(this.editor._normalizeCommand(_c)) || "";
108 // strip off single quotes, if any
109 var quoted = dojo.isString(value) && value.match(/'([^']*)'/);
110 if(quoted){ value = quoted[1]; }
111 //console.log("selected " + value);
112 if(this.generic && _c == "fontName"){
114 "Arial": "sans-serif",
115 "Helvetica": "sans-serif",
116 "Myriad": "sans-serif",
118 "Times New Roman": "serif",
119 "Comic Sans MS": "cursive",
120 "Apple Chancery": "cursive",
121 "Courier": "monospace",
122 "Courier New": "monospace",
124 // ,"????": "fantasy" TODO: IE doesn't map fantasy font-family?
126 //console.log("mapped to " + map[value]);
127 value = map[value] || value;
128 }else if(_c == "fontSize" && value.indexOf && value.indexOf("px") != -1){
129 var pixels = parseInt(value);
130 value = {10:1, 13:2, 16:3, 18:4, 24:5, 32:6, 48:7}[pixels] || value;
132 this.updating = true;
133 this.button.setValue(value);
134 delete this.updating;
137 // FIXME: IE is *really* b0rken
139 this._savedSelection = this.editor._getBookmark();
141 this._focusHandle = dijit.getFocus(this.editor.iframe);
144 setToolbar: function(){
145 this.inherited(arguments);
147 var forRef = this.button;
148 if(!forRef.id){ forRef.id = dijit._scopeName+"EditorButton-"+this.command+(this._uniqueId++); } //TODO: is this necessary? FilteringSelects always seem to have an id?
149 var label = dojo.doc.createElement("label");
150 dojo.addClass(label, "dijit dijitReset dijitLeft dijitInline");
151 label.setAttribute("for", forRef.id);
152 var strings = dojo.i18n.getLocalization("dijit._editor", "FontChoice");
153 label.appendChild(dojo.doc.createTextNode(strings[this.command]));
154 dojo.place(label, this.button.domNode, "before");
159 dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
160 if(o.plugin){ return; }
162 case "fontName": case "fontSize": case "formatBlock":
163 o.plugin = new dijit._editor.plugins.FontChoice({command: o.args.name});