/* This is part of the DCC++ EX Project for model railroading and more.
For licence information, please see index.html.
For more information, see us at dcc-ex.com.
storageController.js
Manages the setting storage capabilities
*/
$(document).ready(function(){
// This is displays message about Local storage Support of the Local browser
if (typeof Storage !== "undefined") {
console.log("Your browser supports Local Storage");
} else {
console.log("Sorry! Your browser does not supporting Local Storage");
}
// Opens NEW MAP window with all fields empty
$("#add-map").on("click", function () {
$("#save-fn-map").attr("mode", "new");
$(".fn-heading").html("New Mapping");
showBtnConfig({ mname: "", fnData: fnMasterData });
});
// This will Load buttons on selecting a map from the select box
$("#select-map").change(function () {
selectedval = $(this).val();
if (selectedval != "default") {
data = getStoredMapData(selectedval);
loadButtons(data);
} else {
loadButtons({ mname: "default", fnData: fnMasterData });
}
});
// Opens MAP window with all fields filled from the selected map that allows editing Map
$(document).on("click", "#edit-cur-map", function () {
$("#save-fn-map").attr("mode", "edit");
$(".fn-heading").html("Edit Mapping");
selectedval = $("#cur-map-val").attr("cur-map");
if (selectedval != "Default") {
data = getStoredMapData(selectedval);
showBtnConfig(data);
} else {
alert("Cannot edit Default mapping!");
}
//showBtnConfig();
});
// Closes MAP window on clicking X icon
$("#close-model").on("click", function () {
$("#fnModal").hide();
});
//This will check for Save mode (NEW MAP / EDIT MAP) and delegate the functionality
$("#save-fn-map").on("click", function () {
mode = $(this).attr("mode");
// alert(mode); // debug line
if (mode == "new") {
addNewMap();
} else {
editMap();
}
});
//Allows user to download the selected map in .JSON format
$(document).on("click", "#export-cur-map", function () {
map = $("#cur-map-val").attr("cur-map");
if (map != "default") {
downloadMapData(map);
} else {
alert("Please select Custom map");
}
});
// This remove whole exthrottle app data but with confirmation
$("#wipe-map").on("click", function () {
var r = confirm("Are you sure on deletion?");
if (r == true) {
window.localStorage.removeItem("mapData");
console.log("!!!!!!MAPS WIPED!!!!!!");
loadmaps();
}
});
// This allows user to download whole exthrottle app data
$("#export-all-maps").on("click", function () {
exportMapData();
});
// This allows user to upload previously downloaded Map (JSON format must adhere)
$(document).on("click", "#import-map", function (e) {
e.preventDefault();
$("#map-upload").trigger("click");
});
// This part with above which is responsible for actual file upload for MAP
$("#map-upload").on("change", function (e) {
var file = e.target.files[0];
var field = $(this);
var freader = new FileReader();
freader.onload = function (evt) {
data = JSON.parse(evt.target.result);
setMapData(data);
field.val("");
};
freader.readAsText(file);
});
// This is responsible for Cab upload
$("#cabs-upload").on("change", function (e) {
var file = e.target.files[0];
var field = $(this);
var freader = new FileReader();
freader.onload = function (evt) {
data = JSON.parse(evt.target.result);
importLocoData(data);
field.val("");
};
freader.readAsText(file);
});
// This allows user to upload previously downloaded MAP DATA (JSON format must adhere)
$(document).on("click", "#import-all-maps", function (e) {
e.preventDefault();
//$("#appdata-upload").attr("mode", "mapData");
$("#maps-upload").trigger("click");
});
// This part of above which is responsible for actual file upload for MAP DATA
$("#maps-upload").on("change", function (e) {
var file = e.target.files[0];
var field = $(this);
var freader = new FileReader();
freader.onload = function (evt) {
data = JSON.parse(evt.target.result);
importMapdata(data);
field.val("");
};
freader.readAsText(file);
});
// Set height of throttle container according to functions panel
$(".throttle-container").height($(".functionKeys").first().height());
//Temparory function Shows APP DATA in console
$("#loco-info").on("click", function () {
console.log(getMapData());
});
//Functions for the storage page in settings
$("#backup-app-settings").on("click", function () {
exportAppData();
});
// This allows user to upload previously downloaded APP DATA (JSON format must adhere)
$("#restore-app-settings").on("click", function (e) {
e.preventDefault();
$("#app-upload").trigger("click");
});
// This part of above which is responsible for actual file upload for APP DATA
$("#app-upload").on("change", function (e) {
var file = e.target.files[0];
var field = $(this);
var freader = new FileReader();
freader.onload = function (evt) {
data = JSON.parse(evt.target.result);
importAppdata(data);
field.val("");
};
freader.readAsText(file);
});
$("#wipe-app-settings").on("click", function () {
var r = confirm("Are you sure on deletion?");
if (r == true) {
window.localStorage.removeItem("mapData");
window.localStorage.removeItem("cabList");
window.localStorage.removeItem("userpref");
console.log("!!!!!DATA IS WIPED!!!!!!");
loadmaps();
}
});
});
// Load all maps to select box
function loadmaps(){
$("#select-map").empty();
$("#select-map").append($("").val("default").text("Default"));
getMapData().forEach(map => {
$("#select-map").append($("").val(map.mname).text(map.mname));
})
}
// Load button layout of selected Map
function loadButtons(data){
$("#fn-wrapper").empty();
$.each(data.fnData, function(key, value){
isPressed = value[0] != 0 ? true : false;
btnType = value[1] != 0 ? "press" : "toggle";
if(value[3]==1){
$("#fn-wrapper").append(
"
"
+"
");
}
});
}
// Show the Custom Map fields inside Custom map window while adding and editing a Map.
function showBtnConfig(data){
$("#fnModal").show();
$('#fnModal').css({"top":"7%", "left": "18%"});
$('#fnModal').draggable();
$("#fnModal .fn-modal-content").empty();
$("#fnModal .fn-modal-content").append('