vivo-ui
Version:
vivo ui component lib for vue
102 lines (89 loc) • 2.53 kB
JavaScript
import _Object$keys from 'babel-runtime/core-js/object/keys';
import ajax from '../../tools/ajax/index';
import PopupPicker from '../popup-picker/index';
(function () {
if (typeof document !== 'undefined') {
var head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style'),
css = "";style.type = 'text/css';if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}head.appendChild(style);
}
})();
function format(address) {
var data = {};
if (address instanceof Array) {
data.options = address.slice();
} else {
data.options = [];
_Object$keys(address).forEach(function (value) {
if (address[value] instanceof Array && !address[value].length) {
data.options.push(value);
} else {
data.options.push({
value: value,
children: format(address[value])
});
}
});
}
return data;
}
var index = { render: function render() {
var _vm = this;var _h = _vm.$createElement;var _c = _vm._self._c || _h;return _vm.address ? _c('popup-picker', _vm._b({ attrs: { "show": _vm.value, "data": _vm.address }, on: { "toggle": function toggle($event) {
_vm.$emit('input', $event);
}, "update": _vm.update } }, 'popup-picker', _vm.$attrs, false)) : _vm._e();
}, staticRenderFns: [],
name: 'vui-city-picker',
components: {
PopupPicker: PopupPicker
},
props: {
value: {
type: Boolean,
default: false
},
filter: { // 过滤特殊地区
type: Boolean,
default: true
}
},
data: function data() {
return {
address: undefined
};
},
watch: {
filter: {
handler: function handler() {
this.getAddress();
},
immediate: true
}
},
methods: {
getAddress: function getAddress() {
var _this = this;
ajax({
url: '//shequwsdl.vivo.com.cn/shequ/address_' + (this.filter ? 'filter' : 'full') + '.json',
success: function success(result) {
_this.address = format(result);
}
});
},
update: function update(indexs) {
var options = this.address.options;
this.$emit('update', indexs.map(function (value) {
value = options[value];
if (value.children) {
options = value.children.options;
value = value.value;
}
return value;
}));
}
}
};
export default index;