source: trunk/admin/themes/default/js/common.js @ 28039

Last change on this file since 28039 was 28039, checked in by plg, 10 years ago

merge r28038 from branch 2.6 to trunk

bug 3068 fixed: on Internet Explorer 8, indexOf function is broken.

File size: 2.1 KB
Line 
1function array_delete(arr, item) {
2  var i = arr.indexOf(item);
3  if (i != -1) arr.splice(i, 1);
4}
5
6function str_repeat(i, m) {
7  for (var o = []; m > 0; o[--m] = i);
8  return o.join('');
9}
10
11if (!Array.prototype.indexOf)
12{
13  Array.prototype.indexOf = function(elt /*, from*/)
14  {
15    var len = this.length;
16
17    var from = Number(arguments[1]) || 0;
18    from = (from < 0)
19         ? Math.ceil(from)
20         : Math.floor(from);
21    if (from < 0)
22      from += len;
23
24    for (; from < len; from++)
25    {
26      if (from in this &&
27          this[from] === elt)
28        return from;
29    }
30    return -1;
31  };
32}
33
34function sprintf() {
35  var i = 0, a, f = arguments[i++], o = [], m, p, c, x, s = '';
36  while (f) {
37    if (m = /^[^\x25]+/.exec(f)) {
38      o.push(m[0]);
39    }
40    else if (m = /^\x25{2}/.exec(f)) {
41      o.push('%');
42    }
43    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
44      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) {
45        throw('Too few arguments.');
46      }
47      if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) {
48        throw('Expecting number but found ' + typeof(a));
49      }
50
51      switch (m[7]) {
52        case 'b': a = a.toString(2); break;
53        case 'c': a = String.fromCharCode(a); break;
54        case 'd': a = parseInt(a); break;
55        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
56        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
57        case 'o': a = a.toString(8); break;
58        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
59        case 'u': a = Math.abs(a); break;
60        case 'x': a = a.toString(16); break;
61        case 'X': a = a.toString(16).toUpperCase(); break;
62      }
63
64      a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+'+ a : a);
65      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
66      x = m[5] - String(a).length - s.length;
67      p = m[5] ? str_repeat(c, x) : '';
68      o.push(s + (m[4] ? a + p : p + a));
69    }
70    else {
71      throw('Huh ?!');
72    }
73
74    f = f.substring(m[0].length);
75  }
76
77  return o.join('');
78}
Note: See TracBrowser for help on using the repository browser.