gitweb / static / js / lib / common-lib.json commit gitweb: Split JavaScript for maintability, combining on build (9a86dd5)
   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 number N with nonbreakable spaces on the left, to WIDTH characters
  17 * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
  18 *          ('\u00A0' is nonbreakable space)
  19 *
  20 * @param {Number|String} input: number to pad
  21 * @param {Number} width: visible width of output
  22 * @param {String} str: string to prefix to string, e.g. '\u00A0'
  23 * @returns {String} INPUT prefixed with (WIDTH - INPUT.length) x STR
  24 */
  25function padLeftStr(input, width, str) {
  26        var prefix = '';
  27
  28        width -= input.toString().length;
  29        while (width > 0) {
  30                prefix += str;
  31                width--;
  32        }
  33        return prefix + input;
  34}
  35
  36/**
  37 * Pad INPUT on the left to SIZE width, using given padding character CH,
  38 * for example padLeft('a', 3, '_') is '__a'.
  39 *
  40 * @param {String} input: input value converted to string.
  41 * @param {Number} width: desired length of output.
  42 * @param {String} ch: single character to prefix to string.
  43 *
  44 * @returns {String} Modified string, at least SIZE length.
  45 */
  46function padLeft(input, width, ch) {
  47        var s = input + "";
  48        while (s.length < width) {
  49                s = ch + s;
  50        }
  51        return s;
  52}
  53
  54
  55/* ............................................................ */
  56/* Ajax */
  57
  58/**
  59 * Create XMLHttpRequest object in cross-browser way
  60 * @returns XMLHttpRequest object, or null
  61 */
  62function createRequestObject() {
  63        try {
  64                return new XMLHttpRequest();
  65        } catch (e) {}
  66        try {
  67                return window.createRequest();
  68        } catch (e) {}
  69        try {
  70                return new ActiveXObject("Msxml2.XMLHTTP");
  71        } catch (e) {}
  72        try {
  73                return new ActiveXObject("Microsoft.XMLHTTP");
  74        } catch (e) {}
  75
  76        return null;
  77}
  78
  79
  80/* ............................................................ */
  81/* time and data */
  82
  83/**
  84 * used to extract hours and minutes from timezone info, e.g '-0900'
  85 * @constant
  86 */
  87var tzRe = /^([+-])([0-9][0-9])([0-9][0-9])$/;
  88
  89/**
  90 * convert numeric timezone +/-ZZZZ to offset from UTC in seconds
  91 *
  92 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
  93 * @returns {Number} offset from UTC in seconds for timezone
  94 *
  95 * @globals tzRe
  96 */
  97function timezoneOffset(timezoneInfo) {
  98        var match = tzRe.exec(timezoneInfo);
  99        var tz_sign = (match[1] === '-' ? -1 : +1);
 100        var tz_hour = parseInt(match[2],10);
 101        var tz_min  = parseInt(match[3],10);
 102
 103        return tz_sign*(((tz_hour*60) + tz_min)*60);
 104}
 105
 106/**
 107 * return date in local time formatted in iso-8601 like format
 108 * 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200'
 109 *
 110 * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
 111 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
 112 * @returns {String} date in local time in iso-8601 like format
 113 */
 114function formatDateISOLocal(epoch, timezoneInfo) {
 115        // date corrected by timezone
 116        var localDate = new Date(1000 * (epoch +
 117                timezoneOffset(timezoneInfo)));
 118        var localDateStr = // e.g. '2005-08-07'
 119                localDate.getUTCFullYear()                 + '-' +
 120                padLeft(localDate.getUTCMonth()+1, 2, '0') + '-' +
 121                padLeft(localDate.getUTCDate(),    2, '0');
 122        var localTimeStr = // e.g. '21:49:46'
 123                padLeft(localDate.getUTCHours(),   2, '0') + ':' +
 124                padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
 125                padLeft(localDate.getUTCSeconds(), 2, '0');
 126
 127        return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
 128}
 129
 130
 131/* ............................................................ */
 132/* unquoting/unescaping filenames */
 133
 134/**#@+
 135 * @constant
 136 */
 137var escCodeRe = /\\([^0-7]|[0-7]{1,3})/g;
 138var octEscRe = /^[0-7]{1,3}$/;
 139var maybeQuotedRe = /^\"(.*)\"$/;
 140/**#@-*/
 141
 142/**
 143 * unquote maybe git-quoted filename
 144 * e.g. 'aa' -> 'aa', '"a\ta"' -> 'a    a'
 145 *
 146 * @param {String} str: git-quoted string
 147 * @returns {String} Unquoted and unescaped string
 148 *
 149 * @globals escCodeRe, octEscRe, maybeQuotedRe
 150 */
 151function unquote(str) {
 152        function unq(seq) {
 153                var es = {
 154                        // character escape codes, aka escape sequences (from C)
 155                        // replacements are to some extent JavaScript specific
 156                        t: "\t",   // tab            (HT, TAB)
 157                        n: "\n",   // newline        (NL)
 158                        r: "\r",   // return         (CR)
 159                        f: "\f",   // form feed      (FF)
 160                        b: "\b",   // backspace      (BS)
 161                        a: "\x07", // alarm (bell)   (BEL)
 162                        e: "\x1B", // escape         (ESC)
 163                        v: "\v"    // vertical tab   (VT)
 164                };
 165
 166                if (seq.search(octEscRe) !== -1) {
 167                        // octal char sequence
 168                        return String.fromCharCode(parseInt(seq, 8));
 169                } else if (seq in es) {
 170                        // C escape sequence, aka character escape code
 171                        return es[seq];
 172                }
 173                // quoted ordinary character
 174                return seq;
 175        }
 176
 177        var match = str.match(maybeQuotedRe);
 178        if (match) {
 179                str = match[1];
 180                // perhaps str = eval('"'+str+'"'); would be enough?
 181                str = str.replace(escCodeRe,
 182                        function (substr, p1, offset, s) { return unq(p1); });
 183        }
 184        return str;
 185}
 186
 187/* end of common-lib.js */