Blame view

public/js/home/common-address.js 6.49 KB
weiqingting authored
1 2 3 4 5 6
/**
 * 个人中心页-地址管理
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/02/24
 */
陈轩 authored
7
var $ = require('yoho-jquery'),
yyq authored
8 9 10
    dialog = require('../common/dialog');

var Alert = dialog.Alert;
weiqingting authored
11 12 13 14 15 16 17 18 19 20 21 22

var address = {

    // 省的控件ID
    provinceDomId: 'province',

    // 城市的控件ID
    cityDomId: 'city',

    // 地区的控件ID
    areaDomId: 'areaCode',
hongweigao authored
23 24 25
    // 乡镇、街道的控件ID
    streetsDomId: 'streetsCode',
weiqingting authored
26 27 28
    initDomIds: function(domOptions) {
        var the = this;
yyq authored
29
        if (domOptions.provinceDomId) {
weiqingting authored
30 31
            the.provinceDomId = domOptions.provinceDomId;
        }
yyq authored
32
        if (domOptions.cityDomId) {
weiqingting authored
33 34
            the.cityDomId = domOptions.cityDomId;
        }
yyq authored
35
        if (domOptions.areaDomId) {
weiqingting authored
36 37
            the.areaDomId = domOptions.areaDomId;
        }
yyq authored
38
        if (domOptions.streetsDomId) {
hongweigao authored
39 40
            the.streetsDomId = domOptions.streetsDomId;
        }
weiqingting authored
41 42 43
    },

    // 初始化地址数据
hongweigao authored
44
    loadAreaData: function(pCode, toDomId, defaultValue, allCode) {
weiqingting authored
45 46
        var the = this,
            $toDom = $('#' + toDomId),
47
            i = 0;
weiqingting authored
48 49 50 51

        var active,
            val,
            nId,
yyq authored
52
            selecter;
weiqingting authored
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

        $toDom.empty();

        if (pCode < 91) {
            $('#' + the.areaDomId).empty();
            $('#county').attr('disabled', 'disabled');
        }

        $.ajax({
            type: 'GET',
            url: '/home/address/area',
            data: 'id=' + pCode,

            success: function(jsonData) {
                jsonData.code = 200;

                if (jsonData.code !== 200) {
                    active = new Alert('暂无数据');
                    active.show();
                    return false;
                }

                $toDom.append('<option value="0">' + defaultValue + '</option>');
                for (i in jsonData.options) {
郝肖肖 authored
77
                    if (jsonData.options[i] && jsonData.options[i].value) {
weiqingting authored
78 79 80 81 82 83 84 85
                        val = jsonData.options[i];
                        nId = val.value;
                        selecter = '';

                        if (typeof (allCode) !== 'undefined' && allCode !== 0 &&
                            nId === allCode.substr(0, nId.length)) {
                            selecter = 'selected';
                        }
86
                        $toDom.append('<option value="' + nId + '" ' + selecter + '>' + val.text + '</option>');
weiqingting authored
87 88 89 90 91 92 93 94
                    }
                }
            }
        });
    },
    bindAreaChange: function(domOptions) {
        var the = this;
yyq authored
95
        if ($('#' + the.provinceDomId).data('events')) {
weiqingting authored
96 97 98 99 100 101 102 103 104 105 106
            return;
        }

        // 初始化
        $('#' + the.provinceDomId).change(function() {
            var pCode = $('#' + the.provinceDomId).val();

            if (pCode === '0') {
                return;
            }
hongweigao authored
107
            the.loadAreaData(pCode, the.cityDomId, '请选择城市');
weiqingting authored
108
            $('#' + the.areaDomId).hide();
109
            $('#' + the.streetsDomId).hide().empty();
weiqingting authored
110 111 112 113 114 115 116 117 118 119
            the.showAreaSel(domOptions.dispDomId);
        });

        $('#' + this.cityDomId).change(function() {
            var pCode = $('#' + the.cityDomId).val();

            if (pCode === '0') {
                return;
            }
hongweigao authored
120
            the.loadAreaData(pCode, the.areaDomId, '请选择区县');
weiqingting authored
121
            $('#' + the.areaDomId).show();
122
            $('#' + the.streetsDomId).hide().empty();
weiqingting authored
123 124 125 126
            the.showAreaSel(domOptions.dispDomId);
        });

        $('#' + the.areaDomId).change(function() {
hongweigao authored
127 128 129 130 131 132
            var pCode = $('#' + the.areaDomId).val();

            if (pCode === '0') {
                return;
            }
hongweigao authored
133
            the.loadAreaData(pCode, the.streetsDomId, '请选择乡镇/街道');
hongweigao authored
134
            $('#' + the.streetsDomId).show();
weiqingting authored
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
            the.showAreaSel(domOptions.dispDomId);
        });

        the.showAreaSel(domOptions.dispDomId);
    },

    /**
     * 显示地区选择
     */
    showAreaSel: function(dispDomId) {
        var the = this,
            strAddr = '',
            strProvince = $('#' + the.provinceDomId).find('option:selected').text(),
            strCity = $('#' + the.cityDomId).find('option:selected').text(),
            strArea = $('#' + the.areaDomId).find('option:selected').text();
yyq authored
151
        if (typeof dispDomId !== 'undefined' && dispDomId !== '') {
weiqingting authored
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            if (strProvince.indexOf('选择') < 0) {
                strAddr = strProvince;
            }

            if (strCity !== '' && strCity.indexOf('选择') < 0) {
                strAddr += ',' + strCity;
            }

            if (strArea !== '' && strArea.indexOf('选择') < 0) {
                strAddr += ',' + strArea;
            }
            $('#' + dispDomId).html(strAddr);
        }
    },
167
    loadAllData: function(areaCode, domOptions, noBind) {
hongweigao authored
168
        var the = this;
weiqingting authored
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

        the.initDomIds(domOptions);
        areaCode += '';
        if (areaCode < 91) {
            the.loadAreaData(0, the.provinceDomId, '请选择省份', '');
            $('#' + the.areaDomId).hide();
            $('#' + the.cityDomId).html('<option value="0">请选择市</option>');
        } else if (areaCode.length === 4) {
            the.loadAreaData(0, the.provinceDomId, '请选择省份', areaCode);
            the.loadAreaData(areaCode.substr(0, 2), the.cityDomId, '请选择城市', areaCode);
            the.loadAreaData(areaCode, the.areaDomId, '请选择区县', areaCode);
            $('#' + the.areaDomId).show();
        } else if (areaCode.length === 6) {
            the.loadAreaData(0, the.provinceDomId, '请选择省份', areaCode);
            the.loadAreaData(areaCode.substr(0, 2), the.cityDomId, '请选择城市', areaCode);
            the.loadAreaData(areaCode.substr(0, 4), the.areaDomId, '请选择区县', areaCode);
hongweigao authored
185
            the.loadAreaData(areaCode.substr(0, 6), the.streetsDomId, '请选择乡镇/街道', areaCode);
weiqingting authored
186
            $('#' + this.areaDomId).show();
hongweigao authored
187 188 189 190 191 192 193 194 195 196
            $('#' + this.streetsDomId).show();
        } else if (areaCode.length === 9) {
            the.loadAreaData(0, the.provinceDomId, '请选择省份', areaCode);
            the.loadAreaData(areaCode.substr(0, 2), the.cityDomId, '请选择城市', areaCode);
            the.loadAreaData(areaCode.substr(0, 4), the.areaDomId, '请选择区县', areaCode);
            the.loadAreaData(areaCode.substr(0, 6), the.streetsDomId, '请选择乡镇/街道', areaCode);
            $('#' + this.areaDomId).show();
            $('#' + this.streetsDomId).show();
        }
197
        if (!noBind) {
hongweigao authored
198
            the.bindAreaChange(domOptions);
weiqingting authored
199 200 201 202 203
        }
    }
};

module.exports = address;