1/**
2 * Various helper functions
3 */
4
5/**
6 * A PHP-style substr_replace
7 *
8 * Supports negative start and length and omitting length, but not
9 * str and replace arrays.
10 * See http://php.net/substr-replace for further documentation.
11 */
12function substr_replace(str, replace, start, length) {
13    var a2, b1;
14    a2 = (start < 0 ? str.length : 0) + start;
15    if (typeof length === 'undefined') {
16        length = str.length - a2;
17    } else if (length < 0 && start < 0 && length <= start) {
18        length = 0;
19    }
20    b1 = (length < 0 ? str.length : a2) + length;
21    return str.substring(0, a2) + replace + str.substring(b1);
22}
23
24/**
25 * Bind variables to a function call creating a closure
26 *
27 * Use this to circumvent variable scope problems when creating closures
28 * inside a loop
29 *
30 * @author  Adrian Lang <lang@cosmocode.de>
31 * @link    http://www.cosmocode.de/en/blog/gohr/2009-10/15-javascript-fixing-the-closure-scope-in-loops
32 * @param   functionref fnc - the function to be called
33 * @param   mixed - any arguments to be passed to the function
34 * @returns functionref
35 */
36function bind(fnc/*, ... */) {
37    var Aps = Array.prototype.slice,
38    // Store passed arguments in this scope.
39    // Since arguments is no Array nor has an own slice method,
40    // we have to apply the slice method from the Array.prototype
41        static_args = Aps.call(arguments, 1);
42
43    // Return a function evaluating the passed function with the
44    // given args and optional arguments passed on invocation.
45    return function (/* ... */) {
46        // Same here, but we use Array.prototype.slice solely for
47        // converting arguments to an Array.
48        return fnc.apply(this,
49                         static_args.concat(Aps.call(arguments, 0)));
50    };
51}
52
53/**
54 * Report an error from a JS file to the console
55 *
56 * @param e    The error object
57 * @param file The file in which the error occurred
58 */
59function logError(e, file) {
60    if (window.console && console.error) {
61        console.error('The error "%s: %s" occurred in file "%s". ' +
62            'If this is in a plugin try updating or disabling the plugin, ' +
63            'if this is in a template try updating the template or switching to the "dokuwiki" template.',
64            e.name, e.message, file);
65        if(e.stack) {
66            console.error(e.stack);
67        }
68    }
69}
70