forked from openannotation/annotator
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmarginviewer.coffee
353 lines (319 loc) · 14.8 KB
/
marginviewer.coffee
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
clone = (obj) ->
if not obj? or typeof obj isnt 'object'
return obj
newInstance = new obj.constructor()
for key of obj
newInstance[key] = clone obj[key]
return newInstance
class Annotator.Plugin.MarginViewerObjectStore
constructor: (data=[] , paramFuncObject , @idfield="id" , @marginobjfield="_marginObject" , @indexfield="_marginindex") ->
@funcObject = clone(paramFuncObject)
@funcObject.sortDataComparison = (x,y) => paramFuncObject.sortComparison(x[0],y[0])
@funcObject.mapFunc = (x) => [paramFuncObject.sortDataMap(x),x]
@data=data.map(@funcObject.mapFunc)
@data.sort(@funcObject.sortDataComparison)
@deletions=0
@insertions=0
if(@data.length>0)
for index in [[email protected]]
obj=@data[index][1]
obj[@indexfield]=index
getMarginObjects: -> @data.map((x) -> x[1])
updateObjectLocation: (obj) ->
objIndex = @getObjectLocation(obj)
@data[objIndex]=[@funcObject.sortDataMap(obj),obj]
obj[@indexfield]=objIndex
objectEquals: (obj1,obj2) ->
if ("id" of obj1) and ("id" of obj2)
return obj1.id is obj2.id
if ("id" of obj1) or ("id" of obj2)
return false
if (@indexfield of obj1) and (@indexfield of obj2)
return obj1[@indexfield] is obj2[@indexfield]
return false
getObjectLocation: (obj) ->
supposedLocation = obj[@indexfield]
# object is at its internally stored location
if supposedLocation<@data.length and @objectEquals(@data[supposedLocation][1],obj)
return supposedLocation
minimumIndex=Math.max(0,supposedLocation-@deletions)
maximumIndex=Math.min(@data.length-1,supposedLocation+@insertions)
for index in [minimumIndex..maximumIndex]
currentObject = @data[index][1]
if @objectEquals(currentObject,obj)
currentObject[@indexField]=index
return index
return -1
getNewLocationsForObject : (top,bottom,obj) ->
objectIndex = @getObjectLocation(obj.annotation)
currentIndex = objectIndex-1
currentNewTop = top
currentNewBottom = bottom
locationChanges=[]
# get preceding objects that need to be moved
while currentIndex>=0
currentObject=@data[currentIndex][1][@marginobjfield]
currentObjectSize=$(currentObject).outerHeight(true)
currentObjectTop=$(currentObject).offset().top
currentObjectBottom=currentObjectTop+currentObjectSize
[email protected](currentObject.annotation)
currentObjectGoalBottom=currentObjectGoalLocation.top+currentObjectSize
if currentObjectBottom>currentNewTop
# move up
objectNewTop=currentNewTop-currentObjectSize
locationChanges.push([objectNewTop,currentObject])
currentNewTop=objectNewTop
else
# object doesnt needs to be moved up, maybe it should be moved down
# if object isnt at its 'natural' location, move down as far as possible
if currentObjectGoalLocation.top>currentObjectTop
if currentObjectGoalBottom<currentNewTop
# if object can reach goal, send to goal
objectNewTop=currentObjectGoalLocation.top
locationChanges.push([objectNewTop,currentObject])
currentNewTop=currentObjectGoalLocation.top
else
# bring object as close as possible
objectNewTop=currentNewTop-currentObjectSize
locationChanges.push([objectNewTop,currentObject])
currentNewTop=objectNewTop
else
break
currentIndex-=1
# get succeeding objects that need to be moved
currentIndex = objectIndex+1
while currentIndex<@data.length
currentObject=@data[currentIndex][1][@marginobjfield]
currentObjectSize=$(currentObject).outerHeight(true)
currentObjectTop=$(currentObject).offset().top
currentObjectBottom=currentObjectTop+currentObjectSize
[email protected](currentObject.annotation)
currentObjectGoalBottom=currentObjectGoalLocation.top+currentObjectSize
if currentObjectTop<currentNewBottom
# move down
objectNewTop=currentNewBottom
locationChanges.push([objectNewTop,currentObject])
currentNewBottom=objectNewTop+currentObjectSize
else
# object doesnt need to be moved down, maybe it should be moved up
# if object isnt at its 'natural' location, move up as far as possible
if currentObjectGoalLocation.top<currentObjectTop
if currentObjectGoalLocation.top>currentNewBottom
# if object can reach goal, send to goal
objectNewTop=currentObjectGoalLocation.top
locationChanges.push([objectNewTop,currentObject])
currentNewBottom=objectNewTop+currentObjectSize
else
# bring object as close as possible
objectNewTop=currentNewBottom
locationChanges.push([objectNewTop,currentObject])
currentNewBottom=objectNewTop+currentObjectSize
else
break
currentIndex+=1
return locationChanges
# binary search
findIndexForNewObject : (location) ->
startIndex=0
while startIndex<endIndex
currentIndex=Math.floor((startIndex+endIndex)/2)
if @funcObject.sortComparison(location,@data[currentIndex][0])>0
startIndex=currentIndex+1
else
endIndex=currentIndex
return startIndex
addNewObject : (obj,topval,leftval) ->
location={top:topval,left:leftval}
newObjectLocation=@findIndexForNewObject(location)
@data.splice(newObjectLocation,0,@funcObject.mapFunc(obj))
obj[@indexfield]=newObjectLocation
@insertions+=1
deleteObject : (object) ->
objectLocation=@getObjectLocation(object)
@data.splice(objectLocation,1)
@deletions+=1
getObject : (object) ->
objectLocation=@getObjectLocation(object)
return @data[objectLocation]
getNextObject : (object) ->
currentLocation = @getObjectLocation(object)
if currentLocation+1<@data.length
return @data[currentLocation+1]
else
return null
class Annotator.Plugin.MarginViewer extends Annotator.Plugin
constructor : (element,options) ->
super
# TODO: get id of div for margin objects from options
events:
'annotationsLoaded': 'onAnnotationsLoaded'
'annotationCreated': 'onAnnotationCreated'
'annotationDeleted': 'onAnnotationDeleted'
'annotationUpdated': 'onAnnotationUpdated'
'.annotator-hl click': 'onAnnotationSelected'
pluginInit: ->
return unless Annotator.supported()
@annotator.viewer =
on: ->
hide: (annotations) => @hideHighlightedMargin(annotations)
load: (annotations) => @highlightMargin(annotations)
isShown: ->
addField: ->
element:
position: ->
css: ->
@highlightedObjects = []
@currentSelectedAnnotation = null
RTL_MULT = -1 #should be -1 if RTL else 1
sign = (x) ->
if(x is 0)
return 0
else
return x/Math.abs(x)
@funcObject =
sortDataMap: (annotation) ->
dbg = {top:$(annotation.highlights[0]).offset().top,left:$(annotation.highlights[0]).offset().left}
return dbg
sortComparison : (left,right) ->
return sign(sign(left.top - right.top)*2 + sign(left.left - right.left)*RTL_MULT)
idFunction : (annotation) -> annotation.id
sizeFunction : (element) -> element.outerHeight(true)
@marginData = new Annotator.Plugin.MarginViewerObjectStore [],@funcObject
onAnnotationsLoaded: (annotations) ->
@marginData = new Annotator.Plugin.MarginViewerObjectStore annotations,@funcObject
if annotations.length>0
currentLocation = 0
for annotation in @marginData.getMarginObjects()
annotationStart = annotation.highlights[0]
newLocation = $(annotationStart).offset().top;
if currentLocation>newLocation
newLocation=currentLocation
marginObject=@createMarginObject(annotation,newLocation)
@marginData.updateObjectLocation(annotation)
currentLocation = $(marginObject).offset().top+$(marginObject).outerHeight(true)
onAnnotationCreated: (annotation) ->
marginObject=@createMarginObject(annotation,hide=true)
newObjectTop=$(annotation.highlights[0]).offset().top
newObjectBottom=newObjectTop+$(marginObject).outerHeight(true)
@marginData.addNewObject(annotation,newObjectTop,$(annotation.highlights[0]).offset().left)
$(marginObject).fadeIn('fast')
@onMarginSelected(marginObject)
onAnnotationSelected: (event) ->
event.stopPropagation()
annotations = $(event.target)
.parents('.annotator-hl')
.andSelf()
.map -> return $(this).data("annotation")
#cycle annotations
selectIndex=0
if annotations.length>1
# find currently annotated object
for i in [0..annotations.length-1]
if $(annotations[i]._marginObject).hasClass("annotator-marginviewer-selected")
selectIndex=(i+1)%annotations.length
break
@onMarginSelected(annotations[selectIndex]._marginObject)
onAnnotationDeleted: (annotation) ->
nextObject = @marginData.getNextObject(annotation)
@marginData.deleteObject(annotation)
if nextObject!=null
@onMarginSelected(nextObject[1]._marginObject)
zeroPad : (num,count) ->
numZeroPad = String(num)
while numZeroPad.length<count
numZeroPad = "0" + numZeroPad
return numZeroPad
formattedDateTime : (dateobj) ->
meridiemString = if dateobj.getHours()<12 then "AM" else "PM"
timeString = dateobj.getHours() + ":" + @zeroPad(dateobj.getMinutes(),2) + meridiemString
dateString = @zeroPad(dateobj.getDate(),2) + "." + @zeroPad(dateobj.getMonth(),2) + "." + dateobj.getFullYear()
return timeString + " " + dateString
renderMarginObject : (annotation) ->
datetime = if annotation.created then @formattedDateTime(new Date annotation.created) else ''
user = @annotator.plugins.AnnotateItPermissions.user.userId
delel = if annotation.user is user or annotation.permissions.delete.indexOf(user)>=0 then '<span class="annotator-marginviewer-delete" style="float: left; direction: ltr;">X</span>' else ''
return '<div class="annotator-marginviewer-element"><div class="annotator-marginviewer-header"><span class="annotator-marginviewer-user">'+annotation.user+'</span>'+delel+'<span class="annotator-marginviewer-date" style="float: left; direction: ltr;">'+datetime+'</span></div><div class="annotator-marginviewer-text">'+annotation.text+'</div></div>'
createMarginObject : (annotation, location=null, hide=false) ->
marginObjects=$(@renderMarginObject(annotation)).appendTo('.secondary').click((event) => @onMarginSelected(event.target)).mouseenter((event) => @onMarginMouseIn(event.target)).mouseleave((event) => @onMarginMouseOut(event.target))
marginObjects.children(".annotator-marginviewer-header").children(".annotator-marginviewer-delete").click((event) => @onMarginDeleted(event.target))
if location!=null
marginObjects.offset({top: location})
if hide
marginObjects.hide()
marginObject=marginObjects[0]
annotation._marginObject=marginObject
marginObject.annotation=annotation
return marginObject
onAnnotationUpdated: (annotation) ->
# updates not supported right now
onMarginMouseIn: (obj) ->
$($(obj).closest(".annotator-marginviewer-element")[0].annotation.highlights).addClass("annotator-hl-uber-temp").removeClass("annotator-hl")
onMarginMouseOut: (obj) ->
$($(obj).closest(".annotator-marginviewer-element")[0].annotation.highlights).addClass("annotator-hl").removeClass("annotator-hl-uber-temp")
onMarginSelected: (obj) ->
marginObject = $(obj).closest(".annotator-marginviewer-element")[0]
annotation = marginObject.annotation
horizontalSlide=[]
newTop = $(annotation.highlights[0]).offset().top
newBottom = $(marginObject).outerHeight(true)+newTop
newLocationsByObject = @marginData.getNewLocationsForObject(newTop,newBottom,marginObject)
if @currentSelectedAnnotation != null
if annotation.id is @currentSelectedAnnotation.id
return
else
currentMarginObject=@currentSelectedAnnotation._marginObject
if currentMarginObject!=null
$(@currentSelectedAnnotation.highlights).removeClass("annotator-hl-uber").removeClass("annotator-hl-uber-temp").addClass("annotator-hl")
# find object's new top if it needs to change, also remove it from the list
currentObjectNewTop = $(currentMarginObject).offset().top
newLocationsByObject=$.grep(newLocationsByObject,(value) ->
if value[1].annotation.id is currentMarginObject.annotation.id
currentObjectNewTop=value[0]
return false
else
return true
)
horizontalSlide.push([currentObjectNewTop,"+=20px",currentMarginObject])
$(currentMarginObject).removeClass("annotator-marginviewer-selected")
$(marginObject).addClass("annotator-marginviewer-selected")
horizontalSlide.push([newTop,"-=20px",marginObject])
@moveObjectsToNewLocation(newLocationsByObject,horizontalSlide)
$(annotation.highlights).addClass("annotator-hl-uber").removeClass("annotator-hl")
@currentSelectedAnnotation = annotation
onMarginDeleted: (obj) ->
marginObject = $(obj).closest(".annotator-marginviewer-element")[0]
annotation = marginObject.annotation
$(marginObject).remove()
annotation._marginObject = null
@annotator.deleteAnnotation(annotation)
moveObjectsToNewLocation: (newLocations,horizontalSlideObjects=[]) ->
for newLocationStructure in newLocations
newTop = newLocationStructure[0]
currentObject = newLocationStructure[1]
$(currentObject).animate({top:"+="+(newTop-$(currentObject).offset().top)},'fast','swing')
@marginData.updateObjectLocation(currentObject.annotation)
for horizontalSlide in horizontalSlideObjects
newTop = horizontalSlide[0]
newMarginRight = horizontalSlide[1]
currentObject = horizontalSlide[2]
$(currentObject).animate({top:"+="+(newTop-$(currentObject).offset().top),'margin-right':newMarginRight},'fast','swing')
@marginData.updateObjectLocation(currentObject.annotation)
highlightMargin: (annotations) ->
if @highlightedObjects.length>0
oldObjects=[]
for existingHighlight in @highlightedObjects
found=false
for newHighlight in annotations
if newHighlight.id == existingHighlight.id
found=true
if not found
oldObjects.push(existingHighlight)
@hideHighlightedMargin oldObjects
@highlightedObjects=annotations
marginObjects=jQuery.map(annotations,(val,i)->val._marginObject)
$(marginObjects).addClass("annotator-marginviewer-highlighted")
hideHighlightedMargin: (annotations) ->
@highlightedObjects=[]
marginObjects=jQuery.map(annotations,(val,i)->val._marginObject)
$(marginObjects).removeClass("annotator-marginviewer-highlighted")