]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/adapter/jquery/adapter.js
Release 6.2.2
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / adapter / jquery / adapter.js
1 /**
2  * adapter.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 // #ifdef jquery_adapter
12
13 (function($, tinymce) {
14         var is = tinymce.is, attrRegExp = /^(href|src|style)$/i, undefined;
15
16         // jQuery is undefined
17         if (!$)
18                 return alert("Load jQuery first!");
19
20         // Stick jQuery into the tinymce namespace
21         tinymce.$ = $;
22
23         // Setup adapter
24         tinymce.adapter = {
25                 patchEditor : function(editor) {
26                         var fn = $.fn;
27
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) {
31                                 var self = this;
32
33                                 // Remove data-mce-style when set operation occurs
34                                 if (value)
35                                         self.removeAttr('data-mce-style');
36
37                                 return fn.css.apply(self, arguments);
38                         };
39
40                         // Apapt the attr function to make sure that it uses the data-mce- prefixed variants
41                         function attr(name, value) {
42                                 var self = this;
43
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);
50                                                 });
51
52                                                 return self;
53                                         } else
54                                                 return self.attr('data-mce-' + name);
55                                 }
56
57                                 // Default behavior
58                                 return fn.attr.apply(self, arguments);
59                         };
60
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) {
66                                         if (content)
67                                                 content = editor.dom.processHTML(content);
68
69                                         return func.call(this, content);
70                                 };
71                         };
72
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.
76                         function patch(jq) {
77                                 // Patch some functions, only patch the object once
78                                 if (jq.css !== css) {
79                                         // Patch css/attr to use the data-mce- prefixed attribute variants
80                                         jq.css = css;
81                                         jq.attr = attr;
82
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);
90                                         jq.tinymce = editor;
91
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));
96                                         };
97                                 }
98
99                                 return jq;
100                         };
101
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();
106
107                                 return patch($(selector || doc, doc || scope));
108                         };
109                 }
110         };
111
112         // Patch in core NS functions
113         tinymce.extend = $.extend;
114         tinymce.extend(tinymce, {
115                 map : $.map,
116                 grep : function(a, f) {return $.grep(a, f || function(){return 1;});},
117                 inArray : function(a, v) {return $.inArray(v, a || []);}
118
119                 /* Didn't iterate stylesheets
120                 each : function(o, cb, s) {
121                         if (!o)
122                                 return 0;
123
124                         var r = 1;
125
126                         $.each(o, function(nr, el){
127                                 if (cb.call(s, el, nr, o) === false) {
128                                         r = 0;
129                                         return false;
130                                 }
131                         });
132
133                         return r;
134                 }*/
135         });
136
137         // Patch in functions in various clases
138         // Add a "#ifndefjquery" statement around each core API function you add below
139         var patches = {
140                 'tinymce.dom.DOMUtils' : {
141                         /*
142                         addClass : function(e, c) {
143                                 if (is(e, 'array') && is(e[0], 'string'))
144                                         e = e.join(',#');
145                                 return (e && $(is(e, 'string') ? '#' + e : e)
146                                         .addClass(c)
147                                         .attr('class')) || false;
148                         },
149
150                         hasClass : function(n, c) {
151                                 return $(is(n, 'string') ? '#' + n : n).hasClass(c);
152                         },
153
154                         removeClass : function(e, c) {
155                                 if (!e)
156                                         return false;
157
158                                 var r = [];
159
160                                 $(is(e, 'string') ? '#' + e : e)
161                                         .removeClass(c)
162                                         .each(function(){
163                                                 r.push(this.className);
164                                         });
165
166                                 return r.length == 1 ? r[0] : r;
167                         },
168                         */
169
170                         select : function(pattern, scope) {
171                                 var t = this;
172
173                                 return $.find(pattern, t.get(scope) || t.get(t.settings.root_element) || t.doc, []);
174                         },
175
176                         is : function(n, patt) {
177                                 return $(this.get(n)).is(patt);
178                         }
179
180                         /*
181                         show : function(e) {
182                                 if (is(e, 'array') && is(e[0], 'string'))
183                                         e = e.join(',#');
184
185                                 $(is(e, 'string') ? '#' + e : e).css('display', 'block');
186                         },
187
188                         hide : function(e) {
189                                 if (is(e, 'array') && is(e[0], 'string'))
190                                         e = e.join(',#');
191
192                                 $(is(e, 'string') ? '#' + e : e).css('display', 'none');
193                         },
194
195                         isHidden : function(e) {
196                                 return $(is(e, 'string') ? '#' + e : e).is(':hidden');
197                         },
198
199                         insertAfter : function(n, e) {
200                                 return $(is(e, 'string') ? '#' + e : e).after(n);
201                         },
202
203                         replace : function(o, n, k) {
204                                 n = $(is(n, 'string') ? '#' + n : n);
205
206                                 if (k)
207                                         n.children().appendTo(o);
208
209                                 n.replaceWith(o);
210                         },
211
212                         setStyle : function(n, na, v) {
213                                 if (is(n, 'array') && is(n[0], 'string'))
214                                         n = n.join(',#');
215
216                                 $(is(n, 'string') ? '#' + n : n).css(na, v);
217                         },
218
219                         getStyle : function(n, na, c) {
220                                 return $(is(n, 'string') ? '#' + n : n).css(na);
221                         },
222
223                         setStyles : function(e, o) {
224                                 if (is(e, 'array') && is(e[0], 'string'))
225                                         e = e.join(',#');
226                                 $(is(e, 'string') ? '#' + e : e).css(o);
227                         },
228
229                         setAttrib : function(e, n, v) {
230                                 var t = this, s = t.settings;
231
232                                 if (is(e, 'array') && is(e[0], 'string'))
233                                         e = e.join(',#');
234
235                                 e = $(is(e, 'string') ? '#' + e : e);
236
237                                 switch (n) {
238                                         case "style":
239                                                 e.each(function(i, v){
240                                                         if (s.keep_values)
241                                                                 $(v).attr('data-mce-style', v);
242
243                                                         v.style.cssText = v;
244                                                 });
245                                                 break;
246
247                                         case "class":
248                                                 e.each(function(){
249                                                         this.className = v;
250                                                 });
251                                                 break;
252
253                                         case "src":
254                                         case "href":
255                                                 e.each(function(i, v){
256                                                         if (s.keep_values) {
257                                                                 if (s.url_converter)
258                                                                         v = s.url_converter.call(s.url_converter_scope || t, v, n, v);
259
260                                                                 t.setAttrib(v, 'data-mce-' + n, v);
261                                                         }
262                                                 });
263
264                                                 break;
265                                 }
266
267                                 if (v !== null && v.length !== 0)
268                                         e.attr(n, '' + v);
269                                 else
270                                         e.removeAttr(n);
271                         },
272
273                         setAttribs : function(e, o) {
274                                 var t = this;
275
276                                 $.each(o, function(n, v){
277                                         t.setAttrib(e,n,v);
278                                 });
279                         }
280                         */
281                 }
282
283 /*
284                 'tinymce.dom.Event' : {
285                         add : function (o, n, f, s) {
286                                 var lo, cb;
287
288                                 cb = function(e) {
289                                         e.target = e.target || this;
290                                         f.call(s || this, e);
291                                 };
292
293                                 if (is(o, 'array') && is(o[0], 'string'))
294                                         o = o.join(',#');
295                                 o = $(is(o, 'string') ? '#' + o : o);
296                                 if (n == 'init') {
297                                         o.ready(cb, s);
298                                 } else {
299                                         if (s) {
300                                                 o.bind(n, s, cb);
301                                         } else {
302                                                 o.bind(n, cb);
303                                         }
304                                 }
305
306                                 lo = this._jqLookup || (this._jqLookup = []);
307                                 lo.push({func : f, cfunc : cb});
308
309                                 return cb;
310                         },
311
312                         remove : function(o, n, f) {
313                                 // Find cfunc
314                                 $(this._jqLookup).each(function() {
315                                         if (this.func === f)
316                                                 f = this.cfunc;
317                                 });
318
319                                 if (is(o, 'array') && is(o[0], 'string'))
320                                         o = o.join(',#');
321
322                                 $(is(o, 'string') ? '#' + o : o).unbind(n,f);
323
324                                 return true;
325                         }
326                 }
327 */
328         };
329
330         // Patch functions after a class is created
331         tinymce.onCreate = function(ty, c, p) {
332                 tinymce.extend(p, patches[c]);
333         };
334 })(window.jQuery, tinymce);
335
336 // #endif