Skip to content

Commit

Permalink
Settings class
Browse files Browse the repository at this point in the history
  • Loading branch information
McBen committed Feb 11, 2018
1 parent c53acc0 commit 36996d8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
5 changes: 4 additions & 1 deletion code/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ function boot() {
popupAnchor: new L.Point(1, -34),
}});


Settings.init();

window.extractFromStock();
window.setupIdle();
window.setupTaphold();
Expand All @@ -323,6 +326,7 @@ function boot() {
window.setupLayerChooserSelectOne();
window.setupLayerChooserStatusRecorder();


// read here ONCE, so the URL is only evaluated one time after the
// necessary data has been loaded.
urlPortalLL = getURLParam('pll');
Expand All @@ -336,7 +340,6 @@ function boot() {

window.setupPlugins();

// static Plugins
RegionScoreboard.setup();
Redeem.setup();
Menu.setup();
Expand Down
8 changes: 5 additions & 3 deletions code/menu.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
Menu= (function () {

// TODO: accesskey
// maybe use: https://github.com/jeresig/jquery.hotkeys

/*
addMenu({
name: 'View/Example'
Expand Down Expand Up @@ -154,19 +151,24 @@ Menu= (function () {
addMenu({name: 'Help/About IITC', onclick: window.aboutIITC });
addMenu({name: 'View/Zoom Control', onclick: toggleZoomControl, isToggle: true, checked: true});
addMenu({name: 'View/Toolbox', onclick: toggleToolbox, isToggle: true, checked: true});

if (Settings.get('iitc','hide_zoom',false)) toggleZoomControl();
if (Settings.get('iitc','hide_toolbox',false)) toggleToolbox();
}


function toggleZoomControl() {
let $ctrl = $('.leaflet-control-zoom');
$ctrl.toggle();
setChecked('View/Zoom Control', $ctrl.is(':visible'));
Settings.set('iitc','hide_zoom',!$ctrl.is(':visible'));
}

function toggleToolbox() {
let $ctrl = $('#toolbox');
$ctrl.toggle();
setChecked('View/Toolbox', $ctrl.is(':visible'));
Settings.set('iitc','hide_toolbox',!$ctrl.is(':visible'));
}

function setup() {
Expand Down
69 changes: 69 additions & 0 deletions code/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Manage settings
**/
Settings = (function () {

let storage;
let date = Date.now();


function get(group, key, defaultValues) {
storage[group] = storage[group] || {};
storage[group]._lastAccess = date;
return (storage[group][key] || defaultValues);
}


function set(group, key, value) {
storage[group] = storage[group] || {};
storage[group]._lastAccess=date;
storage[group][key] = value;
}


function getGroupRef(group, defaults) {

storage[group] = storage[group] || {};
if (defaults) {
for (let key in defaults) {
storage[group][key] = storage[group][key] || defaults[key];
}
}
storage[group]._lastAccess=date;
return storage[group];
}



function init() {
let store = localStorage.getItem('iitc_settings');
storage = JSON.parse(store || '{}');

$(window).on('beforeunload',save);
}


function save() {
cleanUp();
localStorage.setItem('iitc_settings', JSON.stringify(storage));
}

function cleanUp() {
let old = date - 1000*60*60*24*7; // 1Week
for (let group in storage) {
if (storage[group]._lastAccess<old) {
delete storage[group];
}
}
}


return {
init: init,
get: get,
set: set,
getGroupRef: getGroupRef
};


})();

0 comments on commit 36996d8

Please sign in to comment.