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