2 * Various helper functions
6 * A PHP-style substr_replace
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.
12 function substr_replace(str, replace, start, length) {
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) {
20 b1 = (length < 0 ? str.length : a2) + length;
21 return str.substring(0, a2) + replace + str.substring(b1);
25 * Bind variables to a function call creating a closure
27 * Use this to circumvent variable scope problems when creating closures
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
36 function 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);
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)));
54 * Report an error from a JS file to the console
56 * @param e The error object
57 * @param file The file in which the error occurred
59 function 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);
66 console.error(e.stack);