##// END OF EJS Templates
Unselect issues when clicking outside of the list....
Jean-Philippe Lang -
r3430:69769e1dfde5
parent child
Show More
@@ -1,233 +1,237
1 /* redMine - project management software
1 /* redMine - project management software
2 Copyright (C) 2006-2008 Jean-Philippe Lang */
2 Copyright (C) 2006-2008 Jean-Philippe Lang */
3
3
4 var observingContextMenuClick;
4 var observingContextMenuClick;
5
5
6 ContextMenu = Class.create();
6 ContextMenu = Class.create();
7 ContextMenu.prototype = {
7 ContextMenu.prototype = {
8 initialize: function (url) {
8 initialize: function (url) {
9 this.url = url;
9 this.url = url;
10 this.createMenu();
10 this.createMenu();
11
11
12 if (!observingContextMenuClick) {
12 if (!observingContextMenuClick) {
13 Event.observe(document, 'click', this.Click.bindAsEventListener(this));
13 Event.observe(document, 'click', this.Click.bindAsEventListener(this));
14 Event.observe(document, (window.opera ? 'click' : 'contextmenu'), this.RightClick.bindAsEventListener(this));
14 Event.observe(document, (window.opera ? 'click' : 'contextmenu'), this.RightClick.bindAsEventListener(this));
15 observingContextMenuClick = true;
15 observingContextMenuClick = true;
16 }
16 }
17
17
18 this.unselectAll();
18 this.unselectAll();
19 this.lastSelected = null;
19 this.lastSelected = null;
20 },
20 },
21
21
22 RightClick: function(e) {
22 RightClick: function(e) {
23 this.hideMenu();
23 this.hideMenu();
24 // do not show the context menu on links
24 // do not show the context menu on links
25 if (Event.element(e).tagName == 'A') { return; }
25 if (Event.element(e).tagName == 'A') { return; }
26 // right-click simulated by Alt+Click with Opera
26 // right-click simulated by Alt+Click with Opera
27 if (window.opera && !e.altKey) { return; }
27 if (window.opera && !e.altKey) { return; }
28 var tr = Event.findElement(e, 'tr');
28 var tr = Event.findElement(e, 'tr');
29 if (tr == document || tr == undefined || !tr.hasClassName('hascontextmenu')) { return; }
29 if (tr == document || tr == undefined || !tr.hasClassName('hascontextmenu')) { return; }
30 Event.stop(e);
30 Event.stop(e);
31 if (!this.isSelected(tr)) {
31 if (!this.isSelected(tr)) {
32 this.unselectAll();
32 this.unselectAll();
33 this.addSelection(tr);
33 this.addSelection(tr);
34 this.lastSelected = tr;
34 this.lastSelected = tr;
35 }
35 }
36 this.showMenu(e);
36 this.showMenu(e);
37 },
37 },
38
38
39 Click: function(e) {
39 Click: function(e) {
40 this.hideMenu();
40 this.hideMenu();
41 if (Event.element(e).tagName == 'A') { return; }
41 if (Event.element(e).tagName == 'A') { return; }
42 if (window.opera && e.altKey) { return; }
42 if (window.opera && e.altKey) { return; }
43 if (Event.isLeftClick(e) || (navigator.appVersion.match(/\bMSIE\b/))) {
43 if (Event.isLeftClick(e) || (navigator.appVersion.match(/\bMSIE\b/))) {
44 var tr = Event.findElement(e, 'tr');
44 var tr = Event.findElement(e, 'tr');
45 if (tr!=null && tr!=document && tr.hasClassName('hascontextmenu')) {
45 if (tr!=null && tr!=document && tr.hasClassName('hascontextmenu')) {
46 // a row was clicked, check if the click was on checkbox
46 // a row was clicked, check if the click was on checkbox
47 var box = Event.findElement(e, 'input');
47 var box = Event.findElement(e, 'input');
48 if (box!=document && box!=undefined) {
48 if (box!=document && box!=undefined) {
49 // a checkbox may be clicked
49 // a checkbox may be clicked
50 if (box.checked) {
50 if (box.checked) {
51 tr.addClassName('context-menu-selection');
51 tr.addClassName('context-menu-selection');
52 } else {
52 } else {
53 tr.removeClassName('context-menu-selection');
53 tr.removeClassName('context-menu-selection');
54 }
54 }
55 } else {
55 } else {
56 if (e.ctrlKey) {
56 if (e.ctrlKey) {
57 this.toggleSelection(tr);
57 this.toggleSelection(tr);
58 } else if (e.shiftKey) {
58 } else if (e.shiftKey) {
59 if (this.lastSelected != null) {
59 if (this.lastSelected != null) {
60 var toggling = false;
60 var toggling = false;
61 var rows = $$('.hascontextmenu');
61 var rows = $$('.hascontextmenu');
62 for (i=0; i<rows.length; i++) {
62 for (i=0; i<rows.length; i++) {
63 if (toggling || rows[i]==tr) {
63 if (toggling || rows[i]==tr) {
64 this.addSelection(rows[i]);
64 this.addSelection(rows[i]);
65 }
65 }
66 if (rows[i]==tr || rows[i]==this.lastSelected) {
66 if (rows[i]==tr || rows[i]==this.lastSelected) {
67 toggling = !toggling;
67 toggling = !toggling;
68 }
68 }
69 }
69 }
70 } else {
70 } else {
71 this.addSelection(tr);
71 this.addSelection(tr);
72 }
72 }
73 } else {
73 } else {
74 this.unselectAll();
74 this.unselectAll();
75 this.addSelection(tr);
75 this.addSelection(tr);
76 }
76 }
77 this.lastSelected = tr;
77 this.lastSelected = tr;
78 }
78 }
79 } else {
79 } else {
80 // click is outside the rows
80 // click is outside the rows
81 var t = Event.findElement(e, 'a');
81 var t = Event.findElement(e, 'a');
82 if ((t != document) && (Element.hasClassName(t, 'disabled') || Element.hasClassName(t, 'submenu'))) {
82 if (t == document || t == undefined) {
83 Event.stop(e);
83 this.unselectAll();
84 } else {
85 if (Element.hasClassName(t, 'disabled') || Element.hasClassName(t, 'submenu')) {
86 Event.stop(e);
87 }
84 }
88 }
85 }
89 }
86 }
90 }
87 else{
91 else{
88 this.RightClick(e);
92 this.RightClick(e);
89 }
93 }
90 },
94 },
91
95
92 createMenu: function() {
96 createMenu: function() {
93 if (!$('context-menu')) {
97 if (!$('context-menu')) {
94 var menu = document.createElement("div");
98 var menu = document.createElement("div");
95 menu.setAttribute("id", "context-menu");
99 menu.setAttribute("id", "context-menu");
96 menu.setAttribute("style", "display:none;");
100 menu.setAttribute("style", "display:none;");
97 document.getElementById("content").appendChild(menu);
101 document.getElementById("content").appendChild(menu);
98 }
102 }
99 },
103 },
100
104
101 showMenu: function(e) {
105 showMenu: function(e) {
102 var mouse_x = Event.pointerX(e);
106 var mouse_x = Event.pointerX(e);
103 var mouse_y = Event.pointerY(e);
107 var mouse_y = Event.pointerY(e);
104 var render_x = mouse_x;
108 var render_x = mouse_x;
105 var render_y = mouse_y;
109 var render_y = mouse_y;
106 var dims;
110 var dims;
107 var menu_width;
111 var menu_width;
108 var menu_height;
112 var menu_height;
109 var window_width;
113 var window_width;
110 var window_height;
114 var window_height;
111 var max_width;
115 var max_width;
112 var max_height;
116 var max_height;
113
117
114 $('context-menu').style['left'] = (render_x + 'px');
118 $('context-menu').style['left'] = (render_x + 'px');
115 $('context-menu').style['top'] = (render_y + 'px');
119 $('context-menu').style['top'] = (render_y + 'px');
116 Element.update('context-menu', '');
120 Element.update('context-menu', '');
117
121
118 new Ajax.Updater({success:'context-menu'}, this.url,
122 new Ajax.Updater({success:'context-menu'}, this.url,
119 {asynchronous:true,
123 {asynchronous:true,
120 evalScripts:true,
124 evalScripts:true,
121 parameters:Form.serialize(Event.findElement(e, 'form')),
125 parameters:Form.serialize(Event.findElement(e, 'form')),
122 onComplete:function(request){
126 onComplete:function(request){
123 dims = $('context-menu').getDimensions();
127 dims = $('context-menu').getDimensions();
124 menu_width = dims.width;
128 menu_width = dims.width;
125 menu_height = dims.height;
129 menu_height = dims.height;
126 max_width = mouse_x + 2*menu_width;
130 max_width = mouse_x + 2*menu_width;
127 max_height = mouse_y + menu_height;
131 max_height = mouse_y + menu_height;
128
132
129 var ws = window_size();
133 var ws = window_size();
130 window_width = ws.width;
134 window_width = ws.width;
131 window_height = ws.height;
135 window_height = ws.height;
132
136
133 /* display the menu above and/or to the left of the click if needed */
137 /* display the menu above and/or to the left of the click if needed */
134 if (max_width > window_width) {
138 if (max_width > window_width) {
135 render_x -= menu_width;
139 render_x -= menu_width;
136 $('context-menu').addClassName('reverse-x');
140 $('context-menu').addClassName('reverse-x');
137 } else {
141 } else {
138 $('context-menu').removeClassName('reverse-x');
142 $('context-menu').removeClassName('reverse-x');
139 }
143 }
140 if (max_height > window_height) {
144 if (max_height > window_height) {
141 render_y -= menu_height;
145 render_y -= menu_height;
142 $('context-menu').addClassName('reverse-y');
146 $('context-menu').addClassName('reverse-y');
143 } else {
147 } else {
144 $('context-menu').removeClassName('reverse-y');
148 $('context-menu').removeClassName('reverse-y');
145 }
149 }
146 if (render_x <= 0) render_x = 1;
150 if (render_x <= 0) render_x = 1;
147 if (render_y <= 0) render_y = 1;
151 if (render_y <= 0) render_y = 1;
148 $('context-menu').style['left'] = (render_x + 'px');
152 $('context-menu').style['left'] = (render_x + 'px');
149 $('context-menu').style['top'] = (render_y + 'px');
153 $('context-menu').style['top'] = (render_y + 'px');
150
154
151 Effect.Appear('context-menu', {duration: 0.20});
155 Effect.Appear('context-menu', {duration: 0.20});
152 if (window.parseStylesheets) { window.parseStylesheets(); } // IE
156 if (window.parseStylesheets) { window.parseStylesheets(); } // IE
153 }})
157 }})
154 },
158 },
155
159
156 hideMenu: function() {
160 hideMenu: function() {
157 Element.hide('context-menu');
161 Element.hide('context-menu');
158 },
162 },
159
163
160 addSelection: function(tr) {
164 addSelection: function(tr) {
161 tr.addClassName('context-menu-selection');
165 tr.addClassName('context-menu-selection');
162 this.checkSelectionBox(tr, true);
166 this.checkSelectionBox(tr, true);
163 this.clearDocumentSelection();
167 this.clearDocumentSelection();
164 },
168 },
165
169
166 toggleSelection: function(tr) {
170 toggleSelection: function(tr) {
167 if (this.isSelected(tr)) {
171 if (this.isSelected(tr)) {
168 this.removeSelection(tr);
172 this.removeSelection(tr);
169 } else {
173 } else {
170 this.addSelection(tr);
174 this.addSelection(tr);
171 }
175 }
172 },
176 },
173
177
174 removeSelection: function(tr) {
178 removeSelection: function(tr) {
175 tr.removeClassName('context-menu-selection');
179 tr.removeClassName('context-menu-selection');
176 this.checkSelectionBox(tr, false);
180 this.checkSelectionBox(tr, false);
177 },
181 },
178
182
179 unselectAll: function() {
183 unselectAll: function() {
180 var rows = $$('.hascontextmenu');
184 var rows = $$('.hascontextmenu');
181 for (i=0; i<rows.length; i++) {
185 for (i=0; i<rows.length; i++) {
182 this.removeSelection(rows[i]);
186 this.removeSelection(rows[i]);
183 }
187 }
184 },
188 },
185
189
186 checkSelectionBox: function(tr, checked) {
190 checkSelectionBox: function(tr, checked) {
187 var inputs = Element.getElementsBySelector(tr, 'input');
191 var inputs = Element.getElementsBySelector(tr, 'input');
188 if (inputs.length > 0) { inputs[0].checked = checked; }
192 if (inputs.length > 0) { inputs[0].checked = checked; }
189 },
193 },
190
194
191 isSelected: function(tr) {
195 isSelected: function(tr) {
192 return Element.hasClassName(tr, 'context-menu-selection');
196 return Element.hasClassName(tr, 'context-menu-selection');
193 },
197 },
194
198
195 clearDocumentSelection: function() {
199 clearDocumentSelection: function() {
196 if (document.selection) {
200 if (document.selection) {
197 document.selection.clear(); // IE
201 document.selection.clear(); // IE
198 } else {
202 } else {
199 window.getSelection().removeAllRanges();
203 window.getSelection().removeAllRanges();
200 }
204 }
201 }
205 }
202 }
206 }
203
207
204 function toggleIssuesSelection(el) {
208 function toggleIssuesSelection(el) {
205 var boxes = el.getElementsBySelector('input[type=checkbox]');
209 var boxes = el.getElementsBySelector('input[type=checkbox]');
206 var all_checked = true;
210 var all_checked = true;
207 for (i = 0; i < boxes.length; i++) { if (boxes[i].checked == false) { all_checked = false; } }
211 for (i = 0; i < boxes.length; i++) { if (boxes[i].checked == false) { all_checked = false; } }
208 for (i = 0; i < boxes.length; i++) {
212 for (i = 0; i < boxes.length; i++) {
209 if (all_checked) {
213 if (all_checked) {
210 boxes[i].checked = false;
214 boxes[i].checked = false;
211 boxes[i].up('tr').removeClassName('context-menu-selection');
215 boxes[i].up('tr').removeClassName('context-menu-selection');
212 } else if (boxes[i].checked == false) {
216 } else if (boxes[i].checked == false) {
213 boxes[i].checked = true;
217 boxes[i].checked = true;
214 boxes[i].up('tr').addClassName('context-menu-selection');
218 boxes[i].up('tr').addClassName('context-menu-selection');
215 }
219 }
216 }
220 }
217 }
221 }
218
222
219 function window_size() {
223 function window_size() {
220 var w;
224 var w;
221 var h;
225 var h;
222 if (window.innerWidth) {
226 if (window.innerWidth) {
223 w = window.innerWidth;
227 w = window.innerWidth;
224 h = window.innerHeight;
228 h = window.innerHeight;
225 } else if (document.documentElement) {
229 } else if (document.documentElement) {
226 w = document.documentElement.clientWidth;
230 w = document.documentElement.clientWidth;
227 h = document.documentElement.clientHeight;
231 h = document.documentElement.clientHeight;
228 } else {
232 } else {
229 w = document.body.clientWidth;
233 w = document.body.clientWidth;
230 h = document.body.clientHeight;
234 h = document.body.clientHeight;
231 }
235 }
232 return {width: w, height: h};
236 return {width: w, height: h};
233 }
237 }
General Comments 0
You need to be logged in to leave comments. Login now