48e089696181231103a5da8bb04e36318434e7e2
[racktables] / wwwroot / js / tag-cb.js
1 var tag_cb = new function () { // singletone class
2
3 var self = this;
4 this.enableNegation = function() { enableNegation = true; };
5 var enableNegation = false;
6
7 // called from interface.php:renderCellFilterPortlet() if SHRINK_TAG_TREE_ON_CLICK is set
8 this.enableSubmitOnClick = function() { enableSubmitOnClick = true; };
9 var enableSubmitOnClick = false;
10
11 this.setTagShortList = function(list) { tagShortList = list; };
12 var tagShortList = {};
13
14 $(document).ready (function () {
15 if (! enableNegation)
16 return;
17 $('.tag-cb').click(function (event) {
18 cbClick(event, false);
19 });
20 $('.tag-cb').closest('label')
21 .mousedown(function(){return false})
22 .click(function (event) {
23 cbClick(event, true);
24 return false;
25 });
26 $('input[name=andor]').click(function (event) {
27 if ($('.tag-cb:checked').size() != 0 && event.target.value == 'and') {
28 $(event.target).closest('form')[0].submit();
29 }
30 });
31 });
32
33 // this is a handler for both checkbox click and label click
34 // suppresses the default action, sets cb checked state and brings input focus on int
35 // changes the name of checkbox if it is negated
36 // submits form if this feature is enabled, and filter combination function is 'and'
37 function cbClick (event, bInvert) {
38 event.stopImmediatePropagation();
39 var td = $(event.target).closest('td');
40 var cb = td.find('.tag-cb');
41 if (cb.length != 1)
42 return;
43 var tagId = cb.attr('value');
44 var checked = cb.attr('checked');
45 if (bInvert)
46 checked = ! checked;
47 if (event.ctrlKey && ! td.hasClass('inverted') && checked) {
48 var name = cb.attr('name');
49 if (name.match(/^cf([tp]\[\])$/))
50 cb.attr('name', 'nf' + RegExp.$1);
51 td.addClass ('inverted');
52 }
53 else {
54 var name = cb.attr('name');
55 if (name.match(/^nf([tp]\[\])$/))
56 cb.attr('name', 'cf' + RegExp.$1);
57 td.removeClass('inverted');
58 }
59 if (bInvert)
60 cb.attr('checked', checked ? 'checked' : '');
61 cb.focus();
62
63 if (enableSubmitOnClick) {
64 var and_check = $('input[name=andor][value=and]')[0];
65 if (and_check.checked)
66 $(event.target).closest('form')[0].submit();
67 }
68 }
69
70 this.compactTreeMode = function() {
71 // reconfigure toggle link
72 var link = $('a.toggleTreeMode')[0];
73 if ($(link).filter(':visible')) {
74 $(link).after('<p>');
75 }
76 link.onclick = function () {self.fullTreeMode(); return false;};
77 $(link).html('show full tree').show();
78
79 $('.tagtree').addClass('compact'); // disable hierachical padding
80
81 var separator = false; // next visible row is separator
82 var bPrevSeparator = true; // prev visible row was separator
83 $('input.tag-cb').each(function (i, item) {
84 var tr = $(item).closest('tr');
85
86 if ($(item).hasClass('root'))
87 separator = true;
88
89 if (! item.checked && ! tagShortList[item.value]) {
90 tr.hide();
91 return;
92 }
93
94 if (separator && ! bPrevSeparator) { // do not draw two separators together or very first separator
95 tr.addClass('separator');
96 bPrevSeparator = true;
97 }
98 else {
99 tr.removeClass('separator');
100 bPrevSeparator = false;
101 }
102 separator = false;
103 });
104 }
105
106 this.fullTreeMode = function() {
107 // reconfigure toggle link
108 var link = $('a.toggleTreeMode')[0];
109 link.onclick = function () {self.compactTreeMode(); return false;};
110 $(link).html('show compact tree').show();
111
112 $('.tagtree').removeClass('compact'); // restore hierachical padding
113
114 var bPrevSeparator = true; // prev visible row was separator
115 $('input.tag-cb').each(function (i, item) { // // do not draw two separators together or very first separator
116 var tr = $(item).closest('tr');
117 tr.removeClass('separator');
118
119 var separator = $(item).hasClass('root');
120 if (separator && ! bPrevSeparator)
121 tr.addClass('separator');
122 bPrevSeparator = separator;
123
124 tr.show();
125 });
126 }
127
128 };