logistics-region.js 13.9 KB
/**
 * @author zzf
 */



var region = (function(){
    var regions = [],sorts = [];//对象管理池
    var param = {
        floor : 0 //只允许选择多少层
    };
    var MultiLevel = function(){
        var _this = this;

        var config = {
            data : {},
            view : "",
            parameter : {
                url : "",
                setUrl : "",
                id : 0,
                field : {
                    pid : "parentId",
                    did : "regionId",
                    name : "regionName"
                },
                param : "parentId",
                setNmae : "city"
            },
            heads : [],
            sub : 0,
            constraintTier : 0,
            page : 0,
            isEvents : true,
            isinternation : 0
        };

        var methods = {
            foundView : function(){
                var view =  '<div class="title"><div title="\"></div>'+
                            '<span class="s-live"></span>'+
                            '<p class="region-data" style="display: none;"></p></div>'+
                            '<div class="region-view">'+
                            '<div class="head"><ul></ul><span class="reset">重置</span></div>'+
                            '<div class="cont clearfix"><ul></ul></div></div>';
                config.view.html(view);
                this.parseParam();
                methods.events();
            },
            parseParam : function(){
                if(config.view.attr("setUrl")){
                    config.parameter.setUrl = config.view.attr("setUrl");
                    methods.setValue();
                    return false;
                }
                try{
                    var param = config.view.attr("data");
                    if(!param){methods.http(); return false;}
                    param = param.split(",");
                    config.parameter.setNmae = param[0];//上传参数name
                    config.parameter.field.pid = param[1];//父节点id
                    config.parameter.field.did = param[2];//子节点id
                    config.parameter.field.name = param[3];//文字对应名称
                    config.parameter.param = param[4];//请求参数key
                    config.constraintTier = parseInt(param[5]);//操作显示page
                }catch (error){
                    methods.error("朋友参数传递错误了,请仔细检查下", error);
                };
                methods.http();
            },
            setUrl : function(){
                if(config.view.attr("url")){
                    config.parameter.url = config.view.attr("url");
                }else{
                    methods.error("朋友参数url没有设置,请仔细检查下");
                };
                config.parameter.url = config.parameter.url+'?'+config.parameter.param+'='+config.parameter.id;
            },
            http : function(){
                if(param.floor && config.heads.length >=  param.floor){
                    methods.setData();
                    return false;
                };
                if(methods.getStorage(config.parameter.id)){
                    methods.setView(methods.getStorage(config.parameter.id));
                    return true;
                };
                methods.setUrl();
                $.ajax({
                    type : 'GET',
                    url : config.parameter.url,
                    dataType : 'jsonp',
                    success : function(data){
                        methods.setStorage(data);
                    },
                    error : function(error){
                        methods.error("请求数据异常~~~", error);
                    }
                });
            },
            setStorage : function(data){
                data = methods.tidiesData(data);
                if(data && data.length > 0){
                    config.data[config.parameter.id] = data;
                    methods.setView(data);
                }else{
                    methods.setData();
                    config.view.find(".region-view").hide();
                };
            },
            tidiesData : function(data){
                var list = [];
                for(i in data){
                    if(data[i][config.parameter.field.pid] == -9999){
                        data[i][config.parameter.field.pid] = 0;
                        list.push(data[i]);
                        config.isinternation = 1;
                    }else if(data[i][config.parameter.field.pid] == 0 && config.isinternation){
                        data[i][config.parameter.field.pid] = -10;
                        list.push(data[i]);
                    }else{
                        return data;
                    }
                };
                return list;
            },
            getStorage : function(id){
                if(config.data[id]){
                    return config.data[id];
                }else{
                    return false;
                }
            },
            setView : function(data){
                methods.setHead(data);
                methods.setCont(data);
            },
            setHead : function(data){
                config.view.find(".head ul").html('');
                var view = "";
                for(i in config.heads){
                    view += '<li class="" zindex="'+config.page+'" did="'+config.heads[i].id+'" pid="'+config.heads[i].pid+'">'
                        +config.heads[i].name+'</li>';
                }
                view += '<li class="act" zindex="'+config.page+'" pid="">请选择</li>';
                config.view.find(".head ul").html(view);
            },
            setCont : function(data){
                config.view.find(".cont ul").html('');
                var view = "";
                for(i in data){
                    view += '<li class="" zindex="'+config.page+'"\
                        pid="'+data[i][config.parameter.field.pid]+'"\
                        did="'+data[i][config.parameter.field.did]+'">'+data[i][config.parameter.field.name]+'</li>';
                }
                config.view.find(".cont ul").html(view);
            },
            setData : function(data){
                var list = [];
                var id = 0;
                var str = "";
                if(data){
                    list = data;
                }else{
                    list = config.heads;
                };
                for(i in list){
                    str += "  "+list[i].name;
                    if(parseInt(i) == (list.length - 1)){
                        id = list[i].id;
                    };
                }
                var isInput = config.view.prevAll(".region-hide");
                if(isInput.attr("type") && (isInput.attr("type") == "hidden" || isInput.attr("type") == "text")){
                    isInput.val(str+"/"+id);
                }else{
                    var view = '<input type="hidden" class="region-hide" name="'+config.parameter.setNmae+'" value="'+(str+"/"+id)+'" />';
                    config.view.before($(view));
                    isInput.val(str+"/"+id);
                }
                config.view.find(".region-data").html(JSON.stringify(config.heads));
                config.view.find(".title div").html(str);
                var leng = config.heads.length - 1;
            },
            headDel : function(id){
                var page = 0;
                for(i in config.heads){
                    if(config.heads[i].pid == id){
                        page = parseInt(i);
                    }
                }
                config.heads.splice(page, (config.heads.length - page));
                methods.http();
            },
            headIsNmae : function(view){
                var text = view.html();
                var pid =  view.attr("pid");
                var did =  view.attr("did");
                for(i in config.heads){
                    if(config.heads[i].pid == pid){
                        config.heads[i] = {
                            id : did,
                            name : text,
                            pid : pid
                        }
                        return false;
                    }
                }
                if(text != "其他区县" && text != "其他城市"){
                    return true;
                }
                return false;
            },
            isConstraintTier : function(){
                var leng = config.heads.length - 1;
                if(config.constraintTier && leng >= config.constraintTier){
                    return true;
                }
                return false;
            },
            events : function(){
                config.view.find(".head").on("click", 'li', function(){
                    config.parameter.id = $(this).attr("pid");
                    methods.headDel(config.parameter.id);
                });
                config.view.find(".cont").on("click", 'li', function(){
                    config.parameter.id = $(this).attr("did");
                    if(methods.headIsNmae($(this))){
                        config.heads.push({id : $(this).attr("did"),
                            name : $(this).html(),
                            pid : $(this).attr("pid")
                        });
                    }
                    if(! methods.isConstraintTier()){
                        if(methods.isCity()){
                            methods.setData();
                            config.view.find(".region-view").hide();
                        }else{
                            methods.http();
                        }
                    }else{
                        methods.setData();
                        config.view.find(".region-view").hide();
                    }
                });
                config.view.find(".title").click(function () {

                    var rg = $("body").find(".m-region .region-view");
                    for ( var i=0,l=rg.length; i<l; i++ ){
                        if ( config.view.attr('sub') == rg.eq(i).attr('sub') ){
                            continue;
                        };
                        rg.eq(i).hide();
                    }
                    //_this.reset();
                    $(this).next().show();
                    return false;
                });
                config.view.find(".reset").click(function(){
                    _this.reset();
                    return false;
                });
                config.view.click(function(){
                    return false;
                });
                $(document).click(function(e) {
                    if($(e.target).hasClass("m-region")){
                        return false;
                    }
                    config.view.find(".region-view").hide();
                });
            },
            isCity : function(){//检测是不是直辖市
                var id = config.parameter.id;
                var cids = ["111", "112", "150", "131"];
                for(i in cids){
                    if(cids[i] == id){
                        return true;
                    }
                }
                return false;
            },
            error : function(txt, error){
               // alert(txt+", 详情请查看控制台log");
               // if(error){console.log(error)};
            },
            setValue : function(){
                $.ajax({
                    type : 'GET',
                    url : config.parameter.setUrl,
                    dataType : 'jsonp',
                    success : function(data){
                        data = data.reverse();
                        var list = [];
                        for(i in data){
                            list.push({
                                id : data[i][config.parameter.field.did],
                                name : data[i][config.parameter.field.name],
                                pid : data[i][config.parameter.field.pid]
                            });
                        };
                        methods.setData(list);
                        config.parameter.id = 0;
                        config.heads = [];
                        methods.http();
                    },
                    error : function(error){
                        methods.error("请求数据异常~~~", error);
                    }
                });
            }
        };

        this.start = function(view, sub){
            config.sub = sub;
            config.view = $(view);
            config.view.attr("sub", sub);
            methods.foundView();
        };
        this.getData = function(){
            methods.getData();
        };
        this.update = function(){
            if(config.view.attr("setUrl")){
                config.parameter.setUrl = config.view.attr("setUrl");
                methods.setValue();
            }
        };
        this.reset = function(){
            config.heads = [];
            config.parameter.id = 0;
            methods.http();
            config.view.find(".title div").html('');
            config.view.find(".s-live").html('');
        };
        return this;
    };

	return {
        start : function(){
            this.allregion();
        },
        allregion : function(){
            $(".m-region").each(function(){
                if(!$(this).attr("sub")){
                    var ml = new MultiLevel();
                    regions.push(ml);
                    ml.start(this, (regions.length - 1));
                };
            });
        },
        aRegion : function(view){
            var ml = new MultiLevel();
            regions.push(ml);
            ml.start(view, (regions.length - 1));
            return ml;
        },
        del : function(page){
            if(!page){return false};
            $(".m-region").each(function(i){
                if(i == page){
                    regions.splice(i, 1);
                    return true;
                };
            });
            return false;
        },
        setParam : function(obj){
            $.extend(param, obj);
        }
    };
})();