Commit 9cd78ecd authored by Aron Carroll's avatar Aron Carroll

Merge pull request #1622 from hypothesis/simplify-highlights

Simplify highlight visibility toggling.
parents 5812f5c5 004f82a9
...@@ -3,13 +3,15 @@ $ = Annotator.$ ...@@ -3,13 +3,15 @@ $ = Annotator.$
class Annotator.Guest extends Annotator class Annotator.Guest extends Annotator
SHOW_HIGHLIGHTS_CLASS = 'annotator-highlights-always-on'
# Events to be bound on Annotator#element. # Events to be bound on Annotator#element.
events: events:
".annotator-adder button click": "onAdderClick" ".annotator-adder button click": "onAdderClick"
".annotator-adder button mousedown": "onAdderMousedown" ".annotator-adder button mousedown": "onAdderMousedown"
".annotator-adder button mouseup": "onAdderMouseup" ".annotator-adder button mouseup": "onAdderMouseup"
"setTool": "onSetTool" "setTool": "onSetTool"
"setVisibleHighlights": "onSetVisibleHighlights" "setVisibleHighlights": "setVisibleHighlights"
# Plugin configuration # Plugin configuration
options: options:
...@@ -188,7 +190,6 @@ class Annotator.Guest extends Annotator ...@@ -188,7 +190,6 @@ class Annotator.Guest extends Annotator
) )
.bind('setVisibleHighlights', (ctx, state) => .bind('setVisibleHighlights', (ctx, state) =>
this.setVisibleHighlights state, false
this.publish 'setVisibleHighlights', state this.publish 'setVisibleHighlights', state
) )
...@@ -315,17 +316,23 @@ class Annotator.Guest extends Annotator ...@@ -315,17 +316,23 @@ class Annotator.Guest extends Annotator
method: 'setTool' method: 'setTool'
params: name params: name
setVisibleHighlights: (state=true, notify=true) -> # Pass true to show the highlights in the frame or false to disable.
if notify setVisibleHighlights: (shouldShowHighlights) ->
@panel?.notify return if @visibleHighlights == shouldShowHighlights
method: 'setVisibleHighlights'
params: state @panel?.notify
method: 'setVisibleHighlights'
params: shouldShowHighlights
this.toggleHighlightClass(shouldShowHighlights or @tool == 'highlight')
toggleHighlightClass: (shouldShowHighlights) ->
if shouldShowHighlights
@element.addClass(SHOW_HIGHLIGHTS_CLASS)
else else
markerClass = 'annotator-highlights-always-on' @element.removeClass(SHOW_HIGHLIGHTS_CLASS)
if state or this.tool is 'highlight'
@element.addClass markerClass @visibleHighlights = shouldShowHighlights
else
@element.removeClass markerClass
addComment: -> addComment: ->
this.showEditor(this.createAnnotation()) this.showEditor(this.createAnnotation())
...@@ -360,10 +367,6 @@ class Annotator.Guest extends Annotator ...@@ -360,10 +367,6 @@ class Annotator.Guest extends Annotator
onSetTool: (name) -> onSetTool: (name) ->
switch name switch name
when 'comment' when 'comment'
this.setVisibleHighlights this.visibleHighlights, false this.setVisibleHighlights this.visibleHighlights
when 'highlight' when 'highlight'
this.setVisibleHighlights true, false this.setVisibleHighlights true
onSetVisibleHighlights: (state) =>
this.visibleHighlights = state
this.setVisibleHighlights state, false
$ = Annotator.$ $ = Annotator.$
makeButton = (item) ->
anchor = $('<a></a>')
.attr('href', '')
.attr('title', item.title)
.on(item.on)
.addClass(item.class)
button = $('<li></li>').append(anchor)
return button[0]
class Annotator.Plugin.Toolbar extends Annotator.Plugin class Annotator.Plugin.Toolbar extends Annotator.Plugin
PUSHED_CLASS = 'annotator-pushed' PUSHED_CLASS = 'annotator-pushed'
...@@ -11,19 +20,25 @@ class Annotator.Plugin.Toolbar extends Annotator.Plugin ...@@ -11,19 +20,25 @@ class Annotator.Plugin.Toolbar extends Annotator.Plugin
html: '<div class="annotator-toolbar annotator-hide"></div>' html: '<div class="annotator-toolbar annotator-hide"></div>'
options: pluginInit: ->
items: [ @annotator.toolbar = @toolbar = $(@html)
if @options.container?
$(@options.container).append @toolbar
else
$(@element).append @toolbar
items = [
"title": "Toggle Sidebar" "title": "Toggle Sidebar"
"class": "annotator-toolbar-toggle h-icon-comment" "class": "annotator-toolbar-toggle h-icon-comment"
"on": "on":
"click": (event) -> "click": (event) =>
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
collapsed = window.annotator.frame.hasClass('annotator-collapsed') collapsed = @annotator.frame.hasClass('annotator-collapsed')
if collapsed if collapsed
window.annotator.showFrame() @annotator.showFrame()
else else
window.annotator.hideFrame() @annotator.hideFrame()
# Remove focus from the anchor when clicked, this removes the focus # Remove focus from the anchor when clicked, this removes the focus
# styles intended only for keyboard navigation. IE/FF apply the focus # styles intended only for keyboard navigation. IE/FF apply the focus
# psuedo-class to a clicked element. # psuedo-class to a clicked element.
...@@ -32,47 +47,31 @@ class Annotator.Plugin.Toolbar extends Annotator.Plugin ...@@ -32,47 +47,31 @@ class Annotator.Plugin.Toolbar extends Annotator.Plugin
"title": "Show Annotations" "title": "Show Annotations"
"class": "h-icon-visible" "class": "h-icon-visible"
"on": "on":
"click": (event) -> "click": (event) =>
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
state = not window.annotator.visibleHighlights state = not @annotator.visibleHighlights
window.annotator.setVisibleHighlights state @annotator.setVisibleHighlights state
, ,
"title": "Highlighting Mode" "title": "Highlighting Mode"
"class": "h-icon-highlighter" "class": "h-icon-highlighter"
"on": "on":
"click": (event) -> "click": (event) =>
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
state = not (window.annotator.tool is 'highlight') state = not (@annotator.tool is 'highlight')
tool = if state then 'highlight' else 'comment' tool = if state then 'highlight' else 'comment'
window.annotator.setTool tool @annotator.setTool tool
, ,
"title": "New Comment" "title": "New Comment"
"class": "h-icon-plus" "class": "h-icon-plus"
"on": "on":
"click": (event) -> "click": (event) =>
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
window.annotator.addComment() @annotator.addComment()
] ]
@buttons = $(makeButton(item) for item in items)
pluginInit: ->
@annotator.toolbar = @toolbar = $(@html)
if @options.container?
$(@options.container).append @toolbar
else
$(@element).append @toolbar
@buttons = @options.items.reduce (buttons, item) =>
anchor = $('<a></a>')
.attr('href', '')
.attr('title', item.title)
.on(item.on)
.addClass(item.class)
button = $('<li></li>').append(anchor)
buttons.add button
, $()
list = $('<ul></ul>') list = $('<ul></ul>')
@buttons.appendTo(list) @buttons.appendTo(list)
......
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