PageRenderTime 158ms CodeModel.GetById 70ms app.highlight 63ms RepoModel.GetById 17ms app.codeStats 1ms

/static/scripts/tiny_mce/plugins/compat2x/editor_plugin_src.js

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