From 07dc054ca9c60cd54d70caac6ef6cbb7dc23862e Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 12 Apr 2016 22:20:09 +0200 Subject: [PATCH] Added Text Editing for teams --- .../views/teambuilder/teambuilder_view.coffee | 26 ++++++++++++-- client/views/modals/export_team.jade | 1 + client/views/teambuilder/team.jade | 2 +- public/js/app.js | 36 ++++++++++++++++--- public/js/templates.js | 4 +-- 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/client/app/js/views/teambuilder/teambuilder_view.coffee b/client/app/js/views/teambuilder/teambuilder_view.coffee index c9742f4..82cfb59 100644 --- a/client/app/js/views/teambuilder/teambuilder_view.coffee +++ b/client/app/js/views/teambuilder/teambuilder_view.coffee @@ -133,6 +133,7 @@ class @TeambuilderView extends Backbone.View exportTeam: (e) => $team = $(e.currentTarget).closest('.select-team') id = $team.data('id') + name = $team.data('name') if not @getTeam(id).hasNonNullPokemon() alert("You cannot export empty teams. Please add some pokemon first.") return false @@ -140,9 +141,28 @@ class @TeambuilderView extends Backbone.View teamJSON = @getTeam(id).toNonNullJSON() teamString = PokeBattle.exportTeam(teamJSON.pokemon) - $modal = PokeBattle.modal('modals/export_team') - $modal.find('.exported-team').val(teamString) - $modal.find('textarea, input').first().focus().select() + $modal = PokeBattle.modal 'modals/export_team', ($modal) => + $modal.find('.exported-team').val(teamString) + $modal.on 'click', '.import-team-submit', (e) => + teamString = $modal.find('.exported-team').val() + pokemonJSON = PokeBattle.parseTeam(teamString) + errors = @validateImportedTeam(pokemonJSON) + if errors.length > 0 + listErrors = errors.map((e) -> "
  • #{e}
  • ").join('') + $errors = $modal.find('.form-errors') + $errors.html("").removeClass('hidden') + else + jint = 0 + for jPkmn in pokemonJSON + @getTeam(id).replace(jint, jPkmn) + jint = jint+1 + @getTeam(id).save() + $modal.find('.imported-team').val("") + $modal.modal('hide') + return false + $modal.find('.exported-team').first().focus() + + return false addNewPokemonEvent: => diff --git a/client/views/modals/export_team.jade b/client/views/modals/export_team.jade index 9de511a..27e7da0 100644 --- a/client/views/modals/export_team.jade +++ b/client/views/modals/export_team.jade @@ -7,4 +7,5 @@ .control-group textarea.exported-team.textarea_modal .modal-footer + button.button.import-team-submit(data-dismiss="modal", aria-hidden="true", style="margin-right: 5px") Import button.button(data-dismiss="modal", aria-hidden="true") Close diff --git a/client/views/teambuilder/team.jade b/client/views/teambuilder/team.jade index dd20584..68899b4 100644 --- a/client/views/teambuilder/team.jade +++ b/client/views/teambuilder/team.jade @@ -4,7 +4,7 @@ .team-meta span.fake_link.export-team span.icon-upload - | Export + | Text Editor | | span.fake_link.clone-team span.icon-copy diff --git a/public/js/app.js b/public/js/app.js index 1ab1428..18dcc13 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -6559,18 +6559,46 @@ }; TeambuilderView.prototype.exportTeam = function(e) { - var $modal, $team, id, teamJSON, teamString; + var $modal, $team, id, name, teamJSON, teamString; $team = $(e.currentTarget).closest('.select-team'); id = $team.data('id'); + name = $team.data('name'); if (!this.getTeam(id).hasNonNullPokemon()) { alert("You cannot export empty teams. Please add some pokemon first."); return false; } teamJSON = this.getTeam(id).toNonNullJSON(); teamString = PokeBattle.exportTeam(teamJSON.pokemon); - $modal = PokeBattle.modal('modals/export_team'); - $modal.find('.exported-team').val(teamString); - $modal.find('textarea, input').first().focus().select(); + $modal = PokeBattle.modal('modals/export_team', (function(_this) { + return function($modal) { + $modal.find('.exported-team').val(teamString); + return $modal.on('click', '.import-team-submit', function(e) { + var $errors, errors, jPkmn, jint, listErrors, pokemonJSON, _i, _len; + teamString = $modal.find('.exported-team').val(); + pokemonJSON = PokeBattle.parseTeam(teamString); + errors = _this.validateImportedTeam(pokemonJSON); + if (errors.length > 0) { + listErrors = errors.map(function(e) { + return "
  • " + e + "
  • "; + }).join(''); + $errors = $modal.find('.form-errors'); + $errors.html("").removeClass('hidden'); + } else { + jint = 0; + for (_i = 0, _len = pokemonJSON.length; _i < _len; _i++) { + jPkmn = pokemonJSON[_i]; + _this.getTeam(id).replace(jint, jPkmn); + jint = jint + 1; + } + _this.getTeam(id).save(); + $modal.find('.imported-team').val(""); + $modal.modal('hide'); + } + return false; + }); + }; + })(this)); + $modal.find('.exported-team').first().focus(); return false; }; diff --git a/public/js/templates.js b/public/js/templates.js index 0869717..723b7a5 100644 --- a/public/js/templates.js +++ b/public/js/templates.js @@ -552,7 +552,7 @@ var locals_ = (locals || {}),title = locals_.title,body = locals_.body;buf.push( this["JST"]["modals/export_team"] = function anonymous(locals /**/) { var buf = []; -buf.push("

    Your exported team

    ");;return buf.join(""); +buf.push("

    Your exported team

    ");;return buf.join(""); }; this["JST"]["modals/import_team"] = function anonymous(locals @@ -1077,7 +1077,7 @@ buf.push(""); this["JST"]["teambuilder/team"] = function anonymous(locals /**/) { var buf = []; -var locals_ = (locals || {}),team = locals_.team,window = locals_.window;buf.push("

    " + (jade.escape(null == (jade.interp = team.getName()) ? "" : jade.interp)) + "

    " + (null == (jade.interp = window.JST['team_small']({team: team, window: window})) ? "" : jade.interp) + "
    Export | Clone | Delete
    " + (null == (jade.interp = window.JST['spinner']()) ? "" : jade.interp) + "
    ");;return buf.join(""); +var locals_ = (locals || {}),team = locals_.team,window = locals_.window;buf.push("

    " + (jade.escape(null == (jade.interp = team.getName()) ? "" : jade.interp)) + "

    " + (null == (jade.interp = window.JST['team_small']({team: team, window: window})) ? "" : jade.interp) + "
    Text Editor | Clone | Delete
    " + (null == (jade.interp = window.JST['spinner']()) ? "" : jade.interp) + "
    ");;return buf.join(""); }; this["JST"]["teambuilder/teams"] = function anonymous(locals