sliding text - refactored common en formatting functions into common module

pull/719/head
adrian w kirk 2021-04-11 15:18:30 +01:00
parent 30a5fe6656
commit fb993f94d1
8 changed files with 88 additions and 100 deletions

View File

@ -226,10 +226,11 @@
"storage": [
{"name":"slidingtext.app.js","url":"slidingtext.js"},
{"name":"slidingtext.img","url":"slidingtext-icon.js","evaluate":true},
{"name":"slidingtext.local.en.js","url":"slidingtext.local.en.js"},
{"name":"slidingtext.local.en2.js","url":"slidingtext.local.en2.js"},
{"name":"slidingtext.local.fr.js","url":"slidingtext.local.fr.js"},
{"name":"slidingtext.local.jp.js","url":"slidingtext.local.jp.js"},
{"name":"slidingtext.locale.en.js","url":"slidingtext.locale.en.js"},
{"name":"slidingtext.locale.en2.js","url":"slidingtext.locale.en2.js"},
{"name":"slidingtext.utils.en.js","url":"slidingtext.utils.en.js"},
{"name":"slidingtext.locale.fr.js","url":"slidingtext.locale.fr.js"},
{"name":"slidingtext.locale.jp.js","url":"slidingtext.locale.jp.js"},
{"name":"slidingtext.dtfmt.js","url":"slidingtext.dtfmt.js"}
]
},

View File

@ -246,7 +246,7 @@ let row_displays = [
function nextColorTheme(){
//console.log("next color theme");
color_scheme_index += 1;
if(color_scheme_index > row_displays.length){
if(color_scheme_index >= row_displays.length){
color_scheme_index = 0;
}
var color_scheme = color_schemes[color_scheme_index];
@ -298,7 +298,7 @@ if(locales == null || locales.length == 0){
let date_formatters = [];
for(var i=0; i< locales.length; i++){
console.log("loading locale:" + locales[i]);
var Formatter = require("slidingtext.local." + locales[i] + ".js");
var Formatter = require("slidingtext.locale." + locales[i] + ".js");
date_formatters.push(new Formatter());
}
@ -382,7 +382,7 @@ function draw_clock(){
}
// If the dateformatter has not returned enough
// rows then treat the reamining rows as empty
for (j = i; j < row_displays.length; j++) {
for (var j = i; j < row_displays.length; j++) {
display = row_displays[j];
//console.log(i + "->''(empty)");
display_row(display,'');
@ -392,7 +392,12 @@ function draw_clock(){
}
function display_row(display,txt){
if(display.txt == ''){
if(display == null) {
console.log("no display for text:" + txt)
return;
}
if(display.txt == null || display.txt == ''){
if(txt != '') {
command_stack_high_priority.unshift(
function () {

View File

@ -1,74 +0,0 @@
var DateFormatter = require("slidingtext.dtfmt.js");
const numberStr = ["ZERO","ONE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN","EIGHT", "NINE", "TEN",
"ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN",
"FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN",
"NINETEEN", "TWENTY"];
const tensStr = ["ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY",
"FIFTY"];
function hoursToText(hours){
hours = hours % 12;
if(hours == 0){
hours = 12;
}
return numberStr[hours];
}
function numberToText(value){
var word1 = '';
var word2 = '';
if(value > 20){
var tens = (value / 10 | 0);
word1 = tensStr[tens];
var remainder = value - tens * 10;
if(remainder > 0){
word2 = numberStr[remainder];
}
} else if(value > 0) {
word1 = numberStr[value];
}
return [word1,word2];
}
class EnglishTraditionalDateFormatter extends DateFormatter {
constructor() {
super();
}
name(){return "English Traditional";}
formatDate(date){
var mins = date.getMinutes();
var hourOfDay = date.getHours();
if(mins > 30){
hourOfDay += 1;
}
var hours = hoursToText(hourOfDay);
// Deal with the special times first
if(mins == 0){
return [hours,"", "O'","CLOCK"];
} else if(mins == 30){
return ["","HALF", "PAST", "", hours];
} else if(mins == 15){
return ["","QUARTER", "PAST", "", hours];
} else if(mins == 45) {
return ["", "QUARTER", "TO", "", hours];
}
var mins_txt;
var from_to;
if(mins > 30){
from_to = "TO";
mins_txt = numberToText(60-mins);
} else {
from_to = "PAST";
mins_txt = numberToText(mins);
}
if(mins_txt[1] != '') {
return ['', mins_txt[0], mins_txt[1], from_to, hours];
} else {
return ['', mins_txt[0], from_to, '', hours];
}
}
}
module.exports = EnglishTraditionalDateFormatter;

View File

@ -0,0 +1,15 @@
var DateFormatter = require("slidingtext.dtfmt.js");
const hoursToText = require("slidingtext.utils.en.js").hoursToText;
const numberToText = require("slidingtext.utils.en.js").numberToText;
class EnglishDateFormatter extends DateFormatter {
constructor() { super();}
name(){return "English";}
formatDate(date){
var hours_txt = hoursToText(date.getHours());
var mins_txt = numberToText(date.getMinutes());
return [hours_txt,mins_txt[0],mins_txt[1]];
}
}
module.exports = EnglishDateFormatter;

View File

@ -0,0 +1,53 @@
var DateFormatter = require("slidingtext.dtfmt.js");
const hoursToText = require("slidingtext.utils.en.js").hoursToText;
const numberToText = require("slidingtext.utils.en.js").numberToText;
class EnglishTraditionalDateFormatter extends DateFormatter {
constructor() {
super();
}
name(){return "English (Traditional)";}
formatDate(date){
var mins = date.getMinutes();
var hourOfDay = date.getHours();
if(mins > 30){
hourOfDay += 1;
}
var hours = hoursToText(hourOfDay);
// Deal with the special times first
if(mins == 0){
return [hours,"", "O'","CLOCK"];
} else if(mins == 30){
return ["","HALF", "PAST", "", hours];
} else if(mins == 15){
return ["","QUARTER", "PAST", "", hours];
} else if(mins == 45) {
return ["", "QUARTER", "TO", "", hours];
}
var mins_txt;
var from_to;
var mins_value;
if(mins > 30){
mins_value = 60-mins;
from_to = "TO";
mins_txt = numberToText(mins_value);
} else {
mins_value = mins;
from_to = "PAST";
mins_txt = numberToText(mins_value);
}
if(mins_txt[1] != '') {
return ['', mins_txt[0], mins_txt[1], from_to, hours];
} else {
if(mins_value % 5 == 0) {
return ['', mins_txt[0], from_to, '', hours];
} else if(mins_value == 1){
return ['', mins_txt[0], 'MINUTE', from_to, hours];
} else {
return ['', mins_txt[0], 'MINUTES', from_to, hours];
}
}
}
}
module.exports = EnglishTraditionalDateFormatter;

View File

@ -28,9 +28,8 @@ function frenchHeures(hours){
}
}
class FrenchDateFormatter {
constructor() {
}
class FrenchDateFormatter extends DateFormatter {
constructor() { super(); }
name(){return "French";}
formatDate(date){
var hours = frenchHoursToText(date.getHours());

View File

@ -1,5 +1,3 @@
var DateFormatter = require("slidingtext.dtfmt.js");
const numberStr = ["ZERO","ONE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN","EIGHT", "NINE", "TEN",
"ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN",
@ -8,7 +6,7 @@ const numberStr = ["ZERO","ONE", "TWO", "THREE", "FOUR", "FIVE",
const tensStr = ["ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY",
"FIFTY"];
function hoursToText(hours){
const hoursToText = (hours)=>{
hours = hours % 12;
if(hours == 0){
hours = 12;
@ -16,7 +14,7 @@ function hoursToText(hours){
return numberStr[hours];
}
function numberToText(value){
const numberToText = (value)=> {
var word1 = '';
var word2 = '';
if(value > 20){
@ -32,14 +30,5 @@ function numberToText(value){
return [word1,word2];
}
class EnglishDateFormatter extends DateFormatter {
constructor() { super();}
name(){return "English";}
formatDate(date){
var hours_txt = hoursToText(date.getHours());
var mins_txt = numberToText(date.getMinutes());
return [hours_txt,mins_txt[0],mins_txt[1]];
}
}
module.exports = EnglishDateFormatter;
exports.hoursToText = hoursToText;
exports.numberToText = numberToText;