All files / src/components CityPicker.js

88.64% Statements 78/88
73.53% Branches 25/34
90.91% Functions 20/22
91.89% Lines 68/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 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 2301x 1x   1x 1x 1x 1x                             1x                                         5x                   2x 2x 1x 1x 391x 30x   1x   1x               1x 1x   1x 34x   1x   385x       1x           10x 10x   10x     10x                         2x             10x 188x               2354x             2x                   10x                         1x 1x   1x 1x 1x 1x       1x         1x 34x 30x 28x   1x 28x 10780x 10780x     1x     2x 2x 2x     4x 4x 4x                 1x           1x     1x 1x 1x     2x     2x   2x 2x     2356x 3130x       1x 1x      
import '../styles/icons.styl'
import '../styles/city.styl'
 
import { srcProvince, srcCity } from '../formatted'
import selector from '../mixins/selector'
import search from '../mixins/selectorWithSearch'
import dropdown from 'v-dropdown'
 
export default {
  name: 'CityPicker',
  mixins: [search, selector],
  inheritAttrs: false,
  components: { dropdown },
  props: {
    i18n: {
      type: String,
      default: 'cn'
    },
    value: Array
  },
  data () {
    return {
      /**
       * data list to display
       * [{
       *   province: { key: '', value: ''},
       *   citys: [
       *     {key: '', value: ''},
       *     {key: '', value: ''},
       *     ...
       *   ]
       * }]
       */
      list: [],
      // converted data list
      listBuilt: [],
      query: '',
      picked: []
    }
  },
  computed: {
    selectedText () {
      return this.picked.map(val => val.value).join(',')
    }
  },
  watch: {
    /**
     * region search
     * search region description first, if no result, then search region key
     * @param value
     */
    query (value) {
      const keyword = value.trim()
      if (keyword) {
        const list = []
        this.listBuilt.forEach(val => {
          const citys = val.citys.filter(city => new RegExp(keyword).test(city.value))
          if (citys.length) list.push({ province: val.province, citys: citys })
        })
        this.list = list
      } else {
        this.list = this.listBuilt.slice()
      }
    },
    /**
     * initialize selected citys
     */
    value: {
      handler (val) {
        Eif (Array.isArray(val)) {
          Iif (this.equal(val)) return
 
          Eif (val.length) {
            const provincialCity = srcProvince.filter(item => val.includes(item.key))
            // marge province and city
            this.picked = [
              ...provincialCity,
              ...srcCity.filter(item => val.includes(item.key))
            ]
          } else this.picked = []
 
          this.emit(false)
        }
      },
      immediate: true
    }
  },
  render (h) {
    const child = []
 
    child.push(this.buildCaller(h))
 
    // search bar
    child.push(h('div', { class: 'rg-search-bar' }, [
      h('input', {
        ref: 'search',
        class: 'rg-input',
        attrs: {
          type: 'text',
          autocomplete: 'off'
        },
        domProps: {
          value: this.query.trim()
        },
        on: {
          input: e => {
            this.query = e.target.value.trim()
          }
        }
      })
    ]))
 
    // province grouped city list
    child.push(h('div', { class: 'rg-picker' }, this.list.map(val => {
      return h('div', {
        class: 'rg-picker__row',
        key: val.province.key
      }, [
        h('dl', [
          h('dt', val.province.value),
          h('dd', [
            h('ul', val.citys.map(city => {
              return h('li', {
                key: city.key,
                class: {
                  selected: this.inPicked(city)
                },
                on: {
                  click: () => {
                    this.pick(city)
                  }
                }
              }, city.value)
            }))
          ])
        ])
      ])
    })))
 
    return h('dropdown', {
      ref: 'drop',
      props: {
        border: false
      },
      on: {
        show: this.showChange
      }
    }, child)
  },
  methods: {
    prepared () {
      // beijing, tianjin, shanghai, chongqing
      const municipalitys = ['110000', '120000', '310000', '500000']
      const municipality = '000000'
      // hongkong, macao
      const specials = ['810000', '820000']
      const special = '000010'
      const listTmp = []
      const municipalityObj = {
        province: { key: municipality, value: '直辖市' },
        citys: []
      }
      const specialObj = {
        province: { key: special, value: '特别行政区' },
        citys: []
      }
      // set provinces
      srcProvince.forEach(val => {
        if (municipalitys.includes(val.key)) municipalityObj.citys.push(val)
        else if (specials.includes(val.key)) specialObj.citys.push(val)
        else listTmp.push({ province: val, citys: [] })
      })
      listTmp.forEach(val => {
        val.citys = srcCity.filter(value => {
          const num = Number.parseInt(val.province.key)
          return (value.key - num) < 1e4 && (value.key % num) < 1e4
        })
      })
      this.listBuilt = [...[municipalityObj], ...listTmp, ...[specialObj]]
    },
    // dropdown position adjust
    adjust () {
      this.$nextTick(() => {
        this.$refs.drop.adjust()
      })
    },
    emit (input = true) {
      if (input) this.$emit('input', this.picked.map(val => val.key))
      this.$emit('values', this.picked)
    },
    /**
     * v-model/value(keys) whether equal to picked keys
     *
     * @param {array} keys
     * @returns
     */
    equal (keys) {
      Iif (keys.length === this.picked.length) {
        if (!keys.length) return true
        this.picked.forEach(val => {
          if (!keys.includes(val.key)) return false
        })
        return true
      } else return false
    },
    clear () {
      this.picked = []
      this.close()
      this.emit()
    },
    pick (item) {
      Iif (this.inPicked(item)) {
        this.picked.splice(this.picked.findIndex(val => val.key === item.key), 1)
      } else {
        this.picked.push(item)
      }
      this.emit()
      this.adjust()
    },
    inPicked (city) {
      if (!city || !this.picked.length) return false
      return this.picked.some(val => val.key === city.key)
    }
  },
  created () {
    this.prepared()
    this.list = this.listBuilt.slice()
  }
}