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