4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 var whiteSpaceRe = /^\s*|\s*$/g,
13 undefined, isRegExpBroken = 'B'.replace(/A(.)|B/, '$1') === '$1';
16 * Core namespace with core functionality for the TinyMCE API all sub classes will be added to this namespace/object.
21 * // Using each method
22 * tinymce.each([1, 2, 3], function(v, i) {
23 * console.log(i + '=' + v);
26 * // Checking for a specific browser
32 * Major version of TinyMCE build.
34 * @property majorVersion
37 majorVersion : '@@tinymce_major_version@@',
40 * Major version of TinyMCE build.
42 * @property minorVersion
45 minorVersion : '@@tinymce_minor_version@@',
48 * Release date of TinyMCE build.
50 * @property releaseDate
53 releaseDate : '@@tinymce_release_date@@',
56 * Initializes the TinyMCE global namespace this will setup browser detection and figure out where TinyMCE is running from.
59 var t = this, d = document, na = navigator, ua = na.userAgent, i, nl, n, base, p, v;
62 * Constant that is true if the browser is Opera.
68 t.isOpera = win.opera && opera.buildNumber;
71 * Constant that is true if the browser is WebKit (Safari/Chrome).
77 t.isWebKit = /WebKit/.test(ua);
80 * Constant that is true if the browser is IE.
86 t.isIE = !t.isWebKit && !t.isOpera && (/MSIE/gi).test(ua) && (/Explorer/gi).test(na.appName);
89 * Constant that is true if the browser is IE 6 or older.
95 t.isIE6 = t.isIE && /MSIE [56]/.test(ua);
98 * Constant that is true if the browser is Gecko.
104 t.isGecko = !t.isWebKit && /Gecko/.test(ua);
107 * Constant that is true if the os is Mac OS.
113 t.isMac = ua.indexOf('Mac') != -1;
116 * Constant that is true if the runtime is Adobe Air.
122 t.isAir = /adobeair/i.test(ua);
125 * Constant that tells if the current browser is an iPhone or iPad.
127 * @property isIDevice
131 t.isIDevice = /(iPad|iPhone)/.test(ua);
133 // TinyMCE .NET webcontrol might be setting the values for TinyMCE
134 if (win.tinyMCEPreInit) {
135 t.suffix = tinyMCEPreInit.suffix;
136 t.baseURL = tinyMCEPreInit.base;
137 t.query = tinyMCEPreInit.query;
141 // Get suffix and base
144 // If base element found, add that infront of baseURL
145 nl = d.getElementsByTagName('base');
146 for (i=0; i<nl.length; i++) {
147 if (v = nl[i].href) {
148 // Host only value like http://site.com or http://site.com:8008
149 if (/^https?:\/\/[^\/]+$/.test(v))
152 base = v ? v.match(/.*\//)[0] : ''; // Get only directory
156 function getBase(n) {
157 if (n.src && /tiny_mce(|_gzip|_jquery|_prototype|_full)(_dev|_src)?.js/.test(n.src)) {
158 if (/_(src|dev)\.js/g.test(n.src))
161 if ((p = n.src.indexOf('?')) != -1)
162 t.query = n.src.substring(p + 1);
164 t.baseURL = n.src.substring(0, n.src.lastIndexOf('/'));
166 // If path to script is relative and a base href was found add that one infront
167 // the src property will always be an absolute one on non IE browsers and IE 8
168 // so this logic will basically only be executed on older IE versions
169 if (base && t.baseURL.indexOf('://') == -1 && t.baseURL.indexOf('/') !== 0)
170 t.baseURL = base + t.baseURL;
179 nl = d.getElementsByTagName('script');
180 for (i=0; i<nl.length; i++) {
186 n = d.getElementsByTagName('head')[0];
188 nl = n.getElementsByTagName('script');
189 for (i=0; i<nl.length; i++) {
199 * Checks if a object is of a specific type for example an array.
202 * @param {Object} o Object to check type of.
203 * @param {string} t Optional type to check for.
204 * @return {Boolean} true/false if the object is of the specified type.
206 is : function(o, t) {
208 return o !== undefined;
210 if (t == 'array' && (o.hasOwnProperty && o instanceof Array))
213 return typeof(o) == t;
217 * Makes a name/object map out of an array with names.
220 * @param {Array/String} items Items to make map out of.
221 * @param {String} delim Optional delimiter to split string by.
222 * @param {Object} map Optional map to add items to.
223 * @return {Object} Name/value map of items.
225 makeMap : function(items, delim, map) {
229 delim = delim || ',';
231 if (typeof(items) == "string")
232 items = items.split(delim);
244 * Performs an iteration of all items in a collection such as an object or array. This method will execure the
245 * callback function for each item in the collection, if the callback returns false the iteration will terminate.
246 * The callback has the following format: cb(value, key_or_index).
249 * @param {Object} o Collection to iterate.
250 * @param {function} cb Callback function to execute for each item.
251 * @param {Object} s Optional scope to execute the callback in.
253 * // Iterate an array
254 * tinymce.each([1,2,3], function(v, i) {
255 * console.debug("Value: " + v + ", Index: " + i);
258 * // Iterate an object
259 * tinymce.each({a : 1, b : 2, c: 3], function(v, k) {
260 * console.debug("Value: " + v + ", Key: " + k);
263 each : function(o, cb, s) {
271 if (o.length !== undefined) {
272 // Indexed arrays, needed for Safari
273 for (n=0, l = o.length; n < l; n++) {
274 if (cb.call(s, o[n], n, o) === false)
280 if (o.hasOwnProperty(n)) {
281 if (cb.call(s, o[n], n, o) === false)
293 * Creates a new array by the return value of each iteration function call. This enables you to convert
294 * one array list into another.
297 * @param {Array} a Array of items to iterate.
298 * @param {function} f Function to call for each item. It's return value will be the new value.
299 * @return {Array} Array with new values based on function return values.
301 map : function(a, f) {
304 tinymce.each(a, function(v) {
312 * Filters out items from the input array by calling the specified function for each item.
313 * If the function returns false the item will be excluded if it returns true it will be included.
316 * @param {Array} a Array of items to loop though.
317 * @param {function} f Function to call for each item. Include/exclude depends on it's return value.
318 * @return {Array} New array with values imported and filtered based in input.
320 * // Filter out some items, this will return an array with 4 and 5
321 * var items = tinymce.grep([1,2,3,4,5], function(v) {return v > 3;});
323 grep : function(a, f) {
326 tinymce.each(a, function(v) {
335 * Returns the index of a value in an array, this method will return -1 if the item wasn't found.
338 * @param {Array} a Array/Object to search for value in.
339 * @param {Object} v Value to check for inside the array.
340 * @return {Number/String} Index of item inside the array inside an object. Or -1 if it wasn't found.
342 * // Get index of value in array this will alert 1 since 2 is at that index
343 * alert(tinymce.inArray([1,2,3], 2));
345 inArray : function(a, v) {
349 for (i = 0, l = a.length; i < l; i++) {
359 * Extends an object with the specified other object(s).
362 * @param {Object} o Object to extend with new items.
363 * @param {Object} e..n Object(s) to extend the specified object with.
364 * @return {Object} o New extended object, same reference as the input object.
366 * // Extends obj1 with two new fields
367 * var obj = tinymce.extend(obj1, {
372 * // Extends obj with obj2 and obj3
373 * tinymce.extend(obj, obj2, obj3);
375 extend : function(o, e) {
376 var i, l, a = arguments;
378 for (i = 1, l = a.length; i < l; i++) {
381 tinymce.each(e, function(v, n) {
393 * Removes whitespace from the beginning and end of a string.
396 * @param {String} s String to remove whitespace from.
397 * @return {String} New string with removed whitespace.
400 return (s ? '' + s : '').replace(whiteSpaceRe, '');
404 * Creates a class, subclass or static singleton.
405 * More details on this method can be found in the Wiki.
408 * @param {String} s Class name, inheritage and prefix.
409 * @param {Object} p Collection of methods to add to the class.
410 * @param {Object} root Optional root object defaults to the global window object.
412 * // Creates a basic class
413 * tinymce.create('tinymce.somepackage.SomeClass', {
414 * SomeClass : function() {
415 * // Class constructor
418 * method : function() {
423 * // Creates a basic subclass class
424 * tinymce.create('tinymce.somepackage.SomeSubClass:tinymce.somepackage.SomeClass', {
425 * SomeSubClass: function() {
426 * // Class constructor
427 * this.parent(); // Call parent constructor
430 * method : function() {
432 * this.parent(); // Call parent method
436 * staticMethod : function() {
442 * // Creates a singleton/static class
443 * tinymce.create('static tinymce.somepackage.SomeSingletonClass', {
444 * method : function() {
449 create : function(s, p, root) {
450 var t = this, sp, ns, cn, scn, c, de = 0;
452 // Parse : <prefix> <class>:<super class>
453 s = /^((static) )?([\w.]+)(:([\w.]+))?/.exec(s);
454 cn = s[3].match(/(^|\.)(\w+)$/i)[2]; // Class name
456 // Create namespace for new class
457 ns = t.createNS(s[3].replace(/\.\w+$/, ''), root);
459 // Class already exists
463 // Make pure static class
464 if (s[2] == 'static') {
468 this.onCreate(s[2], s[3], ns[cn]);
473 // Create default constructor
475 p[cn] = function() {};
479 // Add constructor and methods
481 t.extend(ns[cn].prototype, p);
485 sp = t.resolve(s[5]).prototype;
486 scn = s[5].match(/\.(\w+)$/i)[1]; // Class name
488 // Extend constructor
491 // Add passthrough constructor
492 ns[cn] = function() {
493 return sp[scn].apply(this, arguments);
496 // Add inherit constructor
497 ns[cn] = function() {
498 this.parent = sp[scn];
499 return c.apply(this, arguments);
502 ns[cn].prototype[cn] = ns[cn];
505 t.each(sp, function(f, n) {
506 ns[cn].prototype[n] = sp[n];
509 // Add overridden methods
510 t.each(p, function(f, n) {
511 // Extend methods if needed
513 ns[cn].prototype[n] = function() {
515 return f.apply(this, arguments);
519 ns[cn].prototype[n] = f;
524 // Add static methods
525 t.each(p['static'], function(f, n) {
530 this.onCreate(s[2], s[3], ns[cn].prototype);
534 * Executed the specified function for each item in a object tree.
537 * @param {Object} o Object tree to walk though.
538 * @param {function} f Function to call for each item.
539 * @param {String} n Optional name of collection inside the objects to walk for example childNodes.
540 * @param {String} s Optional scope to execute the function in.
542 walk : function(o, f, n, s) {
549 tinymce.each(o, function(o, i) {
550 if (f.call(s, o, i, n) === false)
553 tinymce.walk(o, f, n, s);
559 * Creates a namespace on a specific object.
562 * @param {String} n Namespace to create for example a.b.c.d.
563 * @param {Object} o Optional object to add namespace to, defaults to window.
564 * @return {Object} New namespace object the last item in path.
566 * // Create some namespace
567 * tinymce.createNS('tinymce.somepackage.subpackage');
570 * var tinymce.somepackage.subpackage.SomeSingleton = {
571 * method : function() {
576 createNS : function(n, o) {
582 for (i=0; i<n.length; i++) {
595 * Resolves a string and returns the object from a specific structure.
598 * @param {String} n Path to resolve for example a.b.c.d.
599 * @param {Object} o Optional object to search though, defaults to window.
600 * @return {Object} Last object in path or null if it couldn't be resolved.
602 * // Resolve a path into an object reference
603 * var obj = tinymce.resolve('a.b.c.d');
605 resolve : function(n, o) {
611 for (i = 0, l = n.length; i < l; i++) {
622 * Adds an unload handler to the document. This handler will be executed when the document gets unloaded.
623 * This method is useful for dealing with browser memory leaks where it might be vital to remove DOM references etc.
626 * @param {function} f Function to execute before the document gets unloaded.
627 * @param {Object} s Optional scope to execute the function in.
628 * @return {function} Returns the specified unload handler function.
630 * // Fixes a leak with a DOM element that was palces in the someObject
631 * tinymce.addUnload(function() {
632 * // Null DOM element to reduce IE memory leak
633 * someObject.someElement = null;
636 addUnload : function(f, s) {
639 f = {func : f, scope : s || this};
643 var li = t.unloads, o, n;
646 // Call unload handlers
651 o.func.call(o.scope, 1); // Send in one arg to distinct unload and user destroy
654 // Detach unload function
655 if (win.detachEvent) {
656 win.detachEvent('onbeforeunload', fakeUnload);
657 win.detachEvent('onunload', unload);
658 } else if (win.removeEventListener)
659 win.removeEventListener('unload', unload, false);
661 // Destroy references
662 t.unloads = o = li = w = unload = 0;
664 // Run garbarge collector on IE
665 if (win.CollectGarbage)
670 function fakeUnload() {
673 // Is there things still loading, then do some magic
674 if (d.readyState == 'interactive') {
676 // Prevent memory leak
677 d.detachEvent('onstop', stop);
679 // Call unload handler
686 // Fire unload when the currently loading page is stopped
688 d.attachEvent('onstop', stop);
690 // Remove onstop listener after a while to prevent the unload function
691 // to execute if the user presses cancel in an onbeforeunload
692 // confirm dialog and then presses the browser stop button
693 win.setTimeout(function() {
695 d.detachEvent('onstop', stop);
700 // Attach unload handler
701 if (win.attachEvent) {
702 win.attachEvent('onunload', unload);
703 win.attachEvent('onbeforeunload', fakeUnload);
704 } else if (win.addEventListener)
705 win.addEventListener('unload', unload, false);
707 // Setup initial unload handler array
716 * Removes the specified function form the unload handler list.
718 * @method removeUnload
719 * @param {function} f Function to remove from unload handler list.
720 * @return {function} Removed function name or null if it wasn't found.
722 removeUnload : function(f) {
723 var u = this.unloads, r = null;
725 tinymce.each(u, function(o, i) {
726 if (o && o.func == f) {
737 * Splits a string but removes the whitespace before and after each value.
740 * @param {string} s String to split.
741 * @param {string} d Delimiter to split by.
743 * // Split a string into an array with a,b,c
744 * var arr = tinymce.explode('a, b, c');
746 explode : function(s, d) {
747 return s ? tinymce.map(s.split(d || ','), tinymce.trim) : s;
750 _addVer : function(u) {
756 v = (u.indexOf('?') == -1 ? '?' : '&') + this.query;
758 if (u.indexOf('#') == -1)
761 return u.replace('#', v + '#');
764 // Fix function for IE 9 where regexps isn't working correctly
765 // Todo: remove me once MS fixes the bug
766 _replace : function(find, replace, str) {
767 // On IE9 we have to fake $x replacement
768 if (isRegExpBroken) {
769 return str.replace(find, function() {
770 var val = replace, args = arguments, i;
772 for (i = 0; i < args.length - 2; i++) {
773 if (args[i] === undefined) {
774 val = val.replace(new RegExp('\\$' + i, 'g'), '');
776 val = val.replace(new RegExp('\\$' + i, 'g'), args[i]);
784 return str.replace(find, replace);
790 // Initialize the API
793 // Expose tinymce namespace to the global namespace (window)
794 win.tinymce = win.tinyMCE = tinymce;
796 // Describe the different namespaces
799 * Root level namespace this contains classes directly releated to the TinyMCE editor.
805 * Contains classes for handling the browsers DOM.
807 * @namespace tinymce.dom
811 * Contains html parser and serializer logic.
813 * @namespace tinymce.html
817 * Contains the different UI types such as buttons, listboxes etc.
819 * @namespace tinymce.ui
823 * Contains various utility classes such as json parser, cookies etc.
825 * @namespace tinymce.util
829 * Contains plugin classes.
831 * @namespace tinymce.plugins