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
11 // #ifdef jquery_adapter
13 (function($, tinymce) {
14 var is = tinymce.is, attrRegExp = /^(href|src|style)$/i, undefined;
16 // jQuery is undefined
18 return alert("Load jQuery first!");
20 // Stick jQuery into the tinymce namespace
25 patchEditor : function(editor) {
28 // Adapt the css function to make sure that the data-mce-style
29 // attribute gets updated with the new style information
30 function css(name, value) {
33 // Remove data-mce-style when set operation occurs
35 self.removeAttr('data-mce-style');
37 return fn.css.apply(self, arguments);
40 // Apapt the attr function to make sure that it uses the data-mce- prefixed variants
41 function attr(name, value) {
44 // Update/retrive data-mce- attribute variants
45 if (attrRegExp.test(name)) {
46 if (value !== undefined) {
47 // Use TinyMCE behavior when setting the specifc attributes
48 self.each(function(i, node) {
49 editor.dom.setAttrib(node, name, value);
54 return self.attr('data-mce-' + name);
58 return fn.attr.apply(self, arguments);
61 function htmlPatchFunc(func) {
62 // Returns a modified function that processes
63 // the HTML before executing the action this makes sure
64 // that href/src etc gets moved into the data-mce- variants
65 return function(content) {
67 content = editor.dom.processHTML(content);
69 return func.call(this, content);
73 // Patch various jQuery functions to handle tinymce specific attribute and content behavior
74 // we don't patch the jQuery.fn directly since it will most likely break compatibility
75 // with other jQuery logic on the page. Only instances created by TinyMCE should be patched.
77 // Patch some functions, only patch the object once
79 // Patch css/attr to use the data-mce- prefixed attribute variants
83 // Patch HTML functions to use the DOMUtils.processHTML filter logic
84 jq.html = htmlPatchFunc(fn.html);
85 jq.append = htmlPatchFunc(fn.append);
86 jq.prepend = htmlPatchFunc(fn.prepend);
87 jq.after = htmlPatchFunc(fn.after);
88 jq.before = htmlPatchFunc(fn.before);
89 jq.replaceWith = htmlPatchFunc(fn.replaceWith);
92 // Each pushed jQuery instance needs to be patched
93 // as well for example when traversing the DOM
94 jq.pushStack = function() {
95 return patch(fn.pushStack.apply(this, arguments));
102 // Add a $ function on each editor instance this one is scoped for the editor document object
103 // this way you can do chaining like this tinymce.get(0).$('p').append('text').css('color', 'red');
104 editor.$ = function(selector, scope) {
105 var doc = editor.getDoc();
107 return patch($(selector || doc, doc || scope));
112 // Patch in core NS functions
113 tinymce.extend = $.extend;
114 tinymce.extend(tinymce, {
116 grep : function(a, f) {return $.grep(a, f || function(){return 1;});},
117 inArray : function(a, v) {return $.inArray(v, a || []);}
119 /* Didn't iterate stylesheets
120 each : function(o, cb, s) {
126 $.each(o, function(nr, el){
127 if (cb.call(s, el, nr, o) === false) {
137 // Patch in functions in various clases
138 // Add a "#ifndefjquery" statement around each core API function you add below
140 'tinymce.dom.DOMUtils' : {
142 addClass : function(e, c) {
143 if (is(e, 'array') && is(e[0], 'string'))
145 return (e && $(is(e, 'string') ? '#' + e : e)
147 .attr('class')) || false;
150 hasClass : function(n, c) {
151 return $(is(n, 'string') ? '#' + n : n).hasClass(c);
154 removeClass : function(e, c) {
160 $(is(e, 'string') ? '#' + e : e)
163 r.push(this.className);
166 return r.length == 1 ? r[0] : r;
170 select : function(pattern, scope) {
173 return $.find(pattern, t.get(scope) || t.get(t.settings.root_element) || t.doc, []);
176 is : function(n, patt) {
177 return $(this.get(n)).is(patt);
182 if (is(e, 'array') && is(e[0], 'string'))
185 $(is(e, 'string') ? '#' + e : e).css('display', 'block');
189 if (is(e, 'array') && is(e[0], 'string'))
192 $(is(e, 'string') ? '#' + e : e).css('display', 'none');
195 isHidden : function(e) {
196 return $(is(e, 'string') ? '#' + e : e).is(':hidden');
199 insertAfter : function(n, e) {
200 return $(is(e, 'string') ? '#' + e : e).after(n);
203 replace : function(o, n, k) {
204 n = $(is(n, 'string') ? '#' + n : n);
207 n.children().appendTo(o);
212 setStyle : function(n, na, v) {
213 if (is(n, 'array') && is(n[0], 'string'))
216 $(is(n, 'string') ? '#' + n : n).css(na, v);
219 getStyle : function(n, na, c) {
220 return $(is(n, 'string') ? '#' + n : n).css(na);
223 setStyles : function(e, o) {
224 if (is(e, 'array') && is(e[0], 'string'))
226 $(is(e, 'string') ? '#' + e : e).css(o);
229 setAttrib : function(e, n, v) {
230 var t = this, s = t.settings;
232 if (is(e, 'array') && is(e[0], 'string'))
235 e = $(is(e, 'string') ? '#' + e : e);
239 e.each(function(i, v){
241 $(v).attr('data-mce-style', v);
255 e.each(function(i, v){
258 v = s.url_converter.call(s.url_converter_scope || t, v, n, v);
260 t.setAttrib(v, 'data-mce-' + n, v);
267 if (v !== null && v.length !== 0)
273 setAttribs : function(e, o) {
276 $.each(o, function(n, v){
284 'tinymce.dom.Event' : {
285 add : function (o, n, f, s) {
289 e.target = e.target || this;
290 f.call(s || this, e);
293 if (is(o, 'array') && is(o[0], 'string'))
295 o = $(is(o, 'string') ? '#' + o : o);
306 lo = this._jqLookup || (this._jqLookup = []);
307 lo.push({func : f, cfunc : cb});
312 remove : function(o, n, f) {
314 $(this._jqLookup).each(function() {
319 if (is(o, 'array') && is(o[0], 'string'))
322 $(is(o, 'string') ? '#' + o : o).unbind(n,f);
330 // Patch functions after a class is created
331 tinymce.onCreate = function(ty, c, p) {
332 tinymce.extend(p, patches[c]);
334 })(window.jQuery, tinymce);