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