4151ac3f8eac9f3d0d2e42c36d1f3ed4a0291c98.svn-base 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <section class="todoapp">
  3. <!-- header -->
  4. <header class="header">
  5. <input class="new-todo" autocomplete="off" placeholder="Todo List" @keyup.enter="addTodo">
  6. </header>
  7. <!-- main section -->
  8. <section v-show="todos.length" class="main">
  9. <input id="toggle-all" :checked="allChecked" class="toggle-all" type="checkbox" @change="toggleAll({ done: !allChecked })">
  10. <label for="toggle-all" />
  11. <ul class="todo-list">
  12. <todo
  13. v-for="(todo, index) in filteredTodos"
  14. :key="index"
  15. :todo="todo"
  16. @toggleTodo="toggleTodo"
  17. @editTodo="editTodo"
  18. @deleteTodo="deleteTodo"
  19. />
  20. </ul>
  21. </section>
  22. <!-- footer -->
  23. <footer v-show="todos.length" class="footer">
  24. <span class="todo-count">
  25. <strong>{{ remaining }}</strong>
  26. {{ remaining | pluralize('item') }} left
  27. </span>
  28. <ul class="filters">
  29. <li v-for="(val, key) in filters" :key="key">
  30. <a :class="{ selected: visibility === key }" @click.prevent="visibility = key">{{ key | capitalize }}</a>
  31. </li>
  32. </ul>
  33. <!-- <button class="clear-completed" v-show="todos.length > remaining" @click="clearCompleted">
  34. Clear completed
  35. </button> -->
  36. </footer>
  37. </section>
  38. </template>
  39. <script>
  40. import Todo from './Todo.vue'
  41. const STORAGE_KEY = 'todos'
  42. const filters = {
  43. all: todos => todos,
  44. active: todos => todos.filter(todo => !todo.done),
  45. completed: todos => todos.filter(todo => todo.done)
  46. }
  47. const defalutList = [
  48. { text: 'star this repository', done: false },
  49. { text: 'fork this repository', done: false },
  50. { text: 'follow author', done: false },
  51. { text: 'vue-element-admin', done: true },
  52. { text: 'vue', done: true },
  53. { text: 'element-ui', done: true },
  54. { text: 'axios', done: true },
  55. { text: 'webpack', done: true }
  56. ]
  57. export default {
  58. components: { Todo },
  59. filters: {
  60. pluralize: (n, w) => n === 1 ? w : w + 's',
  61. capitalize: s => s.charAt(0).toUpperCase() + s.slice(1)
  62. },
  63. data() {
  64. return {
  65. visibility: 'all',
  66. filters,
  67. // todos: JSON.parse(window.localStorage.getItem(STORAGE_KEY)) || defalutList
  68. todos: defalutList
  69. }
  70. },
  71. computed: {
  72. allChecked() {
  73. return this.todos.every(todo => todo.done)
  74. },
  75. filteredTodos() {
  76. return filters[this.visibility](this.todos)
  77. },
  78. remaining() {
  79. return this.todos.filter(todo => !todo.done).length
  80. }
  81. },
  82. methods: {
  83. setLocalStorage() {
  84. window.localStorage.setItem(STORAGE_KEY, JSON.stringify(this.todos))
  85. },
  86. addTodo(e) {
  87. const text = e.target.value
  88. if (text.trim()) {
  89. this.todos.push({
  90. text,
  91. done: false
  92. })
  93. this.setLocalStorage()
  94. }
  95. e.target.value = ''
  96. },
  97. toggleTodo(val) {
  98. val.done = !val.done
  99. this.setLocalStorage()
  100. },
  101. deleteTodo(todo) {
  102. this.todos.splice(this.todos.indexOf(todo), 1)
  103. this.setLocalStorage()
  104. },
  105. editTodo({ todo, value }) {
  106. todo.text = value
  107. this.setLocalStorage()
  108. },
  109. clearCompleted() {
  110. this.todos = this.todos.filter(todo => !todo.done)
  111. this.setLocalStorage()
  112. },
  113. toggleAll({ done }) {
  114. this.todos.forEach(todo => {
  115. todo.done = done
  116. this.setLocalStorage()
  117. })
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. @import './index.scss';
  124. </style>