core.js 8.2 KB

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