xref: /dokuwiki/lib/scripts/compatibility.js (revision 7827dd2afcb37599610894016f2e1e6150e561b7)
1/**
2 * Mark a JavaScript function as deprecated
3 *
4 * This will print a warning to the JavaScript console (if available) in
5 * Firebug and Chrome and a stack trace (if available) to easily locate the
6 * problematic function call.
7 *
8 * @param msg optional message to print
9 */
10function DEPRECATED(msg){
11    if(!window.console) return;
12    if(!msg) msg = '';
13
14    var func;
15    if(arguments.callee) func = arguments.callee.caller.name;
16    if(func) func = ' '+func+'()';
17    var line = 'DEPRECATED function call'+func+'. '+msg;
18
19    if(console.warn){
20        console.warn(line);
21    }else{
22        console.log(line);
23    }
24
25    if(console.trace) console.trace();
26}
27
28/**
29 * Construct a wrapper function for deprecated function names
30 *
31 * This function returns a wrapper function which just calls DEPRECATED
32 * and the new function.
33 *
34 * @param func    The new function
35 * @param context Optional; The context (`this`) of the call
36 */
37function DEPRECATED_WRAP(func, context) {
38    return function () {
39        DEPRECATED();
40        return func.apply(context || this, arguments);
41    };
42}
43
44/**
45 * Handy shortcut to document.getElementById
46 *
47 * This function was taken from the prototype library
48 *
49 * @link http://prototype.conio.net/
50 */
51function $() {
52    DEPRECATED('Please use the JQuery() function instead.');
53
54    var elements = new Array();
55
56    for (var i = 0; i < arguments.length; i++) {
57        var element = arguments[i];
58        if (typeof element == 'string')
59            element = document.getElementById(element);
60
61        if (arguments.length == 1)
62            return element;
63
64        elements.push(element);
65    }
66
67    return elements;
68}
69
70
71
72
73var index = {
74    throbber_delay: dw_index.throbber_delay,
75    toggle: DEPRECATED_WRAP(dw_index.toggle, dw_index),
76    treeattach: DEPRECATED_WRAP(dw_index.treeattach, dw_index)
77};
78
79var ajax_quicksearch = {
80    init: DEPRECATED_WRAP(dw_qsearch.init, dw_qsearch),
81    clear_results: DEPRECATED_WRAP(dw_qsearch.clear_results, dw_qsearch),
82    onCompletion: DEPRECATED_WRAP(dw_qsearch.onCompletion, dw_qsearch)
83};
84
85var linkwiz = {
86    init: DEPRECATED_WRAP(dw_linkwiz.init, dw_linkwiz),
87    onEntry: DEPRECATED_WRAP(dw_linkwiz.onEntry, dw_linkwiz),
88    getResult: DEPRECATED_WRAP(dw_linkwiz.getResult, dw_linkwiz),
89    select: DEPRECATED_WRAP(dw_linkwiz.select, dw_linkwiz),
90    deselect: DEPRECATED_WRAP(dw_linkwiz.deselect, dw_linkwiz),
91    onResultClick: DEPRECATED_WRAP(dw_linkwiz.onResultClick, dw_linkwiz),
92    resultClick: DEPRECATED_WRAP(dw_linkwiz.resultClick, dw_linkwiz),
93    insertLink: DEPRECATED_WRAP(dw_linkwiz.insertLink, dw_linkwiz),
94    autocomplete: DEPRECATED_WRAP(dw_linkwiz.autocomplete, dw_linkwiz),
95    autocomplete_exec: DEPRECATED_WRAP(dw_linkwiz.autocomplete_exec, dw_linkwiz),
96    show: DEPRECATED_WRAP(dw_linkwiz.show, dw_linkwiz),
97    hide: DEPRECATED_WRAP(dw_linkwiz.hide, dw_linkwiz),
98    toggle: DEPRECATED_WRAP(dw_linkwiz.toggle, dw_linkwiz)
99};
100
101var locktimer = {
102    init: DEPRECATED_WRAP(dw_locktimer.init, dw_locktimer),
103    reset: DEPRECATED_WRAP(dw_locktimer.reset, dw_locktimer),
104    warning: DEPRECATED_WRAP(dw_locktimer.warning, dw_locktimer),
105    clear: DEPRECATED_WRAP(dw_locktimer.clear, dw_locktimer),
106    refresh: DEPRECATED_WRAP(dw_locktimer.refresh, dw_locktimer),
107    refreshed: DEPRECATED_WRAP(dw_locktimer.refreshed, dw_locktimer)
108};
109
110var media_manager = {
111    // treeattach, selectorattach, confirmattach are munched together into
112    // dw_mediamanager.init
113    attachoptions: DEPRECATED_WRAP(dw_mediamanager.attachoptions, dw_mediamanager),
114    togglekeepopen: function (event, cb) {
115        DEPRECATED('Use dw_mediamanager.toggleOption instead');
116        return dw_mediamanager.toggleOption.call(cb, 'keepopen');
117    },
118    togglehide: function (event, cb) {
119        DEPRECATED('Use dw_mediamanager.toggleOption instead');
120        return dw_mediamanager.toggleOption.call(cb, 'hide');
121    },
122    updatehide: DEPRECATED_WRAP(dw_mediamanager.updatehide, dw_mediamanager),
123    select: function (event, link) {
124        DEPRECATED('Use dw_mediamanager.select instead');
125        return dw_mediamanager.select.call(link, event);
126    },
127    initpopup: DEPRECATED_WRAP(dw_mediamanager.initpopup, dw_mediamanager),
128    insert: DEPRECATED_WRAP(dw_mediamanager.insert, dw_mediamanager),
129    list: function (event, link) {
130        DEPRECATED('Use dw_mediamanager.list instead');
131        return dw_mediamanager.list.call(link, event);
132    },
133    // toggle is handled by dw_tree
134    suggest: DEPRECATED_WRAP(dw_mediamanager.suggest, dw_mediamanager),
135    initFlashUpload: DEPRECATED_WRAP(dw_mediamanager.initFlashUpload, dw_mediamanager),
136    closePopup: function () {
137        DEPRECATED();
138        dw_mediamanager.$popup.dialog('close');
139    },
140    setalign: function (event, cb) {
141        DEPRECATED('Use dw_mediamanager.setOpt instead');
142        return dw_mediamanager.setOpt.call(this, 'align', event);
143    },
144    setlink: function (event, cb) {
145        DEPRECATED('Use dw_mediamanager.setOpt instead');
146        return dw_mediamanager.setOpt.call(this, 'link', event);
147    },
148    setsize: function (event, cb) {
149        DEPRECATED('Use dw_mediamanager.setOpt instead');
150        return dw_mediamanager.setOpt.call(this, 'size', event);
151    },
152    outSet: function (id) {
153        DEPRECATED();
154        return jQuery(id).removeClass('selected');
155    },
156    inSet: function (id) {
157        DEPRECATED();
158        return jQuery(id).addClass('selected');
159    }
160};
161
162initSizeCtl = DEPRECATED_WRAP(dw_editor.initSizeCtl);
163sizeCtl = DEPRECATED_WRAP(dw_editor.sizeCtl);
164toggleWrap = DEPRECATED_WRAP(dw_editor.toggleWrap);
165setWrap = DEPRECATED_WRAP(dw_editor.setWrap);
166
167function findPosX(object){
168    DEPRECATED('Use jQuery.position() instead');
169    return jQuery(object).position().left;
170}
171
172function findPosY(object){
173    DEPRECATED('Use jQuery.position() instead');
174    return jQuery(object).position().top;
175}
176
177function getElementsByClass(searchClass,node,tag){
178    DEPRECATED('Use jQuery() instead');
179    if(node == null) node = document;
180    if(typeof tag === 'undefined') tag = '';
181    return jQuery(node).find(tag+'.'+searchClass).toArray();
182}
183
184function prependChild(parent,element) {
185    DEPRECATED('Use jQuery.prepend() instead');
186    jQuery(parent).prepend(element);
187}
188
189function addEvent(element, type, handler) {
190    DEPRECATED('Use jQuery.bind() instead.');
191    jQuery(element).bind(type,{},handler);
192}
193
194function removeEvent(element, type, handler) {
195    DEPRECATED('Use jQuery.unbind() instead.');
196    jQuery(element).unbind(type,handler);
197}
198
199function addInitEvent(func) {
200    DEPRECATED('Use jQuery(<function>) instead');
201    jQuery(func);
202}
203
204
205function jsEscape(text){
206    DEPRECATED('Insert text through jQuery.text() instead of escaping on your own');
207    var re=new RegExp("\\\\","g");
208    text=text.replace(re,"\\\\");
209    re=new RegExp("'","g");
210    text=text.replace(re,"\\'");
211    re=new RegExp('"',"g");
212    text=text.replace(re,'&quot;');
213    re=new RegExp("\\\\\\\\n","g");
214    text=text.replace(re,"\\n");
215    return text;
216}
217
218/**
219 * Simple function to check if a global var is defined
220 *
221 * @author Kae Verens
222 * @link http://verens.com/archives/2005/07/25/isset-for-javascript/#comment-2835
223 */
224function isset(varname){
225    DEPRECATED("Use `typeof var !== 'undefined'` instead");
226    return(typeof(window[varname])!='undefined');
227}
228
229/**
230 * Checks if property is undefined
231 *
232 * @param {Object} prop value to check
233 * @return {Boolean} true if matched
234 * @scope public
235 * @author Ilya Lebedev <ilya@lebedev.net>
236 */
237function isUndefined (prop /* :Object */) /* :Boolean */ {
238    DEPRECATED("Use `typeof var === 'undefined'` instead");
239    return (typeof prop == 'undefined');
240}
241
242/**
243 * Checks if property is function
244 *
245 * @param {Object} prop value to check
246 * @return {Boolean} true if matched
247 * @scope public
248 * @author Ilya Lebedev <ilya@lebedev.net>
249 */
250function isFunction (prop /* :Object */) /* :Boolean */ {
251    DEPRECATED("Use `typeof var === 'function'` instead");
252    return (typeof prop == 'function');
253}
254/**
255 * Checks if property is string
256 *
257 * @param {Object} prop value to check
258 * @return {Boolean} true if matched
259 * @scope public
260 * @author Ilya Lebedev <ilya@lebedev.net>
261 */
262function isString (prop /* :Object */) /* :Boolean */ {
263    DEPRECATED("Use `typeof var === 'string'` instead");
264    return (typeof prop == 'string');
265}
266
267/**
268 * Checks if property is number
269 *
270 * @param {Object} prop value to check
271 * @return {Boolean} true if matched
272 * @scope public
273 * @author Ilya Lebedev <ilya@lebedev.net>
274 */
275function isNumber (prop /* :Object */) /* :Boolean */ {
276    DEPRECATED("Use `typeof var === 'number'` instead");
277    return (typeof prop == 'number');
278}
279
280/**
281 * Checks if property is the calculable number
282 *
283 * @param {Object} prop value to check
284 * @return {Boolean} true if matched
285 * @scope public
286 * @author Ilya Lebedev <ilya@lebedev.net>
287 */
288function isNumeric (prop /* :Object */) /* :Boolean */ {
289    DEPRECATED("Use `typeof var === 'number' && !isNaN(var) && isFinite(var)` instead");
290    return isNumber(prop)&&!isNaN(prop)&&isFinite(prop);
291}
292
293/**
294 * Checks if property is array
295 *
296 * @param {Object} prop value to check
297 * @return {Boolean} true if matched
298 * @scope public
299 * @author Ilya Lebedev <ilya@lebedev.net>
300 */
301function isArray (prop /* :Object */) /* :Boolean */ {
302    DEPRECATED("Use `var instanceof Array` instead");
303    return (prop instanceof Array);
304}
305
306/**
307 *  Checks if property is regexp
308 *
309 * @param {Object} prop value to check
310 * @return {Boolean} true if matched
311 * @scope public
312 * @author Ilya Lebedev <ilya@lebedev.net>
313 */
314function isRegExp (prop /* :Object */) /* :Boolean */ {
315    DEPRECATED("Use `var instanceof RegExp` instead");
316    return (prop instanceof RegExp);
317}
318
319/**
320 * Checks if property is a boolean value
321 *
322 * @param {Object} prop value to check
323 * @return {Boolean} true if matched
324 * @scope public
325 * @author Ilya Lebedev <ilya@lebedev.net>
326 */
327function isBoolean (prop /* :Object */) /* :Boolean */ {
328    DEPRECATED("Use `typeof var === 'boolean'` instead");
329    return ('boolean' == typeof prop);
330}
331
332/**
333 * Checks if property is a scalar value (value that could be used as the hash key)
334 *
335 * @param {Object} prop value to check
336 * @return {Boolean} true if matched
337 * @scope public
338 * @author Ilya Lebedev <ilya@lebedev.net>
339 */
340function isScalar (prop /* :Object */) /* :Boolean */ {
341    DEPRECATED("Use `typeof var === 'string' || (typeof var === 'number' &&" +
342               " !isNaN(var) && isFinite(var))` instead");
343    return isNumeric(prop)||isString(prop);
344}
345
346/**
347 * Checks if property is empty
348 *
349 * @param {Object} prop value to check
350 * @return {Boolean} true if matched
351 * @scope public
352 * @author Ilya Lebedev <ilya@lebedev.net>
353 */
354function isEmpty (prop /* :Object */) /* :Boolean */ {
355    DEPRECATED();
356    var i;
357    if (isBoolean(prop)) {
358        return false;
359    } else if (isRegExp(prop) && new RegExp("").toString() == prop.toString()) {
360        return true;
361    } else if (isString(prop) || isNumber(prop)) {
362        return !prop;
363    } else if (Boolean(prop) && false != prop) {
364        for (i in prop) {
365            if(prop.hasOwnProperty(i)) {
366                return false;
367            }
368        }
369    }
370    return true;
371}
372
373/**
374 * Get the computed style of a node.
375 *
376 * @link https://acidmartin.wordpress.com/2008/08/26/style-get-any-css-property-value-of-an-object/
377 * @link http://svn.dojotoolkit.org/src/dojo/trunk/_base/html.js
378 */
379function gcs(node){
380    DEPRECATED('Use jQuery(node).style() instead');
381    if(node.currentStyle){
382        return node.currentStyle;
383    }else{
384        return node.ownerDocument.defaultView.getComputedStyle(node, null);
385    }
386}
387
388/**
389 * Until 2011-05-25 "Rincewind", a code intended to fix some Safari issue
390 * always declared the global _timer. plugin:sortablejs relies on _timer
391 * being declared.
392 */
393var _timer;
394