Blame view

public/js/home/common-address.js 6.73 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,
hongweigao authored
52 53
            selecter,
            selectAll;
weiqingting authored
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>');
hongweigao authored
77
                if (toDomId === the.streetsDomId) {
hongweigao authored
78
                    allCode === pCode ? selectAll = 'selected' : selectAll = '';
79
                    $toDom.append('<option value="' + pCode + '" ' + selectAll + '>全部</option>');
hongweigao authored
80
                }
weiqingting authored
81 82 83 84 85 86 87 88 89 90
                for (i in jsonData.options) {
                    if (jsonData.options[i]) {
                        val = jsonData.options[i];
                        nId = val.value;
                        selecter = '';

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

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

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

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

            if (pCode === '0') {
                return;
            }
hongweigao authored
125
            the.loadAreaData(pCode, the.areaDomId, '请选择区县');
weiqingting authored
126
            $('#' + the.areaDomId).show();
127
            $('#' + the.streetsDomId).hide().empty();
weiqingting authored
128 129 130 131
            the.showAreaSel(domOptions.dispDomId);
        });

        $('#' + the.areaDomId).change(function() {
hongweigao authored
132 133 134 135 136 137
            var pCode = $('#' + the.areaDomId).val();

            if (pCode === '0') {
                return;
            }
hongweigao authored
138
            the.loadAreaData(pCode, the.streetsDomId, '请选择乡镇/街道');
hongweigao authored
139
            $('#' + the.streetsDomId).show();
weiqingting authored
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            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
156
        if (typeof dispDomId !== 'undefined' && dispDomId !== '') {
weiqingting authored
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
            if (strProvince.indexOf('选择') < 0) {
                strAddr = strProvince;
            }

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

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

        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
190
            the.loadAreaData(areaCode.substr(0, 6), the.streetsDomId, '请选择乡镇/街道', areaCode);
weiqingting authored
191
            $('#' + this.areaDomId).show();
hongweigao authored
192 193 194 195 196 197 198 199 200 201
            $('#' + 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();
        }
202
        if (!noBind) {
hongweigao authored
203
            the.bindAreaChange(domOptions);
weiqingting authored
204 205 206 207 208
        }
    }
};

module.exports = address;