MultiLevel.js 10.6 KB
/**
 * founder : zzf
 * Creation time :  2015/3/18
 */
define(function( require, exports, module ) {

    var regions = [],sorts = [];//对象管理池
    var MultiLevel = function(){
        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
        };

        var methods = {
            foundView : function(){
                var view = '<div class="title"><div title=""></div>'
                           +'<span class="s-live"></span>'
                           +'<p class="region-data"></p></div>'
                           +'<div class="region-view">'
                           +'<div class="head"><ul></ul></div>'
                           +'<div class="cont clearfix"><ul></ul></div></div>';
                config.view.html(view);
                this.parseParam();
            },
            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];
                    config.parameter.field.pid = param[1];
                    config.parameter.field.did = param[2];
                    config.parameter.field.name = param[3];
                    config.parameter.param = param[4];
                    config.constraintTier = parseInt(param[5]);
                }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(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){
                if(data && data.length > 0){
                    config.data[config.parameter.id] = data;
                    methods.setView(data);
                }else{
                    methods.setData();
                };
            },
            getStorage : function(id){
                if(config.data[id]){
                    return config.data[id];
                }else{
                    return false;
                }
            },
            setView : function(data){
                methods.setHead(data);
                methods.setCont(data);
                if(config.isEvents){
                    methods.events();
                    config.isEvents = false;
                };
            },
            setHead : function(data){
                config.view.find(".head ul").html('');
                var view = "";
                for(i in config.heads){
                    view += '<li class="" zindex="'+config.page+'" pid="'
                            +config.heads[i].id+'">'
                            +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.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(config.heads);
                config.view.find(".title div").html(str);
                config.view.find(".region-view").hide();
            },
            headDel : function(id){
                var page = 0;
                for(i in config.heads){
                    if(config.heads[i].id == 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");
                for(i in config.heads){
                    if(config.heads[i].id == pid){
                        config.heads[i] = {
                            id : pid,
                            name : text,
                            did : view.attr("did")
                        }
                        methods.setData();
                        return false;
                    }
                }
                if(text != "其他区县" && text != "其他城市"){
                    return true;
                }
                return false;
            },
            events : function(){
                config.view.find(".head li").live("click", function(){
                    config.parameter.id = $(this).attr("pid");
                    methods.headDel(config.parameter.id);
                });
                config.view.find(".cont li").live("click", function(){
                    config.parameter.id = $(this).attr("did");
                    if(methods.headIsNmae($(this))){
                        config.heads.push({id : $(this).attr("pid"),
                            name : $(this).html(),
                            did : $(this).attr("did")
                        });
                        methods.http();
                    }
                });
                config.view.hover(function(){
                    $(this).find(".region-view").show();
                }, function(){
                    $(this).find(".region-view").hide();
                });
            },
            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.pid],
                                name : data[i][config.parameter.field.name],
                                did : data[i][config.parameter.field.did]
                            });
                        };
                        methods.setData(list);
                        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();
        };
        return this;
    };

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