xref: /dokuwiki/lib/scripts/compatibility.js (revision ab8fecc6efc832771505467be33a88e0cafec04e)
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 media_manager = {
102    // treeattach, selectorattach, confirmattach are munched together into
103    // dw_mediamanager.init
104    attachoptions: DEPRECATED_WRAP(dw_mediamanager.attachoptions, dw_mediamanager),
105    togglekeepopen: function (event, cb) {
106        DEPRECATED('Use dw_mediamanager.toggleOption instead');
107        return dw_mediamanager.toggleOption.call(cb, 'keepopen');
108    },
109    togglehide: function (event, cb) {
110        DEPRECATED('Use dw_mediamanager.toggleOption instead');
111        return dw_mediamanager.toggleOption.call(cb, 'hide');
112    },
113    updatehide: DEPRECATED_WRAP(dw_mediamanager.updatehide, dw_mediamanager),
114    select: function (event, link) {
115        DEPRECATED('Use dw_mediamanager.select instead');
116        return dw_mediamanager.select.call(link, event);
117    },
118    initpopup: DEPRECATED_WRAP(dw_mediamanager.initpopup, dw_mediamanager),
119    insert: DEPRECATED_WRAP(dw_mediamanager.insert, dw_mediamanager),
120    list: function (event, link) {
121        DEPRECATED('Use dw_mediamanager.list instead');
122        return dw_mediamanager.list.call(link, event);
123    },
124    // toggle is handled by dw_tree
125    suggest: DEPRECATED_WRAP(dw_mediamanager.suggest, dw_mediamanager),
126    initFlashUpload: DEPRECATED_WRAP(dw_mediamanager.initFlashUpload, dw_mediamanager),
127    closePopup: function () {
128        DEPRECATED();
129        dw_mediamanager.$popup.dialog('close');
130    },
131    setalign: function (event, cb) {
132        DEPRECATED('Use dw_mediamanager.setOpt instead');
133        return dw_mediamanager.setOpt.call(this, 'align', event);
134    },
135    setlink: function (event, cb) {
136        DEPRECATED('Use dw_mediamanager.setOpt instead');
137        return dw_mediamanager.setOpt.call(this, 'link', event);
138    },
139    setsize: function (event, cb) {
140        DEPRECATED('Use dw_mediamanager.setOpt instead');
141        return dw_mediamanager.setOpt.call(this, 'size', event);
142    },
143    outSet: function (id) {
144        DEPRECATED();
145        return jQuery(id).removeClass('selected');
146    },
147    inSet: function (id) {
148        DEPRECATED();
149        return jQuery(id).addClass('selected');
150    }
151};
152
153initSizeCtl = DEPRECATED_WRAP(dw_editor.initSizeCtl);
154sizeCtl = DEPRECATED_WRAP(dw_editor.sizeCtl);
155toggleWrap = DEPRECATED_WRAP(dw_editor.toggleWrap);
156setWrap = DEPRECATED_WRAP(dw_editor.setWrap);
157
158function findPosX(object){
159    DEPRECATED('Use jQuery.position() instead');
160    return jQuery(object).position().left;
161}
162
163function findPosY(object){
164    DEPRECATED('Use jQuery.position() instead');
165    return jQuery(object).position().top;
166}
167
168function getElementsByClass(searchClass,node,tag){
169    DEPRECATED('Use jQuery() instead');
170    if(node == null) node = document;
171    if(typeof tag === 'undefined') tag = '';
172    return jQuery(node).find(tag+'.'+searchClass).toArray();
173}
174
175function prependChild(parent,element) {
176    DEPRECATED('Use jQuery.prepend() instead');
177    jQuery(parent).prepend(element);
178}
179
180function addEvent(element, type, handler) {
181    DEPRECATED('Use jQuery.bind() instead.');
182    jQuery(element).bind(type,{},handler);
183}
184
185function removeEvent(element, type, handler) {
186    DEPRECATED('Use jQuery.unbind() instead.');
187    jQuery(element).unbind(type,handler);
188}
189
190function addInitEvent(func) {
191    DEPRECATED('Use jQuery(<function>) instead');
192    jQuery(func);
193}
194
195
196function jsEscape(text){
197    DEPRECATED('Insert text through jQuery.text() instead of escaping on your own');
198    var re=new RegExp("\\\\","g");
199    text=text.replace(re,"\\\\");
200    re=new RegExp("'","g");
201    text=text.replace(re,"\\'");
202    re=new RegExp('"',"g");
203    text=text.replace(re,'&quot;');
204    re=new RegExp("\\\\\\\\n","g");
205    text=text.replace(re,"\\n");
206    return text;
207}
208
209/**
210 * Simple function to check if a global var is defined
211 *
212 * @author Kae Verens
213 * @link http://verens.com/archives/2005/07/25/isset-for-javascript/#comment-2835
214 */
215function isset(varname){
216    DEPRECATED("Use `typeof var !== 'undefined'` instead");
217    return(typeof(window[varname])!='undefined');
218}
219
220/**
221 * Checks if property is undefined
222 *
223 * @param {Object} prop value to check
224 * @return {Boolean} true if matched
225 * @scope public
226 * @author Ilya Lebedev <ilya@lebedev.net>
227 */
228function isUndefined (prop /* :Object */) /* :Boolean */ {
229    DEPRECATED("Use `typeof var === 'undefined'` instead");
230    return (typeof prop == 'undefined');
231}
232
233/**
234 * Checks if property is function
235 *
236 * @param {Object} prop value to check
237 * @return {Boolean} true if matched
238 * @scope public
239 * @author Ilya Lebedev <ilya@lebedev.net>
240 */
241function isFunction (prop /* :Object */) /* :Boolean */ {
242    DEPRECATED("Use `typeof var === 'function'` instead");
243    return (typeof prop == 'function');
244}
245/**
246 * Checks if property is string
247 *
248 * @param {Object} prop value to check
249 * @return {Boolean} true if matched
250 * @scope public
251 * @author Ilya Lebedev <ilya@lebedev.net>
252 */
253function isString (prop /* :Object */) /* :Boolean */ {
254    DEPRECATED("Use `typeof var === 'string'` instead");
255    return (typeof prop == 'string');
256}
257
258/**
259 * Checks if property is number
260 *
261 * @param {Object} prop value to check
262 * @return {Boolean} true if matched
263 * @scope public
264 * @author Ilya Lebedev <ilya@lebedev.net>
265 */
266function isNumber (prop /* :Object */) /* :Boolean */ {
267    DEPRECATED("Use `typeof var === 'number'` instead");
268    return (typeof prop == 'number');
269}
270
271/**
272 * Checks if property is the calculable number
273 *
274 * @param {Object} prop value to check
275 * @return {Boolean} true if matched
276 * @scope public
277 * @author Ilya Lebedev <ilya@lebedev.net>
278 */
279function isNumeric (prop /* :Object */) /* :Boolean */ {
280    DEPRECATED("Use `typeof var === 'number' && !isNaN(var) && isFinite(var)` instead");
281    return isNumber(prop)&&!isNaN(prop)&&isFinite(prop);
282}
283
284/**
285 * Checks if property is array
286 *
287 * @param {Object} prop value to check
288 * @return {Boolean} true if matched
289 * @scope public
290 * @author Ilya Lebedev <ilya@lebedev.net>
291 */
292function isArray (prop /* :Object */) /* :Boolean */ {
293    DEPRECATED("Use `var instanceof Array` instead");
294    return (prop instanceof Array);
295}
296
297/**
298 *  Checks if property is regexp
299 *
300 * @param {Object} prop value to check
301 * @return {Boolean} true if matched
302 * @scope public
303 * @author Ilya Lebedev <ilya@lebedev.net>
304 */
305function isRegExp (prop /* :Object */) /* :Boolean */ {
306    DEPRECATED("Use `var instanceof RegExp` instead");
307    return (prop instanceof RegExp);
308}
309
310/**
311 * Checks if property is a boolean value
312 *
313 * @param {Object} prop value to check
314 * @return {Boolean} true if matched
315 * @scope public
316 * @author Ilya Lebedev <ilya@lebedev.net>
317 */
318function isBoolean (prop /* :Object */) /* :Boolean */ {
319    DEPRECATED("Use `typeof var === 'boolean'` instead");
320    return ('boolean' == typeof prop);
321}
322
323/**
324 * Checks if property is a scalar value (value that could be used as the hash key)
325 *
326 * @param {Object} prop value to check
327 * @return {Boolean} true if matched
328 * @scope public
329 * @author Ilya Lebedev <ilya@lebedev.net>
330 */
331function isScalar (prop /* :Object */) /* :Boolean */ {
332    DEPRECATED("Use `typeof var === 'string' || (typeof var === 'number' &&" +
333               " !isNaN(var) && isFinite(var))` instead");
334    return isNumeric(prop)||isString(prop);
335}
336
337/**
338 * Checks if property is empty
339 *
340 * @param {Object} prop value to check
341 * @return {Boolean} true if matched
342 * @scope public
343 * @author Ilya Lebedev <ilya@lebedev.net>
344 */
345function isEmpty (prop /* :Object */) /* :Boolean */ {
346    DEPRECATED();
347    var i;
348    if (isBoolean(prop)) {
349        return false;
350    } else if (isRegExp(prop) && new RegExp("").toString() == prop.toString()) {
351        return true;
352    } else if (isString(prop) || isNumber(prop)) {
353        return !prop;
354    } else if (Boolean(prop) && false != prop) {
355        for (i in prop) {
356            if(prop.hasOwnProperty(i)) {
357                return false;
358            }
359        }
360    }
361    return true;
362}
363
364/**
365 * Get the computed style of a node.
366 *
367 * @link https://acidmartin.wordpress.com/2008/08/26/style-get-any-css-property-value-of-an-object/
368 * @link http://svn.dojotoolkit.org/src/dojo/trunk/_base/html.js
369 */
370function gcs(node){
371    DEPRECATED('Use jQuery(node).style() instead');
372    if(node.currentStyle){
373        return node.currentStyle;
374    }else{
375        return node.ownerDocument.defaultView.getComputedStyle(node, null);
376    }
377}
378
379