Advertisement
codetalkinhawkin

Untitled

Mar 29th, 2020
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.   <div>
  3.     <!-- justice leage application begins here -->
  4.     <h1 id="jl">Justice League Mission Planner</h1>
  5.  
  6.     <ul class="roster">
  7.       <h3>Roster:</h3>
  8.       <li v-for="hero in heroes"
  9.           :key="hero.name">
  10.  
  11.         <!-- to do: conditionally display this span -->
  12.         <span v-if="isInList(hero.name)">&nbsp;</span>
  13.  
  14.         <span>{{ hero.name }}&nbsp;</span>
  15.         <span class="edit"
  16.               @click="editHero(hero)">edit</span>
  17.       </li>
  18.       <br>
  19.       <input type="text"
  20.              placeholder="new name"
  21.              v-model="newName"
  22.              v-if="isEdit"
  23.              @keyup.enter="changeName"
  24.              @blur="clear">
  25.       <br>
  26.       <span v-if="isEdit">enter to submit, click outside the box to cancel</span>
  27.     </ul>
  28.     <chosen-heroes :heroes="heroes" :chosenHeroes="chosenHeroes" @add-hero="addHero" />
  29.   </div>
  30. </template>
  31.  
  32. <script>
  33. import ChosenHeroes from "./components/ChosenHeroes.vue";
  34.  
  35. export default {
  36.   components: {
  37.    "chosen-heroes" : ChosenHeroes
  38.   },
  39.   data() {
  40.     return {
  41.       heroes: [
  42.         { name: "Superman" },
  43.         { name: "Batman" },
  44.         { name: "Aquaman" },
  45.         { name: "Wonder Woman" },
  46.         { name: "Green Lantern" },
  47.         { name: "Martian Manhunter" },
  48.         { name: "Flash" }
  49.       ],
  50.       newName: "",
  51.       isEdit: false,
  52.       heroToModify: null,
  53.       chosenHeroes: ChosenHeroes.data
  54.     };
  55.  
  56. methods: {
  57.     editHero(hero) {
  58.       this.isEdit = true;
  59.       this.newName = hero.name;
  60.       this.heroToModify = hero;
  61.     },
  62.  
  63.     changeName() {
  64.       this.heroToModify.name = this.newName;
  65.       this.isEdit = false;
  66.     },
  67.  
  68.     clear() {
  69.       this.heroToModify = null;
  70.       this.newName = "";
  71.       this.isEdit = false;
  72.     },
  73.  
  74.     isInList(heroName) {
  75.       return this.chosenHeroes.map(heroObject => heroObject.name).includes(heroName);
  76.     }
  77.  
  78.  
  79. ...
  80. <template>
  81.   <div>
  82.     <select v-model="chosenHero">
  83.       <!-- placeholder value -->
  84.       <option :value="null">Select a hero</option>
  85.  
  86.       <!-- available heroes -->
  87.       <option v-for="hero in heroes"
  88.               :key="hero.name"
  89.               :value="hero.name">
  90.         {{ hero.name }}
  91.       </option>
  92.     </select>
  93.     <span>&nbsp;</span>
  94.     <button @click="addHero(chosenHero)"
  95.             :disabled="chosenHero === null || chosenHeroes.length >= 3">Add Hero</button>
  96.     <br>
  97.     <h3>Chosen Heroes</h3>
  98.     <div class="chosen-heroes">
  99.       <div v-for="(hero, i) in chosenHeroes"
  100.            :key="hero.name">
  101.         <strong>Slot {{ i + 1 }}:</strong>
  102.         <Hero :hero="hero"
  103.               @removeHero="removeHero(hero)" />
  104.       </div>
  105.     </div>
  106.   </div>
  107. </template>
  108.  
  109. <script>
  110. import Hero from "./Hero";
  111.  
  112. export default {
  113.   components: {
  114.     Hero
  115.   },
  116.   props: {
  117.     "heroes": Array
  118.   },
  119.   data() {
  120.     return {
  121.       chosenHero: null,
  122.       chosenHeroes: []
  123.     };
  124.   },
  125.   methods: {
  126.     addHero(name) {
  127.       if(this.chosenHeroes.length < 3) {
  128.         this.chosenHeroes.push({ name });
  129.         this.chosenHero = null;
  130.       }
  131.       this.$emit("add-hero",this.chosenHeroes);
  132.     },
  133.  
  134.     removeHero(hero) {
  135.       this.chosenHeroes = this.chosenHeroes.filter(h => h.name != hero.name);
  136.     }
  137.   }
  138. };
  139. </script>
  140.  
  141. <style scoped>
  142. .chosen-heroes {
  143.   display: flex;
  144.   flex-flow: column;
  145.   align-items: center;
  146. }
  147. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement