fileUpdate.vue 9.3 KB
Newer Older
K
khadgarmage 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
L
ligang 已提交
17 18 19
<template>
  <m-popup
          ref="popup"
20
          :ok-text="$t('Upload')"
G
i18n  
gongzijian 已提交
21
          :nameText="$t('File Upload')"
L
ligang 已提交
22 23 24 25 26 27 28 29 30 31
          @ok="_ok"
          :disabled="progress === 0 ? false : true">
    <template slot="content">
      <form name="files" enctype="multipart/form-data" method="post">
        <div class="file-update-model"
             @drop.prevent="_onDrop"
             @dragover.prevent="dragOver = true"
             @dragleave.prevent="dragOver = false"
             id="file-update-model">
          <div class="tooltip-info">
32
            <em class="ans ans-icon-warn-solid"></em>
G
i18n  
gongzijian 已提交
33
            <span>{{$t('Drag the file into the current upload window')}}</span>
L
ligang 已提交
34 35
          </div>
          <!--<div class="hide-archive" v-if="progress !== 0" @click="_ckArchive">
36
            <em class="fa fa-minus" data-toggle="tooltip" title="关闭窗口 继续上传" data-container="body" ></em>
L
ligang 已提交
37 38 39
          </div>-->
          <div class="update-popup" v-if="dragOver">
            <div class="icon-box">
40
              <em class="ans ans-icon-upload"></em>
L
ligang 已提交
41 42
            </div>
            <p class="p1">
G
i18n  
gongzijian 已提交
43
              <span>{{$t('Drag area upload')}}</span>
L
ligang 已提交
44 45 46
            </p>
          </div>
          <m-list-box-f>
47
            <template slot="name"><strong>*</strong>{{$t('File Name')}}</template>
L
ligang 已提交
48 49 50 51 52
            <template slot="content">
              <x-input
                      type="input"
                      v-model="name"
                      :disabled="progress !== 0"
G
i18n  
gongzijian 已提交
53
                      :placeholder="$t('Please enter name')"
L
ligang 已提交
54 55 56 57 58
                      autocomplete="off">
              </x-input>
            </template>
          </m-list-box-f>
          <m-list-box-f>
G
i18n  
gongzijian 已提交
59
            <template slot="name">{{$t('Description')}}</template>
L
ligang 已提交
60 61 62
            <template slot="content">
              <x-input
                      type="textarea"
B
break60 已提交
63
                      v-model="description"
L
ligang 已提交
64
                      :disabled="progress !== 0"
G
i18n  
gongzijian 已提交
65
                      :placeholder="$t('Please enter description')"
L
ligang 已提交
66 67 68 69 70
                      autocomplete="off">
              </x-input>
            </template>
          </m-list-box-f>
          <m-list-box-f>
71
            <template slot="name"><strong>*</strong>{{$t('Upload Files')}}</template>
L
ligang 已提交
72 73 74 75
            <template slot="content">
              <div class="file-update-box">
                <template v-if="progress === 0">
                  <input name="file" id="file" type="file" class="file-update">
G
i18n  
gongzijian 已提交
76
                  <x-button type="dashed" size="xsmall"> {{$t('Upload')}} </x-button>
L
ligang 已提交
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
                </template>
                <div class="progress-box" v-if="progress !== 0">
                  <m-progress-bar :value="progress" text-placement="left-right"></m-progress-bar>
                </div>
              </div>
            </template>
          </m-list-box-f>
        </div>
      </form>
    </template>
  </m-popup>
</template>
<script>
  import io from '@/module/io'
  import i18n from '@/module/i18n'
  import store from '@/conf/home/store'
  import mPopup from '@/module/components/popup/popup'
  import mListBoxF from '@/module/components/listBoxF/listBoxF'
  import mProgressBar from '@/module/components/progressBar/progressBar'

  export default {
    name: 'file-update',
    data () {
      return {
        store,
        // name
        name: '',
B
break60 已提交
104 105
        // description
        description: '',
L
ligang 已提交
106 107 108 109
        // progress
        progress: 0,
        // file
        file: '',
B
break60 已提交
110 111
        currentDir: '/',
        pid: -1,
L
ligang 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        // Whether to drag upload
        dragOver: false
      }
    },
    watch: {
    },
    props: {
      type: String
    },
    methods: {
      /**
       * submit
       */
      _ok () {
        this.$refs['popup'].spinnerLoading = true
        if (this._validation()) {
          this.store.dispatch('resource/resourceVerifyName', {
B
break60 已提交
129
            fullName: '/'+this.name,
L
ligang 已提交
130 131
            type: this.type
          }).then(res => {
132 133 134 135 136 137 138
            const isLt1024M = this.file.size / 1024 / 1024 < 1024
            if(isLt1024M) {
              this._formDataUpdate().then(res => {
                setTimeout(() => {
                  this.$refs['popup'].spinnerLoading = false
                }, 800)
              }).catch(e => {
L
ligang 已提交
139
                this.$refs['popup'].spinnerLoading = false
140 141 142
              })
            } else {
              this.$message.warning(`${i18n.$t('Upload File Size')}`)
L
ligang 已提交
143
              this.$refs['popup'].spinnerLoading = false
144
            }
L
ligang 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
          }).catch(e => {
            this.$message.error(e.msg || '')
            this.$refs['popup'].spinnerLoading = false
          })
        } else {
          this.$refs['popup'].spinnerLoading = false
        }
      },
      /**
       * validation
       */
      _validation () {
        if (!this.name) {
G
i18n  
gongzijian 已提交
158
          this.$message.warning(`${i18n.$t('Please enter file name')}`)
L
ligang 已提交
159 160 161
          return false
        }
        if (!this.file) {
G
i18n  
gongzijian 已提交
162
          this.$message.warning(`${i18n.$t('Please select the file to upload')}`)
L
ligang 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176
          return false
        }
        return true
      },
      /**
       * update file
       */
      _formDataUpdate () {
        return new Promise((resolve, reject) => {
          let self = this
          let formData = new FormData()
          formData.append('file', this.file)
          formData.append('type', this.type)
          formData.append('name', this.name)
B
break60 已提交
177 178
          formData.append('pid', this.pid)
          formData.append('currentDir', this.currentDir)
B
break60 已提交
179
          formData.append('description', this.description)
L
ligang 已提交
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
          io.post(`resources/create`, res => {
            this.$message.success(res.msg)
            resolve()
            self.$emit('onUpdate')
          }, e => {
            reject(e)
            self.$emit('close')
            this.$message.error(e.msg || '')
          }, {
            data: formData,
            emulateJSON: false,
            onUploadProgress (progressEvent) {
              // Size has been uploaded
              let loaded = progressEvent.loaded
              // Total attachment size
              let total = progressEvent.total
              self.progress = Math.floor(100 * loaded / total)
              self.$emit('onProgress', self.progress)
            }
          })
        })
      },
      /**
       * Archive to the top right corner Continue uploading
       */
      _ckArchive () {
        $('.update-file-modal').hide()
        this.$emit('onArchive')
      },
      /**
       * Drag and drop upload
       */
      _onDrop (e) {
        let file = e.dataTransfer.files[0]
        this.file = file
        this.name = file.name
        this.dragOver = false
      }
    },
    mounted () {
      $('#file').change(() => {
        let file = $('#file')[0].files[0]
        this.file = file
        this.name = file.name
      })
    },
    components: { mPopup, mListBoxF, mProgressBar }
  }
</script>

<style lang="scss" rel="stylesheet/scss">
  .file-update-model {
    .tooltip-info {
      position: absolute;
      left: 20px;
      bottom: 26px;
      span {
        font-size: 12px;
        color: #666;
        vertical-align: middle;
      }
241
      .fa,.ans {
L
ligang 已提交
242 243 244 245 246 247 248 249 250
        color: #0097e0;
        font-size: 14px;
        vertical-align: middle;
      }
    }
    .hide-archive {
      position: absolute;
      right: 22px;
      top: 17px;
251
      .fa,.ans{
L
ligang 已提交
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        font-size: 16px;
        color: #333;
        font-weight: normal;
        cursor: pointer;
        &:hover {
          color: #0097e0;
        }
      }
    }
    .file-update-box {
      padding-top: 4px;
      position: relative;
      .file-update {
        width: 70px;
        height: 40px;
        position: absolute;
        left: 0;
        top: 0;
        cursor: pointer;
        filter: alpha(opacity=0);
        -moz-opacity: 0;
        opacity: 0;
      }
      &:hover {
        .v-btn-dashed {
          background-color: transparent;
          border-color: #47c3ff;
          color: #47c3ff;
          cursor: pointer;
        }
      }
      .progress-box {
        width: 200px;
        position: absolute;
        left: 70px;
        top: 14px;
      }
    }
    .update-popup {
      width: calc(100% - 20px);
      height: calc(100% - 20px);
      background: rgba(255,253,239,.7);
      position: absolute;
      top: 10px;
      left: 10px;
      border-radius: 3px;
      z-index: 1;
      border: .18rem dashed #cccccc;
      .icon-box {
        text-align: center;
        margin-top: 96px;
303
        .fa,.ans {
L
ligang 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316
          font-size: 50px;
          color: #2d8cf0;
        }
      }
      .p1 {
        text-align: center;
        font-size: 16px;
        color: #333;
        padding-top: 8px;
      }
    }
  }
</style>