banner-editor.vue 4.77 KB
<template>
  <div>
    <table class="banner-table">
      <tbody>
        <draggable v-model="value_" @start="drag=true" @end="drag=false">
          <tr v-for="(i,index) in value_" :key="i.id">
          <th valign="middle" width="5%">{{+index + 1}}</th>

          <th valign="middle">
            <div style="display: inline-block;">

              <file-upload
                :default-file="i.src | removeImageParams"
                :id="index"
                @success="onUploadSuccess"
                @remove="onUploadRemove"
                >
              </file-upload>
            </div>
          </th>

          <th class="banner-item" valign="middle">
            <banner-params v-model="i.url"></banner-params>
          </th>

          <th valign="middle" v-if="!hideDeleteButton">
            <Button type="error" @click="onDeleteBtnClick(i, index)">删除</Button>
          </th>
        </tr>
        </draggable>
      </tbody>
    </table>

    <div v-if="!hideAddButton" style="margin-top: 20px;">
      <Button @click="onAddBtnClick" type="primary">添加一张</Button>
    </div>
  </div>
</template>

<script>

import fileUpload from './drag-file-upload';
import bannerParams from './banner-params';
import util from '@/libs/util';

import draggable from 'vuedraggable';

export default {
  props: ['value', 'hideAddButton', 'hideDeleteButton'],
  filters: {
    removeImageParams(url) {
      if (!url) {
        return;
      }

      return url.split('?')[0];
    }
  },
  data() {
    return {
      value_: this.handleData(util.clone(this.value))
    };
  },
  methods: {
    onDeleteBtnClick(i, index) {
      this.value_.splice(index, 1);
      this.$emit('input', this.handleSaveData(this.value_));
    },
    onAddBtnClick() {
      this.value_.push({
        alt: '',
        bgColor: '',
        imgId: '0',
        isFocusRec:'0',
        src: '',
        url: {
          action: 'go.h5',
          url: '',
          islogin: 'Y',
          headerid: 1
        }
      });

      this.$emit('input', this.handleSaveData(this.value_));
    },
    onUploadSuccess(id, {url}) {
      this.value_[id].src = url + '?imageView2/{mode}/w/{width}/h/{height}';
      this.$emit('input', this.handleSaveData(this.value_));
    },
    onUploadRemove(id) {
      this.value_[id].src = '';
      this.$emit('input', this.handleSaveData(this.value_));
    },
    handleData(m) {
      let keys = Object.keys(m);

      keys.sort((a,b) => {
        a = +a;
        b = +b;

        if (a > b) {
          return 1;
        }

        if (a < b) {
          return -1;
        }

        return 0;
      });



      let result = [];

      for (const i of keys) {
        let a = m[i];
        this.addId(a);

        if (a.url.action === 'go.ufo') {
          if (util.getUrlQueryString(a.url.url, 'pagename') === 'productDetail') {
            a.url.action = 'go.detail';
          } else if (util.getUrlQueryString(a.url.url, 'pagename') === 'productList') {
            a.url.action = 'go.pool';
          }  else if(util.getUrlQueryString(a.url.url, 'pagename') === 'saleCalendar') {
            a.url.action = 'go.calendar';
          } else if(util.getUrlQueryString(a.url.url, 'pagename') === 'hotSale') {
            a.url.action = 'go.hotlist';
          } else {
            a.url.action = 'go.list';
          }
        }

        result.push(a);
      }

      return result;
    },
    handleSaveData(m) {
      let temp = util.clone(m);

      for (const i of Object.keys(temp)) {
        if (['go.pool', 'go.detail', 'go.list', 'go.calendar', 'go.hotlist'].includes(temp[i].url.action)) {
          temp[i].url.action = 'go.ufo';
        }

        if (util.getUrlQueryString(temp[i].url.url, 'headerid') === '-1') {
          temp[i].url.headerid = '-1';
        }

        if (util.getUrlQueryString(temp[i].url.url, 'islogin') === 'Y') {
          temp[i].url.islogin = 'Y'
        }
      }

      // array to object
      const result = {}

      for (const i of Object.keys(temp)) {
        result[i] = temp[i];
      }

      return result;
    },
    getValue(){
      console.log('getValue=>',this.handleSaveData(this.value_))
      return this.handleSaveData(this.value_);
    },
    addId(m) {
      m.id = this.$bus.genId();
    },
    setValue(newVal) {
      this.value_ = this.handleData(util.clone(newVal));
    },
    reset() {
      this.value_ = [];
    }

  },
  watch: {
    value(newVal)  {
      this.value_ = this.handleData(util.clone(newVal));
    }
  },
  components: {
    fileUpload,
    bannerParams,
    draggable
  }
};
</script>


<style lang="scss" scoped>

.banner-table {
  width: 100%;
  border-collapse: collapse;
}

.banner-table {
  tr {
    height: 200px;

    th {
      border: 1px solid #dddee1;
    }
  }
}

tr:hover {
  background-color: #ebf7ff;
}

.banner-item {
  padding: 20px;
}

</style>