vue-ant-patterns
Version:
vue-ant-patterns
233 lines (190 loc) • 6.16 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _vue = require('vue');
var _vue2 = _interopRequireDefault(_vue);
var _antDesignVue = require('ant-design-vue');
var _defaultProps = require('./defaultProps');
var _defaultProps2 = _interopRequireDefault(_defaultProps);
var _bizUtils = require('../_biz-utils');
var _renderType = require('./renderType');
var _renderType2 = _interopRequireDefault(_renderType);
var _extend = require('extend');
var _extend2 = _interopRequireDefault(_extend);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
// import { hasProp } from 'ant-design-vue/lib/_util/props-util';
var globalExpire = (global || window).globalJSConfig.globalExpire;
// const token = Vue.ils.get('token');
var cacheName = 'tableCollection';
var BTableCollection = {
mixins: [_renderType2['default']],
name: 'b-table-collection',
props: (0, _extends3['default'])({}, _defaultProps2['default']),
cacheCodeOptions: ((0, _bizUtils.getCache)(cacheName) || {})[cacheName] || {},
model: {
prop: 'value',
event: 'change'
},
created: function created() {
// const { tableCollection = {} } = getCache(cacheName) || {};
// this.$options.cacheCodeOptions = tableCollection;
this.queryOptions();
},
mounted: function mounted() {},
data: function data() {
var _$props = this.$props,
value = _$props.value,
type = _$props.type,
multiple = _$props.multiple;
var val = value;
var vType = type;
if (multiple) {
if (typeof val === 'string') {
val = [val];
}
if (type === 'radio') {
vType = 'checkbox';
}
}
if (!multiple && type === 'checkbox') {
if (Array.isArray(val)) {
val = val[val.length - 1];
}
vType = 'radio';
}
return {
sValue: val,
vType: vType,
options: [],
sMultiple: multiple ? 'multiple' : 'default'
};
},
watch: {
multiple: function multiple(v, oV) {
var sValue = this.sValue;
this.sMultiple = v ? 'multiple' : 'default';
this.setTypeAndVal();
},
tableName: function tableName(nName, oName) {
if (oName !== nName) {
this.queryOptions();
}
},
type: function type(v) {
this.vType = v;
this.setTypeAndVal();
},
value: function value(v) {
this.sValue = v;
}
},
methods: {
setTypeAndVal: function setTypeAndVal() {
var _$props2 = this.$props,
multiple = _$props2.multiple,
type = _$props2.type;
var sValue = this.sValue,
vType = this.vType;
var val = sValue;
if (multiple) {
if (typeof val === 'string') {
this.sValue = [val];
}
if (vType === 'radio') {
this.vType = 'checkbox';
}
}
if (!multiple) {
if (Array.isArray(val)) {
this.sValue = val[val.length - 1];
}
if (vType === 'checkbox') {
this.vType = 'radio';
}
}
},
renderComponent: function renderComponent() {
var h = this.$createElement;
var _$props3 = this.$props,
status = _$props3.status,
keys = _$props3.keys;
var options = this.options,
sValue = this.sValue,
vType = this.vType;
var _keys = (0, _slicedToArray3['default'])(keys, 2),
k = _keys[0],
n = _keys[1];
var opt = options.map(function (it) {
var v = it[k],
label = it[n];
if (Array.isArray(sValue)) {
return sValue.includes(v) ? it : null;
}
return v === sValue ? it : null;
}).filter(function (T) {
return T;
});
if (status === 'view') {
return h('span', [opt.length ? opt.length === 1 ? opt[0][n] : opt.map(function (it) {
return h(_antDesignVue.Tag, [it[n]]);
}) : null]);
}
if (!vType) return null;
return this['render$' + vType]();
},
queryOptions: function queryOptions() {
var _this = this;
var _$props4 = this.$props,
tableName = _$props4.tableName,
queryParam = _$props4.queryParam;
var cacheCodeOptions = this.$options.cacheCodeOptions;
if (!tableName) return;
var cloneQueryParam = (0, _extend2['default'])(true, { data: {} }, queryParam);
var cacheData = cacheCodeOptions[tableName];
if (cacheData) {
this.options = cacheData;
this.$emit('optionLoad', (0, _extend2['default'])({}, cacheData));
return;
}
cloneQueryParam.data.tableName = tableName;
(0, _bizUtils.request)(cloneQueryParam).then(function (res) {
var resData = res.data;
var msg = resData.msg,
code = resData.code,
data = resData.data;
if (code === 200) {
_this.options = data;
_this.$options.cacheCodeOptions[tableName] = data;
(0, _bizUtils.setCache)(data, tableName, cacheName);
// const cacheData = {
// ...(cache || {}),
// 'table-collection': {
// ...(tableCollectionCache || {}),
// [tableName]: data,
// },
// };
// const expire = Vue.ils.get('cookieExpire') || globalExpire;
// Vue.ils.set('dataCache', cacheData, expire);
_this.$emit('optionLoad', data);
} else {
_this.$message.error(msg);
}
})['finally'](function () {
_this.$root.isLoading = false;
});
}
},
render: function render() {
return this.renderComponent();
}
};
BTableCollection.tableCollectionProps = _defaultProps2['default'];
BTableCollection.install = function (Vue) {
Vue.component(BTableCollection.name, BTableCollection);
};
exports['default'] = BTableCollection;