App.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <style src="todomvc-app-css/index.css"></style>
  2. <template>
  3. <section class="todoapp">
  4. <!-- header -->
  5. <header class="header">
  6. <h1>todos</h1>
  7. <input class="new-todo"
  8. autofocus
  9. autocomplete="off"
  10. placeholder="What needs to be done?"
  11. @keyup.enter="addTodo">
  12. </header>
  13. <!-- main section -->
  14. <section class="main" v-show="todos.length">
  15. <input class="toggle-all" id="toggle-all"
  16. type="checkbox"
  17. :checked="allChecked"
  18. @change="toggleAll(!allChecked)">
  19. <label for="toggle-all"></label>
  20. <ul class="todo-list">
  21. <TodoItem
  22. v-for="(todo, index) in filteredTodos"
  23. :key="index"
  24. :todo="todo"
  25. />
  26. </ul>
  27. </section>
  28. <!-- footer -->
  29. <footer class="footer" v-show="todos.length">
  30. <span class="todo-count">
  31. <strong>{{ remaining }}</strong>
  32. {{ pluralize(remaining, 'item') }} left
  33. </span>
  34. <ul class="filters">
  35. <li v-for="(val, key) in filters">
  36. <a :href="'#/' + key"
  37. :class="{ selected: visibility === key }"
  38. @click="visibility = key">{{ capitalize(key) }}</a>
  39. </li>
  40. </ul>
  41. <button class="clear-completed"
  42. v-show="todos.length > remaining"
  43. @click="clearCompleted">
  44. Clear completed
  45. </button>
  46. </footer>
  47. </section>
  48. </template>
  49. <script>
  50. import { mapActions } from 'vuex'
  51. import TodoItem from './TodoItem.vue'
  52. const filters = {
  53. all: todos => todos,
  54. active: todos => todos.filter(todo => !todo.done),
  55. completed: todos => todos.filter(todo => todo.done)
  56. }
  57. export default {
  58. components: { TodoItem },
  59. data () {
  60. return {
  61. visibility: 'all',
  62. filters: filters
  63. }
  64. },
  65. computed: {
  66. todos () {
  67. return this.$store.state.todos
  68. },
  69. allChecked () {
  70. return this.todos.every(todo => todo.done)
  71. },
  72. filteredTodos () {
  73. return filters[this.visibility](this.todos)
  74. },
  75. remaining () {
  76. return this.todos.filter(todo => !todo.done).length
  77. }
  78. },
  79. methods: {
  80. ...mapActions([
  81. 'toggleAll',
  82. 'clearCompleted'
  83. ]),
  84. addTodo (e) {
  85. const text = e.target.value
  86. if (text.trim()) {
  87. this.$store.dispatch('addTodo', text)
  88. }
  89. e.target.value = ''
  90. },
  91. pluralize (n, w) {
  92. return n === 1 ? w : (w + 's')
  93. },
  94. capitalize (s) {
  95. return s.charAt(0).toUpperCase() + s.slice(1)
  96. }
  97. }
  98. }
  99. </script>