core.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Core JavaScript helper functions
  2. 'use strict';
  3. // quickElement(tagType, parentReference [, textInChildNode, attribute, attributeValue ...]);
  4. function quickElement() {
  5. const obj = document.createElement(arguments[0]);
  6. if (arguments[2]) {
  7. const textNode = document.createTextNode(arguments[2]);
  8. obj.appendChild(textNode);
  9. }
  10. const len = arguments.length;
  11. for (let i = 3; i < len; i += 2) {
  12. obj.setAttribute(arguments[i], arguments[i + 1]);
  13. }
  14. arguments[1].appendChild(obj);
  15. return obj;
  16. }
  17. // "a" is reference to an object
  18. function removeChildren(a) {
  19. while (a.hasChildNodes()) {
  20. a.removeChild(a.lastChild);
  21. }
  22. }
  23. // ----------------------------------------------------------------------------
  24. // Find-position functions by PPK
  25. // See https://www.quirksmode.org/js/findpos.html
  26. // ----------------------------------------------------------------------------
  27. function findPosX(obj) {
  28. let curleft = 0;
  29. if (obj.offsetParent) {
  30. while (obj.offsetParent) {
  31. curleft += obj.offsetLeft - obj.scrollLeft;
  32. obj = obj.offsetParent;
  33. }
  34. } else if (obj.x) {
  35. curleft += obj.x;
  36. }
  37. return curleft;
  38. }
  39. function findPosY(obj) {
  40. let curtop = 0;
  41. if (obj.offsetParent) {
  42. while (obj.offsetParent) {
  43. curtop += obj.offsetTop - obj.scrollTop;
  44. obj = obj.offsetParent;
  45. }
  46. } else if (obj.y) {
  47. curtop += obj.y;
  48. }
  49. return curtop;
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Date object extensions
  53. // ----------------------------------------------------------------------------
  54. {
  55. Date.prototype.getTwelveHours = function() {
  56. return this.getHours() % 12 || 12;
  57. };
  58. Date.prototype.getTwoDigitMonth = function() {
  59. return (this.getMonth() < 9) ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1);
  60. };
  61. Date.prototype.getTwoDigitDate = function() {
  62. return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
  63. };
  64. Date.prototype.getTwoDigitTwelveHour = function() {
  65. return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
  66. };
  67. Date.prototype.getTwoDigitHour = function() {
  68. return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
  69. };
  70. Date.prototype.getTwoDigitMinute = function() {
  71. return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
  72. };
  73. Date.prototype.getTwoDigitSecond = function() {
  74. return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
  75. };
  76. Date.prototype.getAbbrevMonthName = function() {
  77. return typeof window.CalendarNamespace === "undefined"
  78. ? this.getTwoDigitMonth()
  79. : window.CalendarNamespace.monthsOfYearAbbrev[this.getMonth()];
  80. };
  81. Date.prototype.getFullMonthName = function() {
  82. return typeof window.CalendarNamespace === "undefined"
  83. ? this.getTwoDigitMonth()
  84. : window.CalendarNamespace.monthsOfYear[this.getMonth()];
  85. };
  86. Date.prototype.strftime = function(format) {
  87. const fields = {
  88. b: this.getAbbrevMonthName(),
  89. B: this.getFullMonthName(),
  90. c: this.toString(),
  91. d: this.getTwoDigitDate(),
  92. H: this.getTwoDigitHour(),
  93. I: this.getTwoDigitTwelveHour(),
  94. m: this.getTwoDigitMonth(),
  95. M: this.getTwoDigitMinute(),
  96. p: (this.getHours() >= 12) ? 'PM' : 'AM',
  97. S: this.getTwoDigitSecond(),
  98. w: '0' + this.getDay(),
  99. x: this.toLocaleDateString(),
  100. X: this.toLocaleTimeString(),
  101. y: ('' + this.getFullYear()).substr(2, 4),
  102. Y: '' + this.getFullYear(),
  103. '%': '%'
  104. };
  105. let result = '', i = 0;
  106. while (i < format.length) {
  107. if (format.charAt(i) === '%') {
  108. result = result + fields[format.charAt(i + 1)];
  109. ++i;
  110. }
  111. else {
  112. result = result + format.charAt(i);
  113. }
  114. ++i;
  115. }
  116. return result;
  117. };
  118. // ----------------------------------------------------------------------------
  119. // String object extensions
  120. // ----------------------------------------------------------------------------
  121. String.prototype.strptime = function(format) {
  122. const split_format = format.split(/[.\-/]/);
  123. const date = this.split(/[.\-/]/);
  124. let i = 0;
  125. let day, month, year;
  126. while (i < split_format.length) {
  127. switch (split_format[i]) {
  128. case "%d":
  129. day = date[i];
  130. break;
  131. case "%m":
  132. month = date[i] - 1;
  133. break;
  134. case "%Y":
  135. year = date[i];
  136. break;
  137. case "%y":
  138. // A %y value in the range of [00, 68] is in the current
  139. // century, while [69, 99] is in the previous century,
  140. // according to the Open Group Specification.
  141. if (parseInt(date[i], 10) >= 69) {
  142. year = date[i];
  143. } else {
  144. year = (new Date(Date.UTC(date[i], 0))).getUTCFullYear() + 100;
  145. }
  146. break;
  147. }
  148. ++i;
  149. }
  150. // Create Date object from UTC since the parsed value is supposed to be
  151. // in UTC, not local time. Also, the calendar uses UTC functions for
  152. // date extraction.
  153. return new Date(Date.UTC(year, month, day));
  154. };
  155. }