Add import/export settings buttons in the Settings. #702

This commit is contained in:
Nicolas Stepien 2013-02-21 18:48:21 +01:00
parent 2a9c77c960
commit d416d5f6c4
2 changed files with 132 additions and 0 deletions

View File

@ -1240,6 +1240,10 @@
},
main: function(section) {
var ID, arr, checked, description, hiddenNum, key, li, obj, post, thread, ul, _ref, _ref1, _ref2;
section.innerHTML = "<div class=imp-exp>\n <button class=export>Export settings</button>\n <button class=import>Import settings</button>\n <input type=file style='visibility:hidden'>\n</div>\n<p class=imp-exp-result></p>";
$.on($('.export', section), 'click', Settings["export"]);
$.on($('.import', section), 'click', Settings["import"]);
$.on($('input', section), 'change', Settings.onImport);
_ref = Config.main;
for (key in _ref) {
obj = _ref[key];
@ -1283,6 +1287,74 @@
});
return $.after($('input[name="Stubs"]', section).parentNode.parentNode, li);
},
"export": function() {
var a, data, now, output;
now = Date.now();
data = {
version: g.VERSION,
date: now,
Conf: Conf,
WatchedThreads: $.get('WatchedThreads', {})
};
a = $.el('a', {
className: 'warning',
textContent: 'Save me!',
download: "4chan X Beta-" + now + ".json",
href: "data:application/json;base64," + (btoa(unescape(encodeURIComponent(JSON.stringify(data))))),
target: '_blank'
});
if ($.engine !== 'gecko') {
a.click();
return;
}
output = this.parentNode.nextElementSibling;
output.innerHTML = null;
return $.add(output, a);
},
"import": function() {
return this.nextElementSibling.click();
},
onImport: function() {
var file, output, reader;
if (!(file = this.files[0])) {
return;
}
output = this.parentNode.nextElementSibling;
if (!confirm('Your current settings will be entirely overwritten, are you sure?')) {
output.textContent = 'Import aborted.';
return;
}
reader = new FileReader();
reader.onload = function(e) {
var data;
try {
data = JSON.parse(decodeURIComponent(escape(e.target.result)));
Settings.loadSettings(data);
if (confirm('Import successful. Refresh now?')) {
return window.location.reload();
}
} catch (err) {
output.textContent = 'Import failed due to an error.';
return $.log(err.stack);
}
};
return reader.readAsText(file);
},
loadSettings: function(data) {
var key, val, _ref;
if (data.version.split('.')[0] === '2') {
data = Settings.convertSettingsFromV2(data);
}
_ref = data.Conf;
for (key in _ref) {
val = _ref[key];
$.set(key, val);
}
return $.set('WatchedThreads', data.WatchedThreads);
},
convertSettingsFromV2: function(data) {
return data;
},
filter: function(section) {
var select;
section.innerHTML = "<div class=warning " + (Conf['Sauce'] ? 'hidden' : '') + "><code>Filter</code> is disabled.</div>\n<select name=filter>\n <option value=guide>Guide</option>\n <option value=name>Name</option>\n <option value=uniqueID>Unique ID</option>\n <option value=tripcode>Tripcode</option>\n <option value=capcode>Capcode</option>\n <option value=email>E-mail</option>\n <option value=subject>Subject</option>\n <option value=comment>Comment</option>\n <option value=flag>Flag</option>\n <option value=filename>Filename</option>\n <option value=dimensions>Image dimensions</option>\n <option value=filesize>Filesize</option>\n <option value=MD5>Image MD5</option>\n</select>\n<div></div>";

View File

@ -226,6 +226,18 @@ Settings =
section.scrollTop = 0
main: (section) ->
section.innerHTML = """
<div class=imp-exp>
<button class=export>Export settings</button>
<button class=import>Import settings</button>
<input type=file style='visibility:hidden'>
</div>
<p class=imp-exp-result></p>
"""
$.on $('.export', section), 'click', Settings.export
$.on $('.import', section), 'click', Settings.import
$.on $('input', section), 'change', Settings.onImport
for key, obj of Config.main
ul = $.el 'ul',
textContent: key
@ -252,6 +264,54 @@ Settings =
$.delete "hiddenThreads.#{g.BOARD}"
$.delete "hiddenPosts.#{g.BOARD}"
$.after $('input[name="Stubs"]', section).parentNode.parentNode, li
export: ->
now = Date.now()
data =
version: g.VERSION
date: now
Conf: Conf
WatchedThreads: $.get('WatchedThreads', {})
a = $.el 'a',
className: 'warning'
textContent: 'Save me!'
download: "<%= meta.name %>-#{now}.json"
href: "data:application/json;base64,#{btoa unescape encodeURIComponent JSON.stringify data}"
target: '_blank'
if $.engine isnt 'gecko'
a.click()
return
# XXX Firefox won't let us download automatically.
output = @parentNode.nextElementSibling
output.innerHTML = null
$.add output, a
import: ->
@nextElementSibling.click()
onImport: ->
return unless file = @files[0]
output = @parentNode.nextElementSibling
unless confirm 'Your current settings will be entirely overwritten, are you sure?'
output.textContent = 'Import aborted.'
return
reader = new FileReader()
reader.onload = (e) ->
try
data = JSON.parse decodeURIComponent escape e.target.result
Settings.loadSettings data
if confirm 'Import successful. Refresh now?'
window.location.reload()
catch err
output.textContent = 'Import failed due to an error.'
$.log err.stack
reader.readAsText file
loadSettings: (data) ->
if data.version.split('.')[0] is '2'
data = Settings.convertSettingsFromV2 data
for key, val of data.Conf
$.set key, val
$.set 'WatchedThreads', data.WatchedThreads
convertSettingsFromV2: (data) ->
# XXX TODO
data
filter: (section) ->
section.innerHTML = """