[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 = [ const jsDependencies = [
path.nodeModules + '/html5sortable/dist/html5sortable.min.js', 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 = [ const cssFiles = [

3
package-lock.json generated
View File

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

View File

@ -50,6 +50,7 @@
"gulp-watch": "^5.0.1", "gulp-watch": "^5.0.1",
"html5sortable": "^0.9.16", "html5sortable": "^0.9.16",
"invert-color": "^2.0.0", "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 --> <!-- vendor -->
<script src="../node_modules/html5sortable/dist/html5sortable.min.js"></script> <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/invert-color/lib/invert.min.js"></script>
<script src="../node_modules/moment/min/moment.min.js"></script>
<!-- nightTab --> <!-- nightTab -->
<script src="js/helper.js"></script> <script src="js/helper.js"></script>
<script src="js/data.js"></script> <script src="js/data.js"></script>

View File

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

View File

@ -1,19 +1,5 @@
var date = (function() { 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 = {}; var bind = {};
bind.tick = function() { bind.tick = function() {
@ -34,75 +20,82 @@ var date = (function() {
render.all = 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) { 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 timeDateNow = moment();
var dateObject = _makeTimeObject(); var dateStrings = {
day: null,
date: null,
month: null,
year: null
};
var wordOrNumber = { var wordOrNumber = {
day: { day: {
word: function(value) { word: function() {
return _day(value); 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 (state.get().header.date.day.weekStart == "monday") {
if (value == 0) { if (dateStrings.day == 0) {
value = 7; dateStrings.day = 7;
}; };
} else if (state.get().header.date.day.weekStart == "sunday") { } else if (state.get().header.date.day.weekStart == "sunday") {
value = value + 1; dateStrings.day = dateStrings.day + 1;
}; };
return value;
} }
}, },
date: { date: {
word: function(value) { word: function() {
if (state.get().header.date.date.ordinal) { if (state.get().header.date.date.ordinal) {
return helper.ordinalWords(helper.toWords(value)); dateStrings.date = helper.ordinalWords(helper.toWords(timeDateNow.date()));
} else { } else {
return helper.toWords(value); dateStrings.date = helper.toWords(timeDateNow.date());
}; };
}, },
number: function(value) { number: function() {
if (state.get().header.date.date.ordinal) { if (state.get().header.date.date.ordinal) {
return helper.ordinalNumber(value); dateStrings.date = timeDateNow.format("Do");
} else { } else {
return value; dateStrings.date = timeDateNow.format("DD");
}; };
} }
}, },
month: { month: {
word: function(value) { word: function() {
return _month(value); 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) { if (state.get().header.date.month.ordinal) {
return helper.ordinalNumber(value + 1); dateStrings.month = helper.ordinalNumber(timeDateNow.month() + 1);
} else { } else {
return value + 1; dateStrings.month = timeDateNow.month() + 1;
}; };
} }
}, },
year: { year: {
word: function(value) { word: function() {
return helper.toWords(value); dateStrings.year = helper.toWords(timeDateNow.format("YYYY"));
}, },
number: function(value) { number: function() {
return value; dateStrings.year = timeDateNow.format("YYYY");
} }
} }
}; };
dateObject.day = wordOrNumber.day[state.get().header.date.day.display](dateObject.day); wordOrNumber.day[state.get().header.date.day.display]();
dateObject.date = wordOrNumber.date[state.get().header.date.date.display](dateObject.date); wordOrNumber.date[state.get().header.date.date.display]();
dateObject.month = wordOrNumber.month[state.get().header.date.month.display](dateObject.month); wordOrNumber.month[state.get().header.date.month.display]();
dateObject.year = wordOrNumber.year[state.get().header.date.year.display](dateObject.year); wordOrNumber.year[state.get().header.date.year.display]();
if (state.get().header.date.day.display == "word" && state.get().header.date.day.length == "short") { var elementDay = helper.node("span:" + dateStrings.day + "|class:date-item date-day");
dateObject.day = dateObject.day.substring(0, 3); var elementDate = helper.node("span:" + dateStrings.date + "|class:date-item date-date");
}; var elementMonth = helper.node("span:" + dateStrings.month + "|class:date-item date-month");
if (state.get().header.date.month.display == "word" && state.get().header.date.month.length == "short") { var elementYear = helper.node("span:" + dateStrings.year + "|class:date-item date-year");
dateObject.month = dateObject.month.substring(0, 3); var date = helper.e(".date");
};
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");
if (state.get().header.date.day.show) { if (state.get().header.date.day.show) {
date.appendChild(elementDay); date.appendChild(elementDay);
}; };
@ -131,7 +124,7 @@ var date = (function() {
}; };
}; };
if (state.get().header.date.year.show) { if (state.get().header.date.year.show) {
date.appendChild(elementyear); date.appendChild(elementYear);
}; };
if (state.get().header.date.separator.show) { if (state.get().header.date.separator.show) {
var separatorCharacter = "/"; var separatorCharacter = "/";