From 366c2d9e2af76c8158b3b2dfc235d95512baf420 Mon Sep 17 00:00:00 2001 From: unsettledgames <47360416+unsettledgames@users.noreply.github.com> Date: Fri, 23 Jul 2021 18:45:23 +0200 Subject: [PATCH] Turned _settings.js into an IIFE --- js/LayerList.js | 2 -- js/Settings.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++ js/_settings.js | 51 ------------------------------------- js/pixel-editor.js | 2 +- 4 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 js/Settings.js delete mode 100644 js/_settings.js diff --git a/js/LayerList.js b/js/LayerList.js index dc17781..0c30fab 100644 --- a/js/LayerList.js +++ b/js/LayerList.js @@ -264,8 +264,6 @@ const LayerList = (() => { closeOptionsMenu(); } - // TODO: Can't select the first layer - function merge(saveHistory = true) { // Saving the layer that should be merged let toMerge = currentLayer; diff --git a/js/Settings.js b/js/Settings.js new file mode 100644 index 0000000..ba8a13a --- /dev/null +++ b/js/Settings.js @@ -0,0 +1,63 @@ +// REFACTOR: convert to IIFE + +const Settings = (() => { + let settings; + let settingsFromCookie; + + //on clicking the save button in the settings dialog + Events.on('click', 'save-settings', saveSettings); + + init(); + + function init() { + if (!Cookies.enabled) { + document.getElementById('cookies-disabled-warning').style.display = 'block'; + } + settingsFromCookie = Cookies.get('pixelEditorSettings'); + + if(!settingsFromCookie) { + console.log('settings cookie not found'); + + settings = { + switchToChangedColor: true, + enableDynamicCursorOutline: true, //unused - performance + enableBrushPreview: true, //unused - performance + enableEyedropperPreview: true, //unused - performance + numberOfHistoryStates: 20, + maxColorsOnImportedImage: 128, + pixelGridColour: '#000000' + }; + } + else{ + console.log('settings cookie found'); + console.log(settingsFromCookie); + + settings = JSON.parse(settingsFromCookie); + } + } + + function saveSettings() { + //check if values are valid + if (isNaN(Util.getValue('setting-numberOfHistoryStates'))) { + alert('Invalid value for numberOfHistoryStates'); + return; + } + + //save new settings to settings object + settings.numberOfHistoryStates = Util.getValue('setting-numberOfHistoryStates'); + settings.pixelGridColour = Util.getValue('setting-pixelGridColour'); + // Filling pixel grid again if colour changed + fillPixelGrid(); + + //save settings object to cookie + var cookieValue = JSON.stringify(settings); + Cookies.set('pixelEditorSettings', cookieValue, { expires: Infinity }); + + //close window + Dialogue.closeDialogue(); + } + + return { + + } +})(); \ No newline at end of file diff --git a/js/_settings.js b/js/_settings.js deleted file mode 100644 index 137e77a..0000000 --- a/js/_settings.js +++ /dev/null @@ -1,51 +0,0 @@ -// REFACTOR: convert to IIFE -var settings; - -if (!Cookies.enabled) { - document.getElementById('cookies-disabled-warning').style.display = 'block'; -} - -//try to load settings from cookie -var settingsFromCookie = Cookies.get('pixelEditorSettings'); -if(!settingsFromCookie) { - console.log('settings cookie not found'); - settings = { - switchToChangedColor: true, - enableDynamicCursorOutline: true, //unused - performance - enableBrushPreview: true, //unused - performance - enableEyedropperPreview: true, //unused - performance - numberOfHistoryStates: 20, - maxColorsOnImportedImage: 128, - pixelGridColour: '#000000' - }; -} -else{ - console.log('settings cookie found'); - console.log(settingsFromCookie); - var settings = JSON.parse(settingsFromCookie); -} -console.log(settings); - -//on clicking the save button in the settings dialog -Events.on('click', 'save-settings', saveSettings); - -function saveSettings() { - //check if values are valid - if (isNaN(Util.getValue('setting-numberOfHistoryStates'))) { - alert('Invalid value for numberOfHistoryStates'); - return; - } - - //save new settings to settings object - settings.numberOfHistoryStates = Util.getValue('setting-numberOfHistoryStates'); - settings.pixelGridColour = Util.getValue('setting-pixelGridColour'); - // Filling pixel grid again if colour changed - fillPixelGrid(); - - //save settings object to cookie - var cookieValue = JSON.stringify(settings); - Cookies.set('pixelEditorSettings', cookieValue, { expires: Infinity }); - - //close window - Dialogue.closeDialogue(); -} \ No newline at end of file diff --git a/js/pixel-editor.js b/js/pixel-editor.js index 111ec89..0148397 100644 --- a/js/pixel-editor.js +++ b/js/pixel-editor.js @@ -12,7 +12,7 @@ /**init**/ //=include _consts.js -//=include _settings.js +//=include Settings.js //=include LayerList.js //=include Layer.js //=include Startup.js