index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 defaultList = [
  48. { text: '工作流功能绘制工具', done: false },
  49. { text: '工作流流转方法', done: false },
  50. { text: '自动化代码优化', done: false }
  51. ]
  52. export default {
  53. components: { Todo },
  54. filters: {
  55. pluralize: (n, w) => n === 1 ? w : w + 's',
  56. capitalize: s => s.charAt(0).toUpperCase() + s.slice(1)
  57. },
  58. data() {
  59. return {
  60. visibility: 'all',
  61. filters,
  62. // todos: JSON.parse(window.localStorage.getItem(STORAGE_KEY)) || defaultList
  63. todos: defaultList
  64. }
  65. },
  66. computed: {
  67. allChecked() {
  68. return this.todos.every(todo => todo.done)
  69. },
  70. filteredTodos() {
  71. return filters[this.visibility](this.todos)
  72. },
  73. remaining() {
  74. return this.todos.filter(todo => !todo.done).length
  75. }
  76. },
  77. methods: {
  78. setLocalStorage() {
  79. window.localStorage.setItem(STORAGE_KEY, JSON.stringify(this.todos))
  80. },
  81. addTodo(e) {
  82. const text = e.target.value
  83. if (text.trim()) {
  84. this.todos.push({
  85. text,
  86. done: false
  87. })
  88. this.setLocalStorage()
  89. }
  90. e.target.value = ''
  91. },
  92. toggleTodo(val) {
  93. val.done = !val.done
  94. this.setLocalStorage()
  95. },
  96. deleteTodo(todo) {
  97. this.todos.splice(this.todos.indexOf(todo), 1)
  98. this.setLocalStorage()
  99. },
  100. editTodo({ todo, value }) {
  101. todo.text = value
  102. this.setLocalStorage()
  103. },
  104. clearCompleted() {
  105. this.todos = this.todos.filter(todo => !todo.done)
  106. this.setLocalStorage()
  107. },
  108. toggleAll({ done }) {
  109. this.todos.forEach(todo => {
  110. todo.done = done
  111. this.setLocalStorage()
  112. })
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss">
  118. @import './index.scss';
  119. </style>