[refactor] switch to moment js

This commit is contained in:
zombieFox 2019-11-19 08:01:57 +00:00
parent da84b2ea10
commit b02e1d3074
6 changed files with 106 additions and 137 deletions

View File

@ -32,7 +32,8 @@ const filename = {
const jsDependencies = [
path.nodeModules + '/html5sortable/dist/html5sortable.min.js',
path.nodeModules + '/invert-color/lib/invert.min.js'
path.nodeModules + '/invert-color/lib/invert.min.js',
path.nodeModules + '/moment/min/moment.min.js'
]
const cssFiles = [

3
package-lock.json generated
View File

@ -6692,8 +6692,7 @@
"version": "2.24.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz",
"integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==",
"dev": true,
"optional": true
"dev": true
},
"ms": {
"version": "2.0.0",

View File

@ -50,6 +50,7 @@
"gulp-watch": "^5.0.1",
"html5sortable": "^0.9.16",
"invert-color": "^2.0.0",
"web-ext": "^3.1.1"
"web-ext": "^3.1.1",
"moment": "^2.24.0"
}
}

View File

@ -1338,6 +1338,7 @@
<!-- vendor -->
<script src="../node_modules/html5sortable/dist/html5sortable.min.js"></script>
<script src="../node_modules/invert-color/lib/invert.min.js"></script>
<script src="../node_modules/moment/min/moment.min.js"></script>
<!-- nightTab -->
<script src="js/helper.js"></script>
<script src="js/data.js"></script>

View File

@ -1,30 +1,5 @@
var clock = (function() {
var _makeTimeObject = function() {
var time = helper.getDateTime();
time.meridiem = "AM";
if (state.get().header.clock.hour24.show) {
if (time.hours < 10) {
time.hours = "0" + time.hours;
};
} else {
if (time.hours > 12) {
time.meridiem = "PM";
time.hours = time.hours - 12;
};
if (time.hours == 0) {
time.hours = 12;
};
};
if (time.minutes < 10) {
time.minutes = "0" + time.minutes;
};
if (time.seconds < 10) {
time.seconds = "0" + time.seconds;
};
return time;
};
var bind = {};
bind.tick = function() {
@ -45,77 +20,76 @@ var clock = (function() {
render.all = function() {
if (state.get().header.clock.seconds.show || state.get().header.clock.minutes.show || state.get().header.clock.hours.show) {
var clock = helper.e(".clock");
var timeObject = _makeTimeObject();
var timeDateNow = moment();
var timeStrings = {
hours: null,
minutes: null,
seconds: null,
meridiem: null
};
var action = {
hours: {
word: function(value) {
if (state.get().header.clock.hour24.show && value < 10) {
return "Zero " + helper.toWords(value);
} else {
return helper.toWords(value);
word: function() {
timeStrings.hours = timeDateNow.hours();
if (!state.get().header.clock.hour24.show && timeDateNow.hours() > 12) {
timeStrings.hours = timeStrings.hours - 12;
};
timeStrings.hours = helper.toWords(timeDateNow.hours());
if (state.get().header.clock.hour24.show && timeDateNow.hours() > 0 && timeDateNow.hours() < 10) {
timeStrings.hours = "Zero " + timeStrings.hours;
};
},
number: function(value) {
return value;
number: function() {
timeStrings.hours = timeDateNow.hours();
if (!state.get().header.clock.hour24.show && timeDateNow.hours() > 12) {
timeStrings.hours = timeStrings.hours - 12;
};
if (state.get().header.clock.hour24.show && timeDateNow.hours() < 10) {
timeStrings.hours = "0" + timeStrings.hours;
};
}
},
minutes: {
word: function(value) {
if (value < 10) {
return "Zero " + helper.toWords(value);
} else {
return helper.toWords(value);
word: function() {
timeStrings.minutes = helper.toWords(timeDateNow.minutes());
if (timeDateNow.minutes() > 0 && timeDateNow.minutes() < 10) {
timeStrings.minutes = "Zero " + timeStrings.minutes;
};
},
number: function(value) {
return value;
number: function() {
timeStrings.minutes = timeDateNow.minutes();
if (timeDateNow.minutes() < 10) {
timeStrings.minutes = "0" + timeStrings.minutes;
};
}
},
seconds: {
word: function(value) {
return helper.toWords(value);
word: function() {
timeStrings.seconds = helper.toWords(timeDateNow.seconds());
if (timeDateNow.seconds() > 0 && timeDateNow.seconds() < 10) {
timeStrings.seconds = "Zero " + timeStrings.seconds;
};
},
number: function(value) {
return value;
number: function() {
timeStrings.seconds = timeDateNow.seconds();
if (timeDateNow.seconds() < 10) {
timeStrings.seconds = "0" + timeStrings.seconds;
};
}
},
meridiem: function() {
timeStrings.meridiem = timeDateNow.format("A");
}
};
timeObject.hours = action.hours[state.get().header.clock.hours.display](timeObject.hours);
timeObject.minutes = action.minutes[state.get().header.clock.minutes.display](timeObject.minutes);
timeObject.seconds = action.seconds[state.get().header.clock.seconds.display](timeObject.seconds);
var elementHours = helper.makeNode({
tag: "span",
text: timeObject.hours,
attr: [{
key: "class",
value: "clock-item clock-hours"
}]
});
var elementMinutes = helper.makeNode({
tag: "span",
text: timeObject.minutes,
attr: [{
key: "class",
value: "clock-item clock-minutes"
}]
});
var elementSeconds = helper.makeNode({
tag: "span",
text: timeObject.seconds,
attr: [{
key: "class",
value: "clock-item clock-seconds"
}]
});
var elementMeridiem = helper.makeNode({
tag: "span",
text: timeObject.meridiem,
attr: [{
key: "class",
value: "clock-item clock-meridiem"
}]
});
action.hours[state.get().header.clock.hours.display]();
action.minutes[state.get().header.clock.minutes.display]();
action.seconds[state.get().header.clock.seconds.display]();
action.meridiem();
var clock = helper.e(".clock");
var elementHours = helper.node("span:" + timeStrings.hours + "|class:clock-item clock-hours");
var elementMinutes = helper.node("span:" + timeStrings.minutes + "|class:clock-item clock-minutes");
var elementSeconds = helper.node("span:" + timeStrings.seconds + "|class:clock-item clock-seconds");
var elementMeridiem = helper.node("span:" + timeStrings.meridiem + "|class:clock-item clock-meridiem");
if (state.get().header.clock.hours.show) {
clock.appendChild(elementHours);
};

View File

@ -1,19 +1,5 @@
var date = (function() {
var _makeTimeObject = function() {
return helper.getDateTime();
};
var _month = function(index) {
var all = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return all[index];
};
var _day = function(index) {
var all = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return all[index];
};
var bind = {};
bind.tick = function() {
@ -34,75 +20,82 @@ var date = (function() {
render.all = function() {
if (state.get().header.date.date.show || state.get().header.date.day.show || state.get().header.date.month.show || state.get().header.date.year.show) {
var date = helper.e(".date");
var dateObject = _makeTimeObject();
var timeDateNow = moment();
var dateStrings = {
day: null,
date: null,
month: null,
year: null
};
var wordOrNumber = {
day: {
word: function(value) {
return _day(value);
word: function() {
dateStrings.day = timeDateNow.format("dddd");
if (state.get().header.date.day.length == "short") {
dateStrings.day = dateStrings.day.substring(0, 3);
console.log(dateStrings.day);
};
},
number: function(value) {
number: function() {
dateStrings.day = timeDateNow.day();
if (state.get().header.date.day.weekStart == "monday") {
if (value == 0) {
value = 7;
if (dateStrings.day == 0) {
dateStrings.day = 7;
};
} else if (state.get().header.date.day.weekStart == "sunday") {
value = value + 1;
dateStrings.day = dateStrings.day + 1;
};
return value;
}
},
date: {
word: function(value) {
word: function() {
if (state.get().header.date.date.ordinal) {
return helper.ordinalWords(helper.toWords(value));
dateStrings.date = helper.ordinalWords(helper.toWords(timeDateNow.date()));
} else {
return helper.toWords(value);
dateStrings.date = helper.toWords(timeDateNow.date());
};
},
number: function(value) {
number: function() {
if (state.get().header.date.date.ordinal) {
return helper.ordinalNumber(value);
dateStrings.date = timeDateNow.format("Do");
} else {
return value;
dateStrings.date = timeDateNow.format("DD");
};
}
},
month: {
word: function(value) {
return _month(value);
word: function() {
dateStrings.month = timeDateNow.format("MMMM");
if (state.get().header.date.month.length == "short") {
dateStrings.month = dateStrings.month.substring(0, 3);
};
},
number: function(value) {
number: function() {
if (state.get().header.date.month.ordinal) {
return helper.ordinalNumber(value + 1);
dateStrings.month = helper.ordinalNumber(timeDateNow.month() + 1);
} else {
return value + 1;
dateStrings.month = timeDateNow.month() + 1;
};
}
},
year: {
word: function(value) {
return helper.toWords(value);
word: function() {
dateStrings.year = helper.toWords(timeDateNow.format("YYYY"));
},
number: function(value) {
return value;
number: function() {
dateStrings.year = timeDateNow.format("YYYY");
}
}
};
dateObject.day = wordOrNumber.day[state.get().header.date.day.display](dateObject.day);
dateObject.date = wordOrNumber.date[state.get().header.date.date.display](dateObject.date);
dateObject.month = wordOrNumber.month[state.get().header.date.month.display](dateObject.month);
dateObject.year = wordOrNumber.year[state.get().header.date.year.display](dateObject.year);
if (state.get().header.date.day.display == "word" && state.get().header.date.day.length == "short") {
dateObject.day = dateObject.day.substring(0, 3);
};
if (state.get().header.date.month.display == "word" && state.get().header.date.month.length == "short") {
dateObject.month = dateObject.month.substring(0, 3);
};
var elementDay = helper.node("span:" + dateObject.day + "|class:date-item date-day");
var elementDate = helper.node("span:" + dateObject.date + "|class:date-item date-date");
var elementMonth = helper.node("span:" + dateObject.month + "|class:date-item date-month");
var elementyear = helper.node("span:" + dateObject.year + "|class:date-item date-year");
wordOrNumber.day[state.get().header.date.day.display]();
wordOrNumber.date[state.get().header.date.date.display]();
wordOrNumber.month[state.get().header.date.month.display]();
wordOrNumber.year[state.get().header.date.year.display]();
var elementDay = helper.node("span:" + dateStrings.day + "|class:date-item date-day");
var elementDate = helper.node("span:" + dateStrings.date + "|class:date-item date-date");
var elementMonth = helper.node("span:" + dateStrings.month + "|class:date-item date-month");
var elementYear = helper.node("span:" + dateStrings.year + "|class:date-item date-year");
var date = helper.e(".date");
if (state.get().header.date.day.show) {
date.appendChild(elementDay);
};
@ -131,7 +124,7 @@ var date = (function() {
};
};
if (state.get().header.date.year.show) {
date.appendChild(elementyear);
date.appendChild(elementYear);
};
if (state.get().header.date.separator.show) {
var separatorCharacter = "/";