Simplify import/export of settings.

Also clear all stored data before importing.
This commit is contained in:
Mayhem 2014-02-24 23:56:30 +01:00
parent c6fc3428aa
commit ac8e05dd22
2 changed files with 16 additions and 24 deletions

View File

@ -1,7 +1,4 @@
<div class="imp-exp"> <button class="export">Export Settings</button>
<button class="export">Export Settings</button> <button class="import">Import Settings</button>
<button class="import">Import Settings</button> <button class="reset">Reset Settings</button>
<button class="reset">Reset Settings</button> <input type="file" hidden>
<input type="file" hidden>
</div>
<p class="imp-exp-result"></p>

View File

@ -138,34 +138,29 @@ Settings =
$.get Conf, (Conf) -> $.get Conf, (Conf) ->
# XXX don't export archives. # XXX don't export archives.
delete Conf['archives'] delete Conf['archives']
Settings.downloadExport {version: g.VERSION, date: Date.now(), Conf} Settings.downloadExport 'Settings', {version: g.VERSION, date: Date.now(), Conf}
downloadExport: (data) -> downloadExport: (title, data) ->
a = $.el 'a', a = $.el 'a',
download: "<%= meta.name %> v#{g.VERSION}-#{data.date}.json" download: "<%= meta.name %> v#{g.VERSION} #{title}.#{data.date}.json"
href: "data:application/json;base64,#{btoa unescape encodeURIComponent JSON.stringify data, null, 2}" href: "data:application/json;base64,#{btoa unescape encodeURIComponent JSON.stringify data, null, 2}"
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>$.add d.body, a<% } %>
p = $ '.imp-exp-result', Settings.dialog
$.rmAll p
$.add p, a
<% } %>
a.click() a.click()
<% if (type === 'userscript') { %>$.rm a<% } %>
import: -> import: ->
$('input', @parentNode).click() $('input[type=file]', @parentNode).click()
onImport: -> onImport: ->
return unless file = @files[0] return unless file = @files[0]
output = @parentNode.nextElementSibling return unless confirm 'Your current settings will be entirely overwritten, are you sure?'
unless confirm 'Your current settings will be entirely overwritten, are you sure?'
output.textContent = 'Import aborted.'
return
reader = new FileReader() reader = new FileReader()
reader.onload = (e) -> reader.onload = (e) ->
try try
Settings.loadSettings JSON.parse e.target.result Settings.loadSettings JSON.parse e.target.result
if confirm 'Import successful. Reload now?'
window.location.reload()
catch err catch err
output.textContent = 'Import failed due to an error.' alert 'Import failed due to an error.'
c.error err.stack c.error err.stack
return
if confirm 'Import successful. Reload now?'
window.location.reload()
reader.readAsText file reader.readAsText file
loadSettings: (data) -> loadSettings: (data) ->
version = data.version.split '.' version = data.version.split '.'
@ -243,7 +238,7 @@ Settings =
if data.Conf['WatchedThreads'] if data.Conf['WatchedThreads']
data.Conf['watchedThreads'] = boards: ThreadWatcher.convert data.Conf['WatchedThreads'] data.Conf['watchedThreads'] = boards: ThreadWatcher.convert data.Conf['WatchedThreads']
delete data.Conf['WatchedThreads'] delete data.Conf['WatchedThreads']
$.set data.Conf $.clear -> $.set data.Conf
reset: -> reset: ->
if confirm 'Your current settings will be entirely wiped, are you sure?' if confirm 'Your current settings will be entirely wiped, are you sure?'
$.clear -> window.location.reload() if confirm 'Reset successful. Reload now?' $.clear -> window.location.reload() if confirm 'Reset successful. Reload now?'