1
0

App.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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"
  16. type="checkbox"
  17. :checked="allChecked"
  18. @change="toggleAll({ done: !allChecked })">
  19. <ul class="todo-list">
  20. <todo v-for="todo in filteredTodos" :todo="todo"></todo>
  21. </ul>
  22. </section>
  23. <!-- footer -->
  24. <footer class="footer" v-show="todos.length">
  25. <span class="todo-count">
  26. <strong>{{ remaining }}</strong>
  27. {{ remaining | pluralize('item') }} left
  28. </span>
  29. <ul class="filters">
  30. <li v-for="(val, key) in filters">
  31. <a :href="'#/' + key"
  32. :class="{ selected: visibility === key }"
  33. @click="visibility = key">
  34. {{ key | capitalize }}
  35. </a>
  36. </li>
  37. </ul>
  38. <button class="clear-completed"
  39. v-show="todos.length > remaining"
  40. @click="clearCompleted">
  41. Clear completed
  42. </button>
  43. </footer>
  44. </section>
  45. </template>
  46. <script>
  47. import { mapActions } from 'vuex'
  48. import Todo from './Todo.vue'
  49. const filters = {
  50. all: todos => todos,
  51. active: todos => todos.filter(todo => !todo.done),
  52. completed: todos => todos.filter(todo => todo.done)
  53. }
  54. export default {
  55. components: { Todo },
  56. data () {
  57. return {
  58. visibility: 'all',
  59. filters: filters
  60. }
  61. },
  62. computed: {
  63. todos () {
  64. return this.$store.state.todos
  65. },
  66. allChecked () {
  67. return this.todos.every(todo => todo.done)
  68. },
  69. filteredTodos () {
  70. return filters[this.visibility](this.todos)
  71. },
  72. remaining () {
  73. return this.todos.filter(todo => !todo.done).length
  74. }
  75. },
  76. methods: {
  77. addTodo (e) {
  78. var text = e.target.value
  79. if (text.trim()) {
  80. this.$store.dispatch('addTodo', { text })
  81. }
  82. e.target.value = ''
  83. },
  84. ...mapActions([
  85. 'toggleAll',
  86. 'clearCompleted'
  87. ])
  88. },
  89. filters: {
  90. pluralize: (n, w) => n === 1 ? w : (w + 's'),
  91. capitalize: s => s.charAt(0).toUpperCase() + s.slice(1)
  92. }
  93. }
  94. </script>