core.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // Find-position functions by PPK
  64. // See http://www.quirksmode.org/js/findpos.html
  65. // ----------------------------------------------------------------------------
  66. function findPosX(obj) {
  67. 'use strict';
  68. var curleft = 0;
  69. if (obj.offsetParent) {
  70. while (obj.offsetParent) {
  71. curleft += obj.offsetLeft - ((isOpera) ? 0 : obj.scrollLeft);
  72. obj = obj.offsetParent;
  73. }
  74. // IE offsetParent does not include the top-level
  75. if (isIE && obj.parentElement) {
  76. curleft += obj.offsetLeft - obj.scrollLeft;
  77. }
  78. } else if (obj.x) {
  79. curleft += obj.x;
  80. }
  81. return curleft;
  82. }
  83. function findPosY(obj) {
  84. 'use strict';
  85. var curtop = 0;
  86. if (obj.offsetParent) {
  87. while (obj.offsetParent) {
  88. curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop);
  89. obj = obj.offsetParent;
  90. }
  91. // IE offsetParent does not include the top-level
  92. if (isIE && obj.parentElement) {
  93. curtop += obj.offsetTop - obj.scrollTop;
  94. }
  95. } else if (obj.y) {
  96. curtop += obj.y;
  97. }
  98. return curtop;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Date object extensions
  102. // ----------------------------------------------------------------------------
  103. (function() {
  104. 'use strict';
  105. Date.prototype.getTwelveHours = function() {
  106. var hours = this.getHours();
  107. if (hours === 0) {
  108. return 12;
  109. }
  110. else {
  111. return hours <= 12 ? hours : hours - 12;
  112. }
  113. };
  114. Date.prototype.getTwoDigitMonth = function() {
  115. return (this.getMonth() < 9) ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1);
  116. };
  117. Date.prototype.getTwoDigitDate = function() {
  118. return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
  119. };
  120. Date.prototype.getTwoDigitTwelveHour = function() {
  121. return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
  122. };
  123. Date.prototype.getTwoDigitHour = function() {
  124. return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
  125. };
  126. Date.prototype.getTwoDigitMinute = function() {
  127. return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
  128. };
  129. Date.prototype.getTwoDigitSecond = function() {
  130. return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
  131. };
  132. Date.prototype.getHourMinute = function() {
  133. return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
  134. };
  135. Date.prototype.getHourMinuteSecond = function() {
  136. return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond();
  137. };
  138. Date.prototype.getFullMonthName = function() {
  139. return typeof window.CalendarNamespace === "undefined"
  140. ? this.getTwoDigitMonth()
  141. : window.CalendarNamespace.monthsOfYear[this.getMonth()];
  142. };
  143. Date.prototype.strftime = function(format) {
  144. var fields = {
  145. B: this.getFullMonthName(),
  146. c: this.toString(),
  147. d: this.getTwoDigitDate(),
  148. H: this.getTwoDigitHour(),
  149. I: this.getTwoDigitTwelveHour(),
  150. m: this.getTwoDigitMonth(),
  151. M: this.getTwoDigitMinute(),
  152. p: (this.getHours() >= 12) ? 'PM' : 'AM',
  153. S: this.getTwoDigitSecond(),
  154. w: '0' + this.getDay(),
  155. x: this.toLocaleDateString(),
  156. X: this.toLocaleTimeString(),
  157. y: ('' + this.getFullYear()).substr(2, 4),
  158. Y: '' + this.getFullYear(),
  159. '%': '%'
  160. };
  161. var result = '', i = 0;
  162. while (i < format.length) {
  163. if (format.charAt(i) === '%') {
  164. result = result + fields[format.charAt(i + 1)];
  165. ++i;
  166. }
  167. else {
  168. result = result + format.charAt(i);
  169. }
  170. ++i;
  171. }
  172. return result;
  173. };
  174. // ----------------------------------------------------------------------------
  175. // String object extensions
  176. // ----------------------------------------------------------------------------
  177. String.prototype.pad_left = function(pad_length, pad_string) {
  178. var new_string = this;
  179. for (var i = 0; new_string.length < pad_length; i++) {
  180. new_string = pad_string + new_string;
  181. }
  182. return new_string;
  183. };
  184. String.prototype.strptime = function(format) {
  185. var split_format = format.split(/[.\-/]/);
  186. var date = this.split(/[.\-/]/);
  187. var i = 0;
  188. var day, month, year;
  189. while (i < split_format.length) {
  190. switch (split_format[i]) {
  191. case "%d":
  192. day = date[i];
  193. break;
  194. case "%m":
  195. month = date[i] - 1;
  196. break;
  197. case "%Y":
  198. year = date[i];
  199. break;
  200. case "%y":
  201. year = date[i];
  202. break;
  203. }
  204. ++i;
  205. }
  206. // Create Date object from UTC since the parsed value is supposed to be
  207. // in UTC, not local time. Also, the calendar uses UTC functions for
  208. // date extraction.
  209. return new Date(Date.UTC(year, month, day));
  210. };
  211. })();
  212. // ----------------------------------------------------------------------------
  213. // Get the computed style for and element
  214. // ----------------------------------------------------------------------------
  215. function getStyle(oElm, strCssRule) {
  216. 'use strict';
  217. var strValue = "";
  218. if(document.defaultView && document.defaultView.getComputedStyle) {
  219. strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
  220. }
  221. else if(oElm.currentStyle) {
  222. strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) {
  223. return p1.toUpperCase();
  224. });
  225. strValue = oElm.currentStyle[strCssRule];
  226. }
  227. return strValue;
  228. }