web/static/admin/js/core.js
changeset 0 ecdfc63274bf
equal deleted inserted replaced
-1:000000000000 0:ecdfc63274bf
       
     1 // Core javascript helper functions
       
     2 
       
     3 // basic browser identification & version
       
     4 var isOpera = (navigator.userAgent.indexOf("Opera")>=0) && parseFloat(navigator.appVersion);
       
     5 var isIE = ((document.all) && (!isOpera)) && parseFloat(navigator.appVersion.split("MSIE ")[1].split(";")[0]);
       
     6 
       
     7 // Cross-browser event handlers.
       
     8 function addEvent(obj, evType, fn) {
       
     9     if (obj.addEventListener) {
       
    10         obj.addEventListener(evType, fn, false);
       
    11         return true;
       
    12     } else if (obj.attachEvent) {
       
    13         var r = obj.attachEvent("on" + evType, fn);
       
    14         return r;
       
    15     } else {
       
    16         return false;
       
    17     }
       
    18 }
       
    19 
       
    20 function removeEvent(obj, evType, fn) {
       
    21     if (obj.removeEventListener) {
       
    22         obj.removeEventListener(evType, fn, false);
       
    23         return true;
       
    24     } else if (obj.detachEvent) {
       
    25         obj.detachEvent("on" + evType, fn);
       
    26         return true;
       
    27     } else {
       
    28         return false;
       
    29     }
       
    30 }
       
    31 
       
    32 // quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]);
       
    33 function quickElement() {
       
    34     var obj = document.createElement(arguments[0]);
       
    35     if (arguments[2] != '' && arguments[2] != null) {
       
    36         var textNode = document.createTextNode(arguments[2]);
       
    37         obj.appendChild(textNode);
       
    38     }
       
    39     var len = arguments.length;
       
    40     for (var i = 3; i < len; i += 2) {
       
    41         obj.setAttribute(arguments[i], arguments[i+1]);
       
    42     }
       
    43     arguments[1].appendChild(obj);
       
    44     return obj;
       
    45 }
       
    46 
       
    47 // ----------------------------------------------------------------------------
       
    48 // Cross-browser xmlhttp object
       
    49 // from http://jibbering.com/2002/4/httprequest.html
       
    50 // ----------------------------------------------------------------------------
       
    51 var xmlhttp;
       
    52 /*@cc_on @*/
       
    53 /*@if (@_jscript_version >= 5)
       
    54     try {
       
    55         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
       
    56     } catch (e) {
       
    57         try {
       
    58             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       
    59         } catch (E) {
       
    60             xmlhttp = false;
       
    61         }
       
    62     }
       
    63 @else
       
    64     xmlhttp = false;
       
    65 @end @*/
       
    66 if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
       
    67   xmlhttp = new XMLHttpRequest();
       
    68 }
       
    69 
       
    70 // ----------------------------------------------------------------------------
       
    71 // Find-position functions by PPK
       
    72 // See http://www.quirksmode.org/js/findpos.html
       
    73 // ----------------------------------------------------------------------------
       
    74 function findPosX(obj) {
       
    75     var curleft = 0;
       
    76     if (obj.offsetParent) {
       
    77         while (obj.offsetParent) {
       
    78             curleft += obj.offsetLeft - ((isOpera) ? 0 : obj.scrollLeft);
       
    79             obj = obj.offsetParent;
       
    80         }
       
    81         // IE offsetParent does not include the top-level
       
    82         if (isIE && obj.parentElement){
       
    83             curleft += obj.offsetLeft - obj.scrollLeft;
       
    84         }
       
    85     } else if (obj.x) {
       
    86         curleft += obj.x;
       
    87     }
       
    88     return curleft;
       
    89 }
       
    90 
       
    91 function findPosY(obj) {
       
    92     var curtop = 0;
       
    93     if (obj.offsetParent) {
       
    94         while (obj.offsetParent) {
       
    95             curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop);
       
    96             obj = obj.offsetParent;
       
    97         }
       
    98         // IE offsetParent does not include the top-level
       
    99         if (isIE && obj.parentElement){
       
   100             curtop += obj.offsetTop - obj.scrollTop;
       
   101         }
       
   102     } else if (obj.y) {
       
   103         curtop += obj.y;
       
   104     }
       
   105     return curtop;
       
   106 }
       
   107 
       
   108 //-----------------------------------------------------------------------------
       
   109 // Date object extensions
       
   110 // ----------------------------------------------------------------------------
       
   111 Date.prototype.getCorrectYear = function() {
       
   112     // Date.getYear() is unreliable --
       
   113     // see http://www.quirksmode.org/js/introdate.html#year
       
   114     var y = this.getYear() % 100;
       
   115     return (y < 38) ? y + 2000 : y + 1900;
       
   116 }
       
   117 
       
   118 Date.prototype.getTwelveHours = function() {
       
   119     hours = this.getHours();
       
   120     if (hours == 0) {
       
   121         return 12;
       
   122     }
       
   123     else {
       
   124         return hours <= 12 ? hours : hours-12
       
   125     }
       
   126 }
       
   127 
       
   128 Date.prototype.getTwoDigitMonth = function() {
       
   129     return (this.getMonth() < 9) ? '0' + (this.getMonth()+1) : (this.getMonth()+1);
       
   130 }
       
   131 
       
   132 Date.prototype.getTwoDigitDate = function() {
       
   133     return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
       
   134 }
       
   135 
       
   136 Date.prototype.getTwoDigitTwelveHour = function() {
       
   137     return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
       
   138 }
       
   139 
       
   140 Date.prototype.getTwoDigitHour = function() {
       
   141     return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
       
   142 }
       
   143 
       
   144 Date.prototype.getTwoDigitMinute = function() {
       
   145     return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
       
   146 }
       
   147 
       
   148 Date.prototype.getTwoDigitSecond = function() {
       
   149     return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
       
   150 }
       
   151 
       
   152 Date.prototype.getISODate = function() {
       
   153     return this.getCorrectYear() + '-' + this.getTwoDigitMonth() + '-' + this.getTwoDigitDate();
       
   154 }
       
   155 
       
   156 Date.prototype.getHourMinute = function() {
       
   157     return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
       
   158 }
       
   159 
       
   160 Date.prototype.getHourMinuteSecond = function() {
       
   161     return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond();
       
   162 }
       
   163 
       
   164 Date.prototype.strftime = function(format) {
       
   165     var fields = {
       
   166         c: this.toString(),
       
   167         d: this.getTwoDigitDate(),
       
   168         H: this.getTwoDigitHour(),
       
   169         I: this.getTwoDigitTwelveHour(),
       
   170         m: this.getTwoDigitMonth(),
       
   171         M: this.getTwoDigitMinute(),
       
   172         p: (this.getHours() >= 12) ? 'PM' : 'AM',
       
   173         S: this.getTwoDigitSecond(),
       
   174         w: '0' + this.getDay(),
       
   175         x: this.toLocaleDateString(),
       
   176         X: this.toLocaleTimeString(),
       
   177         y: ('' + this.getFullYear()).substr(2, 4),
       
   178         Y: '' + this.getFullYear(),
       
   179         '%' : '%'
       
   180     };
       
   181     var result = '', i = 0;
       
   182     while (i < format.length) {
       
   183         if (format.charAt(i) === '%') {
       
   184             result = result + fields[format.charAt(i + 1)];
       
   185             ++i;
       
   186         }
       
   187         else {
       
   188             result = result + format.charAt(i);
       
   189         }
       
   190         ++i;
       
   191     }
       
   192     return result;
       
   193 }
       
   194 
       
   195 // ----------------------------------------------------------------------------
       
   196 // String object extensions
       
   197 // ----------------------------------------------------------------------------
       
   198 String.prototype.pad_left = function(pad_length, pad_string) {
       
   199     var new_string = this;
       
   200     for (var i = 0; new_string.length < pad_length; i++) {
       
   201         new_string = pad_string + new_string;
       
   202     }
       
   203     return new_string;
       
   204 }
       
   205 
       
   206 // ----------------------------------------------------------------------------
       
   207 // Get the computed style for and element
       
   208 // ----------------------------------------------------------------------------
       
   209 function getStyle(oElm, strCssRule){
       
   210     var strValue = "";
       
   211     if(document.defaultView && document.defaultView.getComputedStyle){
       
   212         strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
       
   213     }
       
   214     else if(oElm.currentStyle){
       
   215         strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
       
   216             return p1.toUpperCase();
       
   217         });
       
   218         strValue = oElm.currentStyle[strCssRule];
       
   219     }
       
   220     return strValue;
       
   221 }