Allow multiple mascots to be imported at the same time

This commit is contained in:
Zixaphir 2014-01-14 01:25:49 -07:00
parent 141f9b6dd3
commit 0e184ec153
4 changed files with 89 additions and 54 deletions

View File

@ -1,5 +1,5 @@
/* /*
* appchan x - Version 2.8.4 - 2014-01-13 * appchan x - Version 2.8.4 - 2014-01-14
* *
* Licensed under the MIT license. * Licensed under the MIT license.
* https://github.com/zixaphir/appchan-x/blob/master/LICENSE * https://github.com/zixaphir/appchan-x/blob/master/LICENSE

View File

@ -22,7 +22,7 @@
// ==/UserScript== // ==/UserScript==
/* /*
* appchan x - Version 2.8.4 - 2014-01-13 * appchan x - Version 2.8.4 - 2014-01-14
* *
* Licensed under the MIT license. * Licensed under the MIT license.
* https://github.com/zixaphir/appchan-x/blob/master/LICENSE * https://github.com/zixaphir/appchan-x/blob/master/LICENSE
@ -13327,36 +13327,48 @@
file = this.files[0]; file = this.files[0];
reader = new FileReader(); reader = new FileReader();
reader.onload = function(e) { reader.onload = function(e) {
var err, imported, name; var err, len, mascot, mascots, message, name, _i, _len;
try { try {
imported = JSON.parse(e.target.result); mascots = JSON.parse(e.target.result);
} catch (_error) { } catch (_error) {
err = _error; err = _error;
alert(err); alert(err);
return; return;
} }
if (!imported["Mascot"]) { if (name = mascots["Mascot"]) {
alert("Mascot file is invalid."); MascotTools.parse(mascot);
} else {
for (_i = 0, _len = mascots.length; _i < _len; _i++) {
mascot = mascots[_i];
MascotTools.parse(mascot);
} }
name = imported["Mascot"]; }
delete imported["Mascot"]; message = (len = mascots.length) ? "Mascots imported!" : "" + name + " successfully imported!";
new Notice('info', message, 10);
$.rm($("#mascotContainer", d.body));
return Settings.open('Mascots');
};
return reader.readAsText(file);
},
parse: function(mascot) {
var name;
if (!(name = mascot["Mascot"])) {
new Notice('warning', "Failed to import a mascot. Mascot has no name.", 5);
return;
}
delete mascot["Mascot"];
if (Mascots[name] && !$.remove(Conf["Deleted Mascots"], name)) { if (Mascots[name] && !$.remove(Conf["Deleted Mascots"], name)) {
if (!confirm("A mascot with this name already exists. Would you like to over-write?")) { if (!confirm("The mascot " + name + " already exists? Would you like to overwrite it?")) {
return; return;
} }
} }
Mascots[name] = imported; Mascots[name] = imported;
$.get("userMascots", {}, function(_arg) { return $.get("userMascots", {}, function(_arg) {
var userMascots; var userMascots;
userMascots = _arg.userMascots; userMascots = _arg.userMascots;
userMascots[name] = Mascots[name]; userMascots[name] = Mascots[name];
return $.set('userMascots', userMascots); return $.set('userMascots', userMascots);
}); });
alert("Mascot \"" + name + "\" imported!");
$.rm($("#mascotContainer", d.body));
return Settings.open('Mascots');
};
return reader.readAsText(file);
}, },
position: function(mascot) { position: function(mascot) {
if (!Style.sheets.mascots) { if (!Style.sheets.mascots) {

View File

@ -1,6 +1,6 @@
// Generated by CoffeeScript // Generated by CoffeeScript
/* /*
* appchan x - Version 2.8.4 - 2014-01-13 * appchan x - Version 2.8.4 - 2014-01-14
* *
* Licensed under the MIT license. * Licensed under the MIT license.
* https://github.com/zixaphir/appchan-x/blob/master/LICENSE * https://github.com/zixaphir/appchan-x/blob/master/LICENSE
@ -13316,36 +13316,48 @@
file = this.files[0]; file = this.files[0];
reader = new FileReader(); reader = new FileReader();
reader.onload = function(e) { reader.onload = function(e) {
var err, imported, name; var err, len, mascot, mascots, message, name, _i, _len;
try { try {
imported = JSON.parse(e.target.result); mascots = JSON.parse(e.target.result);
} catch (_error) { } catch (_error) {
err = _error; err = _error;
alert(err); alert(err);
return; return;
} }
if (!imported["Mascot"]) { if (name = mascots["Mascot"]) {
alert("Mascot file is invalid."); MascotTools.parse(mascot);
} else {
for (_i = 0, _len = mascots.length; _i < _len; _i++) {
mascot = mascots[_i];
MascotTools.parse(mascot);
} }
name = imported["Mascot"]; }
delete imported["Mascot"]; message = (len = mascots.length) ? "Mascots imported!" : "" + name + " successfully imported!";
new Notice('info', message, 10);
$.rm($("#mascotContainer", d.body));
return Settings.open('Mascots');
};
return reader.readAsText(file);
},
parse: function(mascot) {
var name;
if (!(name = mascot["Mascot"])) {
new Notice('warning', "Failed to import a mascot. Mascot has no name.", 5);
return;
}
delete mascot["Mascot"];
if (Mascots[name] && !$.remove(Conf["Deleted Mascots"], name)) { if (Mascots[name] && !$.remove(Conf["Deleted Mascots"], name)) {
if (!confirm("A mascot with this name already exists. Would you like to over-write?")) { if (!confirm("The mascot " + name + " already exists? Would you like to overwrite it?")) {
return; return;
} }
} }
Mascots[name] = imported; Mascots[name] = imported;
$.get("userMascots", {}, function(_arg) { return $.get("userMascots", {}, function(_arg) {
var userMascots; var userMascots;
userMascots = _arg.userMascots; userMascots = _arg.userMascots;
userMascots[name] = Mascots[name]; userMascots[name] = Mascots[name];
return $.set('userMascots', userMascots); return $.set('userMascots', userMascots);
}); });
alert("Mascot \"" + name + "\" imported!");
$.rm($("#mascotContainer", d.body));
return Settings.open('Mascots');
};
return reader.readAsText(file);
}, },
position: function(mascot) { position: function(mascot) {
if (!Style.sheets.mascots) { if (!Style.sheets.mascots) {

View File

@ -339,20 +339,37 @@ MascotTools =
reader.onload = (e) -> reader.onload = (e) ->
try try
imported = JSON.parse e.target.result mascots = JSON.parse e.target.result
catch err catch err
alert err alert err
return return
unless (imported["Mascot"]) if name = mascots["Mascot"]
alert "Mascot file is invalid." MascotTools.parse mascot
else
MascotTools.parse mascot for mascot in mascots
name = imported["Mascot"] message = if len = mascots.length
delete imported["Mascot"] "Mascots imported!"
else
"#{name} successfully imported!"
new Notice 'info', message, 10
$.rm $ "#mascotContainer", d.body
Settings.open 'Mascots'
reader.readAsText file
parse: (mascot) ->
unless name = mascot["Mascot"]
new Notice 'warning', "Failed to import a mascot. Mascot has no name.", 5
return
delete mascot["Mascot"]
if Mascots[name] and not $.remove Conf["Deleted Mascots"], name if Mascots[name] and not $.remove Conf["Deleted Mascots"], name
unless confirm "A mascot with this name already exists. Would you like to over-write?" return unless confirm "The mascot #{name} already exists? Would you like to overwrite it?"
return
Mascots[name] = imported Mascots[name] = imported
@ -360,12 +377,6 @@ MascotTools =
userMascots[name] = Mascots[name] userMascots[name] = Mascots[name]
$.set 'userMascots', userMascots $.set 'userMascots', userMascots
alert "Mascot \"#{name}\" imported!"
$.rm $ "#mascotContainer", d.body
Settings.open 'Mascots'
reader.readAsText file
position: (mascot) -> position: (mascot) ->
return unless Style.sheets.mascots return unless Style.sheets.mascots
mascot.image? or mascot = Mascots[Conf['mascot']] or {} # event mascot.image? or mascot = Mascots[Conf['mascot']] or {} # event