forked from lopezs/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdragdrop.js
More file actions
411 lines (369 loc) · 12.9 KB
/
dragdrop.js
File metadata and controls
411 lines (369 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
(function ($) {
AblePlayer.prototype.initDragDrop = function ( $element ) {
// Accessible Drag & Drop based on these resources:
// Accessible Drag and Drop Using WAI-ARIA
// http://dev.opera.com/articles/accessible-drag-and-drop/
// Accessible Drag and Drop script on quirksmode
// http://www.quirksmode.org/js/this.html
var thisObj = this;
this.$activeWindow = $element;
// able-sign-window is currently the only draggable window,
// but this functionality could ultimately be extended to other windows
if ($element.is('.able-sign-window')) {
this.windowName = 'sign-window';
}
this.addWindowMenu();
};
AblePlayer.prototype.addWindowMenu = function() {
var thisObj = this;
// add alert div to window
this.$windowAlert = $('<div role="alert"></div>');
this.$windowAlert.addClass('able-alert');
this.$windowAlert.appendTo(this.$activeWindow);
this.$windowAlert.css({
top: this.$activeWindow.offset().top
});
// add button to draggable window which triggers a popup menu
// for now, re-use preferences icon for this purpose
var $newButton = $('<button>',{
'type': 'button',
'tabindex': '0',
'aria-label': this.tt.windowButtonLabel,
'class': 'able-button-handler-preferences'
});
if (this.iconType === 'font') {
var $buttonIcon = $('<span>',{
'class': 'icon-preferences',
'aria-hidden': 'true'
});
$newButton.append($buttonIcon);
}
else {
// use image
var buttonImgSrc = '../images/' + this.iconColor + '/preferences.png';
var $buttonImg = $('<img>',{
'src': buttonImgSrc,
'alt': '',
'role': 'presentation'
});
$newButton.append($buttonImg);
}
// add the visibly-hidden label for screen readers that don't support aria-label on the button
var $buttonLabel = $('<span>',{
'class': 'able-clipped'
}).text(this.tt.windowButtonLabel);
$newButton.append($buttonLabel);
// add an event listener that displays a tooltip on mouseenter or focus
var tooltipId = this.mediaId + '-' + this.windowName + '-tooltip';
var $tooltip = $('<div>',{
'class' : 'able-tooltip',
'id' : tooltipId
});
$newButton.on('mouseenter focus',function(event) {
var label = $(this).attr('aria-label');
// get position of this button
var position = $(this).position();
var buttonHeight = $(this).height();
var buttonWidth = $(this).width();
var tooltipY = position.top - buttonHeight - 5;
var tooltipX = 0;
var tooltipStyle = {
left: '',
right: tooltipX + 'px',
top: tooltipY + 'px'
};
var tooltip = $('#' + tooltipId).text(label).css(tooltipStyle);
thisObj.showTooltip(tooltip);
$(this).on('mouseleave blur',function() {
$('#' + tooltipId).text('').hide();
});
});
this.addResizeDialog();
// add a popup menu
var $popup = this.createPopup(this.windowName);
var $optionList = $('<ul></ul>');
var radioName = this.mediaId + '-' + this.windowName + '-choice';
if (this.windowName == 'sign-window') {
var options = [];
options.push({
'name': 'move',
'label': this.tt.windowMove
});
options.push({
'name': 'resize',
'label': this.tt.windowResize
});
if (this.$activeWindow.css('z-index') > 0) {
options.push({
'name': 'sendBack',
'label': this.tt.windowSendBack
});
}
else {
options.push({
'name': 'bringTop',
'label': this.tt.windowBringTop
});
}
for (var i in options) {
var $optionItem = $('<li></li>');
var option = options[i];
var radioId = radioName + '-' + i;
var $radioButton = $('<input>',{
'type': 'radio',
'val': option.name,
'name': radioName,
'id': radioId
});
var $radioLabel = $('<label>',{
'for': radioId
});
$radioLabel.text(option.label);
$radioButton.on('click keypress',function(e) {
e.preventDefault();
thisObj.handleMenuChoice($(this).val());
});
$optionItem.append($radioButton,$radioLabel);
$optionList.append($optionItem);
}
}
$popup.append($optionList);
$newButton.on('click keydown',function(e) {
thisObj.handleWindowButtonClick(e);
});
this.$activeWindow.append($newButton,$tooltip,$popup);
this.$windowButton = $newButton;
this.$windowPopup = $popup;
};
AblePlayer.prototype.addResizeDialog = function () {
var thisObj = this;
var widthId = this.mediaId + '-resize-width';
var heightId = this.mediaId + '-resize-height';
var startingWidth = this.$activeWindow.width();
var startingHeight = this.$activeWindow.height();
var $resizeForm = $('<div></div>',{
'class' : 'able-resize-form'
});
// inner container for all content, will be assigned to modal div's aria-describedby
var $resizeWrapper = $('<div></div>');
// width field
var $resizeWidthDiv = $('<div></div>');
var $resizeWidthInput = $('<input>',{
'type': 'text',
'id': widthId,
'value': startingWidth
});
var $resizeWidthLabel = $('<label>',{
'for': widthId
}).text(this.tt.width);
/* // Don't prompt for height
// height field
var $resizeHeightDiv = $('<div></div>');
var $resizeHeightInput = $('<input>',{
'type': 'text',
'id': heightId,
'value': this.$activeWindow.height()
});
var $resizeHeightLabel = $('<label>',{
'for': heightId
}).text(this.tt.height);
*/
// Add save and cancel buttons.
var $saveButton = $('<button class="modal-button">' + this.tt.save + '</button>');
var $cancelButton = $('<button class="modal-button">' + this.tt.cancel + '</button>');
$saveButton.click(function () {
var newWidth = $('#' + widthId).val();
if (newWidth !== startingWidth) {
// var newHeight = Math.round(newWidth * (startingHeight/startingWidth),0);
thisObj.$activeWindow.css('width',newWidth);
thisObj.$activeWindow.find('video').css({
'width' : newWidth + 'px'
//'height' : newHeight + 'px'
});
}
thisObj.resizeDialog.hide();
thisObj.$windowPopup.hide();
thisObj.$windowButton.show().focus();
});
$cancelButton.click(function () {
dialog.hide();
});
// Now assemble all the parts
$resizeWidthDiv.append($resizeWidthLabel,$resizeWidthInput);
// $resizeHeightDiv.append($resizeHeightLabel,$resizeHeightInput);
$resizeWrapper.append($resizeWidthDiv);
$resizeForm.append($resizeWrapper,'<hr>',$saveButton,$cancelButton);
// must be appended to the BODY!
// otherwise when aria-hidden="true" is applied to all background content
// that will include an ancestor of the dialog,
// which will render the dialog unreadable by screen readers
$('body').append($resizeForm);
this.resizeDialog = new AccessibleDialog($resizeForm, 'alert', this.tt.windowResizeHeading, $resizeWrapper, this.tt.closeButtonLabel, '20em');
};
AblePlayer.prototype.handleWindowButtonClick = function (e) {
if (e.which > 1) {
// user pressed a key
if (!(e.which === 32 || e.which === 13)) {
// this was not Enter or space. Ignore it
return false;
}
}
if (this.hidingPopup) {
// stopgap to prevent keydown from reopening popup
// immediately after closing it
this.hidingPopup = false;
return false;
}
this.$windowButton.hide();
this.$windowPopup.show();
// Focus on the checked button, if any buttons are checked
// Otherwise, focus on the first button
this.$windowPopup.find('li').removeClass('able-focus');
if (this.$windowPopup.find('input:checked').val()) {
this.$windowPopup.find('input:checked').focus().parent().addClass('able-focus');
}
else {
this.$windowPopup.find('input').first().focus().parent().addClass('able-focus');
}
e.preventDefault();
};
AblePlayer.prototype.handleMenuChoice = function ( choice ) {
var thisObj = this;
if (choice == 'move') {
this.showAlert(this.tt.windowMoveAlert,'sign');
thisObj.startDrag();
this.$windowPopup.hide().parent().focus();
}
else if (choice == 'resize') {
this.resizeDialog.show();
this.showAlert(this.tt.windowResizeAlert,'sign');
}
else if (choice == 'sendBack') {
this.$activeWindow.css('z-index','0');
// this has the side-effect of making the popup unclickable
this.$windowPopup.css('z-index','4000').hide();
this.$windowButton.show().focus();
this.showAlert(this.tt.windowSendBackAlert,'sign');
// change content of radio button
var $thisRadio = this.$windowPopup.find('input:last');
$thisRadio.val('bringTop');
$thisRadio.next('label').text(this.tt.windowBringTop);
}
else if (choice == 'bringTop') {
this.$activeWindow.css({
'z-index':'4000'
});
this.$windowPopup.hide();
this.$windowButton.show().focus();
this.showAlert(this.tt.windowBringTopAlert,'sign');
// change content of radio button
var $thisRadio = this.$windowPopup.find('input:last');
$thisRadio.val('sendBack');
$thisRadio.next('label').text(this.tt.windowSendBack);
}
};
AblePlayer.prototype.startDrag = function() {
var thisObj, startPos, newX, newY;
thisObj = this;
// prepare element for dragging
this.$activeWindow.addClass('able-drag').css({
'position': 'absolute',
'top': this.dragStartY + 'px',
'left': this.dragStartX + 'px'
});
// get starting position of element
startPos = this.$activeWindow.offset();
this.dragStartX = this.dXKeys = startPos.left;
this.dragStartY = this.dYKeys = startPos.top;
// add listeners
$(document).on('mousedown',function(e) {
thisObj.dragging = true;
// get starting position of mouse
thisObj.startMouseX = e.pageX;
thisObj.startMouseY = e.pageY;
// get offset between mouse position and top left corner of draggable element
thisObj.dragOffsetX = thisObj.startMouseX - thisObj.dragStartX;
thisObj.dragOffsetY = thisObj.startMouseY - thisObj.dragStartY;
});
$(document).on('mousemove',function(e) {
if (thisObj.dragging) {
// calculate new top left based on current mouse position - offset
newX = e.pageX - thisObj.dragOffsetX;
newY = e.pageY - thisObj.dragOffsetY;
thisObj.resetDraggedObject( newX, newY );
}
});
$(document).on('mouseup',function() {
if (thisObj.dragging) {
// finalize the drop
thisObj.dragEnd();
}
});
this.startingDrag = true;
this.$activeWindow.on('keydown',function(e) {
thisObj.dragKeys(e);
});
return false;
};
AblePlayer.prototype.dragKeys = function(e) {
var key, keySpeed;
// stopgap to prevent firing on initial Enter or space
// that selected "Move" from menu
if (this.startingDrag) {
this.startingDrag = false;
return false;
}
key = e.which;
keySpeed = 10; // pixels per keypress event
switch (key) {
case 37: // left
case 63234:
this.dXKeys -= keySpeed;
break;
case 38: // up
case 63232:
this.dYKeys -= keySpeed;
break;
case 39: // right
case 63235:
this.dXKeys += keySpeed;
break;
case 40: // down
case 63233:
this.dYKeys += keySpeed;
break;
case 13: // enter
case 27: // escape
this.dragEnd();
return false;
default:
return false;
}
this.resetDraggedObject(this.dXKeys,this.dYKeys);
if (e.preventDefault) {
e.preventDefault();
}
return false;
};
AblePlayer.prototype.resetDraggedObject = function ( x, y) {
this.$activeWindow.css({
'left': x + 'px',
'top': y + 'px'
});
},
AblePlayer.prototype.dragEnd = function() {
$(document).off('mousemove mouseup');
this.$activeWindow.off('keydown').removeClass('able-drag');
// stopgap to prevent spacebar in Firefox from reopening popup
// immediately after closing it (used in handleWindowButtonClick())
this.hidingPopup = true;
this.$windowPopup.hide();
// Ensure stopgap gets cancelled if handleWindowButtonClick() isn't called
// e.g., if user triggered button with Enter or mouse click, not spacebar
setTimeout(function() {
this.hidingPopup = false;
}, 100);
this.$windowButton.show().focus();
this.dragging = false;
};
})(jQuery);