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
17 if (!$ && window.console) {
18 return console.log("Load jQuery first!");
21 // Stick jQuery into the tinymce namespace
26 patchEditor : function(editor) {
29 // Adapt the css function to make sure that the data-mce-style
30 // attribute gets updated with the new style information
31 function css(name, value) {
34 // Remove data-mce-style when set operation occurs
36 self.removeAttr('data-mce-style');
38 return fn.css.apply(self, arguments);
41 // Apapt the attr function to make sure that it uses the data-mce- prefixed variants
42 function attr(name, value) {
45 // Update/retrive data-mce- attribute variants
46 if (attrRegExp.test(name)) {
47 if (value !== undefined) {
48 // Use TinyMCE behavior when setting the specifc attributes
49 self.each(function(i, node) {
50 editor.dom.setAttrib(node, name, value);
55 return self.attr('data-mce-' + name);
59 return fn.attr.apply(self, arguments);
62 function htmlPatchFunc(func) {
63 // Returns a modified function that processes
64 // the HTML before executing the action this makes sure
65 // that href/src etc gets moved into the data-mce- variants
66 return function(content) {
68 content = editor.dom.processHTML(content);
70 return func.call(this, content);
74 // Patch various jQuery functions to handle tinymce specific attribute and content behavior
75 // we don't patch the jQuery.fn directly since it will most likely break compatibility
76 // with other jQuery logic on the page. Only instances created by TinyMCE should be patched.
78 // Patch some functions, only patch the object once
80 // Patch css/attr to use the data-mce- prefixed attribute variants
84 // Patch HTML functions to use the DOMUtils.processHTML filter logic
85 jq.html = htmlPatchFunc(fn.html);
86 jq.append = htmlPatchFunc(fn.append);
87 jq.prepend = htmlPatchFunc(fn.prepend);
88 jq.after = htmlPatchFunc(fn.after);
89 jq.before = htmlPatchFunc(fn.before);
90 jq.replaceWith = htmlPatchFunc(fn.replaceWith);
93 // Each pushed jQuery instance needs to be patched
94 // as well for example when traversing the DOM
95 jq.pushStack = function() {
96 return patch(fn.pushStack.apply(this, arguments));
103 // Add a $ function on each editor instance this one is scoped for the editor document object
104 // this way you can do chaining like this tinymce.get(0).$('p').append('text').css('color', 'red');
105 editor.$ = function(selector, scope) {
106 var doc = editor.getDoc();
108 return patch($(selector || doc, doc || scope));
113 // Patch in core NS functions
114 tinymce.extend = $.extend;
115 tinymce.extend(tinymce, {
117 grep : function(a, f) {return $.grep(a, f || function(){return 1;});},
118 inArray : function(a, v) {return $.inArray(v, a || []);}
120 /* Didn't iterate stylesheets
121 each : function(o, cb, s) {
127 $.each(o, function(nr, el){
128 if (cb.call(s, el, nr, o) === false) {
138 // Patch in functions in various clases
139 // Add a "#ifndefjquery" statement around each core API function you add below
141 'tinymce.dom.DOMUtils' : {
143 addClass : function(e, c) {
144 if (is(e, 'array') && is(e[0], 'string'))
146 return (e && $(is(e, 'string') ? '#' + e : e)
148 .attr('class')) || false;
151 hasClass : function(n, c) {
152 return $(is(n, 'string') ? '#' + n : n).hasClass(c);
155 removeClass : function(e, c) {
161 $(is(e, 'string') ? '#' + e : e)
164 r.push(this.className);
167 return r.length == 1 ? r[0] : r;
171 select : function(pattern, scope) {
174 return $.find(pattern, t.get(scope) || t.get(t.settings.root_element) || t.doc, []);
177 is : function(n, patt) {
178 return $(this.get(n)).is(patt);
183 if (is(e, 'array') && is(e[0], 'string'))
186 $(is(e, 'string') ? '#' + e : e).css('display', 'block');
190 if (is(e, 'array') && is(e[0], 'string'))
193 $(is(e, 'string') ? '#' + e : e).css('display', 'none');
196 isHidden : function(e) {
197 return $(is(e, 'string') ? '#' + e : e).is(':hidden');
200 insertAfter : function(n, e) {
201 return $(is(e, 'string') ? '#' + e : e).after(n);
204 replace : function(o, n, k) {
205 n = $(is(n, 'string') ? '#' + n : n);
208 n.children().appendTo(o);
213 setStyle : function(n, na, v) {
214 if (is(n, 'array') && is(n[0], 'string'))
217 $(is(n, 'string') ? '#' + n : n).css(na, v);
220 getStyle : function(n, na, c) {
221 return $(is(n, 'string') ? '#' + n : n).css(na);
224 setStyles : function(e, o) {
225 if (is(e, 'array') && is(e[0], 'string'))
227 $(is(e, 'string') ? '#' + e : e).css(o);
230 setAttrib : function(e, n, v) {
231 var t = this, s = t.settings;
233 if (is(e, 'array') && is(e[0], 'string'))
236 e = $(is(e, 'string') ? '#' + e : e);
240 e.each(function(i, v){
242 $(v).attr('data-mce-style', v);
256 e.each(function(i, v){
259 v = s.url_converter.call(s.url_converter_scope || t, v, n, v);
261 t.setAttrib(v, 'data-mce-' + n, v);
268 if (v !== null && v.length !== 0)
274 setAttribs : function(e, o) {
277 $.each(o, function(n, v){
285 'tinymce.dom.Event' : {
286 add : function (o, n, f, s) {
290 e.target = e.target || this;
291 f.call(s || this, e);
294 if (is(o, 'array') && is(o[0], 'string'))
296 o = $(is(o, 'string') ? '#' + o : o);
307 lo = this._jqLookup || (this._jqLookup = []);
308 lo.push({func : f, cfunc : cb});
313 remove : function(o, n, f) {
315 $(this._jqLookup).each(function() {
320 if (is(o, 'array') && is(o[0], 'string'))
323 $(is(o, 'string') ? '#' + o : o).unbind(n,f);
331 // Patch functions after a class is created
332 tinymce.onCreate = function(ty, c, p) {
333 tinymce.extend(p, patches[c]);
335 })(window.jQuery, tinymce);