File size: 6,444 Bytes
27867f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
$(document).ready(setTimeout(function(){
    $('form').submit(function() { // catch the form's submit
        $(this).find('input[type="checkbox"]').each(function() {
            if ($(this).is(":checked") == true) {
                var n = '#'+$(this).attr("id")+'hidden';
                $(n).prop("disabled", true);
            } else {
                var n = '#'+$(this).attr("id")+'hidden';
                $(n).prop("disabled", false);
            }
        });
        $('div[id$=-menu]').each(function() {
            $(this).hide();
        });

        if ( $('input[name=sort_dir]').val() ) {
            $.ajax({
                data: $(this).serialize(),
                type: $(this).attr('method'), // GET or POST
                url: $(this).attr('action'),
                success: function(response) { // on success
                    $('#tablecontent').html(response);
                }
            });
        } else {
            $.ajax({
                data: $(this).serialize(),
                type: $(this).attr('method'), // GET or POST
                url: $(this).attr('action'),
                success: function(response) { // on success
                    $('#status').html(response);
                }
            });
        }
        return false; // cancel original submit event
    });

    $('div#enabled-menu input').each(function() {
        $(this).click(function() {
            cbEnabledCheckboxClicked($(this));
        });
    });

    $("div[id$=-menu] input[type=text]").each(function() {
        var id = $(this).parent().parent().parent().prop('id');
        var name = id.replace('-menu', '');
        $(this).on('input', function() {
            var $this = $(this);
            keyDelay(function() {
                cbTextfilterTextKeyInput($this, name, id)
            }, 1200 );
        });
    });
    var keyDelay = (function() {
        var timer = 0;
        return function(callback, ms) {
            clearTimeout(timer);
            timer = setTimeout(callback, ms);
        };
    })();
    
    $('div[id$=-menu] input[id=text-mi][type=checkbox]').each(function() {
        var id = $(this).parent().parent().parent().prop('id');
        var name = id.replace('-menu', '');
        $(this).click(function() {
            cbTextfilterCheckboxClicked($(this), id, name);
        });
    });

    var cbEnabledCheckboxClicked = function(elemClicked) {
        if ( elemClicked.is(':checked') ) {
            c = true;
        } else {
            c = false;
        }
        filterText = elemClicked.prop('id').replace('-mi','');
        $('td.'+filterText).each(function() {
            if ( c ) {
                $(this).parent().removeClass(filterText+"-hide");
            } else {
                $(this).parent().addClass(filterText+"-hide");
            }
        });
        if ( $('div#enabled-menu input:checkbox:not(:checked)').length > 0 ) {
            $('table.sortable th:nth-child(1)').css({"background": "rgba(155,255,155,0.3)"});
        } else {
            $('table.sortable th:nth-child(1)').css({"background": ""});
        }
    }

    var cbTextfilterCheckboxClicked = function(elemClicked, id, name) {
        $("table.sortable th label:contains('"+name+"')").each(function() {
            index = $(this).parent().index()+1;
        });
        if ( !elemClicked.is(':checked') ) {
            $('div[id='+id+'] input[type=text]').val('');
            $('table.sortable th:nth-child('+index+')').css({"font-style": "inherit", "background": ""});
            $('table.sortable td:nth-child('+index+')').each(function() {
                $(this).parent().removeClass(name+"-hide");
            });
        }
    }

    var cbTextfilterTextKeyInput = function(elemClicked, name, id) {
        var keyValue = elemClicked.val();
        var index;
        $("table.sortable th label:contains('"+name+"')").each(function() {
            index = $(this).parent().index()+1;
        });
        if ( keyValue == "" ) {
            $('div[id='+id+'] input[type=checkbox]').prop('checked', false);
            $('table.sortable td:nth-child('+index+')').each(function() {
                $(this).parent().removeClass(name+"-hide");
            });
            $('table.sortable th:nth-child('+index+')').css({"font-style": "inherit", "background": ""});
        } else {
            $('div[id='+id+'] input[type=checkbox]').prop('checked', true);
            $('table.sortable td:nth-child('+index+')').each(function() {
                if ( $(this).find('input').length > 0 ) {
                    if ( keyValue.toLowerCase() == "blank" ) {
                        if ( !$(this).find('input').val() ) {
                            $(this).parent().removeClass(name+"-hide");
                        } else {
                            $(this).parent().addClass(name+"-hide");
                        }
                    } else {
                        if ( $(this).find('input').val().toLowerCase().includes(keyValue.toLowerCase()) ) {
                            $(this).parent().removeClass(name+"-hide");
                        } else {
                            $(this).parent().addClass(name+"-hide");
                        }
                    }
                } else {
                    if ( $(this).text().toLowerCase().includes(keyValue.toLowerCase()) ) {
                        $(this).parent().removeClass(name+"-hide");
                    } else {
                        $(this).parent().addClass(name+"-hide");
                    }
                }
            });
            $('table.sortable th:nth-child('+index+')').css(
                {"font-style": "italic", "background": "rgba(155,255,155,0.3)"});
        }
    }

    var resetFilters = function(arg1) {
        $('div.xmenu input[type=checkbox]').each(function() {
            var id = $(this).parent().parent().parent().prop('id');
            var name = id.replace('-menu', '');
            inputName = $(this).attr('name');
            ckbxStatus = $(this).is(':checked');
            if ( inputName.endsWith('-mi') ) {
                if (!ckbxStatus) {
                    cbEnabledCheckboxClicked($(this));
                }
            } else {
                if (ckbxStatus) {
                    cbTextfilterTextKeyInput($(this).parent().find('input[type=text]'), name, id);
                }
            }
        });
    }
    resetFilters();
    
}, 500));