aboutsummaryrefslogtreecommitdiffstats
path: root/hesla.ts
diff options
context:
space:
mode:
Diffstat (limited to 'hesla.ts')
-rw-r--r--hesla.ts160
1 files changed, 160 insertions, 0 deletions
diff --git a/hesla.ts b/hesla.ts
new file mode 100644
index 0000000..05e7f2f
--- /dev/null
+++ b/hesla.ts
@@ -0,0 +1,160 @@
+import {ActivePage} from "activePage";
+
+function toISODateString(date) {
+ function pad(n) {
+ return n < 10 ? '0' + n : n;
+ }
+
+ return date.getFullYear() + '-' + pad(date.getMonth() + 1) +
+ '-' + pad(date.getDate());
+}
+
+export class Hesla extends ActivePage {
+ public cur_section = null;
+
+ constructor(appName?:string) {
+ super(appName);
+ this.cur_section = null;
+ document.getElementById("czech_nav").addEventListener("click",
+ (evt) => {
+ localStorage.setItem("language", "cs");
+ this.lang_reload();
+ }, false);
+
+ document.getElementById("german_nav").addEventListener("click",
+ (evt) => {
+ localStorage.setItem("language", "de");
+ this.lang_reload();
+ }, false);
+
+ // requires IE 10, Opera 12.10, Safari 7,
+ // FIXME Android browser requires 4.4 and has only
+ // webkitHidden and the event is webkitvisibilitychange
+ if (typeof document.hidden !== "undefined") {
+ document.addEventListener("visibilitychange",
+ (evt) => {
+ if (! document.hidden) {
+ this.today();
+ }
+ }, false);
+ }
+ }
+
+ /**
+ * react to the discovered distance of swipe
+ *
+ * @param distX Number distance in points in direction X
+ * @param distY Number distance in points in direction Y
+ *
+ * There is a preference for the horizontal swipe; if that doesn't
+ * happen (i.e., the distance travelled horizontally is less than
+ * negligible), then the vertical swipe (in either direction) means
+ * jump to today.
+ */
+ handle_move(distX, distY) {
+ var negligible = 100;
+ console.log('distX = ' + distX);
+ console.log('distY = ' + distY);
+
+ if (distX < -negligible) {
+ console.log("swipe left");
+ this.next();
+ }
+ else if (distX > negligible) {
+ console.log("swipe right");
+ this.prev();
+ }
+ }
+
+ // cur_watchword is actually just a virtual property, which is not stored anywhere and its
+ // accessor methods change the value of the cur_section property instead.
+ get cur_watchword() {
+ console.log(`this.cur_section = ${this.cur_section}`);
+ if (!this.cur_section) {
+ this.cur_section = new Date();
+ }
+
+ return this.cur_section;
+ }
+
+ set cur_watchword(value) {
+ // Remove 'visible' style from the currently displayed psalm, if
+ // there is any
+
+ this.cur_section = value;
+
+ // set the parameter directly to avoid call to getter.
+ console.log('Displaying ' + this.cur_section);
+ this.display();
+ }
+
+ /**
+ * Display losungen for given day
+ *
+ * Displays the Losungen for the given date (or today if not defined)
+ */
+ display() {
+ super.display();
+
+ console.log("cur_watchword = " + toISODateString(this.cur_watchword));
+ document.getElementById(toISODateString(this.cur_watchword)).
+ classList.add("visible");
+
+ window.scroll(0, 0);
+ }
+
+ today() {
+ console.log("jump to today!");
+ this.cur_watchword = new Date();
+ this.display();
+ }
+
+ shiftDate(shift) {
+ var cur_date = this.cur_watchword;
+ cur_date.setDate(cur_date.getDate() + shift);
+ this.cur_watchword = cur_date;
+ }
+
+ next() {
+ console.log("Next day!");
+ this.shiftDate(+1);
+ }
+
+ prev() {
+ console.log("Previous day!");
+ this.shiftDate(-1);
+ }
+
+ // Switch langauge
+ /**
+ * Handler for the switch language event
+ *
+ * Switches the displayed langauge page according to the state stored
+ * in the localStorage.langauge variable which contains ISO code for
+ * the chosen language ('de' and 'cs' currently).
+ */
+ lang_reload() {
+ var new_basename = "index.html", new_url = window.location.href,
+ old_basename = window.location.pathname.split("/").slice(-1)[0];
+ console.log('old_basename = ' + old_basename);
+
+ if (localStorage.getItem("language") &&
+ (localStorage.getItem("language") === "de")) {
+ new_basename = "index_de.html";
+ }
+
+ console.log('new_basename = ' + new_basename);
+ if ((old_basename !== new_basename) || (window.location.pathname.length === 0)) {
+ new_url = window.location.href.replace(/(index[-\w]*\.html)?$/,
+ new_basename);
+ }
+
+ if (window.location.href !== new_url) {
+ window.location.assign(new_url);
+ }
+ }
+}
+
+var thisHesla = new Hesla();
+thisHesla.lang_reload();
+thisHesla.display();