All files / vue-koa-demo/src/components TodoList.vue

0% Statements 0/84
0% Branches 0/22
0% Functions 0/23
0% Lines 0/79
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
<template>
  <div class="todo-container">
    <el-input class="todo-input" type="textarea" :rows="2" autofocus
      resize="none"
      placeholder="请输入待办事项,Ctrl + Enter 添加"
      v-model="todoToAdd"
      @keyup.ctrl.enter.native="submitTodo">
    </el-input>
    <div class="state-btns">
      <el-radio-group @change="getTodoList(undefined, undefined, true)" v-model="filterStatus" size="small" text-color="#FFF" fill="#66D4EB">
        <el-radio-button label="all">全部</el-radio-button>
        <el-radio-button label="todo">待完成</el-radio-button>
        <el-radio-button label="done">已完成</el-radio-button>
      </el-radio-group>
    </div>
    <ul>
      <li v-for="(item, index) in todoList" :key="index">
        <div @mouseover="item.btnShow = true" @mouseout="item.btnShow = false" class="item-container">
          <p :class="item.todoState === 'todo' ? 'todo': 'done'" @click="todoClick(item, index)">
            <el-tag :type="item.todoState === 'todo' ? 'warning' : 'success'">{{ index + 1 }}</el-tag>
            <span ref="span" v-show="!item.inputShow">{{ item.todoDetail }}</span>
            <input ref="input" type="text" @keyup.enter="todoInputBlur(item, index)" @blur="todoInputBlur(item, index)" v-show="item.inputShow && item.todoState === 'todo'">
          </p>
          <transition name="fade">
            <div class="item-btns" v-if="item.btnShow === true">
              <el-button @click="completeTodo(item)" key="delete" class="item-delete" type="danger" size="mini" plain>{{ item.todoState === 'todo' ? '标为完成' : '标为未完成' }}</el-button>
              <el-button @click="$router.push(`/detail/${item._id}`)" key="detail" class="item-detail" type="primary" size="mini" plain>详情</el-button>
            </div>
          </transition>
        </div>
      </li>
    </ul>
    <p class="encourage" v-if="todoTotal === 0">空的日程列表</p>
    <div class="pagination-container">
      <el-pagination v-if="todoTotal !== 0" :current-page.sync="page" @current-change="getTodoList" layout="prev, pager, next" :page-size="8" :total="todoTotal"></el-pagination>
    </div>
  </div>
</template>
 
<script>
import axios from 'axios'
 
export default {
  data () {
    return {
      todoToAdd: '',
      todoList: [],
      filterStatus: sessionStorage.getItem('filter') ? sessionStorage.getItem('filter') : 'all',
      todoTotal: 0,
      page: sessionStorage.getItem('pagination') ? Number(sessionStorage.getItem('pagination')) : 1
    }
  },
  methods: {
    checkHasLogin () {
      axios.get('/api/login/hasLogin')
        .then((res) => {
          sessionStorage.username = res.data.msg
          this.$emit('setUserName', { username: res.data.msg })
          if (res.data.success) {
            if (!sessionStorage.hasLogin) {
              this.$message({ message: `你好呀 ${res.data.msg}!`, type: 'success', duration: 1500 })
            }
            sessionStorage.hasLogin = true
            this.getTodoList(this.page, this.filterStatus, false)
          } else {
            this.$router.replace('/login')
          }
        })
        .catch((err) => {
          console.log(err)
          this.$router.replace('/login')
        })
    },
    submitTodo () {
      if (this.todoToAdd.trim() === '') return
      axios.post(`/api/todo/add`, { todoDetail: this.todoToAdd, todoTime: Date.now() })
        .then(res => {
          if (res.data.success) this.$message({ message: res.data.msg, type: 'success', duration: 1500 })
          else this.$message.error({ message: res.data.msg, duration: 1500 })
          this.todoToAdd = ''
          this.page = 1
          this.getTodoList(this.page, this.filterStatus, false)
        })
        .catch(err => { console.log(err) })
    },
    getTodoList (page = 1, filter = this.filterStatus, backToFirst) {
      this.page = page
      axios.get(`/api/todo/get?page=${this.page}&filter=${filter}`)
        .then(res => {
          res.data.todoList.forEach(todo => {
            todo.btnShow = false
            todo.inputShow = false
          })
          this.todoList = res.data.todoList
          this.todoTotal = res.data.todoTotal
          if (backToFirst) this.page = 1
        })
        .catch(err => {
          console.log(err)
          this.$message.error({ message: '获取事项列表失败', duration: 1500 })
        })
    },
    completeTodo (item) {
      axios.post(`/api/todo/done/${item._id}`, item)
        .then(res => {
          if (res.data.success) {
            this.getTodoList(this.page, this.filterStatus, false)
            return this.$message({ message: '操作事项成功', type: 'success', duration: 1500 })
          }
          this.$message.error({ message: '操作事项失败', duration: 1500 })
        })
        .catch(err => {
          console.log(err)
          this.$message.error({ message: '操作事项失败', duration: 1500 })
        })
    },
    todoClick (item, index) {
      if (item.todoState === 'done' || item.inputShow) return
      const val = this.$refs.span[index].innerText
      item.inputShow = true
      this.$nextTick(() => {
        this.$refs.input[index].focus()
        this.$refs.input[index].value = val
      })
    },
    todoInputBlur (item, index) {
      const val = this.$refs.input[index].value
      item.inputShow = false
      this.$nextTick(() => {
        this.$refs.span[index].innerText = val
        item.todoDetail = val
        this.updateTodoDetail(item)
      })
    },
    updateTodoDetail (item) {
      axios.post('/api/todo/update', item)
        .then((res) => {
          if (res.data.success) {
            this.getTodoList(this.page, this.filterStatus, false)
            return this.$message({ message: '更改事项内容成功', type: 'success', duration: 1500 })
          }
          this.$message.error({ message: '更改事项内容失败', duration: 1500 })
        })
        .catch((err) => {
          console.log(err)
          this.$message.error({ message: '更改事项内容失败', duration: 1500 })
        })
    }
  },
  mounted () {
    this.checkHasLogin()
  },
  beforeDestroy () {
    sessionStorage.setItem('pagination', this.page)
    sessionStorage.setItem('filter', this.filterStatus)
  }
}
</script>
 
<style>
.el-pagination .btn-prev,
.el-pagination .btn-prev.disabled,
.el-pagination .btn-next,
.el-pagination .btn-next.disabled {
  background-color: transparent !important;
}
.el-pager li {
  background-color: transparent !important;
}
</style>
 
<style lang="scss" scoped>
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
.todo-container {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  .todo-input {
    width: 480px;
  }
  .state-btns {
    margin-top: 12px;
  }
  ul {
    margin-top: 12px;
    list-style: none;
    &::-webkit-scrollbar {
      display: none;
    }
    li {
      width: 100%;
      margin-top: 16px;
      height: 32px;
      position: relative;
      &:nth-child(1) {
        margin-top: 0px;
      }
    }
    .item-container {
      height: 100%;
      text-align: left;
      cursor: pointer;
      font-size: 14px;
    }
    p {
      text-align: left;
      width: 500px;
      display: inline-block;
      line-height: 32px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      color: #666;
      box-sizing: border-box;
      padding-right: 16px;
      border-radius: 6px;
      cursor: pointer;
      &.todo {
        border: 1px solid #F2E0C9;
      }
      &.done {
        border: 1px solid #D8EDCD;
      }
      .el-tag {
        width: 40px;
        float: left;
        border: none;
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
        text-align: center;
        margin-right: 16px;
      }
    }
    .item-btns {
      position: absolute;
      top: 3px;
      right: -154px;
    }
    .item-detail,
    .item-delete {
      float: right;
    }
    .item-delete {
      width: 90px;
    }
    .item-detail {
      margin-right: 4px;
    }
    input {
      width: 422px;
      border: none;
      outline: none;
      height: 100%;
      line-height: 32px;
      border-top-right-radius: 6px;
      border-bottom-right-radius: 6px;
      color: #666;
      box-sizing: border-box;
      background: transparent;
      font-size: 14px;
    }
  }
  .encourage {
    color: #65D4EB;
    margin-top: 160px;
  }
  .pagination-container {
    margin-top: 24px;
    width: 480px;
  }
}
</style>