]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dijit/_editor/plugins/FontChoice.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dijit / _editor / plugins / FontChoice.js
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");
4
5 dojo.require("dijit._editor._Plugin");
6 dojo.require("dijit.form.FilteringSelect");
7 dojo.require("dojo.data.ItemFileReadStore");
8 dojo.require("dojo.i18n");
9
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");
11
12 dojo.declare("dijit._editor.plugins.FontChoice",
13         dijit._editor._Plugin,
14         {
15                 //      summary:
16                 //              This plugin provides three dropdowns for setting font information in the editor
17                 //
18                 //      description:
19                 //              The commands provided by this plugin are:
20                 //
21                 //              * fontName
22                 //      |               Provides a dropdown to select from a list of generic font names
23                 //              * fontSize
24                 //      |               Provides a dropdown to select from a list of pre-defined font sizes
25                 //              * formatBlock
26                 //      |               Provides a dropdown to select from a list of styles
27                 //  |
28                 //
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:
31                 //
32                 //      |       plugins="['fontName','fontSize',...]"
33                 //
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.
36                 //
37                 //      |       plugins="[{name:'dijit._editor.plugins.FontChoice', command:'fontName', custom:['Verdana','Myriad','Garamond']},...]"
38                 //
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)
41                 //
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.
44
45                 _uniqueId: 0,
46
47                 buttonClass: dijit.form.FilteringSelect,
48
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 ||
53                         {
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"]
58                         }[cmd];
59                         var strings = dojo.i18n.getLocalization("dijit._editor", "FontChoice");
60                         var items = dojo.map(names, function(value){
61                                 var name = strings[value] || value;
62                                 var label = name;
63                                 switch(cmd){
64                                 case "fontName":
65                                         label = "<div style='font-family: "+value+"'>" + name + "</div>";
66                                         break;
67                                 case "fontSize":
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>";
70                                         break;
71                                 case "formatBlock":
72                                         label = "<" + value + ">" + name + "</" + value + ">";
73                                 }
74                                 return { label: label, name: name, value: value };
75                         });
76                         items.push({label: "", name:"", value:""}); // FilteringSelect doesn't like unmatched blank strings
77
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 } })}]);
81
82                         this.button.setValue("");
83
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;
90                                         this.editor.focus();
91                                         this.editor._moveToBookmark(b);
92                                 }else{
93 //                                      this.editor.focus();
94                                         dijit.focus(this._focusHandle);
95                                 }
96                                 if(this.command == "fontName" && choice.indexOf(" ") != -1){ choice = "'" + choice + "'"; }
97                                 this.editor.execCommand(this.editor._normalizeCommand(this.command), choice);
98                         });
99                 },
100
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; }
106                         if(this.button){
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"){
113                                         var map = {
114                                                 "Arial": "sans-serif",
115                                                 "Helvetica": "sans-serif",
116                                                 "Myriad": "sans-serif",
117                                                 "Times": "serif",
118                                                 "Times New Roman": "serif",
119                                                 "Comic Sans MS": "cursive",
120                                                 "Apple Chancery": "cursive",
121                                                 "Courier": "monospace",
122                                                 "Courier New": "monospace",
123                                                 "Papyrus": "fantasy"
124 //                                              ,"????": "fantasy" TODO: IE doesn't map fantasy font-family?
125                                         };
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;
131                                 }
132                                 this.updating = true;
133                                 this.button.setValue(value);
134                                 delete this.updating;
135                         }
136
137                         // FIXME: IE is *really* b0rken
138                         if(dojo.isIE){
139                                 this._savedSelection = this.editor._getBookmark();
140                         }
141                         this._focusHandle = dijit.getFocus(this.editor.iframe);
142                 },
143
144                 setToolbar: function(){
145                         this.inherited(arguments);
146
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");
155                 }
156         }
157 );
158
159 dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
160         if(o.plugin){ return; }
161         switch(o.args.name){
162         case "fontName": case "fontSize": case "formatBlock":
163                 o.plugin = new dijit._editor.plugins.FontChoice({command: o.args.name});
164         }
165 });
166
167 }