1
0

core.js 5.3 KB

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