core.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // quickElement(tagType, parentReference [, textInChildNode, attribute, attributeValue ...]);
  6. function quickElement() {
  7. 'use strict';
  8. var obj = document.createElement(arguments[0]);
  9. if (arguments[2]) {
  10. var textNode = document.createTextNode(arguments[2]);
  11. obj.appendChild(textNode);
  12. }
  13. var len = arguments.length;
  14. for (var i = 3; i < len; i += 2) {
  15. obj.setAttribute(arguments[i], arguments[i + 1]);
  16. }
  17. arguments[1].appendChild(obj);
  18. return obj;
  19. }
  20. // "a" is reference to an object
  21. function removeChildren(a) {
  22. 'use strict';
  23. while (a.hasChildNodes()) {
  24. a.removeChild(a.lastChild);
  25. }
  26. }
  27. // ----------------------------------------------------------------------------
  28. // Find-position functions by PPK
  29. // See https://www.quirksmode.org/js/findpos.html
  30. // ----------------------------------------------------------------------------
  31. function findPosX(obj) {
  32. 'use strict';
  33. var curleft = 0;
  34. if (obj.offsetParent) {
  35. while (obj.offsetParent) {
  36. curleft += obj.offsetLeft - ((isOpera) ? 0 : obj.scrollLeft);
  37. obj = obj.offsetParent;
  38. }
  39. // IE offsetParent does not include the top-level
  40. if (isIE && obj.parentElement) {
  41. curleft += obj.offsetLeft - obj.scrollLeft;
  42. }
  43. } else if (obj.x) {
  44. curleft += obj.x;
  45. }
  46. return curleft;
  47. }
  48. function findPosY(obj) {
  49. 'use strict';
  50. var curtop = 0;
  51. if (obj.offsetParent) {
  52. while (obj.offsetParent) {
  53. curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop);
  54. obj = obj.offsetParent;
  55. }
  56. // IE offsetParent does not include the top-level
  57. if (isIE && obj.parentElement) {
  58. curtop += obj.offsetTop - obj.scrollTop;
  59. }
  60. } else if (obj.y) {
  61. curtop += obj.y;
  62. }
  63. return curtop;
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Date object extensions
  67. // ----------------------------------------------------------------------------
  68. (function() {
  69. 'use strict';
  70. Date.prototype.getTwelveHours = function() {
  71. var hours = this.getHours();
  72. if (hours === 0) {
  73. return 12;
  74. }
  75. else {
  76. return hours <= 12 ? hours : hours - 12;
  77. }
  78. };
  79. Date.prototype.getTwoDigitMonth = function() {
  80. return (this.getMonth() < 9) ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1);
  81. };
  82. Date.prototype.getTwoDigitDate = function() {
  83. return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
  84. };
  85. Date.prototype.getTwoDigitTwelveHour = function() {
  86. return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
  87. };
  88. Date.prototype.getTwoDigitHour = function() {
  89. return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
  90. };
  91. Date.prototype.getTwoDigitMinute = function() {
  92. return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
  93. };
  94. Date.prototype.getTwoDigitSecond = function() {
  95. return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
  96. };
  97. Date.prototype.getHourMinute = function() {
  98. return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
  99. };
  100. Date.prototype.getHourMinuteSecond = function() {
  101. return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond();
  102. };
  103. Date.prototype.getFullMonthName = function() {
  104. return typeof window.CalendarNamespace === "undefined"
  105. ? this.getTwoDigitMonth()
  106. : window.CalendarNamespace.monthsOfYear[this.getMonth()];
  107. };
  108. Date.prototype.strftime = function(format) {
  109. var fields = {
  110. B: this.getFullMonthName(),
  111. c: this.toString(),
  112. d: this.getTwoDigitDate(),
  113. H: this.getTwoDigitHour(),
  114. I: this.getTwoDigitTwelveHour(),
  115. m: this.getTwoDigitMonth(),
  116. M: this.getTwoDigitMinute(),
  117. p: (this.getHours() >= 12) ? 'PM' : 'AM',
  118. S: this.getTwoDigitSecond(),
  119. w: '0' + this.getDay(),
  120. x: this.toLocaleDateString(),
  121. X: this.toLocaleTimeString(),
  122. y: ('' + this.getFullYear()).substr(2, 4),
  123. Y: '' + this.getFullYear(),
  124. '%': '%'
  125. };
  126. var result = '', i = 0;
  127. while (i < format.length) {
  128. if (format.charAt(i) === '%') {
  129. result = result + fields[format.charAt(i + 1)];
  130. ++i;
  131. }
  132. else {
  133. result = result + format.charAt(i);
  134. }
  135. ++i;
  136. }
  137. return result;
  138. };
  139. // ----------------------------------------------------------------------------
  140. // String object extensions
  141. // ----------------------------------------------------------------------------
  142. String.prototype.pad_left = function(pad_length, pad_string) {
  143. var new_string = this;
  144. for (var i = 0; new_string.length < pad_length; i++) {
  145. new_string = pad_string + new_string;
  146. }
  147. return new_string;
  148. };
  149. String.prototype.strptime = function(format) {
  150. var split_format = format.split(/[.\-/]/);
  151. var date = this.split(/[.\-/]/);
  152. var i = 0;
  153. var day, month, year;
  154. while (i < split_format.length) {
  155. switch (split_format[i]) {
  156. case "%d":
  157. day = date[i];
  158. break;
  159. case "%m":
  160. month = date[i] - 1;
  161. break;
  162. case "%Y":
  163. year = date[i];
  164. break;
  165. case "%y":
  166. year = date[i];
  167. break;
  168. }
  169. ++i;
  170. }
  171. // Create Date object from UTC since the parsed value is supposed to be
  172. // in UTC, not local time. Also, the calendar uses UTC functions for
  173. // date extraction.
  174. return new Date(Date.UTC(year, month, day));
  175. };
  176. })();
  177. // ----------------------------------------------------------------------------
  178. // Get the computed style for and element
  179. // ----------------------------------------------------------------------------
  180. function getStyle(oElm, strCssRule) {
  181. 'use strict';
  182. var strValue = "";
  183. if(document.defaultView && document.defaultView.getComputedStyle) {
  184. strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
  185. }
  186. else if(oElm.currentStyle) {
  187. strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) {
  188. return p1.toUpperCase();
  189. });
  190. strValue = oElm.currentStyle[strCssRule];
  191. }
  192. return strValue;
  193. }