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

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

var address = {

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

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

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

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

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

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

        var active,
            val,
            nId,
            selecter;

        $toDom.empty();

        if (pCode < 91) {
            $('#areaCode').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]) {
                        val = jsonData.options[i];
                        point = (toDomId === 'areaCode' && val.is_support === 'Y') ? '*' : '';
                        nId = val.value;
                        selecter = '';

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

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

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

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

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

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

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

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

        $('#' + the.areaDomId).change(function() {
            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 (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) {
        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);
            $('#' + this.areaDomId).show();
        }
        the.bindAreaChange(domOptions);
    }
};

module.exports = address;