core.js 6.92 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8 9 10
// Core javascript helper functions

// basic browser identification & version
var isOpera = (navigator.userAgent.indexOf("Opera") >= 0) && parseFloat(navigator.appVersion);
var isIE = ((document.all) && (!isOpera)) && parseFloat(navigator.appVersion.split("MSIE ")[1].split(";")[0]);

// quickElement(tagType, parentReference [, textInChildNode, attribute, attributeValue ...]);
function quickElement() {
    'use strict';
    var obj = document.createElement(arguments[0]);
11

Administrator committed
12 13
    if (arguments[2]) {
        var textNode = document.createTextNode(arguments[2]);
14

Administrator committed
15 16 17
        obj.appendChild(textNode);
    }
    var len = arguments.length;
18

Administrator committed
19 20 21 22
    for (var i = 3; i < len; i += 2) {
        obj.setAttribute(arguments[i], arguments[i + 1]);
    }
    arguments[1].appendChild(obj);
23

Administrator committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    return obj;
}

// "a" is reference to an object
function removeChildren(a) {
    'use strict';
    while (a.hasChildNodes()) {
        a.removeChild(a.lastChild);
    }
}

// ----------------------------------------------------------------------------
// Find-position functions by PPK
// See http://www.quirksmode.org/js/findpos.html
// ----------------------------------------------------------------------------
function findPosX(obj) {
    'use strict';
    var curleft = 0;
42

Administrator committed
43 44 45 46 47 48 49 50 51 52 53 54
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft - ((isOpera) ? 0 : obj.scrollLeft);
            obj = obj.offsetParent;
        }
        // IE offsetParent does not include the top-level
        if (isIE && obj.parentElement) {
            curleft += obj.offsetLeft - obj.scrollLeft;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
55

Administrator committed
56 57 58 59 60 61
    return curleft;
}

function findPosY(obj) {
    'use strict';
    var curtop = 0;
62

Administrator committed
63 64 65 66 67 68 69 70 71 72 73 74
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop);
            obj = obj.offsetParent;
        }
        // IE offsetParent does not include the top-level
        if (isIE && obj.parentElement) {
            curtop += obj.offsetTop - obj.scrollTop;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
75

Administrator committed
76 77 78 79 80 81 82 83 84 85
    return curtop;
}

//-----------------------------------------------------------------------------
// Date object extensions
// ----------------------------------------------------------------------------
(function() {
    'use strict';
    Date.prototype.getTwelveHours = function() {
        var hours = this.getHours();
86

Administrator committed
87 88
        if (hours === 0) {
            return 12;
89
        } else {
Administrator committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
            return hours <= 12 ? hours : hours - 12;
        }
    };

    Date.prototype.getTwoDigitMonth = function() {
        return (this.getMonth() < 9) ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1);
    };

    Date.prototype.getTwoDigitDate = function() {
        return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
    };

    Date.prototype.getTwoDigitTwelveHour = function() {
        return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
    };

    Date.prototype.getTwoDigitHour = function() {
        return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
    };

    Date.prototype.getTwoDigitMinute = function() {
        return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
    };

    Date.prototype.getTwoDigitSecond = function() {
        return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
    };

    Date.prototype.getHourMinute = function() {
        return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
    };

    Date.prototype.getHourMinuteSecond = function() {
        return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond();
    };

    Date.prototype.getFullMonthName = function() {
        return typeof window.CalendarNamespace === "undefined"
            ? this.getTwoDigitMonth()
            : window.CalendarNamespace.monthsOfYear[this.getMonth()];
    };

    Date.prototype.strftime = function(format) {
        var fields = {
            B: this.getFullMonthName(),
            c: this.toString(),
            d: this.getTwoDigitDate(),
            H: this.getTwoDigitHour(),
            I: this.getTwoDigitTwelveHour(),
            m: this.getTwoDigitMonth(),
            M: this.getTwoDigitMinute(),
            p: (this.getHours() >= 12) ? 'PM' : 'AM',
            S: this.getTwoDigitSecond(),
            w: '0' + this.getDay(),
            x: this.toLocaleDateString(),
            X: this.toLocaleTimeString(),
            y: ('' + this.getFullYear()).substr(2, 4),
            Y: '' + this.getFullYear(),
            '%': '%'
        };
        var result = '', i = 0;
151

Administrator committed
152 153 154 155
        while (i < format.length) {
            if (format.charAt(i) === '%') {
                result = result + fields[format.charAt(i + 1)];
                ++i;
156
            } else {
Administrator committed
157 158 159 160
                result = result + format.charAt(i);
            }
            ++i;
        }
161

Administrator committed
162 163 164
        return result;
    };

165 166 167
    // ----------------------------------------------------------------------------
    // String object extensions
    // ----------------------------------------------------------------------------
Administrator committed
168 169
    String.prototype.pad_left = function(pad_length, pad_string) {
        var new_string = this;
170

Administrator committed
171 172 173
        for (var i = 0; new_string.length < pad_length; i++) {
            new_string = pad_string + new_string;
        }
174

Administrator committed
175 176 177 178 179 180 181 182
        return new_string;
    };

    String.prototype.strptime = function(format) {
        var split_format = format.split(/[.\-/]/);
        var date = this.split(/[.\-/]/);
        var i = 0;
        var day, month, year;
183

Administrator committed
184 185
        while (i < split_format.length) {
            switch (split_format[i]) {
186 187 188 189 190 191 192 193 194 195 196 197
            case "%d":
                day = date[i];
                break;
            case "%m":
                month = date[i] - 1;
                break;
            case "%Y":
                year = date[i];
                break;
            case "%y":
                year = date[i];
                break;
Administrator committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            }
            ++i;
        }
        // Create Date object from UTC since the parsed value is supposed to be
        // in UTC, not local time. Also, the calendar uses UTC functions for
        // date extraction.
        return new Date(Date.UTC(year, month, day));
    };

})();
// ----------------------------------------------------------------------------
// Get the computed style for and element
// ----------------------------------------------------------------------------
function getStyle(oElm, strCssRule) {
    'use strict';
    var strValue = "";
214 215

    if (document.defaultView && document.defaultView.getComputedStyle) {
Administrator committed
216
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
217
    } else if (oElm.currentStyle) {
218
        strCssRule = strCssRule.replace(/-(\w)/g, function(strMatch, p1) {
Administrator committed
219 220 221 222
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
223

Administrator committed
224 225
    return strValue;
}