Commit b7158e9f by Arthur Barrett

Switched to $.scrollTo instead of custom animation and refactored toggle method.

parent 5fc7f1a8
...@@ -2,11 +2,11 @@ class @Annotatable ...@@ -2,11 +2,11 @@ class @Annotatable
@_debug: true @_debug: true
wrapperSelector: '.annotatable-wrapper' wrapperSelector: '.annotatable-wrapper'
toggleSelector: '.annotatable-toggle' toggleSelector: '.annotatable-toggle'
spanSelector: '.annotatable-span' spanSelector: '.annotatable-span'
commentSelector: '.annotatable-comment' commentSelector: '.annotatable-comment'
replySelector: '.annotatable-reply' replySelector: '.annotatable-reply'
helpSelector: '.annotatable-help-icon' helpSelector: '.annotatable-help-icon'
constructor: (el) -> constructor: (el) ->
console.log 'loaded Annotatable' if @_debug console.log 'loaded Annotatable' if @_debug
...@@ -26,10 +26,8 @@ class @Annotatable ...@@ -26,10 +26,8 @@ class @Annotatable
@$(@wrapperSelector).delegate @replySelector, 'click', @onClickReply @$(@wrapperSelector).delegate @replySelector, 'click', @onClickReply
initTips: () -> initTips: () ->
@visibleTips = [] @savedTips = []
@$(@spanSelector).each (index, el) => @$(@spanSelector).each (index, el) => $(el).qtip(@getTipOptions el)
$(el).qtip(@getTipOptions el)
@$(@helpSelector).qtip @$(@helpSelector).qtip
position: position:
my: 'right top' my: 'right top'
...@@ -75,42 +73,42 @@ class @Annotatable ...@@ -75,42 +73,42 @@ class @Annotatable
event.preventDefault() if @annotationsHidden event.preventDefault() if @annotationsHidden
onClickToggleAnnotations: (e) => onClickToggleAnnotations: (e) =>
toggle = @$(@toggleSelector) @annotationsHidden = not @annotationsHidden
spans = @$(@spanSelector) @toggleButtonText @annotationsHidden
@toggleSpans @annotationsHidden
@annotationsHidden = !@annotationsHidden @toggleTips @annotationsHidden
if @annotationsHidden
spans.toggleClass('hide', true)
toggle.text('Show Annotations')
@visibleTips = @getVisibleTips()
@hideTips(@visibleTips)
else
spans.toggleClass('hide', false)
toggle.text('Hide Annotations')
@showTips(@visibleTips)
onClickReply: (e) => onClickReply: (e) =>
hash = $(e.currentTarget).attr('href') hash = $(e.currentTarget).attr('href')
if hash?.charAt(0) == '#' if hash?.charAt(0) == '#'
name = hash.substr(1) name = hash.substr(1)
anchor = $("a[name='#{name}']").first() anchor = $("a[name='#{name}']").first()
@scrollTo(anchor) if anchor.length == 1 @scrollTo(anchor)
scrollTo: (el, padding = 20) -> toggleTips: (hide) ->
props = if hide
scrollTop: (el.offset().top - padding) @closeAndSaveTips()
opts = else
duration: 500 @openSavedTips()
complete: @_once -> el.effect 'highlight', {}, 2000
$('html,body').animate(props, opts) toggleButtonText: (hide) ->
buttonText = (if hide then 'Show' else 'Hide')+' Annotations'
@$(@toggleSelector).text(buttonText)
toggleSpans: (hide) ->
@$(@spanSelector).toggleClass 'hide', hide
scrollTo: (el) ->
options =
duration: 500
onAfter: @_once -> el.effect 'highlight', {}, 2000
$('html,body').scrollTo(el, options)
makeTipContent: (el) -> makeTipContent: (el) ->
(api) => (api) =>
comment = $(@commentSelector, el).first().clone()
anchor = $(el).data('discussion-anchor') anchor = $(el).data('discussion-anchor')
if anchor comment = $(@commentSelector, el).first().clone()
comment.append(@createReplyLink(anchor)) comment.append(@createReplyLink(anchor)) if anchor
comment.contents() comment.contents()
makeTipTitle: (el) -> makeTipTitle: (el) ->
...@@ -120,9 +118,19 @@ class @Annotatable ...@@ -120,9 +118,19 @@ class @Annotatable
(if title then title else 'Commentary') (if title then title else 'Commentary')
createReplyLink: (anchor) -> createReplyLink: (anchor) ->
$("<a class=\"annotatable-reply\" href=\"##{anchor}\">Reply to this comment</a>") cls = 'annotatable-reply'
href = '#' + anchor
text = 'Reply to this comment'
$("<a class=\"#{cls}\" href=\"#{href}\">#{text}</a>")
openSavedTips: () ->
@showTips @savedTips
getVisibleTips: () -> closeAndSaveTips: () ->
@savedTips = @findVisibleTips()
@hideTips @savedTips
findVisibleTips: () ->
visible = [] visible = []
@$(@spanSelector).each (index, el) -> @$(@spanSelector).each (index, el) ->
api = $(el).qtip('api') api = $(el).qtip('api')
...@@ -130,16 +138,16 @@ class @Annotatable ...@@ -130,16 +138,16 @@ class @Annotatable
if tip.is(':visible') if tip.is(':visible')
visible.push [el, tip.offset()] visible.push [el, tip.offset()]
visible visible
hideTips: (items) -> hideTips: (pairs) ->
elements = (pair[0] for pair in items) elements = (pair[0] for pair in pairs)
$(elements).qtip('hide') $(elements).qtip('hide')
showTips: (items) -> showTips: (pairs) ->
$.each items, (index, item) -> $.each pairs, (index, pair) ->
[el, offset] = item [el, offset] = pair
$(el).qtip('show')
api = $(el).qtip('api') api = $(el).qtip('api')
api?.show()
$(api?.elements.tooltip).offset(offset) $(api?.elements.tooltip).offset(offset)
_once: (fn) -> _once: (fn) ->
......
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