core.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. return this.getHours() % 12 || 12;
  72. };
  73. Date.prototype.getTwoDigitMonth = function() {
  74. return (this.getMonth() < 9) ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1);
  75. };
  76. Date.prototype.getTwoDigitDate = function() {
  77. return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
  78. };
  79. Date.prototype.getTwoDigitTwelveHour = function() {
  80. return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
  81. };
  82. Date.prototype.getTwoDigitHour = function() {
  83. return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
  84. };
  85. Date.prototype.getTwoDigitMinute = function() {
  86. return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
  87. };
  88. Date.prototype.getTwoDigitSecond = function() {
  89. return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
  90. };
  91. Date.prototype.getFullMonthName = function() {
  92. return typeof window.CalendarNamespace === "undefined"
  93. ? this.getTwoDigitMonth()
  94. : window.CalendarNamespace.monthsOfYear[this.getMonth()];
  95. };
  96. Date.prototype.strftime = function(format) {
  97. var fields = {
  98. B: this.getFullMonthName(),
  99. c: this.toString(),
  100. d: this.getTwoDigitDate(),
  101. H: this.getTwoDigitHour(),
  102. I: this.getTwoDigitTwelveHour(),
  103. m: this.getTwoDigitMonth(),
  104. M: this.getTwoDigitMinute(),
  105. p: (this.getHours() >= 12) ? 'PM' : 'AM',
  106. S: this.getTwoDigitSecond(),
  107. w: '0' + this.getDay(),
  108. x: this.toLocaleDateString(),
  109. X: this.toLocaleTimeString(),
  110. y: ('' + this.getFullYear()).substr(2, 4),
  111. Y: '' + this.getFullYear(),
  112. '%': '%'
  113. };
  114. var result = '', i = 0;
  115. while (i < format.length) {
  116. if (format.charAt(i) === '%') {
  117. result = result + fields[format.charAt(i + 1)];
  118. ++i;
  119. }
  120. else {
  121. result = result + format.charAt(i);
  122. }
  123. ++i;
  124. }
  125. return result;
  126. };
  127. // ----------------------------------------------------------------------------
  128. // String object extensions
  129. // ----------------------------------------------------------------------------
  130. String.prototype.strptime = function(format) {
  131. var split_format = format.split(/[.\-/]/);
  132. var date = this.split(/[.\-/]/);
  133. var i = 0;
  134. var day, month, year;
  135. while (i < split_format.length) {
  136. switch (split_format[i]) {
  137. case "%d":
  138. day = date[i];
  139. break;
  140. case "%m":
  141. month = date[i] - 1;
  142. break;
  143. case "%Y":
  144. year = date[i];
  145. break;
  146. case "%y":
  147. year = date[i];
  148. break;
  149. }
  150. ++i;
  151. }
  152. // Create Date object from UTC since the parsed value is supposed to be
  153. // in UTC, not local time. Also, the calendar uses UTC functions for
  154. // date extraction.
  155. return new Date(Date.UTC(year, month, day));
  156. };
  157. })();