Skip to content
Permalink
Browse files

src/settings: Move page-specific JS to separate file

  • Loading branch information
GochoMugo committed Mar 17, 2020
1 parent 0c8950f commit 88361fbdbec236687e655396f1263f85eb75e10a
Showing with 75 additions and 65 deletions.
  1. +34 −65 js/main.js
  2. +39 −0 js/settings.js
  3. +2 −0 pages/settings.html
@@ -1,32 +1,13 @@
/* jshint camelcase:false */


(function() {
"use strict";


/**
* Some variables we want to keep up here
*/
var local = "mugo";
var domain = "forfuture.co.ke";
var mail = local + "@" + domain;
// Settings
var settingsConfig = {
forceDarkMode: {
name: "settings:force-dark-mode",
},
};
$(document).ready(function() {
"use strict";

function parseBoolStr(str) {
return str === "0" ? false : true;
}
/** Namespace. */
var mugo = window.gochomugo = {};

function stringifyBool(bool) {
return bool === false ? "0" : "1";
}
var local = "mugo";
var domain = "forfuture.co.ke";
var mail = local + "@" + domain;

$(document).ready(function() {
var $body = $("body");

/**
@@ -41,59 +22,47 @@ $(document).ready(function() {
*/
$(".email").html("<a href='mailto:" + mail+ "'>" + mail+ "</a>");

/**
* Storage.
*/
var storageEnabled = false;
/** Storage. */
try {
localStorage.setItem("_test", "1");
localStorage.getItem("_test");
localStorage.removeItem("_test");
storageEnabled = true;
mugo.storageEnabled = true;
} catch (error) {
/* Do nothing! */
}

/**
* Settings.
*/
if (!storageEnabled) {
$("#settings-alert-storage-disabled").css("display", "block");
}
/** Settings. */
if (mugo.storageEnabled) {
mugo.settingsConfig = {
forceDarkMode: {
name: "settings:force-dark-mode",
},
};

mugo.parseBoolStr = function parseBoolStr(str) {
return str === "0" ? false : true;
};

// Dark mode
var $settingsForceDarkModeCheckbox = $(
"input[name='settings-force-dark-mode']"
);
if (storageEnabled) {
var onChange = function(enabled) {
mugo.localStorageGetBoolItem =
function localStorageGetBoolItem(key) {
return mugo.parseBoolStr(
localStorage.getItem(key) || "0"
);
};

mugo.onDarkModeChanged = function onDarkModeChanged(enabled) {
if (enabled) {
$body.addClass("force-dark-mode");
} else {
$body.removeClass("force-dark-mode");
}
};
$settingsForceDarkModeCheckbox.prop("checked", (function() {
var enabled = parseBoolStr(
localStorage.getItem(settingsConfig.forceDarkMode.name) || "0"
);
onChange(enabled);
return enabled;
})());
$settingsForceDarkModeCheckbox.on("change", function(evt) {
var enabled = evt.target.checked;
onChange(enabled);
if (enabled) {
localStorage.setItem(
settingsConfig.forceDarkMode.name, stringifyBool(enabled)
);
} else {
localStorage.removeItem(settingsConfig.forceDarkMode.name);
}
});
} else {
$settingsForceDarkModeCheckbox.attr("disabled", true);

mugo.onDarkModeChanged(
mugo.localStorageGetBoolItem(
mugo.settingsConfig.forceDarkMode.name
)
);
}
});

})();
@@ -0,0 +1,39 @@
$(document).ready(function() {
"use strict";

/** Namespace. */
var mugo = window.gochomugo;

if (!mugo.storageEnabled) {
$("#settings-alert-storage-disabled").css("display", "block");
}

// Dark mode
var $settingsForceDarkModeCheckbox = $(
"input[name='settings-force-dark-mode']"
);
if (mugo.storageEnabled) {
var settingsKey = mugo.settingsConfig.forceDarkMode.name;

$settingsForceDarkModeCheckbox.prop(
"checked",
mugo.localStorageGetBoolItem(settingsKey)
);

$settingsForceDarkModeCheckbox.on("change", function(evt) {
var enabled = evt.target.checked;
mugo.onDarkModeChanged(enabled);
if (enabled) {
localStorage.setItem(settingsKey, stringifyBool(enabled));
} else {
localStorage.removeItem(settingsKey);
}
});
} else {
$settingsForceDarkModeCheckbox.attr("disabled", true);
}

function stringifyBool(bool) {
return bool === false ? "0" : "1";
}
});
@@ -4,6 +4,8 @@
title: settings
css:
- /css/settings.css
scripts:
- /js/settings.js
---

<div class="container-fluid">

0 comments on commit 88361fb

Please sign in to comment.
You can’t perform that action at this time.