##// END OF EJS Templates
Display the context menu above and/or to the left of the click if needed (patch by Mike Duchene, closes #960)....
Jean-Philippe Lang -
r1308:f6ce427a0065
parent child
Show More
@@ -1,161 +1,218
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
10
11 // prevent selection when using Ctrl/Shit key
11 // prevent selection when using Ctrl/Shit key
12 var tables = $$('table.issues');
12 var tables = $$('table.issues');
13 for (i=0; i<tables.length; i++) {
13 for (i=0; i<tables.length; i++) {
14 tables[i].onselectstart = function () { return false; } // ie
14 tables[i].onselectstart = function () { return false; } // ie
15 tables[i].onmousedown = function () { return false; } // mozilla
15 tables[i].onmousedown = function () { return false; } // mozilla
16 }
16 }
17
17
18 if (!observingContextMenuClick) {
18 if (!observingContextMenuClick) {
19 Event.observe(document, 'click', this.Click.bindAsEventListener(this));
19 Event.observe(document, 'click', this.Click.bindAsEventListener(this));
20 Event.observe(document, (window.opera ? 'click' : 'contextmenu'), this.RightClick.bindAsEventListener(this));
20 Event.observe(document, (window.opera ? 'click' : 'contextmenu'), this.RightClick.bindAsEventListener(this));
21 observingContextMenuClick = true;
21 observingContextMenuClick = true;
22 }
22 }
23
23
24 this.unselectAll();
24 this.unselectAll();
25 this.lastSelected = null;
25 this.lastSelected = null;
26 },
26 },
27
27
28 RightClick: function(e) {
28 RightClick: function(e) {
29 this.hideMenu();
29 this.hideMenu();
30 // do not show the context menu on links
30 // do not show the context menu on links
31 if (Event.findElement(e, 'a') != document) { return; }
31 if (Event.findElement(e, 'a') != document) { return; }
32 // right-click simulated by Alt+Click with Opera
32 // right-click simulated by Alt+Click with Opera
33 if (window.opera && !e.altKey) { return; }
33 if (window.opera && !e.altKey) { return; }
34 var tr = Event.findElement(e, 'tr');
34 var tr = Event.findElement(e, 'tr');
35 if ((tr == document) || !tr.hasClassName('hascontextmenu')) { return; }
35 if ((tr == document) || !tr.hasClassName('hascontextmenu')) { return; }
36 Event.stop(e);
36 Event.stop(e);
37 if (!this.isSelected(tr)) {
37 if (!this.isSelected(tr)) {
38 this.unselectAll();
38 this.unselectAll();
39 this.addSelection(tr);
39 this.addSelection(tr);
40 this.lastSelected = tr;
40 this.lastSelected = tr;
41 }
41 }
42 this.showMenu(e);
42 this.showMenu(e);
43 },
43 },
44
44
45 Click: function(e) {
45 Click: function(e) {
46 this.hideMenu();
46 this.hideMenu();
47 if (Event.findElement(e, 'a') != document) { return; }
47 if (Event.findElement(e, 'a') != document) { return; }
48 if (window.opera && e.altKey) { return; }
48 if (window.opera && e.altKey) { return; }
49 if (Event.isLeftClick(e) || (navigator.appVersion.match(/\bMSIE\b/))) {
49 if (Event.isLeftClick(e) || (navigator.appVersion.match(/\bMSIE\b/))) {
50 var tr = Event.findElement(e, 'tr');
50 var tr = Event.findElement(e, 'tr');
51 if (tr!=document && tr.hasClassName('hascontextmenu')) {
51 if (tr!=document && tr.hasClassName('hascontextmenu')) {
52 // a row was clicked, check if the click was on checkbox
52 // a row was clicked, check if the click was on checkbox
53 var box = Event.findElement(e, 'input');
53 var box = Event.findElement(e, 'input');
54 if (box!=document) {
54 if (box!=document) {
55 // a checkbox may be clicked
55 // a checkbox may be clicked
56 if (box.checked) {
56 if (box.checked) {
57 tr.addClassName('context-menu-selection');
57 tr.addClassName('context-menu-selection');
58 } else {
58 } else {
59 tr.removeClassName('context-menu-selection');
59 tr.removeClassName('context-menu-selection');
60 }
60 }
61 } else {
61 } else {
62 if (e.ctrlKey) {
62 if (e.ctrlKey) {
63 this.toggleSelection(tr);
63 this.toggleSelection(tr);
64 } else if (e.shiftKey) {
64 } else if (e.shiftKey) {
65 if (this.lastSelected != null) {
65 if (this.lastSelected != null) {
66 var toggling = false;
66 var toggling = false;
67 var rows = $$('.hascontextmenu');
67 var rows = $$('.hascontextmenu');
68 for (i=0; i<rows.length; i++) {
68 for (i=0; i<rows.length; i++) {
69 if (toggling || rows[i]==tr) {
69 if (toggling || rows[i]==tr) {
70 this.addSelection(rows[i]);
70 this.addSelection(rows[i]);
71 }
71 }
72 if (rows[i]==tr || rows[i]==this.lastSelected) {
72 if (rows[i]==tr || rows[i]==this.lastSelected) {
73 toggling = !toggling;
73 toggling = !toggling;
74 }
74 }
75 }
75 }
76 } else {
76 } else {
77 this.addSelection(tr);
77 this.addSelection(tr);
78 }
78 }
79 } else {
79 } else {
80 this.unselectAll();
80 this.unselectAll();
81 this.addSelection(tr);
81 this.addSelection(tr);
82 }
82 }
83 this.lastSelected = tr;
83 this.lastSelected = tr;
84 }
84 }
85 } else {
85 } else {
86 // click is outside the rows
86 // click is outside the rows
87 var t = Event.findElement(e, 'a');
87 var t = Event.findElement(e, 'a');
88 if ((t != document) && (Element.hasClassName(t, 'disabled') || Element.hasClassName(t, 'submenu'))) {
88 if ((t != document) && (Element.hasClassName(t, 'disabled') || Element.hasClassName(t, 'submenu'))) {
89 Event.stop(e);
89 Event.stop(e);
90 }
90 }
91 }
91 }
92 }
92 }
93 },
93 },
94
94
95 showMenu: function(e) {
95 showMenu: function(e) {
96 $('context-menu').style['left'] = (Event.pointerX(e) + 'px');
96 var mouse_x = Event.pointerX(e);
97 $('context-menu').style['top'] = (Event.pointerY(e) + 'px');
97 var mouse_y = Event.pointerY(e);
98 Element.update('context-menu', '');
98 var render_x = mouse_x;
99 new Ajax.Updater({success:'context-menu'}, this.url,
99 var render_y = mouse_y;
100 var dims;
101 var menu_width;
102 var menu_height;
103 var window_width;
104 var window_height;
105 var max_width;
106 var max_height;
107
108 $('context-menu').style['left'] = (render_x + 'px');
109 $('context-menu').style['top'] = (render_y + 'px');
110 Element.update('context-menu', '');
111
112 new Ajax.Updater({success:'context-menu'}, this.url,
100 {asynchronous:true,
113 {asynchronous:true,
101 evalScripts:true,
114 evalScripts:true,
102 parameters:Form.serialize(Event.findElement(e, 'form')),
115 parameters:Form.serialize(Event.findElement(e, 'form')),
103 onComplete:function(request){
116 onComplete:function(request){
117 dims = $('context-menu').getDimensions();
118 menu_width = dims.width;
119 menu_height = dims.height;
120 max_width = mouse_x + 2*menu_width;
121 max_height = mouse_y + menu_height;
122
123 var ws = window_size();
124 window_width = ws.width;
125 window_height = ws.height;
126
127 /* display the menu above and/or to the left of the click if needed */
128 if (max_width > window_width) {
129 render_x -= menu_width;
130 $('context-menu').addClassName('reverse-x');
131 } else {
132 $('context-menu').removeClassName('reverse-x');
133 }
134 if (max_height > window_height) {
135 render_y -= menu_height;
136 $('context-menu').addClassName('reverse-y');
137 } else {
138 $('context-menu').removeClassName('reverse-y');
139 }
140 if (render_x <= 0) render_x = 1;
141 if (render_y <= 0) render_y = 1;
142 $('context-menu').style['left'] = (render_x + 'px');
143 $('context-menu').style['top'] = (render_y + 'px');
144
104 Effect.Appear('context-menu', {duration: 0.20});
145 Effect.Appear('context-menu', {duration: 0.20});
105 if (window.parseStylesheets) { window.parseStylesheets(); } // IE
146 if (window.parseStylesheets) { window.parseStylesheets(); } // IE
106 }})
147 }})
107 },
148 },
108
149
109 hideMenu: function() {
150 hideMenu: function() {
110 Element.hide('context-menu');
151 Element.hide('context-menu');
111 },
152 },
112
153
113 addSelection: function(tr) {
154 addSelection: function(tr) {
114 tr.addClassName('context-menu-selection');
155 tr.addClassName('context-menu-selection');
115 this.checkSelectionBox(tr, true);
156 this.checkSelectionBox(tr, true);
116 },
157 },
117
158
118 toggleSelection: function(tr) {
159 toggleSelection: function(tr) {
119 if (this.isSelected(tr)) {
160 if (this.isSelected(tr)) {
120 this.removeSelection(tr);
161 this.removeSelection(tr);
121 } else {
162 } else {
122 this.addSelection(tr);
163 this.addSelection(tr);
123 }
164 }
124 },
165 },
125
166
126 removeSelection: function(tr) {
167 removeSelection: function(tr) {
127 tr.removeClassName('context-menu-selection');
168 tr.removeClassName('context-menu-selection');
128 this.checkSelectionBox(tr, false);
169 this.checkSelectionBox(tr, false);
129 },
170 },
130
171
131 unselectAll: function() {
172 unselectAll: function() {
132 var rows = $$('.hascontextmenu');
173 var rows = $$('.hascontextmenu');
133 for (i=0; i<rows.length; i++) {
174 for (i=0; i<rows.length; i++) {
134 this.removeSelection(rows[i]);
175 this.removeSelection(rows[i]);
135 }
176 }
136 },
177 },
137
178
138 checkSelectionBox: function(tr, checked) {
179 checkSelectionBox: function(tr, checked) {
139 var inputs = Element.getElementsBySelector(tr, 'input');
180 var inputs = Element.getElementsBySelector(tr, 'input');
140 if (inputs.length > 0) { inputs[0].checked = checked; }
181 if (inputs.length > 0) { inputs[0].checked = checked; }
141 },
182 },
142
183
143 isSelected: function(tr) {
184 isSelected: function(tr) {
144 return Element.hasClassName(tr, 'context-menu-selection');
185 return Element.hasClassName(tr, 'context-menu-selection');
145 }
186 }
146 }
187 }
147
188
148 function toggleIssuesSelection(el) {
189 function toggleIssuesSelection(el) {
149 var boxes = el.getElementsBySelector('input[type=checkbox]');
190 var boxes = el.getElementsBySelector('input[type=checkbox]');
150 var all_checked = true;
191 var all_checked = true;
151 for (i = 0; i < boxes.length; i++) { if (boxes[i].checked == false) { all_checked = false; } }
192 for (i = 0; i < boxes.length; i++) { if (boxes[i].checked == false) { all_checked = false; } }
152 for (i = 0; i < boxes.length; i++) {
193 for (i = 0; i < boxes.length; i++) {
153 if (all_checked) {
194 if (all_checked) {
154 boxes[i].checked = false;
195 boxes[i].checked = false;
155 boxes[i].up('tr').removeClassName('context-menu-selection');
196 boxes[i].up('tr').removeClassName('context-menu-selection');
156 } else if (boxes[i].checked == false) {
197 } else if (boxes[i].checked == false) {
157 boxes[i].checked = true;
198 boxes[i].checked = true;
158 boxes[i].up('tr').addClassName('context-menu-selection');
199 boxes[i].up('tr').addClassName('context-menu-selection');
159 }
200 }
160 }
201 }
161 }
202 }
203
204 function window_size() {
205 var w;
206 var h;
207 if (window.innerWidth) {
208 w = window.innerWidth;
209 h = window.innerHeight;
210 } else if (document.documentElement) {
211 w = document.documentElement.clientWidth;
212 h = document.documentElement.clientHeight;
213 } else {
214 w = document.body.clientWidth;
215 h = document.body.clientHeight;
216 }
217 return {width: w, height: h};
218 }
@@ -1,52 +1,52
1 #context-menu { position: absolute; z-index: 10;}
1 #context-menu { position: absolute; z-index: 10;}
2
2
3 #context-menu ul, #context-menu li, #context-menu a {
3 #context-menu ul, #context-menu li, #context-menu a {
4 display:block;
4 display:block;
5 margin:0;
5 margin:0;
6 padding:0;
6 padding:0;
7 border:0;
7 border:0;
8 }
8 }
9
9
10 #context-menu ul {
10 #context-menu ul {
11 width:150px;
11 width:150px;
12 border-top:1px solid #ddd;
12 border-top:1px solid #ddd;
13 border-left:1px solid #ddd;
13 border-left:1px solid #ddd;
14 border-bottom:1px solid #777;
14 border-bottom:1px solid #777;
15 border-right:1px solid #777;
15 border-right:1px solid #777;
16 background:white;
16 background:white;
17 list-style:none;
17 list-style:none;
18 }
18 }
19
19
20 #context-menu li {
20 #context-menu li {
21 position:relative;
21 position:relative;
22 padding:1px;
22 padding:1px;
23 z-index:9;
23 z-index:9;
24 }
24 }
25 #context-menu li.folder ul {
25 #context-menu li.folder ul { position:absolute; left:168px; /* IE6 */ top:-2px; }
26 position:absolute;
27 left:168px; /* IE6 */
28 top:-2px;
29 }
30 #context-menu li.folder>ul { left:148px; }
26 #context-menu li.folder>ul { left:148px; }
31
27
28 #context-menu.reverse-y li.folder>ul { top:auto; bottom:0; }
29 #context-menu.reverse-x li.folder ul { left:auto; right:168px; /* IE6 */ }
30 #context-menu.reverse-x li.folder>ul { right:148px; }
31
32 #context-menu a {
32 #context-menu a {
33 border:1px solid white;
33 border:1px solid white;
34 text-decoration:none;
34 text-decoration:none;
35 background-repeat: no-repeat;
35 background-repeat: no-repeat;
36 background-position: 1px 50%;
36 background-position: 1px 50%;
37 padding: 2px 0px 2px 20px;
37 padding: 2px 0px 2px 20px;
38 width:100%; /* IE */
38 width:100%; /* IE */
39 }
39 }
40 #context-menu li>a { width:auto; } /* others */
40 #context-menu li>a { width:auto; } /* others */
41 #context-menu a.disabled, #context-menu a.disabled:hover {color: #ccc;}
41 #context-menu a.disabled, #context-menu a.disabled:hover {color: #ccc;}
42 #context-menu li a.submenu { background:url("../images/sub.gif") right no-repeat; }
42 #context-menu li a.submenu { background:url("../images/sub.gif") right no-repeat; }
43 #context-menu a:hover { border-color:gray; background-color:#eee; color:#2A5685; }
43 #context-menu a:hover { border-color:gray; background-color:#eee; color:#2A5685; }
44 #context-menu li.folder a:hover { background-color:#eee; }
44 #context-menu li.folder a:hover { background-color:#eee; }
45 #context-menu li.folder:hover { z-index:10; }
45 #context-menu li.folder:hover { z-index:10; }
46 #context-menu ul ul, #context-menu li:hover ul ul { display:none; }
46 #context-menu ul ul, #context-menu li:hover ul ul { display:none; }
47 #context-menu li:hover ul, #context-menu li:hover li:hover ul { display:block; }
47 #context-menu li:hover ul, #context-menu li:hover li:hover ul { display:block; }
48
48
49 /* selected element */
49 /* selected element */
50 .context-menu-selection { background-color:#507AAA !important; color:#f8f8f8 !important; }
50 .context-menu-selection { background-color:#507AAA !important; color:#f8f8f8 !important; }
51 .context-menu-selection a, .context-menu-selection a:hover { color:#f8f8f8 !important; }
51 .context-menu-selection a, .context-menu-selection a:hover { color:#f8f8f8 !important; }
52 .context-menu-selection:hover { background-color:#507AAA !important; color:#f8f8f8 !important; }
52 .context-menu-selection:hover { background-color:#507AAA !important; color:#f8f8f8 !important; }
General Comments 0
You need to be logged in to leave comments. Login now