Initial revision with basic functionability
[bcms.git] / media / tinymce / plugins / compat2x / editor_plugin_src.js
blobafad77c2f4b886bad5ebd269e0cdb1f429210346
1 /**\r
2  * $Id: editor_plugin_src.js 264 2007-04-26 20:53:09Z spocke $\r
3  *\r
4  * @author Moxiecode\r
5  * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.\r
6  */\r
7 \r
8 (function() {\r
9         var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each, is = tinymce.is;\r
11         tinymce.create('tinymce.plugins.Compat2x', {\r
12                 getInfo : function() {\r
13                         return {\r
14                                 longname : 'Compat2x',\r
15                                 author : 'Moxiecode Systems AB',\r
16                                 authorurl : 'http://tinymce.moxiecode.com',\r
17                                 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/compat2x',\r
18                                 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion\r
19                         };\r
20                 }\r
21         });\r
23         (function() {\r
24                 // Extend tinyMCE/EditorManager\r
25                 tinymce.extend(tinyMCE, {\r
26                         addToLang : function(p, l) {\r
27                                 each(l, function(v, k) {\r
28                                         tinyMCE.i18n[(tinyMCE.settings.language || 'en') + '.' + (p ? p + '_' : '') + k] = v;\r
29                                 });\r
30                         },\r
32                         getInstanceById : function(n) {\r
33                                 return this.get(n);\r
34                         }\r
35                 });\r
36         })();\r
38         (function() {\r
39                 var EditorManager = tinymce.EditorManager;\r
41                 tinyMCE.instances = {};\r
42                 tinyMCE.plugins = {};\r
43                 tinymce.PluginManager.onAdd.add(function(pm, n, p) {\r
44                         tinyMCE.plugins[n] = p;\r
45                 });\r
47                 tinyMCE.majorVersion = tinymce.majorVersion;\r
48                 tinyMCE.minorVersion = tinymce.minorVersion;\r
49                 tinyMCE.releaseDate = tinymce.releaseDate;\r
50                 tinyMCE.baseURL = tinymce.baseURL;\r
51                 tinyMCE.isIE = tinyMCE.isMSIE = tinymce.isIE || tinymce.isOpera;\r
52                 tinyMCE.isMSIE5 = tinymce.isIE;\r
53                 tinyMCE.isMSIE5_0 = tinymce.isIE;\r
54                 tinyMCE.isMSIE7 = tinymce.isIE;\r
55                 tinyMCE.isGecko = tinymce.isGecko;\r
56                 tinyMCE.isSafari = tinymce.isWebKit;\r
57                 tinyMCE.isOpera = tinymce.isOpera;\r
58                 tinyMCE.isMac = false;\r
59                 tinyMCE.isNS7 = false;\r
60                 tinyMCE.isNS71 = false;\r
61                 tinyMCE.compat = true;\r
63                 // Extend tinyMCE class\r
64                 TinyMCE_Engine = tinyMCE;\r
65                 tinymce.extend(tinyMCE, {\r
66                         getParam : function(n, dv) {\r
67                                 return this.activeEditor.getParam(n, dv);\r
68                         },\r
70                         addEvent : function(e, na, f, sc) {\r
71                                 tinymce.dom.Event.add(e, na, f, sc || this);\r
72                         },\r
74                         getControlHTML : function(n) {\r
75                                 return EditorManager.activeEditor.controlManager.createControl(n);\r
76                         },\r
78                         loadCSS : function(u) {\r
79                                 tinymce.DOM.loadCSS(u);\r
80                         },\r
82                         importCSS : function(doc, u) {\r
83                                 if (doc == document)\r
84                                         this.loadCSS(u);\r
85                                 else\r
86                                         new tinymce.dom.DOMUtils(doc).loadCSS(u);\r
87                         },\r
89                         log : function() {\r
90                                 console.debug.apply(console, arguments);\r
91                         },\r
93                         getLang : function(n, dv) {\r
94                                 var v = EditorManager.activeEditor.getLang(n.replace(/^lang_/g, ''), dv);\r
96                                 // Is number\r
97                                 if (/^[0-9\-.]+$/g.test(v))\r
98                                         return parseInt(v);\r
100                                 return v;\r
101                         },\r
103                         isInstance : function(o) {\r
104                                 return o != null && typeof(o) == "object" && o.execCommand;\r
105                         },\r
107                         triggerNodeChange : function() {\r
108                                 EditorManager.activeEditor.nodeChanged();\r
109                         },\r
111                         regexpReplace : function(in_str, reg_exp, replace_str, opts) {\r
112                                 var re;\r
114                                 if (in_str == null)\r
115                                         return in_str;\r
117                                 if (typeof(opts) == "undefined")\r
118                                         opts = 'g';\r
120                                 re = new RegExp(reg_exp, opts);\r
122                                 return in_str.replace(re, replace_str);\r
123                         },\r
125                         trim : function(s) {\r
126                                 return tinymce.trim(s);\r
127                         },\r
129                         xmlEncode : function(s) {\r
130                                 return tinymce.DOM.encode(s);\r
131                         },\r
133                         explode : function(s, d) {\r
134                                 var o = [];\r
136                                 tinymce.each(s.split(d), function(v) {\r
137                                         if (v != '')\r
138                                                 o.push(v);\r
139                                 });\r
141                                 return o;\r
142                         },\r
144                         switchClass : function(id, cls) {\r
145                                 var b;\r
147                                 if (/^mceButton/.test(cls)) {\r
148                                         b = EditorManager.activeEditor.controlManager.get(id);\r
150                                         if (!b)\r
151                                                 return;\r
153                                         switch (cls) {\r
154                                                 case "mceButtonNormal":\r
155                                                         b.setDisabled(false);\r
156                                                         b.setActive(false);\r
157                                                         return;\r
159                                                 case "mceButtonDisabled":\r
160                                                         b.setDisabled(true);\r
161                                                         return;\r
163                                                 case "mceButtonSelected":\r
164                                                         b.setActive(true);\r
165                                                         b.setDisabled(false);\r
166                                                         return;\r
167                                         }\r
168                                 }\r
169                         },\r
171                         addCSSClass : function(e, n, b) {\r
172                                 return tinymce.DOM.addClass(e, n, b);\r
173                         },\r
175                         hasCSSClass : function(e, n) {\r
176                                 return tinymce.DOM.hasClass(e, n);\r
177                         },\r
179                         removeCSSClass : function(e, n) {\r
180                                 return tinymce.DOM.removeClass(e, n);\r
181                         },\r
183                         getCSSClasses : function() {\r
184                                 var cl = EditorManager.activeEditor.dom.getClasses(), o = [];\r
186                                 each(cl, function(c) {\r
187                                         o.push(c['class']);\r
188                                 });\r
190                                 return o;\r
191                         },\r
193                         setWindowArg : function(n, v) {\r
194                                 EditorManager.activeEditor.windowManager.params[n] = v;\r
195                         },\r
197                         getWindowArg : function(n, dv) {\r
198                                 var wm = EditorManager.activeEditor.windowManager, v;\r
200                                 v = wm.getParam(n);\r
201                                 if (v === '')\r
202                                         return '';\r
204                                 return v || wm.getFeature(n) || dv;\r
205                         },\r
207                         getParentNode : function(n, f) {\r
208                                 return this._getDOM().getParent(n, f);\r
209                         },\r
211                         selectElements : function(n, na, f) {\r
212                                 var i, a = [], nl, x;\r
214                                 for (x=0, na = na.split(','); x<na.length; x++)\r
215                                         for (i=0, nl = n.getElementsByTagName(na[x]); i<nl.length; i++)\r
216                                                 (!f || f(nl[i])) && a.push(nl[i]);\r
218                                 return a;\r
219                         },\r
221                         getNodeTree : function(n, na, t, nn) {\r
222                                 return this.selectNodes(n, function(n) {\r
223                                         return (!t || n.nodeType == t) && (!nn || n.nodeName == nn);\r
224                                 }, na ? na : []);\r
225                         },\r
227                         getAttrib : function(e, n, dv) {\r
228                                 return this._getDOM().getAttrib(e, n, dv);\r
229                         },\r
231                         setAttrib : function(e, n, v) {\r
232                                 return this._getDOM().setAttrib(e, n, v);\r
233                         },\r
235                         getElementsByAttributeValue : function(n, e, a, v) {\r
236                                 var i, nl = n.getElementsByTagName(e), o = [];\r
238                                 for (i=0; i<nl.length; i++) {\r
239                                         if (tinyMCE.getAttrib(nl[i], a).indexOf(v) != -1)\r
240                                                 o[o.length] = nl[i];\r
241                                 }\r
243                                 return o;\r
244                         },\r
246                         selectNodes : function(n, f, a) {\r
247                                 var i;\r
249                                 if (!a)\r
250                                         a = [];\r
252                                 if (f(n))\r
253                                         a[a.length] = n;\r
255                                 if (n.hasChildNodes()) {\r
256                                         for (i=0; i<n.childNodes.length; i++)\r
257                                                 tinyMCE.selectNodes(n.childNodes[i], f, a);\r
258                                 }\r
260                                 return a;\r
261                         },\r
263                         getContent : function() {\r
264                                 return EditorManager.activeEditor.getContent();\r
265                         },\r
267                         getParentElement : function(n, na, f) {\r
268                                 if (na)\r
269                                         na = new RegExp('^(' + na.toUpperCase().replace(/,/g, '|') + ')$', 'g');\r
271                                 return this._getDOM().getParent(n, function(n) {\r
272                                         return n.nodeType == 1 && (!na || na.test(n.nodeName)) && (!f || f(n));\r
273                                 }, this.activeEditor.getBody());\r
274                         },\r
276                         importPluginLanguagePack : function(n) {\r
277                                 tinymce.PluginManager.requireLangPack(n);\r
278                         },\r
280                         getButtonHTML : function(cn, lang, img, c, u, v) {\r
281                                 var ed = EditorManager.activeEditor;\r
283                                 img = img.replace(/\{\$pluginurl\}/g, tinyMCE.pluginURL);\r
284                                 img = img.replace(/\{\$themeurl\}/g, tinyMCE.themeURL);\r
285                                 lang = lang.replace(/^lang_/g, '');\r
287                                 return ed.controlManager.createButton(cn, {\r
288                                         title : lang,\r
289                                         command : c,\r
290                                         ui : u,\r
291                                         value : v,\r
292                                         scope : this,\r
293                                         'class' : 'compat',\r
294                                         image : img\r
295                                 });\r
296                         },\r
298                         addSelectAccessibility : function(e, s, w) {\r
299                                 // Add event handlers \r
300                                 if (!s._isAccessible) {\r
301                                         s.onkeydown = tinyMCE.accessibleEventHandler;\r
302                                         s.onblur = tinyMCE.accessibleEventHandler;\r
303                                         s._isAccessible = true;\r
304                                         s._win = w;\r
305                                 }\r
307                                 return false;\r
308                         },\r
310                         accessibleEventHandler : function(e) {\r
311                                 var elm, win = this._win;\r
313                                 e = tinymce.isIE ? win.event : e;\r
314                                 elm = tinymce.isIE ? e.srcElement : e.target;\r
316                                 // Unpiggyback onchange on blur\r
317                                 if (e.type == "blur") {\r
318                                         if (elm.oldonchange) {\r
319                                                 elm.onchange = elm.oldonchange;\r
320                                                 elm.oldonchange = null;\r
321                                         }\r
323                                         return true;\r
324                                 }\r
326                                 // Piggyback onchange\r
327                                 if (elm.nodeName == "SELECT" && !elm.oldonchange) {\r
328                                         elm.oldonchange = elm.onchange;\r
329                                         elm.onchange = null;\r
330                                 }\r
332                                 // Execute onchange and remove piggyback\r
333                                 if (e.keyCode == 13 || e.keyCode == 32) {\r
334                                         elm.onchange = elm.oldonchange;\r
335                                         elm.onchange();\r
336                                         elm.oldonchange = null;\r
338                                         tinyMCE.cancelEvent(e);\r
339                                         return false;\r
340                                 }\r
342                                 return true;\r
343                         },\r
345                         cancelEvent : function(e) {\r
346                                 return tinymce.dom.Event.cancel(e);\r
347                         },\r
349                         handleVisualAid : function(e) {\r
350                                 EditorManager.activeEditor.addVisual(e);\r
351                         },\r
353                         getAbsPosition : function(n, r) {\r
354                                 return tinymce.DOM.getPos(n, r);\r
355                         },\r
357                         cleanupEventStr : function(s) {\r
358                                 s = "" + s;\r
359                                 s = s.replace('function anonymous()\n{\n', '');\r
360                                 s = s.replace('\n}', '');\r
361                                 s = s.replace(/^return true;/gi, ''); // Remove event blocker\r
363                                 return s;\r
364                         },\r
366                         getVisualAidClass : function(s) {\r
367                                 // TODO: Implement\r
368                                 return s;\r
369                         },\r
371                         parseStyle : function(s) {\r
372                                 return this._getDOM().parseStyle(s);\r
373                         },\r
375                         serializeStyle : function(s) {\r
376                                 return this._getDOM().serializeStyle(s);\r
377                         },\r
379                         openWindow : function(tpl, args) {\r
380                                 var ed = EditorManager.activeEditor, o = {}, n;\r
382                                 // Convert name/value array to object\r
383                                 for (n in tpl)\r
384                                         o[n] = tpl[n];\r
386                                 tpl = o;\r
388                                 args = args || {};\r
389                                 tpl.url = new tinymce.util.URI(tinymce.ThemeManager.themeURLs[ed.settings.theme]).toAbsolute(tpl.file);\r
390                                 tpl.inline = tpl.inline || args.inline;\r
392                                 ed.windowManager.open(tpl, args);\r
393                         },\r
395                         closeWindow : function(win) {\r
396                                 EditorManager.activeEditor.windowManager.close(win);\r
397                         },\r
399                         getOuterHTML : function(e) {\r
400                                 return tinymce.DOM.getOuterHTML(e);\r
401                         },\r
403                         setOuterHTML : function(e, h, d) {\r
404                                 return tinymce.DOM.setOuterHTML(e, h, d);\r
405                         },\r
407                         hasPlugin : function(n) {\r
408                                 return tinymce.PluginManager.get(n) != null;\r
409                         },\r
411                         _setEventsEnabled : function() {\r
412                                 // Ignore it!!\r
413                         },\r
415                         addPlugin : function(pn, f) {\r
416                                 var t = this;\r
418                                 function PluginWrapper(ed) {\r
419                                         tinyMCE.selectedInstance = ed;\r
421                                         ed.onInit.add(function() {\r
422                                                 t.settings = ed.settings;\r
423                                                 t.settings['base_href'] = tinyMCE.documentBasePath;\r
424                                                 tinyMCE.settings = t.settings;\r
425                                                 tinyMCE.documentBasePath = ed.documentBasePath;\r
426                                                 //ed.formElement = DOM.get(ed.id);\r
428                                                 if (f.initInstance)\r
429                                                         f.initInstance(ed);\r
431                                                 ed.contentDocument = ed.getDoc();\r
432                                                 ed.contentWindow = ed.getWin();\r
433                                                 ed.undoRedo = ed.undoManager;\r
434                                                 ed.startContent = ed.getContent({format : 'raw'});\r
436                                                 tinyMCE.instances[ed.id] = ed;\r
437                                                 tinyMCE.loadedFiles = [];\r
438                                         });\r
440                                         ed.onActivate.add(function() {\r
441                                                 tinyMCE.settings = ed.settings;\r
442                                                 tinyMCE.selectedInstance = ed;\r
443                                         });\r
445                                 /*      if (f.removeInstance) {\r
446                                                 ed.onDestroy.add(function() {\r
447                                                         return f.removeInstance(ed.id);\r
448                                                 });\r
449                                         }*/\r
451                                         if (f.handleNodeChange) {\r
452                                                 ed.onNodeChange.add(function(ed, cm, n) {\r
453                                                         f.handleNodeChange(ed.id, n, 0, 0, false, !ed.selection.isCollapsed());\r
454                                                 });\r
455                                         }\r
457                                         if (f.onChange) {\r
458                                                 ed.onChange.add(function(ed, n) {\r
459                                                         return f.onChange(ed);\r
460                                                 });\r
461                                         }\r
463                                         if (f.cleanup) {\r
464                                                 ed.onGetContent.add(function() {\r
465                                                         //f.cleanup(type, content, inst);\r
466                                                 });\r
467                                         }\r
469                                         this.getInfo = function() {\r
470                                                 return f.getInfo();\r
471                                         };\r
473                                         this.createControl = function(n) {\r
474                                                 tinyMCE.pluginURL = tinymce.baseURL + '/plugins/' + pn;\r
475                                                 tinyMCE.themeURL = tinymce.baseURL + '/themes/' + tinyMCE.activeEditor.settings.theme;\r
477                                                 if (f.getControlHTML)\r
478                                                         return f.getControlHTML(n);\r
480                                                 return null;\r
481                                         };\r
483                                         this.execCommand = function(cmd, ui, val) {\r
484                                                 if (f.execCommand)\r
485                                                         return f.execCommand(ed.id, ed.getBody(), cmd, ui, val);\r
487                                                 return false;\r
488                                         };\r
489                                 };\r
491                                 tinymce.PluginManager.add(pn, PluginWrapper);\r
492                         },\r
494                         _getDOM : function() {\r
495                                 return tinyMCE.activeEditor ? tinyMCE.activeEditor.dom : tinymce.DOM;\r
496                         },\r
498                         convertRelativeToAbsoluteURL : function(b, u) {\r
499                                 return new tinymce.util.URI(b).toAbsolute(u);\r
500                         },\r
502                         convertAbsoluteURLToRelativeURL : function(b, u) {\r
503                                 return new tinymce.util.URI(b).toRelative(u);\r
504                         }\r
505                 });\r
507                 // Extend Editor class\r
508                 tinymce.extend(tinymce.Editor.prototype, {\r
509                         getFocusElement : function() {\r
510                                 return this.selection.getNode();\r
511                         },\r
513                         getData : function(n) {\r
514                                 if (!this.data)\r
515                                         this.data = [];\r
517                                 if (!this.data[n])\r
518                                         this.data[n] = [];\r
520                                 return this.data[n];\r
521                         },\r
523                         hasPlugin : function(n) {\r
524                                 return this.plugins[n] != null;\r
525                         },\r
527                         getContainerWin : function() {\r
528                                 return window;\r
529                         },\r
531                         getHTML : function(raw) {\r
532                                 return this.getContent({ format : raw ? 'raw' : 'html'});\r
533                         },\r
535                         setHTML : function(h) {\r
536                                 this.setContent(h);\r
537                         },\r
539                         getSel : function() {\r
540                                 return this.selection.getSel();\r
541                         },\r
543                         getRng : function() {\r
544                                 return this.selection.getRng();\r
545                         },\r
547                         isHidden : function() {\r
548                                 var s;\r
550                                 if (!tinymce.isGecko)\r
551                                         return false;\r
553                                 s = this.getSel();\r
555                                 // Weird, wheres that cursor selection?\r
556                                 return (!s || !s.rangeCount || s.rangeCount == 0);\r
557                         },\r
559                         translate : function(s) {\r
560                                 var c = this.settings.language, o;\r
562                                 o = tinymce.EditorManager.i18n[c + '.' + s] || s.replace(/{\#([^}]+)\}/g, function(a, b) {\r
563                                         return tinymce.EditorManager.i18n[c + '.' + b] || '{#' + b + '}';\r
564                                 });\r
566                                 o = o.replace(/{\$lang_([^}]+)\}/g, function(a, b) {\r
567                                         return tinymce.EditorManager.i18n[c + '.' + b] || '{$lang_' + b + '}';\r
568                                 });\r
570                                 return o;\r
571                         },\r
573                         repaint : function() {\r
574                                 this.execCommand('mceRepaint');\r
575                         }\r
576                 });\r
578                 // Extend selection\r
579                 tinymce.extend(tinymce.dom.Selection.prototype, {\r
580                         getSelectedText : function() {\r
581                                 return this.getContent({format : 'text'});\r
582                         },\r
584                         getSelectedHTML : function() {\r
585                                 return this.getContent({format : 'html'});\r
586                         },\r
588                         getFocusElement : function() {\r
589                                 return this.getNode();\r
590                         },\r
592                         selectNode : function(node, collapse, select_text_node, to_start) {\r
593                                 var t = this;\r
595                                 t.select(node, select_text_node || 0);\r
597                                 if (!is(collapse))\r
598                                         collapse = true;\r
600                                 if (collapse) {\r
601                                         if (!is(to_start))\r
602                                                 to_start = true;\r
604                                         t.collapse(to_start);\r
605                                 }\r
606                         }\r
607                 });\r
608         }).call(this);\r
610         // Register plugin\r
611         tinymce.PluginManager.add('compat2x', tinymce.plugins.Compat2x);\r
612 })();\r