All files Group.js

65.48% Statements 55/84
48.89% Branches 22/45
50% Functions 5/10
70.51% Lines 55/78

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 230 231 232 233 234 235 236 237 2381x 1x   1x 1x 1x 1x 1x 1x                           1x                                             1x 1x       10x   10x 10x 10x 10x 10x   10x                       10x   10x               10x                         10x                         10x   10x 10x 10x                                     10x 10x 10x 40x 40x                                     10x       10x 10x 10x 9x 306x                           1x       10x           40x 10x 10x 10x 10x         1x 1x             306x 306x 306x 306x 306x                                             2x 2x 2x       1x      
import '../styles/icons.styl'
import '../styles/group.styl'
 
import dropdown from 'v-dropdown'
import data from '../mixins/data'
import method from '../mixins/method'
import search from '../mixins/selectorWithSearch'
import selector from '../mixins/selector'
import { PROVINCE_LEVEL, CITY_LEVEL, AREA_LEVEL, TOWN_LEVEL, LEVELS, LEVEL_LIST } from '../constants'
 
export default {
  name: 'RegionGroup',
  mixins: [data, method, search, selector],
  inheritAttrs: false,
  components: { dropdown },
  props: {
    search: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      list: [],
      query: '',
      level: -1
    }
  },
  watch: {
    /**
     * region search
     * search region description first, if no result, then search region key
     * @param value
     */
    query (value) {
      const list = this.getList(this.level)
      let tmp = []
      tmp = list.filter(val => val.value.toLowerCase().includes(value.toLowerCase()))
      if (tmp.length === 0) tmp = list.filter(val => val.key.includes(value))
      this.list = tmp
    },
    /**
     * current region group
     */
    level (val) {
      this.list = this.getList(val)
      this.adjust()
    }
  },
  render (h) {
    const child = []
 
    child.push(this.buildCaller(h))
    child.push(this.buildHeader(h))
    child.push(this.buildSearch(h))
    child.push(this.buildTabs(h))
    child.push(this.buildContent(h))
 
    return h('dropdown', {
      ref: 'drop',
      props: {
        border: false
      },
      on: {
        show: this.showChange
      }
    }, child)
  },
  methods: {
    buildHeader (h) {
      const child = []
 
      child.push(h('h3', [
        h('span', {
          class: {
            'rg-header-selected': this.selectedText
          }
        }, this.selectedText || this.lang.defaultHead)
      ]))
 
      child.push(h('button', {
        attrs: {
          type: 'button',
          title: this.lang.clear
        },
        class: 'rg-removeall-button',
        on: {
          click: this.clear
        }
      }, [
        h('i', { class: 'rg-iconfont rg-icon-remove' })
      ]))
 
      child.push(h('button', {
        attrs: {
          type: 'button',
          title: this.lang.done
        },
        class: 'rg-done-button',
        on: {
          click: this.close
        }
      }, [
        h('i', { class: 'rg-iconfont rg-icon-done' })
      ]))
 
      return h('div', { class: 'rg-header' }, child)
    },
    buildSearch (h) {
      Iif (!this.search) return
      return h('div', { class: 'rg-search' }, [
        h('input', {
          ref: 'search',
          class: 'rg-input',
          attrs: {
            type: 'text',
            autocomplete: 'off'
          },
          domProps: {
            value: this.query
          },
          on: {
            input: e => {
              this.query = e.target.value.trim()
            }
          }
        })
      ])
    },
    buildTabs (h) {
      const child = []
      LEVELS.forEach(val => {
        Eif (this.levelAvailable(val.index)) {
          child.push(h('li', {
            key: val.index,
            class: {
              active: val.index === this.level
            }
          }, [
            h('a', {
              attrs: {
                href: 'javascript:void(0);'
              },
              on: {
                click: () => {
                  this.level = val.index
                }
              }
            }, val.title)
          ]))
        }
      })
      return h('div', { class: 'rg-level-tabs' }, [
        h('ul', child)
      ])
    },
    buildContent (h) {
      const child = []
      if (this.list.length) {
        child.push(...this.list.map(val => {
          return h('li', {
            class: {
              'rg-item': true,
              active: this.match(val)
            },
            key: val.key,
            on: {
              mouseup: () => {
                this.pick(val)
              }
            }
          }, val.value)
        }))
      } else {
        child.push(h('li', {
          class: 'rg-message-box'
        }, this.lang.noMatch))
      }
      return h('div', { class: 'rg-results-container' }, [
        h('ul', { class: 'rg-results' }, child)
      ])
    },
    // check level available
    levelAvailable (level) {
      switch (level) {
        case PROVINCE_LEVEL: return true
        case CITY_LEVEL: return this.city
        case AREA_LEVEL: return this.city && this.area
        case TOWN_LEVEL: return this.city && this.area && this.town
      }
    },
    // load list when switch to next level
    getList (val) {
      switch (val) {
        case PROVINCE_LEVEL: return this.listProvince
        case CITY_LEVEL: return this.listCity
        case AREA_LEVEL: return this.listArea
        case TOWN_LEVEL: return this.listTown
      }
    },
    match (item) {
      Iif (!item || !Object.keys(item).length) return false
      const R = this.region
      const key = item.key
      switch (this.level) {
        case PROVINCE_LEVEL: return R.province && R.province.key === key
        case CITY_LEVEL: return R.city && R.city.key === key
        case AREA_LEVEL: return R.area && R.area.key === key
        case TOWN_LEVEL: return R.town && R.town.key === key
      }
    },
    nextLevel (level) {
      if (level === TOWN_LEVEL) return level
      return LEVELS[level + 1].index
    },
    pick (item) {
      const nextLevel = this.nextLevel(this.level)
      const attr = LEVEL_LIST[this.level]
      this.region[attr] = item
      this.change()
 
      if (this.levelAvailable(nextLevel) && this.level !== nextLevel) {
        this.level = nextLevel
      } else {
        this.close()
      }
    },
    clear () {
      this.clearRegion(PROVINCE_LEVEL)
      this.level = PROVINCE_LEVEL
      this.change()
    }
  },
  beforeMount () {
    this.level = PROVINCE_LEVEL
  }
}