xref: /dokuwiki/lib/scripts/helpers.js (revision 8628c4fa004048e67f75cd638b6be0b26fe3132c)
1/**
2 * Various helper functions
3 */
4
5/**
6 * Very simplistic Flash plugin check, probably works for Flash 8 and higher only
7 *
8 * @author Andreas Gohr <andi@splitbrain.org>
9 */
10function hasFlash(version){
11    var ver = 0;
12    try{
13        if(navigator.plugins != null && navigator.plugins.length > 0){
14           ver = navigator.plugins["Shockwave Flash"].description.split(' ')[2].split('.')[0];
15        }else{
16           ver = (new ActiveXObject("ShockwaveFlash.ShockwaveFlash"))
17                 .GetVariable("$version").split(' ')[1].split(',')[0];
18        }
19    }catch(e){ }
20
21    return ver >= version;
22}
23
24/**
25 * A PHP-style substr_replace
26 *
27 * Supports negative start and length and omitting length, but not
28 * str and replace arrays.
29 * See http://php.net/substr-replace for further documentation.
30 */
31function substr_replace(str, replace, start, length) {
32    var a2, b1;
33    a2 = (start < 0 ? str.length : 0) + start;
34    if (typeof length === 'undefined') {
35        length = str.length - a2;
36    } else if (length < 0 && start < 0 && length <= start) {
37        length = 0;
38    }
39    b1 = (length < 0 ? str.length : a2) + length;
40    return str.substring(0, a2) + replace + str.substring(b1);
41}
42
43/**
44 * Bind variables to a function call creating a closure
45 *
46 * Use this to circumvent variable scope problems when creating closures
47 * inside a loop
48 *
49 * @author  Adrian Lang <lang@cosmocode.de>
50 * @link    http://www.cosmocode.de/en/blog/gohr/2009-10/15-javascript-fixing-the-closure-scope-in-loops
51 * @param   functionref fnc - the function to be called
52 * @param   mixed - any arguments to be passed to the function
53 * @returns functionref
54 */
55function bind(fnc/*, ... */) {
56    var Aps = Array.prototype.slice,
57    // Store passed arguments in this scope.
58    // Since arguments is no Array nor has an own slice method,
59    // we have to apply the slice method from the Array.prototype
60        static_args = Aps.call(arguments, 1);
61
62    // Return a function evaluating the passed function with the
63    // given args and optional arguments passed on invocation.
64    return function (/* ... */) {
65        // Same here, but we use Array.prototype.slice solely for
66        // converting arguments to an Array.
67        return fnc.apply(this,
68                         static_args.concat(Aps.call(arguments, 0)));
69    };
70}
71