FossilRepo

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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button