Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds buttons to the menu to export / import the settings #2545

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

Ditto-IV100
Copy link

@Ditto-IV100 Ditto-IV100 commented Mar 20, 2018

Description

Adds buttons to the menu to export / import the settings.

Motivation and Context

Because the settings are stored in "Local Storage", they can be lost. Not anymore! You can export the settings for a later import.

By default, the file will have the following name: "RocketMap_DD-MM-YYYY HH:mm.txt".

How Has This Been Tested?

I use it on my own server.

Screenshots (if appropriate):

export-import

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

static/js/map.js Outdated
@@ -153,6 +153,32 @@ function createServiceWorkerReceiver() {
})
}

function download(name, settings) { // eslint-disable-line no-unused-vars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

download -> downloadSettings

static/js/map.js Outdated
a.click()
document.body.removeChild(a)
}
function upload(e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upload -> loadSettings

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(And add a linebreak between this function and the one before.)

static/js/map.js Outdated
console.log(fr.result)
upload(fr.result)
}
fr.readAsText(t.files[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use file.target.files[0].

(And remove the declaration of t.)

@@ -461,6 +461,8 @@ <h3>Location Icon Marker</h3>
</div>
</div>
<div>
<center><button class="settings" onclick="download('RocketMap', JSON.stringify(JSON.stringify(localStorage)))"><i class="fa fa-upload fa-fw"></i>Export Settings</button></center>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of the double JSON.stringify()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need two JSON.stringify()'s to get the quotes escaped (for loadSettings(e)).

@@ -461,6 +461,8 @@ <h3>Location Icon Marker</h3>
</div>
</div>
<div>
<center><button class="settings" onclick="downloadSettings('RocketMap', JSON.stringify(JSON.stringify(localStorage)))"><i class="fa fa-upload fa-fw"></i>Export Settings</button></center>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double JSON.stringify(JSON.stringify( ? Is the double quoting necessary to make it downloadable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to download it, but to deal with the data during import. This code works fine for me. But maybe I have a cleaner solution. I have to test it.

static/js/map.js Outdated
function openSettingsFile(file) { // eslint-disable-line no-unused-vars
var fr = new FileReader()
fr.onload = function () {
console.log(fr.result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove debug.

static/js/map.js Outdated
}

function openSettingsFile(file) { // eslint-disable-line no-unused-vars
var fr = new FileReader()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use more descriptive variable names.

static/js/map.js Outdated
}

function loadSettings(e) {
var t = JSON.parse(JSON.parse(e))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, related to my question about the double stringify.

- use Object.assign instead of Object.keys().forEach
- loadSettings() no longer necessary
- double quoting no longer necessary
@Ditto-IV100
Copy link
Author

Ditto-IV100 commented Mar 21, 2018

@sebastienvercammen
The code is now smaller without double quoting of JSON.stringify and i have update my description.

static/js/map.js Outdated
@@ -153,6 +153,25 @@ function createServiceWorkerReceiver() {
})
}

function downloadSettings(name, settings) { // eslint-disable-line no-unused-vars
var a = document.createElement('a')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one needs a readabillity update -> more descriptive variable name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants