Commit aa8dd549 authored by Randall Leeds's avatar Randall Leeds

even more improved drag resizing

 - drag polyfill
 - suppress ghost image
 - work around gecko bug 505521
parent 870e3522
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
(function() {
var pfx = ['ms', 'moz', 'webkit', 'o'];
for (var i = 0; i < pfx.length && !window.requestAnimationFrame ; i++) {
requestAnimationFrame = window[pfx[i]+'RequestAnimationFrame'];
cancelAnimationFrame = window[pfx[i]+'CancelRequestAnimationFrame'];
if (!cancelAnimationFrame) {
cancelAnimationFrame = window[pfx[i]+'CancelAnimationFrame'];
}
window.requestAnimationFrame = requestAnimationFrame;
window.cancelAnimationFrame = cancelAnimationFrame;
}
if (!window.requestAnimationFrame)
var lastTime = 0;
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}())
...@@ -25,6 +25,7 @@ class Annotator.Host extends Annotator ...@@ -25,6 +25,7 @@ class Annotator.Host extends Annotator
# Drag state variables # Drag state variables
drag: drag:
delta: 0 delta: 0
last: null
tick: false tick: false
constructor: (element, options) -> constructor: (element, options) ->
...@@ -76,11 +77,14 @@ class Annotator.Host extends Annotator ...@@ -76,11 +77,14 @@ class Annotator.Host extends Annotator
@frame.css 'margin-left': '' @frame.css 'margin-left': ''
@frame.removeClass 'annotator-no-transition' @frame.removeClass 'annotator-no-transition'
@frame.addClass 'annotator-collapsed' @frame.addClass 'annotator-collapsed'
dragFrame: (d) => dragFrame: (screenX) =>
@drag.delta += d if screenX > 0
if @drag.last?
@drag.delta += screenX - @drag.last
@drag.last = screenX
unless @drag.tick unless @drag.tick
@drag.tick = true @drag.tick = true
requestAnimationFrame @dragRefresh window.requestAnimationFrame this.dragRefresh
getHighlights: => getHighlights: =>
highlights: $(@wrapper).find('.annotator-hl').map -> highlights: $(@wrapper).find('.annotator-hl').map ->
offset: $(this).offset() offset: $(this).offset()
...@@ -166,6 +170,20 @@ class Annotator.Host extends Annotator ...@@ -166,6 +170,20 @@ class Annotator.Host extends Annotator
document.addEventListener 'touchstart', => document.addEventListener 'touchstart', =>
touch = true touch = true
do update do update
document.addEventListener 'dragover', (event) =>
if @drag.last?
@drag.delta += event.screenX - @drag.last
@drag.last = event.screenX
unless @drag.tick
@drag.tick = true
window.requestAnimationFrame this.dragRefresh
document.addEventListener 'dragleave', (event) =>
if @drag.last?
@drag.delta += event.screenX - @drag.last
@drag.last = event.screenX
unless @drag.tick
@drag.tick = true
window.requestAnimationFrame this.dragRefresh
$(window).on 'resize scroll', update $(window).on 'resize scroll', update
$(document.body).on 'resize scroll', '*', util.debounce => @consumer.update() $(document.body).on 'resize scroll', '*', util.debounce => @consumer.update()
super super
......
...@@ -174,16 +174,22 @@ class Hypothesis extends Annotator ...@@ -174,16 +174,22 @@ class Hypothesis extends Annotator
this._fillDynamicBucket() this._fillDynamicBucket()
this.show() this.show()
dragLast = 0 el = document.createElementNS 'http://www.w3.org/1999/xhtml', 'canvas'
el.width = el.height = 1
@element.append el
handle = @element.find('#toolbar .tri')[0] handle = @element.find('#toolbar .tri')[0]
handle.addEventListener 'dragstart', (event) => handle.addEventListener 'dragstart', (event) =>
dragLast = event.screenX event.dataTransfer.setData 'text/plain', ''
event.dataTransfer.setDragImage(el, 0, 0)
@provider.dragFrame event.screenX
handle.addEventListener 'dragend', (event) => handle.addEventListener 'dragend', (event) =>
@provider.dragFrame (event.screenX - dragLast) @provider.dragFrame event.screenX
handle.addEventListener 'drag', (event) => @element[0].addEventListener 'dragover', (event) =>
if event.screenX > 0 @provider.dragFrame event.screenX
@provider.dragFrame (event.screenX - dragLast) @element[0].addEventListener 'dragleave', (event) =>
dragLast = event.screenX @provider.dragFrame event.screenX
this this
_setupDynamicStyle: -> _setupDynamicStyle: ->
...@@ -608,16 +614,16 @@ class Hypothesis extends Annotator ...@@ -608,16 +614,16 @@ class Hypothesis extends Annotator
@visible = true @visible = true
@provider.setActiveHighlights annotations @provider.setActiveHighlights annotations
@provider.showFrame() @provider.showFrame()
@element.find("#toolbar").addClass "shown" @element.find('#toolbar').addClass('shown')
document.getElementsByClassName('tri')[0].draggable = true .find('.tri').attr('draggable', true)
hide: => hide: =>
@lastWidth = window.innerWidth @lastWidth = window.innerWidth
@visible = false @visible = false
@provider.setActiveHighlights [] @provider.setActiveHighlights []
@provider.hideFrame() @provider.hideFrame()
@element.find("#toolbar").removeClass "shown" @element.find('#toolbar').removeClass('shown')
document.getElementsByClassName('tri')[0].draggable = false .find('.tri').attr('draggable', false)
threadId: (annotation) -> threadId: (annotation) ->
if annotation?.thread? if annotation?.thread?
......
...@@ -236,7 +236,7 @@ svg { -webkit-tap-highlight-color: rgba(255, 255, 255, 0); } ...@@ -236,7 +236,7 @@ svg { -webkit-tap-highlight-color: rgba(255, 255, 255, 0); }
float: left; float: left;
width: $baseLineHeight; width: $baseLineHeight;
height: $baseLineHeight; height: $baseLineHeight;
cursor: pointer; cursor: w-resize;
} }
&.shown { &.shown {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment