common-address.js 6.49 KB
/**
 * 个人中心页-地址管理
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/02/24
 */

var $ = require('yoho-jquery'),
    dialog = require('../common/dialog');

var Alert = dialog.Alert;

var address = {

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

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

    // 地区的控件ID
    areaDomId: 'areaCode',

    // 乡镇、街道的控件ID
    streetsDomId: 'streetsCode',

    initDomIds: function(domOptions) {
        var the = this;

        if (domOptions.provinceDomId) {
            the.provinceDomId = domOptions.provinceDomId;
        }
        if (domOptions.cityDomId) {
            the.cityDomId = domOptions.cityDomId;
        }
        if (domOptions.areaDomId) {
            the.areaDomId = domOptions.areaDomId;
        }
        if (domOptions.streetsDomId) {
            the.streetsDomId = domOptions.streetsDomId;
        }
    },

    // 初始化地址数据
    loadAreaData: function(pCode, toDomId, defaultValue, allCode) {
        var the = this,
            $toDom = $('#' + toDomId),
            i = 0;

        var active,
            val,
            nId,
            selecter;

        $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) {
                    if (jsonData.options[i] && jsonData.options[i].value) {
                        val = jsonData.options[i];
                        nId = val.value;
                        selecter = '';

                        if (typeof(allCode) !== 'undefined' && allCode !== 0 &&
                            nId === allCode.substr(0, nId.length)) {
                            selecter = 'selected';
                        }
                        $toDom.append('<option value="' + nId + '" ' + selecter + '>' + val.text + '</option>');
                    }
                }
            }
        });
    },
    bindAreaChange: function(domOptions) {
        var the = this;

        if ($('#' + the.provinceDomId).data('events')) {
            return;
        }

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

            if (pCode === '0') {
                return;
            }

            the.loadAreaData(pCode, the.cityDomId, '请选择城市');
            $('#' + the.areaDomId).hide();
            $('#' + the.streetsDomId).hide().empty();
            the.showAreaSel(domOptions.dispDomId);
        });

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

            if (pCode === '0') {
                return;
            }

            the.loadAreaData(pCode, the.areaDomId, '请选择区县');
            $('#' + the.areaDomId).show();
            $('#' + the.streetsDomId).hide().empty();
            the.showAreaSel(domOptions.dispDomId);
        });

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

            if (pCode === '0') {
                return;
            }

            the.loadAreaData(pCode, the.streetsDomId, '请选择乡镇/街道');
            $('#' + the.streetsDomId).show();
            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();

        if (typeof dispDomId !== 'undefined' && dispDomId !== '') {
            if (strProvince.indexOf('选择') < 0) {
                strAddr = strProvince;
            }

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

            if (strArea !== '' && strArea.indexOf('选择') < 0) {
                strAddr += ',' + strArea;
            }
            $('#' + dispDomId).html(strAddr);
        }
    },

    loadAllData: function(areaCode, domOptions, noBind) {
        var the = this;

        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);
            the.loadAreaData(areaCode.substr(0, 6), the.streetsDomId, '请选择乡镇/街道', areaCode);
            $('#' + this.areaDomId).show();
            $('#' + 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();
        }

        if (!noBind) {
            the.bindAreaChange(domOptions);
        }
    }
};

module.exports = address;