bin.shen
2016-12-05 a4c9331bbfe3e8765ccdc1c54cc6931bac49cc82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * @author Roger Wu
 */
 
(function($){
    var allSelectBox = [];
    var killAllBox = function(bid){
        $.each(allSelectBox, function(i){
            if (allSelectBox[i] != bid) {
                if (!$("#" + allSelectBox[i])[0]) {
                    $("#op_" + allSelectBox[i]).remove();
                    //allSelectBox.splice(i,1);
                } else {
                    $("#op_" + allSelectBox[i]).css({ height: "", width: "" }).hide();
                }
                $(document).unbind("click", killAllBox);
            }
        });
    };
    
    var _onchange = function (event){
        var $ref = $("#"+event.data.ref);
        if ($ref.size() == 0) return false;
        $.ajax({
            type:'POST', dataType:"json", url:event.data.refUrl.replace("{value}", encodeURIComponent(event.data.$this.attr("value"))), cache: false,
            data:{},
            success: function(json){
                if (!json) return;
                var html = '';
 
                $.each(json, function(i){
                    if (json[i] && json[i].length > 1){
                        html += '<option value="'+json[i][0]+'">' + json[i][1] + '</option>';
                    }
                });
                
                var $refCombox = $ref.parents("div.combox:first");
                $ref.html(html).insertAfter($refCombox);
                $refCombox.remove();
                $ref.trigger("change").combox();
            },
            error: DWZ.ajaxError
        });
    };
                    
    $.extend($.fn, {
        comboxSelect: function(options){
            var op = $.extend({ selector: ">a" }, options);
            
            return this.each(function(){
                var box = $(this);
                var selector = $(op.selector, box);
 
                allSelectBox.push(box.attr("id"));
                $(op.selector, box).click(function(){
                    var options = $("#op_"+box.attr("id"));
                    if (options.is(":hidden")) {
                        if(options.height() > 300) {
                            options.css({height:"300px",overflow:"scroll"});
                        }
                        var top = box.offset().top+box[0].offsetHeight-50;
                        if(top + options.height() > $(window).height() - 20) {
                            top =  $(window).height() - 20 - options.height();
                        }
                        options.css({top:top,left:box.offset().left}).show();
                        killAllBox(box.attr("id"));
                        $(document).click(killAllBox);
                    } else {
                        $(document).unbind("click", killAllBox);
                        killAllBox();
                    }
                    return false;
                });
                $("#op_"+box.attr("id")).find(">li").comboxOption(selector, box);        
            });
        },
        comboxOption: function(selector, box){
            return this.each(function(){
                $(">a", this).click(function(){
                    var $this = $(this);
                    $this.parent().parent().find(".selected").removeClass("selected");
                    $this.addClass("selected");
                    selector.text($this.text());
                    
                    var $input = $("select", box);
                    if ($input.val() != $this.attr("value")) {
                        $("select", box).val($this.attr("value")).trigger("change");
                    }
                });
            });
        },
        combox:function(){
            /* 清理下拉层 */
            var _selectBox = [];
            $.each(allSelectBox, function(i){ 
                if ($("#" + allSelectBox[i])[0]) {
                    _selectBox.push(allSelectBox[i]);
                } else {
                    $("#op_" + allSelectBox[i]).remove();
                }
            });
            allSelectBox = _selectBox;
            
            return this.each(function(i){
                var $this = $(this).removeClass("combox");
                var name = $this.attr("name");
                var value= $this.val();
                var label = $("option[value=" + value + "]",$this).text();
                var ref = $this.attr("ref");
                var refUrl = $this.attr("refUrl") || "";
 
                var cid = $this.attr("id") || Math.round(Math.random()*10000000);
                var select = '<div class="combox"><div id="combox_'+ cid +'" class="select"' + (ref?' ref="' + ref + '"' : '') + '>';
                select += '<a href="javascript:" class="'+$this.attr("class")+'" name="' + name +'" value="' + value + '">' + label +'</a></div></div>';
                var options = '<ul class="comboxop" id="op_combox_'+ cid +'">';
                $("option", $this).each(function(){
                    var option = $(this);
                    options +="<li><a class=\""+ (value==option[0].value?"selected":"") +"\" href=\"#\" value=\"" + option[0].value + "\">" + option[0].text + "</a></li>";
                });
                options +="</ul>";
                
                $("body").append(options);
                $this.after(select);
                $("div.select", $this.next()).comboxSelect().append($this);
                
                if (ref && refUrl) {
                    $this.unbind("change", _onchange).bind("change", {ref:ref, refUrl:refUrl, $this:$this}, _onchange);
                }
                
            });
        }
    });
})(jQuery);