11/*global Annotator*/
2- /*global diff_match_patch*/
32/*jslint newcap: true*/
43Annotator . Plugin . Madison = function ( element , options ) {
54 Annotator . Plugin . apply ( this , arguments ) ;
@@ -96,31 +95,6 @@ $.extend(Annotator.Plugin.Madison.prototype, new Annotator.Plugin(), {
9695 $ ( '#current-comments' ) . collapse ( true ) ;
9796 } ) ;
9897
99- this . annotator . subscribe ( 'annotationViewerTextField' , function ( field , annotation ) {
100- if ( annotation . tags . length === 0 ) {
101- return ;
102- }
103-
104- annotation . tags . forEach ( function ( tag ) {
105- // TODO: support edits?
106- // if (tag === 'edit') {
107- // var jField = $(field);
108- // var differ = new diff_match_patch();
109- // var diffs = differ.diff_main(annotation.quote, annotation.text);
110- // var html = differ.diff_prettyHtml(diffs);
111- // jField.find('p').html(html);
112- // }
113- } ) ;
114- } ) ;
115-
116- // Add Madison-specific fields to the viewer when Annotator loads it
117- this . annotator . viewer . addField ( {
118- load : function ( field , annotation ) {
119- this . addNoteActions ( field , annotation ) ;
120- this . addComments ( field , annotation ) ;
121- } . bind ( this )
122- } ) ;
123-
12498 this . annotator . editor . submit = function ( e ) {
12599 // Clear previous errors
126100 this . annotation . _error = false ;
@@ -142,49 +116,38 @@ $.extend(Annotator.Plugin.Madison.prototype, new Annotator.Plugin(), {
142116 }
143117 } ;
144118
145- this . annotator . editor . addField ( {
146- load : function ( field , annotation ) {
147- this . addEditFields ( field , annotation ) ;
148- } . bind ( this ) ,
149- submit : function ( field , annotation ) {
150- // check it is tagged 'edit'
151- if ( this . hasEditTag ( annotation . tags ) ) {
152- // check we have explanatory content
153- var explanation = $ ( field ) . find ( '#explanation' ) . val ( ) ;
154-
155- // If no explanatory content, show message and don't submit
156- if ( '' === explanation . trim ( ) ) {
157- $ ( '#annotation-error' ) . text ( "Explanation required for edits." ) . toggle ( true ) ;
158-
159- annotation . _error = true ;
160- return false ;
161- }
119+ // We want document events even on readOnly, which isn't the default
120+ // behavior.
121+ if ( this . annotator . options . readOnly && ! this . annotator . options . discussionClosed ) {
122+ this . annotator . _setupDocumentEvents ( ) ;
123+ }
162124
163- annotation . explanation = explanation ;
164- }
165- } ,
166- hasEditTag : function ( tags ) {
167- var hasEditTag = false ;
125+ // Disable viewing annotations through mouseover
126+ this . annotator . onHighlightMouseover = function ( event ) {
127+ return false ;
128+ } ;
168129
169- if ( tags === undefined || tags . length === 0 ) {
170- return false ;
171- }
130+ // Clicking a highlight opens the panel for that annotation group
131+ $ ( '.annotator-wrapper' ) . delegate ( '.annotator-hl' , 'click' , function ( event ) {
132+ $ ( event . target ) . trigger ( 'madison.showNotes' ) ;
133+ } ) ;
172134
173- tags . forEach ( function ( tag ) {
174- if ( tag === 'edit' ) {
175- hasEditTag = true ;
135+ // If the area outside of the editor is clicked, close it.
136+ this . annotator . subscribe ( 'annotationEditorShown' , function ( ) {
137+ setTimeout ( function ( ) {
138+ $ ( document ) . one ( 'click.annotationEditor' , function ( e ) {
139+ let clickIsOutsideEditor = ! $ ( e . target ) . closest ( '.annotator-editor' ) . length ;
140+ if ( clickIsOutsideEditor ) {
141+ $ ( '.annotator-editor:not(.annotator-hide) .annotator-controls .annotator-cancel' ) . trigger ( 'click' ) ;
176142 }
177143 } ) ;
178-
179- return hasEditTag ;
180- }
144+ } , 500 ) ;
181145 } ) ;
182146
183- // We want document events even on readOnly, which isn't the default
184- // behavior.
185- if ( this . annotator . options . readOnly && ! this . annotator . options . discussionClosed ) {
186- this . annotator . _setupDocumentEvents ( ) ;
187- }
147+ // Just in case we close the editor with "cancel", clean up the single event binding
148+ this . annotator . subscribe ( 'annotationEditorHidden' , function ( ) {
149+ $ ( document ) . off ( 'click.annotationEditor' ) ;
150+ } ) ;
188151
189152 this . onAdderClickOld = this . annotator . onAdderClick ;
190153 this . annotator . onAdderClick = function ( event ) {
@@ -254,100 +217,6 @@ $.extend(Annotator.Plugin.Madison.prototype, new Annotator.Plugin(), {
254217 }
255218 } ,
256219
257- addEditFields : function ( field , annotation ) {
258- var newField = $ ( field ) ;
259- var toAdd = $ ( '<div class="annotator-editor-edit-wrapper"></div>' ) ;
260-
261- var buttonGroup = $ ( '<div class="btn-group"></div>' ) ;
262-
263- var explanation = $ ( '<input id="explanation" type="text" name="explanation" placeholder="' + window . trans [ 'messages.document.note_edit_explanation_prompt' ] + '" style="display:none;" />' ) ;
264- var annotationError = $ ( '<p id="annotation-error" style="display:none; color:red;"></p>' ) ;
265-
266- var annotateButton = $ ( '<button type="button" class="btn btn-default active">' + window . trans [ 'messages.document.note' ] + '</button>' ) . click ( function ( ) {
267- $ ( this ) . addClass ( 'active' ) ;
268- $ ( this ) . siblings ( ) . each ( function ( sibling ) {
269- $ ( this ) . removeClass ( 'active' ) ;
270- } ) ;
271- $ ( '#annotator-field-0' ) . val ( '' ) ;
272- $ ( '#annotator-field-1' ) . val ( '' ) ;
273- $ ( '#explanation' ) . toggle ( false ) ;
274- $ ( '#explanation' ) . prop ( 'required' , false ) ;
275- $ ( '#annotator-error' ) . text ( '' ) . toggle ( false ) ;
276- $ ( '#annotator-field-0' ) . focus ( ) ;
277- } ) ;
278-
279- var editButton = $ ( '<button type="button" class="btn btn-default">' + window . trans [ 'messages.edit' ] + '</button>' ) . click ( function ( ) {
280- $ ( this ) . addClass ( 'active' ) ;
281- $ ( this ) . siblings ( ) . each ( function ( sibling ) {
282- $ ( this ) . removeClass ( 'active' ) ;
283- } ) ;
284- $ ( '#annotator-field-0' ) . val ( annotation . quote ) ;
285- $ ( '#annotator-field-1' ) . val ( 'edit ' ) ;
286- $ ( '#explanation' ) . toggle ( true ) ;
287- $ ( '#explanation' ) . prop ( 'required' , true ) ;
288- $ ( '#annotator-field-0' ) . focus ( ) ;
289- } ) ;
290-
291- buttonGroup . append ( annotateButton , editButton ) ;
292- toAdd . append ( buttonGroup ) ;
293- toAdd . append ( explanation ) ;
294- toAdd . append ( annotationError ) ;
295- newField . html ( toAdd ) ;
296- } ,
297-
298- addComments : function ( field , annotation ) {
299- var userId = this . options . userId ;
300-
301- // Add comment wrapper and collapse the comment thread
302- var commentsHeader = $ ( '<div class="comment-toggle" data-toggle-"collapse" data-target="#current-comments">Comments <span id="comment-caret" class="caret caret-right"></span></button>' ) . click ( function ( ) {
303- $ ( '#current-comments' ) . collapse ( 'toggle' ) ;
304- $ ( '#comment-caret' ) . toggleClass ( 'caret-right' ) ;
305- } ) ;
306-
307- // If there are no comments, hide the comment wrapper
308- if ( $ ( annotation . comments ) . length === 0 ) {
309- commentsHeader . addClass ( 'hidden' ) ;
310- }
311-
312- // Add all current comments to the annotation viewer
313- var currentComments = $ ( '<div id="current-comments" class="current-comments collapse"></div>' ) ;
314-
315- /*jslint unparam: true*/
316- $ . each ( annotation . comments , function ( index , comment ) {
317- comment = $ ( '<div class="existing-comment"><blockquote>' + comment . text + '<div class="comment-author">' + comment . user . display_name + '</div></blockquote></div>' ) ;
318- currentComments . append ( comment ) ;
319- } ) ;
320- /*jslint unparam: false*/
321-
322- // Collapse the comment thread on load
323- currentComments . ready ( function ( ) {
324- $ ( '#existing-comments' ) . collapse ( {
325- toggle : false
326- } ) ;
327- } ) ;
328-
329- // If the user is logged in, allow them to comment
330- if ( userId ) {
331- var annotationComments = $ ( '<div class="annotation-comments"></div>' ) ;
332- var commentText = $ ( '<input type="text" class="form-control" />' ) ;
333- var commentSubmit = $ ( '<button type="button" class="btn btn-primary" >Submit</button>' ) ;
334- commentSubmit . click ( function ( ) {
335- this . createComment ( commentText , annotation ) ;
336- } . bind ( this ) ) ;
337- annotationComments . append ( commentText ) ;
338-
339- annotationComments . append ( commentSubmit ) ;
340-
341- $ ( field ) . append ( annotationComments ) ;
342- }
343-
344- $ ( field ) . append ( commentsHeader , currentComments ) ;
345- } ,
346-
347- addNoteActions : function ( field , annotation ) {
348- $ ( field ) . append ( this . noteActionsString ( annotation ) ) ;
349- } ,
350-
351220 noteActionsString : function ( comment ) {
352221 let actions = '<div class="activity-actions">' ;
353222 actions += '<a class="thumbs-up" onclick=$(this).trigger("madison.addAction")'
@@ -543,12 +412,18 @@ $.extend(Annotator.Plugin.Madison.prototype, new Annotator.Plugin(), {
543412 var annotationParentId ;
544413 if ( annotationParent . prop ( 'id' ) ) {
545414 annotationParentId = annotationParent . prop ( 'id' ) ;
415+ annotationGroupCount = parseInt ( annotationParentId . replace ( 'annotationGroup-' , '' ) ) ;
546416 } else {
547417 annotationGroupCount ++ ;
548418 annotationParentId = 'annotationGroup-' + annotationGroupCount ;
549419 annotationParent . prop ( 'id' , annotationParentId ) ;
550420 }
551421
422+ // Set data-group-id on highlights so they can trigger notes pane
423+ annotation . highlights . forEach ( function ( highlight ) {
424+ $ ( highlight ) . data ( 'groupId' , annotationParentId ) ;
425+ } ) ;
426+
552427 if ( ( typeof ( annotationGroups [ annotationParentId ] ) ) . toLowerCase ( ) === 'undefined' ) {
553428 var parentTop = annotationParent . offset ( ) . top ;
554429 var containerTop = $ ( this . options . annotationContainerElem ) . offset ( ) . top ;
0 commit comments