ArticleDetail.vue 8.7 KB
Newer Older
P
Pan 已提交
1 2
<template>
  <div class="createPost-container">
3
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
花裤衩 已提交
4
      <sticky :z-index="10" :class-name="'sub-navbar '+postForm.status">
花裤衩 已提交
5 6 7
        <CommentDropdown v-model="postForm.comment_disabled" />
        <PlatformDropdown v-model="postForm.platforms" />
        <SourceUrlDropdown v-model="postForm.source_uri" />
J
Jere 已提交
8
        <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
9
          Publush
花裤衩 已提交
10
        </el-button>
花裤衩 已提交
11
        <el-button v-loading="loading" type="warning" @click="draftForm">
12
          Draft
花裤衩 已提交
13
        </el-button>
P
Pan 已提交
14
      </sticky>
P
Pan 已提交
15 16 17

      <div class="createPost-main-container">
        <el-row>
花裤衩 已提交
18 19
          <Warning />

20
          <el-col :span="24">
P
Pan 已提交
21
            <el-form-item style="margin-bottom: 40px;" prop="title">
22
              <MDinput v-model="postForm.title" :maxlength="100" name="name" required>
23
                Title
P
Pan 已提交
24 25 26 27 28 29
              </MDinput>
            </el-form-item>

            <div class="postInfo-container">
              <el-row>
                <el-col :span="8">
30 31
                  <el-form-item label-width="60px" label="Author:" class="postInfo-container-item">
                    <el-select v-model="postForm.author" :remote-method="getRemoteUserList" filterable default-first-option remote placeholder="Search user">
J
Jere 已提交
32
                      <el-option v-for="(item,index) in userListOptions" :key="item+index" :label="item" :value="item" />
花裤衩 已提交
33
                    </el-select>
P
Pan 已提交
34 35 36
                  </el-form-item>
                </el-col>

37
                <el-col :span="10">
38
                  <el-form-item label-width="120px" label="Publush Time:" class="postInfo-container-item">
P
Pan 已提交
39
                    <el-date-picker v-model="displayTime" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="Select date and time" />
P
Pan 已提交
40 41
                  </el-form-item>
                </el-col>
花裤衩 已提交
42

43
                <el-col :span="6">
44
                  <el-form-item label-width="90px" label="Importance:" class="postInfo-container-item">
45 46 47 48 49 50
                    <el-rate
                      v-model="postForm.importance"
                      :max="3"
                      :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
                      :low-threshold="1"
                      :high-threshold="3"
J
Jere 已提交
51 52
                      style="margin-top:8px;"
                    />
花裤衩 已提交
53 54
                  </el-form-item>
                </el-col>
P
Pan 已提交
55 56 57 58 59
              </el-row>
            </div>
          </el-col>
        </el-row>

60 61
        <el-form-item style="margin-bottom: 40px;" label-width="70px" label="Summary:">
          <el-input v-model="postForm.content_short" :rows="1" type="textarea" class="article-textarea" autosize placeholder="Please enter the content" />
62
          <span v-show="contentShortLength" class="word-counter">{{ contentShortLength }}words</span>
P
Pan 已提交
63 64
        </el-form-item>

P
Pan 已提交
65
        <el-form-item prop="content" style="margin-bottom: 30px;">
J
Jere 已提交
66
          <Tinymce ref="editor" v-model="postForm.content" :height="400" />
67
        </el-form-item>
P
Pan 已提交
68 69 70

        <el-form-item prop="image_uri" style="margin-bottom: 30px;">
          <Upload v-model="postForm.image_uri" />
71
        </el-form-item>
P
Pan 已提交
72 73 74 75 76 77
      </div>
    </el-form>
  </div>
</template>

<script>
P
Pan 已提交
78
import Tinymce from '@/components/Tinymce'
79
import Upload from '@/components/Upload/SingleImage3'
P
Pan 已提交
80 81
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
P
Pan 已提交
82
import { validURL } from '@/utils/validate'
P
Pan 已提交
83
import { fetchArticle } from '@/api/article'
84
import { searchUser } from '@/api/remote-search'
花裤衩 已提交
85 86
import Warning from './Warning'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from './Dropdown'
P
Pan 已提交
87

P
Pan 已提交
88
const defaultForm = {
P
Pan 已提交
89
  status: 'draft',
P
Pan 已提交
90 91 92 93 94 95 96 97
  title: '', // 文章题目
  content: '', // 文章内容
  content_short: '', // 文章摘要
  source_uri: '', // 文章外链
  image_uri: '', // 文章图片
  display_time: undefined, // 前台展示时间
  id: undefined,
  platforms: ['a-platform'],
花裤衩 已提交
98 99
  comment_disabled: false,
  importance: 0
P
Pan 已提交
100 101
}

P
Pan 已提交
102
export default {
103
  name: 'ArticleDetail',
Z
ZYSzys 已提交
104
  components: { Tinymce, MDinput, Upload, Sticky, Warning, CommentDropdown, PlatformDropdown, SourceUrlDropdown },
105 106 107 108 109 110
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
P
Pan 已提交
111 112 113 114 115 116 117
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
118
        callback(new Error(rule.field + '为必传项'))
P
Pan 已提交
119 120 121 122 123 124
      } else {
        callback()
      }
    }
    const validateSourceUri = (rule, value, callback) => {
      if (value) {
P
Pan 已提交
125
        if (validURL(value)) {
P
Pan 已提交
126 127
          callback()
        } else {
P
Pan 已提交
128
          this.$message({
P
Pan 已提交
129
            message: '外链url填写不正确',
P
Pan 已提交
130
            type: 'error'
P
Pan 已提交
131
          })
132
          callback(new Error('外链url填写不正确'))
P
Pan 已提交
133
        }
P
Pan 已提交
134 135 136 137 138
      } else {
        callback()
      }
    }
    return {
P
Pan 已提交
139
      postForm: Object.assign({}, defaultForm),
P
Pan 已提交
140
      loading: false,
花裤衩 已提交
141
      userListOptions: [],
P
Pan 已提交
142 143 144 145 146
      rules: {
        image_uri: [{ validator: validateRequire }],
        title: [{ validator: validateRequire }],
        content: [{ validator: validateRequire }],
        source_uri: [{ validator: validateSourceUri, trigger: 'blur' }]
147 148
      },
      tempRoute: {}
P
Pan 已提交
149 150 151 152 153
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content_short.length
花裤衩 已提交
154 155 156
    },
    lang() {
      return this.$store.getters.language
P
Pan 已提交
157 158 159 160 161 162 163 164 165 166 167 168
    },
    displayTime: {
      // set and get is useful when the data
      // returned by the back end api is different from the front end
      // back end return => "2013-06-25 06:59:25"
      // front end need timestamp => 1372114765000
      get() {
        return (+new Date(this.postForm.display_time))
      },
      set(val) {
        this.postForm.display_time = new Date(val)
      }
P
Pan 已提交
169 170 171 172
    }
  },
  created() {
    if (this.isEdit) {
花裤衩 已提交
173 174
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
175 176
    } else {
      this.postForm = Object.assign({}, defaultForm)
P
Pan 已提交
177
    }
178 179 180 181 182

    // Why need to make a copy of this.$route here?
    // Because if you enter this page and quickly switch tag, may be in the execution of the setTagsViewTitle function, this.$route is no longer pointing to the current page
    // https://github.com/PanJiaChen/vue-element-admin/issues/1221
    this.tempRoute = Object.assign({}, this.$route)
P
Pan 已提交
183
  },
P
Pan 已提交
184
  methods: {
花裤衩 已提交
185 186
    fetchData(id) {
      fetchArticle(id).then(response => {
P
Pan 已提交
187
        this.postForm = response.data
花裤衩 已提交
188 189 190
        // Just for test
        this.postForm.title += `   Article Id:${this.postForm.id}`
        this.postForm.content_short += `   Article Id:${this.postForm.id}`
花裤衩 已提交
191 192 193

        // Set tagsview title
        this.setTagsViewTitle()
P
Pan 已提交
194 195 196
      }).catch(err => {
        console.log(err)
      })
P
Pan 已提交
197
    },
花裤衩 已提交
198 199
    setTagsViewTitle() {
      const title = this.lang === 'zh' ? '编辑文章' : 'Edit Article'
200
      const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.postForm.id}` })
花裤衩 已提交
201
      this.$store.dispatch('tagsView/updateVisitedView', route)
花裤衩 已提交
202
    },
P
Pan 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    submitForm() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$notify({
            title: '成功',
            message: '发布文章成功',
            type: 'success',
            duration: 2000
          })
          this.postForm.status = 'published'
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
P
Pan 已提交
219
        }
P
Pan 已提交
220 221 222 223
      })
    },
    draftForm() {
      if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
P
Pan 已提交
224
        this.$message({
P
Pan 已提交
225 226
          message: '请填写必要的标题和内容',
          type: 'warning'
P
Pan 已提交
227
        })
P
Pan 已提交
228
        return
P
Pan 已提交
229
      }
P
Pan 已提交
230 231 232 233 234 235 236 237 238
      this.$message({
        message: '保存成功',
        type: 'success',
        showClose: true,
        duration: 1000
      })
      this.postForm.status = 'draft'
    },
    getRemoteUserList(query) {
花裤衩 已提交
239
      searchUser(query).then(response => {
P
Pan 已提交
240
        if (!response.data.items) return
花裤衩 已提交
241
        this.userListOptions = response.data.items.map(v => v.name)
P
Pan 已提交
242
      })
P
Pan 已提交
243
    }
P
Pan 已提交
244
  }
P
Pan 已提交
245
}
P
Pan 已提交
246
</script>
P
Pan 已提交
247

花裤衩 已提交
248
<style lang="scss" scoped>
249
@import "~@/styles/mixin.scss";
250

花裤衩 已提交
251 252
.createPost-container {
  position: relative;
253

花裤衩 已提交
254 255
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
256

花裤衩 已提交
257 258 259 260
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
261

花裤衩 已提交
262 263
      .postInfo-container-item {
        float: left;
P
Pan 已提交
264
      }
花裤衩 已提交
265
    }
P
Pan 已提交
266
  }
267

花裤衩 已提交
268 269 270
  .word-counter {
    width: 40px;
    position: absolute;
271
    right: 10px;
花裤衩 已提交
272 273 274
    top: 0px;
  }
}
275 276 277 278 279 280 281 282 283 284

.article-textarea /deep/ {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
P
Pan 已提交
285
</style>