address.js 6.54 KB
/**
 * 地址管理
 * @author: bikai<kai.bi@yoho.cn>
 * @date: 2015/11/17
 */

var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading');

var $action = $('.action'),
    $addressForm = $('.edit-address'),
    $submit = $('.submit'),
    $addAddress = $('.add-address'),
    $editAddressPage = $('.my-edit-address-page'),
    $addressListPage = $('.my-address-list-page'),
    $area = $('.area'),
    $footer = $('#yoho-footer'),
    $confim = $('.confim-mask'),
    $pageWrap = $('.page-wrap'),
    $backBtn = $('.nav-back'),
    $addressItem = $('.address-item'),
    $navTitle = $('.nav-title'),
    isSubmiting,
    deleteId,
    currentPage = 'address',
    newArea = [];

// 清除返回按钮原有链接
$backBtn.attr('href', 'javascript:void(0);');
window.rePosFooter();

// 自定义返回按钮事件
$backBtn.on('touchend', function(e) {
    if (currentPage === 'edit') {
        $pageWrap.hide();
        $pageWrap.first().show();
        e.preventDefault();
        currentPage = 'address';
        $navTitle.html('地址管理');
        window.rePosFooter();
    } else if (currentPage === 'list') {
        $pageWrap.hide();
        $editAddressPage.show();
        e.preventDefault();
        currentPage = 'edit';
        $navTitle.html('修改地址');
    } else {
        window.history.go(-1);
    }
});

function editAddress(data) {
    data = data || {};
    $addressForm.find('[name="id"]').val(data.id || '');
    $addressForm.find('[name="consignee"]').val(data.consignee || '');
    $addressForm.find('[name="mobile"]').val(data.mobile || '');
    $addressForm.find('[name="area_code"]').val(data.areaCode || '');
    $addressForm.find('[name="area"]').val(data.area || '');
    $addressForm.find('[name="address"]').val(data.address || '');

    currentPage = 'edit';
    $pageWrap.hide();
    $editAddressPage.show();

    $footer.addClass('bottom');
}

$confim.on('touchend', '.cancel', function() {
    deleteId = null;
    $confim.hide();
}).on('touchend', '.confim', function() {
    loading.showLoadingMask();
    $.ajax({
        method: 'POST',
        url: '/home/deladdress',
        data: {
            id: deleteId
        }
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            res = {};
        }
        if (res.code !== 200) {
            tip.show(res.message || '网络出了点问题~');
        } else {
            window.location.reload();
        }
    }).fail(function() {
        tip.show('网络出了点问题~');
    }).always(function() {
        deleteId = null;
        $confim.hide();
        loading.hideLoadingMask();
    });
});

// 提交表单请求
$addressForm.on('submit', function() {
    if (isSubmiting) {
        return false;
    }

    // 简单的表单校验
    if (!$(this).find('[name="consignee"]').val()) {
        tip.show('收件人不能为空');

        // $(this).find('[name="consignee"]').focus();
        return false;
    }
    if (!$(this).find('[name="mobile"]').val()) {
        tip.show('手机号不能为空');

        // $(this).find('[name="mobile"]').focus();
        return false;
    }
    if (!$(this).find('[name="area_code"]').val() || !$(this).find('[name="area"]').val()) {
        tip.show('省市区不能为空');
        return false;
    }
    if (!$(this).find('[name="address"]').val()) {
        tip.show('地址不能为空');

        // $(this).find('[name="address"]').focus();
        return false;
    }

    isSubmiting = true;
    loading.showLoadingMask();
    $.ajax({
        method: 'POST',
        url: '/home/saveaddress',
        data: $(this).serialize()
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            res = {};
        }
        if (res.code !== 200) {
            tip.show(res.message || '网络出了点问题~');
        } else {
            window.location.reload();
        }
    }).fail(function() {
        tip.show('网络出了点问题~');
    }).always(function() {
        isSubmiting = false;
        loading.hideLoadingMask();
    });
    return false;
});

// 添加地址
$addAddress.on('touchend', function() {
    if ($addressItem.length >= 5) {
        tip.show('您最多添加5个收货地址');
        return false;
    }
    editAddress();
    $navTitle.html('添加新地址');
});

// 编辑或删除
$action.on('touchend', '.edit', function() {
    editAddress($(this).data());
    $navTitle.html('修改地址');
}).on('touchend', '.del', function() {
    deleteId = $(this).data('id');
    $confim.show();
});

$submit.on('touchend', function() {
    $addressForm.submit();
    return false;
}).on('touchstart', function() {
    $(this).addClass('highlight');
}).on('touchend touchcancel', function() {
    $(this).removeClass('highlight');
});

// 省市区
$area.on('touchend', function() {
    $pageWrap.hide();
    $addressListPage.show(1, function() {
        $footer.hide();
    });
    currentPage = 'list';
});

// touchend 在下滑的时候会触发
// 省市区联动
$addressListPage.find('.address').each(function(i, elem) {
    var addressHammer = new Hammer(elem);

    addressHammer.on('tap', function(e) {
        var $this = $(e.target);

        newArea.push($this.children('.caption').text());
        $this.siblings().hide();
        $this.children('ul').show().children('li').show();

        e.srcEvent.preventDefault();
        e.srcEvent.stopPropagation();
    });
});

$addressListPage.find('.address-last').each(function(i, elem) {
    var addressLastHammer = new Hammer(elem);

    addressLastHammer.on('tap', function(e) {
        var $this = $(e.target);

        // 填结果到 html
        newArea.push($this.children('.caption').text());
        $('[name="area"]').val(newArea.join(' '));
        $('[name="area_code"]').val($this.data('id'));

        $editAddressPage.show();
        currentPage = 'edit';
        $navTitle.html('修改地址');
        $footer.show();

        // 恢复默认的三级选择
        $addressListPage.hide();
        $addressListPage.find('ul').hide();
        $addressListPage.children('ul').show().children('li').show();
        newArea = [];

        e.srcEvent.preventDefault();
        e.srcEvent.stopPropagation();
    });
});

$($editAddressPage, $addressListPage).css('min-height', function() {
    return $(window).height() - $('#yoho-header').height();
});

$pageWrap.first().css('min-height', function() {
    return $(window).height() - $('#yoho-header').height() - $footer.height();
});

$('input, textarea').on('focus', function() {
    $footer.hide();
}).on('blur', function() {
    $footer.show();
});