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,37 +13327,49 @@
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 {
name = imported["Mascot"]; for (_i = 0, _len = mascots.length; _i < _len; _i++) {
delete imported["Mascot"]; mascot = mascots[_i];
if (Mascots[name] && !$.remove(Conf["Deleted Mascots"], name)) { MascotTools.parse(mascot);
if (!confirm("A mascot with this name already exists. Would you like to over-write?")) {
return;
} }
} }
Mascots[name] = imported; message = (len = mascots.length) ? "Mascots imported!" : "" + name + " successfully imported!";
$.get("userMascots", {}, function(_arg) { new Notice('info', message, 10);
var userMascots;
userMascots = _arg.userMascots;
userMascots[name] = Mascots[name];
return $.set('userMascots', userMascots);
});
alert("Mascot \"" + name + "\" imported!");
$.rm($("#mascotContainer", d.body)); $.rm($("#mascotContainer", d.body));
return Settings.open('Mascots'); return Settings.open('Mascots');
}; };
return reader.readAsText(file); 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 (!confirm("The mascot " + name + " already exists? Would you like to overwrite it?")) {
return;
}
}
Mascots[name] = imported;
return $.get("userMascots", {}, function(_arg) {
var userMascots;
userMascots = _arg.userMascots;
userMascots[name] = Mascots[name];
return $.set('userMascots', userMascots);
});
},
position: function(mascot) { position: function(mascot) {
if (!Style.sheets.mascots) { if (!Style.sheets.mascots) {
return; return;

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,37 +13316,49 @@
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 {
name = imported["Mascot"]; for (_i = 0, _len = mascots.length; _i < _len; _i++) {
delete imported["Mascot"]; mascot = mascots[_i];
if (Mascots[name] && !$.remove(Conf["Deleted Mascots"], name)) { MascotTools.parse(mascot);
if (!confirm("A mascot with this name already exists. Would you like to over-write?")) {
return;
} }
} }
Mascots[name] = imported; message = (len = mascots.length) ? "Mascots imported!" : "" + name + " successfully imported!";
$.get("userMascots", {}, function(_arg) { new Notice('info', message, 10);
var userMascots;
userMascots = _arg.userMascots;
userMascots[name] = Mascots[name];
return $.set('userMascots', userMascots);
});
alert("Mascot \"" + name + "\" imported!");
$.rm($("#mascotContainer", d.body)); $.rm($("#mascotContainer", d.body));
return Settings.open('Mascots'); return Settings.open('Mascots');
}; };
return reader.readAsText(file); 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 (!confirm("The mascot " + name + " already exists? Would you like to overwrite it?")) {
return;
}
}
Mascots[name] = imported;
return $.get("userMascots", {}, function(_arg) {
var userMascots;
userMascots = _arg.userMascots;
userMascots[name] = Mascots[name];
return $.set('userMascots', userMascots);
});
},
position: function(mascot) { position: function(mascot) {
if (!Style.sheets.mascots) { if (!Style.sheets.mascots) {
return; return;

View File

@ -339,33 +339,44 @@ 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!"
if Mascots[name] and not $.remove Conf["Deleted Mascots"], name new Notice 'info', message, 10
unless confirm "A mascot with this name already exists. Would you like to over-write?"
return
Mascots[name] = imported
$.get "userMascots", {}, ({userMascots}) ->
userMascots[name] = Mascots[name]
$.set 'userMascots', userMascots
alert "Mascot \"#{name}\" imported!"
$.rm $ "#mascotContainer", d.body $.rm $ "#mascotContainer", d.body
Settings.open 'Mascots' Settings.open 'Mascots'
reader.readAsText file 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
return unless confirm "The mascot #{name} already exists? Would you like to overwrite it?"
Mascots[name] = imported
$.get "userMascots", {}, ({userMascots}) ->
userMascots[name] = Mascots[name]
$.set 'userMascots', userMascots
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