Move some team storage functionality to storage.js

This commit is contained in:
Guangcong Luo
2013-09-14 20:06:55 -05:00
parent b566aa91b0
commit 4c7eb9a3cd
4 changed files with 70 additions and 96 deletions

View File

@@ -384,7 +384,7 @@
},
updateTeams: function() {
if (!window.BattleFormats) return;
var teams = app.user.teams;
var teams = Storage.teams;
var self = this;
this.$('button[name=team]').each(function(i, el) {
@@ -424,7 +424,7 @@
var format = $pmWindow.find('button[name=format]').val();
var teamIndex = $pmWindow.find('button[name=team]').val();
var team = null;
if (app.user.teams[teamIndex]) team = app.user.teams[teamIndex].team;
if (Storage.teams[teamIndex]) team = Storage.teams[teamIndex].team;
if (!window.BattleFormats[format].team && !team) {
app.addPopupMessage("You need to go into the Teambuilder and build a team for this format.");
return;
@@ -448,7 +448,7 @@
var format = $pmWindow.find('button[name=format]').val();
var teamIndex = $pmWindow.find('button[name=team]').val();
var team = null;
if (app.user.teams[teamIndex]) team = app.user.teams[teamIndex].team;
if (Storage.teams[teamIndex]) team = Storage.teams[teamIndex].team;
if (!window.BattleFormats[format].team && !team) {
app.addPopupMessage("You need to go into the Teambuilder and build a team for this format.");
return;
@@ -505,7 +505,7 @@
curTeamFormat: '',
curTeamIndex: -1,
renderTeams: function(formatid, teamIndex) {
if (!app.user.teams || !window.BattleFormats) {
if (!Storage.teams || !window.BattleFormats) {
return '<button class="select teamselect" name="team" disabled><em>Loading...</em></button>';
}
if (!formatid) formatid = this.curFormat;
@@ -515,7 +515,7 @@
if (window.BattleFormats[formatid].team) {
return '<button class="select teamselect preselected" name="team" value="random" disabled>'+TeamPopup.renderTeam('random')+'</button>';
}
var teams = app.user.teams;
var teams = Storage.teams;
if (!teams.length) {
return '<button class="select teamselect" name="team" disabled>You have no teams</button>'
}
@@ -558,7 +558,7 @@
var format = $formatButton.val();
var teamIndex = $teamButton.val();
var team = null;
if (app.user.teams[teamIndex]) team = app.user.teams[teamIndex].team;
if (Storage.teams[teamIndex]) team = Storage.teams[teamIndex].team;
if (!window.BattleFormats[format].team && !team) {
app.addPopupMessage("You need to go into the Teambuilder and build a team for this format.");
return;
@@ -628,7 +628,7 @@
initialize: function(data) {
var bufs = ['','','','',''];
var curBuf = 0;
var teams = app.user.teams;
var teams = Storage.teams;
var bufBoundary = 128;
if (teams.length > 128 && $(window).width() > 1080) {
@@ -699,7 +699,7 @@
}
return buf;
}
var team = app.user.teams[i];
var team = Storage.teams[i];
if (!team) return 'Error: Corrupted team';
var buf = ''+Tools.escapeHTML(team.name)+'<br />';
for (var i=0; i<team.team.length; i++) {

View File

@@ -6,7 +6,7 @@
var TeambuilderRoom = exports.TeambuilderRoom = exports.Room.extend({
type: 'teambuilder',
initialize: function() {
teams = app.user.teams;
teams = Storage.teams;
// left menu
this.$el.addClass('ps-room-light').addClass('scrollable');
@@ -51,7 +51,7 @@
},
saveTeams: function() {
// save and return
TeambuilderRoom.saveTeams();
Storage.saveTeams();
app.user.trigger('saveteams');
this.update();
},
@@ -80,7 +80,7 @@
curSetLoc: 0,
exportMode: false,
update: function() {
teams = app.user.teams;
teams = Storage.teams;
if (this.curTeam) {
if (this.curSet) {
return this.updateSetView();
@@ -97,7 +97,7 @@
deletedTeam: null,
deletedTeamLoc: -1,
updateTeamList: function() {
var teams = app.user.teams;
var teams = Storage.teams;
var buf = '';
this.deletedSet = null;
@@ -119,7 +119,7 @@
buf = '<div class="pad"><p>y\'know zarel this is a pretty good teambuilder</p>'
buf += '<p>aww thanks I\'m glad you like it :)</p>'
buf += '<ul>';
if (TeambuilderRoom.cantSave) buf += '<li>== CAN\'T SAVE ==<br /><small>You hit your browser\'s limit for team storage! Please backup them and delete some of them. Your teams won\'t be saved until you\'re under the limit again.</small></li>';
if (Storage.cantSave) buf += '<li>== CAN\'T SAVE ==<br /><small>You hit your browser\'s limit for team storage! Please backup them and delete some of them. Your teams won\'t be saved until you\'re under the limit again.</small></li>';
if (!teams.length) {
if (this.deletedTeamLoc >= 0) {
buf += '<li><button name="undoDelete"><i class="icon-undo"></i> Undo Delete</button></li>';
@@ -133,10 +133,6 @@
}
if (i >= teams.length) break;
if (i==2 && app.user.cookieTeams) {
buf += '<li>== UNSAVED TEAM LINE ==<br /><small>All teams below this line will not be saved.</small></li>';
}
var team = teams[i];
if (!team) {
@@ -165,11 +161,8 @@
buf += '<button name="backup"><i class="icon-upload-alt"></i> Backup/Restore all teams</button>';
buf += '<p><strong>Clearing your cookies or <code>localStorage</code> will delete your teams.</strong></p><p>If you want to clear your cookies or <code>localStorage</code>, you can use the Backup/Restore feature to save your teams as text first.</p>';
buf += '<p><strong>Clearing your cookies (specifically, <code>localStorage</code>) will delete your teams.</strong></p><p>If you want to clear your cookies or <code>localStorage</code>, you can use the Backup/Restore feature to save your teams as text first.</p>';
if (teams.length >= 2 && app.user.cookieTeams) {
buf += ' <strong>WARNING:</strong> Additional teams WILL NOT BE SAVED.';
}
buf += '</div>';
this.$el.html(buf);
@@ -399,7 +392,7 @@
saveFlag: false,
save: function() {
this.saveFlag = true;
TeambuilderRoom.saveTeams();
Storage.saveTeams();
},
teamNameChange: function(e) {
this.curTeam.name = ($.trim(e.currentTarget.value) || 'Untitled '+(this.curTeamLoc+1));
@@ -1247,8 +1240,8 @@
var team = [];
var curSet = null;
if (teams === true) {
app.user.teams = [];
teams = app.user.teams;
Storage.teams = [];
teams = Storage.teams;
}
for (var i=0; i<text.length; i++) {
var line = $.trim(text[i]);
@@ -1935,42 +1928,6 @@
app.clearGlobalListeners();
Room.prototype.destroy.call(this);
}
}, {
// static
saveTeams: function() {
if (window.localStorage) {
$.cookie('showdown_team1', null);
$.cookie('showdown_team2', null);
$.cookie('showdown_team3', null);
TeambuilderRoom.cantSave = false;
try {
localStorage.setItem('showdown_teams', JSON.stringify(teams));
} catch (e) {
if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
TeambuilderRoom.cantSave = true;
} else {
throw e;
}
}
} else {
if (teams[0]) {
$.cookie('showdown_team1', null);
$.cookie('showdown_team1', $.toJSON(teams[0]),{expires:60,domain:'pokemonshowdown.com'});
} else {
$.cookie('showdown_team1', null);
$.cookie('showdown_team1', null, {domain:'pokemonshowdown.com'});
}
if (teams[1]) {
$.cookie('showdown_team2', null);
$.cookie('showdown_team2', $.toJSON(teams[1]),{expires:60,domain:'pokemonshowdown.com'});
} else {
$.cookie('showdown_team2', null);
$.cookie('showdown_team2', null, {domain:'pokemonshowdown.com'});
}
$.cookie('showdown_team3', null);
}
}
});
var MovePopup = exports.MovePopup = Popup.extend({

View File

@@ -198,34 +198,6 @@
expires: 14
});
},
/**
* This function loads teams from `localStorage` or cookies. This function
* is only used if the client is running on `play.pokemonshowdown.com`. If the
* client is running on another domain, then teams are received from
* `crossdomain.php` instead.
*/
teams: null,
loadTeams: function() {
this.teams = [];
if (window.localStorage) {
var teamString = localStorage.getItem('showdown_teams');
if (teamString) this.teams = JSON.parse(teamString);
} else {
this.cookieTeams = true;
var savedTeam = $.parseJSON($.cookie('showdown_team1'));
if (savedTeam) {
this.teams.push(savedTeam);
}
savedTeam = $.parseJSON($.cookie('showdown_team2'));
if (savedTeam) {
this.teams.push(savedTeam);
}
savedTeam = $.parseJSON($.cookie('showdown_team3'));
if (savedTeam) {
this.teams.push(savedTeam);
}
}
}
});
var App = this.App = Backbone.Router.extend({
@@ -405,9 +377,9 @@
var origin = 'https://' + Config.origindomain;
if (e.origin !== origin) return;
if (e.data === 'init') {
app.user.loadTeams();
Storage.loadTeams();
e.source.postMessage($.toJSON({
teams: $.toJSON(app.user.teams),
teams: $.toJSON(Storage.teams),
prefs: $.toJSON(Tools.prefs.data)
}), origin);
} else if (e.data === 'done') {
@@ -434,7 +406,7 @@
// Simple connection: no cross-domain logic needed.
Config.server = Config.server || Config.defaultserver;
this.user.loadTeams();
Storage.loadTeams();
this.trigger('init:loadprefs');
return this.connect();
},
@@ -517,13 +489,12 @@
};
// teams
if (data.teams) {
self.user.cookieTeams = false;
self.user.teams = $.parseJSON(data.teams) || [];
Storage.teams = $.parseJSON(data.teams) || [];
} else {
self.user.teams = [];
Storage.teams = [];
}
TeambuilderRoom.saveTeams = function() {
postCrossDomainMessage({teams: $.toJSON(app.user.teams)});
Storage.saveTeams = function() {
postCrossDomainMessage({teams: $.toJSON(Storage.teams)});
};
// prefs
if (data.prefs) {

View File

@@ -10,6 +10,52 @@ function _Storage() {
}
}
/*********************************************************
* Teams
*********************************************************/
/**
* Teams are normally loaded from `localStorage`.
* If the client isn't running on `play.pokemonshowdown.com`, though,
* teams are received from `crossdomain.php` instead.
*/
_Storage.prototype.teams = null;
_Storage.prototype.loadTeams = function() {
this.teams = [];
if (window.nodewebkit) {
return;
}
if (window.localStorage) {
var teamString = localStorage.getItem('showdown_teams');
if (teamString) this.teams = JSON.parse(teamString);
}
};
_Storage.prototype.saveTeams = function() {
if (window.localStorage) {
$.cookie('showdown_team1', null);
$.cookie('showdown_team2', null);
$.cookie('showdown_team3', null);
Storage.cantSave = false;
try {
localStorage.setItem('showdown_teams', JSON.stringify(this.teams));
} catch (e) {
if (e.code === DOMException.QUOTA_EXCEEDED_ERR) {
Storage.cantSave = true;
} else {
throw e;
}
}
}
};
/*********************************************************
* Node-webkit
*********************************************************/
_Storage.prototype.initDirectory = function() {
var self = this;