5a920362 |
1 | /**\r |
2 | * Plone style plugin\r |
3 | *\r |
4 | * @author Four Digits\r |
5 | */\r |
6 | (function() {\r |
7 | tinymce.create('tinymce.plugins.PloneStylePlugin', {\r |
8 | \r |
9 | _previousNode : null,\r |
10 | _styles : null,\r |
11 | _control : null,\r |
12 | labels : null,\r |
13 | \r |
14 | init : function(ed, url) {\r |
15 | this._init(ed, url);\r |
16 | },\r |
17 | \r |
18 | _init : function(ed, url) {\r |
19 | var t = this;\r |
20 | \r |
21 | // Get styles\r |
22 | this._styles = eval(ed.getParam('theme_advanced_styles'));\r |
23 | this.labels = eval(ed.getParam('labels'));\r |
24 | \r |
25 | // Register commands\r |
26 | ed.addCommand('mceSetStyle', function(ui, v) {\r |
27 | t._execCommand(ed, v, t._styles);\r |
28 | });\r |
29 | \r |
30 | ed.onNodeChange.add(this._nodeChange, this);\r |
31 | },\r |
32 | \r |
33 | _execCommand : function(ed, v, styles) {\r |
34 | if (e = ed.selection.getNode()) {\r |
35 | if (v == '-')\r |
36 | return;\r |
37 | \r |
38 | function ReplaceTag (curelm, newtag) {\r |
39 | if (curelm.nodeName.toLowerCase() != newtag) {\r |
40 | // changing to a different node type\r |
41 | var newelm;\r |
42 | \r |
43 | if (((curelm.nodeName.toLowerCase() == "td") || (curelm.nodeName.toLowerCase() == "th")) &&\r |
44 | ((newtag != "td") && (newtag != "th"))) {\r |
45 | newelm = ed.getDoc().createElement(curelm.nodeName);\r |
46 | var child = newelm.appendChild(ed.getDoc().createElement(newtag));\r |
47 | for (var c=0; c<curelm.childNodes.length; c++)\r |
48 | child.appendChild(curelm.childNodes[c].cloneNode(1));\r |
49 | \r |
50 | for (var a=0; a<curelm.attributes.length; a++)\r |
51 | ed.dom.setAttrib(newelm, curelm.attributes[a].name, ed.dom.getAttrib(e, curelm.attributes[a].name));\r |
52 | } else {\r |
53 | newelm = ed.getDoc().createElement(newtag);\r |
54 | for (var c=0; c<curelm.childNodes.length; c++)\r |
55 | newelm.appendChild(curelm.childNodes[c].cloneNode(1));\r |
56 | \r |
57 | for (var a=0; a<curelm.attributes.length; a++)\r |
58 | ed.dom.setAttrib(newelm, curelm.attributes[a].name, ed.dom.getAttrib(e, curelm.attributes[a].name));\r |
59 | }\r |
60 | \r |
61 | b = ed.selection.getBookmark();\r |
62 | curelm.parentNode.replaceChild(newelm, curelm);\r |
63 | ed.selection.moveToBookmark(b);\r |
64 | curelm = newelm;\r |
65 | }\r |
66 | return curelm;\r |
67 | }\r |
68 | \r |
69 | var tag = styles[parseInt(v)].tag, className = styles[parseInt(v)].className;\r |
70 | \r |
71 | switch (styles[parseInt(v)].type) {\r |
72 | case "Text":\r |
73 | case "Print":\r |
74 | tinymce.each(ed.selection.getSelectedBlocks(), function(e) {\r |
75 | if ((tag == "") && (className == "")) {\r |
76 | ed.execCommand('RemoveFormat', false, null);\r |
77 | } else {\r |
78 | if (e.nodeName.toLowerCase() != "body") {\r |
79 | if (e.tagName.toLowerCase() != tag.toLowerCase()) {\r |
80 | e = ReplaceTag (e, tag);\r |
81 | }\r |
82 | if (className != "") {\r |
83 | var classnames = ed.dom.getAttrib(e, 'class').split(' ');\r |
84 | var newclassnames = new Array();\r |
85 | newclassnames.push(className)\r |
86 | for (var i = 0; i < classnames.length; i++) {\r |
87 | if ((classnames[i] == 'image-left') ||\r |
88 | (classnames[i] == 'image-right') ||\r |
89 | (classnames[i] == 'image-inline') ||\r |
90 | (classnames[i] == 'captioned')) {\r |
91 | newclassnames.push(classnames[i]);\r |
92 | }\r |
93 | }\r |
94 | e.className = newclassnames.join(' ');\r |
95 | }\r |
96 | }\r |
97 | }\r |
98 | });\r |
99 | break;\r |
100 | case "Tables":\r |
101 | var n;\r |
102 | switch (tag) {\r |
103 | case "th":\r |
104 | case "td":\r |
105 | if (n = this._getParentNode(e,["th","td"])) {\r |
106 | n = ReplaceTag (n, tag);\r |
107 | n.className = className;\r |
108 | }\r |
109 | break;\r |
110 | case "tr":\r |
111 | if (n = this._getParentNode(e,["tr"])) {\r |
112 | n.className = className;\r |
113 | }\r |
114 | break;\r |
115 | case "table":\r |
116 | if (n = this._getParentNode(e,["table"])) {\r |
117 | n.className = className;\r |
118 | }\r |
119 | break;\r |
120 | }\r |
121 | break;\r |
122 | case "Lists":\r |
123 | if (tag == "dd" || tag == "dt") {\r |
124 | e = ReplaceTag(e, tag);\r |
125 | } else {\r |
126 | var n = this._getParentNode(e,["ol","ul"]);\r |
127 | \r |
128 | n.className = className;\r |
129 | }\r |
130 | break;\r |
131 | case "Selection":\r |
132 | ed.execCommand('mceSetCSSClass', false, className);\r |
133 | break;\r |
134 | }\r |
135 | ed.nodeChanged();\r |
136 | }\r |
137 | },\r |
138 | \r |
139 | _nodeChange : function(ed, cm, n) {\r |
140 | // Check if active editor\r |
141 | if (tinyMCE.activeEditor.id != ed.id) {\r |
142 | return;\r |
143 | }\r |
144 | \r |
145 | // Check if node is the same as previous node\r |
146 | if (n == this._previousNode) {\r |
147 | return;\r |
148 | } else {\r |
149 | this._previousNode = n;\r |
150 | }\r |
151 | \r |
152 | this._rebuildListBox(ed, n);\r |
153 | },\r |
154 | \r |
155 | _inArray : function(s, a) {\r |
156 | for (var i=0; i<a.length; i++) {\r |
157 | if (s == a[i]) {\r |
158 | return true;\r |
159 | }\r |
160 | }\r |
161 | return false;\r |
162 | },\r |
163 | \r |
164 | _getParentNode : function(e, a) {\r |
165 | a.push("body");\r |
166 | var p = e;\r |
167 | while (!this._inArray(p.nodeName.toLowerCase(), a)) {\r |
168 | p = p.parentNode;\r |
169 | }\r |
170 | if (p.nodeName.toLowerCase() == "body") {\r |
171 | return false;\r |
172 | } else {\r |
173 | return p;\r |
174 | }\r |
175 | },\r |
176 | \r |
177 | _rebuildListBox : function(ed, n) {\r |
178 | if (this._control == null)\r |
179 | return;\r |
180 | \r |
181 | if(!Array.prototype.indexOf) {\r |
182 | Array.prototype.indexOf = function(obj,start) {\r |
183 | for(var i=(start||0),j=this.length;i<j;i++) {\r |
184 | if(this[i]==obj){return i;}\r |
185 | }\r |
186 | return -1;\r |
187 | }\r |
188 | }\r |
189 | \r |
190 | // Remove existing items\r |
191 | this._control.items = [];\r |
192 | \r |
193 | // Set old ID\r |
194 | this._control.oldID = null;\r |
195 | \r |
196 | // Select nothing\r |
197 | this._control.select();\r |
198 | \r |
199 | // Check if inside table\r |
200 | var t = this._getParentNode (n, ["td", "th"]);\r |
201 | \r |
202 | // Check if inside list\r |
203 | var ul = this._getParentNode (n, ["ul"]);\r |
204 | var ol = this._getParentNode (n, ["ol"]);\r |
205 | var dl = this._getParentNode (n, ["dl"]);\r |
206 | \r |
207 | // Hardcoded strings translated with this.labels\r |
208 | var label_ids = ["Text", "Selection", "Tables", "Lists", "Print"];\r |
209 | \r |
210 | // Fill the listbox\r |
211 | for (var i = 0; i < this._styles.length; i++) {\r |
212 | \r |
213 | tag = this._styles[i].tag;\r |
214 | if ((((tag != "td") && (tag != "th") && (tag != "tr") && (tag != "table")) || t) &&\r |
215 | (tag != "ul" || ul) &&\r |
216 | (tag != "ol" || ol) &&\r |
217 | (((tag != "dl") && (tag != "dd") && (tag != "dt")) || dl)) {\r |
218 | \r |
219 | // e.g. style_title = this.labels["label_text"]\r |
220 | style_title = label_ids.indexOf(this._styles[i].title)>-1 ? this.labels["label_"+this._styles[i].title.toLowerCase()] : this._styles[i].title;\r |
221 | // Add item\r |
222 | this._control.add(\r |
223 | style_title,\r |
224 | this._styles[i].className == '-' ? '-' : i,\r |
225 | {'class' : this._styles[i].className == '-' ? 'mceMenuItemTitle' : 'mce_formatPreview mce_' + this._styles[i].tag}\r |
226 | );\r |
227 | \r |
228 | var p = this._getParentNode(n, ["th","td","p","h1","h2","h3","h4","h5","h6","pre","div","span","blockquote","samp","code", "ul", "ol","dl","img"]);\r |
229 | var il = false;\r |
230 | if (p && (p.nodeName.toLowerCase() == "ul" || p.nodeName.toLowerCase() == "ol")) {\r |
231 | var lc = ed.dom.getAttrib(p, "class");\r |
232 | \r |
233 | if (lc == this._styles[i].className) {\r |
234 | il = true;\r |
235 | }\r |
236 | } else if (p && p.nodeName.toLowerCase() == "dl") {\r |
237 | var d = this._getParentNode(n, ["dd","dt"]);\r |
238 | if (d && d.nodeName.toLowerCase() == tag) {\r |
239 | il = true;\r |
240 | p = d;\r |
241 | }\r |
242 | } else {\r |
243 | il = true;\r |
244 | }\r |
245 | if (p && (p.nodeName.toLowerCase() == this._styles[i].tag) && (p.className.indexOf(this._styles[i].className) != -1) && il) {\r |
246 | this._control.select(i);\r |
247 | }\r |
248 | }\r |
249 | }\r |
250 | \r |
251 | if (this._control.menu) {\r |
252 | tinymce.DOM.remove("menu_" + this._control.menu.id);\r |
253 | }\r |
254 | this._control.renderMenu();\r |
255 | },\r |
256 | \r |
257 | createControl : function(n, cm) {\r |
258 | if (n == 'style') {\r |
259 | this._control = cm.createListBox('style_' + tinyMCE.activeEditor.id, {\r |
260 | title : this.labels['label_style_ldots'],\r |
261 | cmd : 'mceSetStyle'\r |
262 | });\r |
263 | return this._control;\r |
264 | }\r |
265 | \r |
266 | return null;\r |
267 | },\r |
268 | \r |
269 | getInfo : function() {\r |
270 | return {\r |
271 | longname : 'Plone style',\r |
272 | author : 'Rob Gietema',\r |
273 | authorurl : 'http://plone.org',\r |
274 | infourl : 'http://plone.org/products/tinymce',\r |
275 | version : tinymce.majorVersion + "." + tinymce.minorVersion\r |
276 | };\r |
277 | }\r |
278 | });\r |
279 | \r |
280 | // Register plugin\r |
281 | tinymce.PluginManager.add('plonestyle', tinymce.plugins.PloneStylePlugin);\r |
282 | })();\r |