Skip to content

Commit

Permalink
Copy.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lenincompres committed Oct 22, 2024
1 parent 30a761d commit 027e769
Show file tree
Hide file tree
Showing 10 changed files with 577 additions and 258 deletions.
4 changes: 2 additions & 2 deletions DOM.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Creates DOM structures from a JS object (structure)
* @author Lenin Compres <lenincompres@gmail.com>
* @version 1.1.2
* @version 1.1.3
* @repository https://github.com/lenincompres/DOM.js
*/

Expand Down Expand Up @@ -630,7 +630,7 @@ class DOM {
static headTags = ["meta", "link", "title", "font", "icon", "image", ...DOM.metaNames, ...DOM.htmlEquivs];
static reserveStations = ["tag", "id", "onready", "ready", "done", "ondone"];
static listeners = ["addevent", "addeventlistener", "eventlistener", "listener", "on"];
static getDocType = str => typeof str === "string" ? new Object({
static getDocType = str => typeof str === "string" ? ({
css: "stylesheet",
sass: "stylesheet/sass",
scss: "stylesheet/scss",
Expand Down
135 changes: 135 additions & 0 deletions classes/Copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
This class return text from the map content given to it.
There are several way copy can be indexed.
map = {
key0: Text in only language,
key1: {
en: "Text in English",
es: "Texto en español",
},
key2: [{
en: "Text in English",
es: "Texto en español",
}, {
en: "Next text in English",
es: "Texto siguiente en español",
}]
}
*/

class Copy {

constructor(map = {}) {
this.map = map;
this.lang = Copy.lang;
this.key = Object.keys(this.map)[0];
this.counter = {};
}

add(key, val) {
if (typeof key !== "string") {
return Object.entries(key).forEach(([k, v]) => this.add(k, v));
}
if (this.map[key]) {
console.error(`Key "${key}" already exists in copy.`);
return "sd";
}
this.map[key] = val;
return this.get(key);
}

// returns the text based on key, array index and language
get(key, i) {
if (key === undefined) {
console.error("No key was passed to the copy.");
};
let val = this.map[key];
if (val === undefined) {
console.error(`Key "${key}" not found in copy.`);
return "";
}
this.key = key;
let lang = this.lang;
if (val[lang]) return Copy.treat(val[lang]);
if (typeof val === "string") return Copy.treat(val);
if (!Array.isArray(val)) {
console.error(`Language "${lang}" not found in copy at "${key}".`);
return Copy.treat(val[Object.keys(val)[0]]);
}
if (i === undefined) i = 0;
val = val[i];
if (val === undefined) return "";
this.counter[key] = i;
if (val[lang]) return Copy.treat(val[lang]);
if (typeof val === "string") return Copy.treat(val);
console.error(`Language "${lang}" not found in copy at "${key}[${i}]".`);
return Copy.treat(val[Object.keys(val)[0]]);
}

static treat(s){
if(!s) return s;
if(Array.isArray(s)) return s.map(i => Copy.treat(i));
//if(s.includes('\n')) return Copy.treat(s.split('\n'));
return s.replaceAll('—', '<em class="em-dash">--</em>');
}

next() {
let i = this.counter[this.key];
i = i === undefined ? 0 : i + 1;
return this.get(this.key, i);
}

static copy = new Copy();

static add(...args) {
return Copy.copy.add(...args);
}

static get(...args) {
return Copy.copy.get(...args);
}

static next() {
return Copy.copy.next();
}

static set lang(val) {
localStorage.setItem('copy-lang', val);
location.reload();
}

static LANG = {
es: {
code: "es",
name: "Español",
},
en: {
code: "en",
name: "English",
},
}

static get lang() {
let lang = "en";//Copy.LANG.EN.code;
if (navigator && navigator.language) {
lang = navigator.language.split("-")[0];
}
let savedLang = localStorage.getItem('copy-lang');;
if (savedLang) {
lang = savedLang;
}
return lang;
}

static getToggleLink(...langs){
return langs.map(lang => ({
display: Copy.lang !== lang.code ? "block" : "none",
text: lang.name,
click: () => Copy.lang = lang.code,
}))
}

}

export default Copy;
2 changes: 1 addition & 1 deletion hermanastra/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ DOM.set({
},
},

aside: sponsorsModel,
//aside: sponsorsModel,

main: {
align: "center",
Expand Down
4 changes: 3 additions & 1 deletion hermanastra/modules/funciones.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const data = [
{
date: new Date("2024", "10", "3"),
place: "Cañave, New York, NY.",
link: "https://www.instagram.com/canavenyc/?hl=en",
free: true,
},
{
date: new Date("2024", "09", "20"),
Expand Down Expand Up @@ -44,7 +46,7 @@ function printDate(d) {
margin: "0.5em",
padding: "0.2em 0.7em",
background: "#fe6",
text: "Tickets",
text: d.free ? "Free" : "Tickets",
ready: blinking
},
}
Expand Down
Loading

0 comments on commit 027e769

Please sign in to comment.