gitweb / static / js / lib / common-lib.json commit gitweb: Unify the way long timestamp is displayed (ce71b07)
   1// Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com>
   2//               2007, Petr Baudis <pasky@suse.cz>
   3//          2008-2011, Jakub Narebski <jnareb@gmail.com>
   4
   5/**
   6 * @fileOverview Generic JavaScript code (helper functions)
   7 * @license GPLv2 or later
   8 */
   9
  10
  11/* ============================================================ */
  12/* ............................................................ */
  13/* Padding */
  14
  15/**
  16 * pad INPUT on the left with STR that is assumed to have visible
  17 * width of single character (for example nonbreakable spaces),
  18 * to WIDTH characters
  19 *
  20 * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
  21 *          ('\u00A0' is nonbreakable space)
  22 *
  23 * @param {Number|String} input: number to pad
  24 * @param {Number} width: visible width of output
  25 * @param {String} str: string to prefix to string, defaults to '\u00A0'
  26 * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
  27 */
  28function padLeftStr(input, width, str) {
  29        var prefix = '';
  30        if (typeof str === 'undefined') {
  31                ch = '\u00A0'; // using '&nbsp;' doesn't work in all browsers
  32        }
  33
  34        width -= input.toString().length;
  35        while (width > 0) {
  36                prefix += str;
  37                width--;
  38        }
  39        return prefix + input;
  40}
  41
  42/**
  43 * Pad INPUT on the left to WIDTH, using given padding character CH,
  44 * for example padLeft('a', 3, '_') is '__a'
  45 *             padLeft(4, 2) is '04' (same as padLeft(4, 2, '0'))
  46 *
  47 * @param {String} input: input value converted to string.
  48 * @param {Number} width: desired length of output.
  49 * @param {String} ch: single character to prefix to string, defaults to '0'.
  50 *
  51 * @returns {String} Modified string, at least SIZE length.
  52 */
  53function padLeft(input, width, ch) {
  54        var s = input + "";
  55        if (typeof ch === 'undefined') {
  56                ch = '0';
  57        }
  58
  59        while (s.length < width) {
  60                s = ch + s;
  61        }
  62        return s;
  63}
  64
  65
  66/* ............................................................ */
  67/* Ajax */
  68
  69/**
  70 * Create XMLHttpRequest object in cross-browser way
  71 * @returns XMLHttpRequest object, or null
  72 */
  73function createRequestObject() {
  74        try {
  75                return new XMLHttpRequest();
  76        } catch (e) {}
  77        try {
  78                return window.createRequest();
  79        } catch (e) {}
  80        try {
  81                return new ActiveXObject("Msxml2.XMLHTTP");
  82        } catch (e) {}
  83        try {
  84                return new ActiveXObject("Microsoft.XMLHTTP");
  85        } catch (e) {}
  86
  87        return null;
  88}
  89
  90
  91/* ............................................................ */
  92/* Support for legacy browsers */
  93
  94/**
  95 * Provides getElementsByClassName method, if there is no native
  96 * implementation of this method.
  97 *
  98 * NOTE that there are limits and differences compared to native
  99 * getElementsByClassName as defined by e.g.:
 100 *   https://developer.mozilla.org/en/DOM/document.getElementsByClassName
 101 *   http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-getelementsbyclassname
 102 *   http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-getelementsbyclassname
 103 *
 104 * Namely, this implementation supports only single class name as
 105 * argument and not set of space-separated tokens representing classes,
 106 * it returns Array of nodes rather than live NodeList, and has
 107 * additional optional argument where you can limit search to given tags
 108 * (via getElementsByTagName).
 109 *
 110 * Based on
 111 *   http://code.google.com/p/getelementsbyclassname/
 112 *   http://www.dustindiaz.com/getelementsbyclass/
 113 *   http://stackoverflow.com/questions/1818865/do-we-have-getelementsbyclassname-in-javascript
 114 *
 115 * See also http://ejohn.org/blog/getelementsbyclassname-speed-comparison/
 116 *
 117 * @param {String} class: name of _single_ class to find
 118 * @param {String} [taghint] limit search to given tags
 119 * @returns {Node[]} array of matching elements
 120 */
 121if (!('getElementsByClassName' in document)) {
 122        document.getElementsByClassName = function (classname, taghint) {
 123                taghint = taghint || "*";
 124                var elements = (taghint === "*" && document.all) ?
 125                               document.all :
 126                               document.getElementsByTagName(taghint);
 127                var pattern = new RegExp("(^|\\s)" + classname + "(\\s|$)");
 128                var matches= [];
 129                for (var i = 0, j = 0, n = elements.length; i < n; i++) {
 130                        var el= elements[i];
 131                        if (el.className && pattern.test(el.className)) {
 132                                // matches.push(el);
 133                                matches[j] = el;
 134                                j++;
 135                        }
 136                }
 137                return matches;
 138        };
 139} // end if
 140
 141
 142/* ............................................................ */
 143/* unquoting/unescaping filenames */
 144
 145/**#@+
 146 * @constant
 147 */
 148var escCodeRe = /\\([^0-7]|[0-7]{1,3})/g;
 149var octEscRe = /^[0-7]{1,3}$/;
 150var maybeQuotedRe = /^\"(.*)\"$/;
 151/**#@-*/
 152
 153/**
 154 * unquote maybe C-quoted filename (as used by git, i.e. it is
 155 * in double quotes '"' if there is any escape character used)
 156 * e.g. 'aa' -> 'aa', '"a\ta"' -> 'a    a'
 157 *
 158 * @param {String} str: git-quoted string
 159 * @returns {String} Unquoted and unescaped string
 160 *
 161 * @globals escCodeRe, octEscRe, maybeQuotedRe
 162 */
 163function unquote(str) {
 164        function unq(seq) {
 165                var es = {
 166                        // character escape codes, aka escape sequences (from C)
 167                        // replacements are to some extent JavaScript specific
 168                        t: "\t",   // tab            (HT, TAB)
 169                        n: "\n",   // newline        (NL)
 170                        r: "\r",   // return         (CR)
 171                        f: "\f",   // form feed      (FF)
 172                        b: "\b",   // backspace      (BS)
 173                        a: "\x07", // alarm (bell)   (BEL)
 174                        e: "\x1B", // escape         (ESC)
 175                        v: "\v"    // vertical tab   (VT)
 176                };
 177
 178                if (seq.search(octEscRe) !== -1) {
 179                        // octal char sequence
 180                        return String.fromCharCode(parseInt(seq, 8));
 181                } else if (seq in es) {
 182                        // C escape sequence, aka character escape code
 183                        return es[seq];
 184                }
 185                // quoted ordinary character
 186                return seq;
 187        }
 188
 189        var match = str.match(maybeQuotedRe);
 190        if (match) {
 191                str = match[1];
 192                // perhaps str = eval('"'+str+'"'); would be enough?
 193                str = str.replace(escCodeRe,
 194                        function (substr, p1, offset, s) { return unq(p1); });
 195        }
 196        return str;
 197}
 198
 199/* end of common-lib.js */