-
Notifications
You must be signed in to change notification settings - Fork 36
/
refresh.coffee
501 lines (446 loc) · 16.5 KB
/
refresh.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# Refresh will fetch a page and use it to fill a dom
# element that has been ready made to hold it.
#
# cycle: have a div, $(this), with id = slug
# whenGotten: have a pageObject we just fetched
# buildPage: have a pageObject from somewhere
# rebuildPage: have a key from saving pageObject in lineup
# renderPageIntoPageElement: have $page annotated from pageObject
# pageObject.seqItems: get back each item sequentially
# plugin.do: have $item in dom for item
#
# The various calling conventions are due to async
# requirements and the work of many hands.
_ = require 'underscore'
pageHandler = require './pageHandler'
plugin = require './plugin'
state = require './state'
neighborhood = require './neighborhood'
addToJournal = require './addToJournal'
actionSymbols = require './actionSymbols'
lineup = require './lineup'
resolve = require './resolve'
random = require './random'
pageModule = require('./page')
newPage = pageModule.newPage
asSlug = pageModule.asSlug
pageEmitter = pageModule.pageEmitter
getItem = ($item) ->
$($item).data("item") or $($item).data('staticItem') if $($item).length > 0
aliasItem = ($page, $item, oldItem) ->
item = $.extend {}, oldItem
$item.data('item', item);
pageObject = lineup.atKey($page.data('key'))
if pageObject.getItem(item.id)?
item.alias ||= item.id
item.id = random.itemId()
$item.attr 'data-id', item.id
$item.data('id', item.id)
$item.data('item').id = item.id
else if item.alias?
unless pageObject.getItem(item.alias)?
item.id = item.alias
delete item.alias
$item.attr 'data-id', item.id
item
equals = (a, b) -> a and b and a.get(0) == b.get(0)
getStoryItemOrder = ($story) ->
$story.children().map((_, value) -> $(value).attr('data-id')).get()
handleDrop = (evt, ui, originalIndex, originalOrder) ->
$item = ui.item
item = getItem($item)
$sourcePage = $item.data('pageElement')
sourceIsReadOnly = $sourcePage.hasClass('ghost') || $sourcePage.hasClass('remote')
unless $sourcePage.hasClass('ghost')
dragAttribution = {
page: $sourcePage.data().data['title']
}
if $sourcePage.data().site?
dragAttribution['site'] = $sourcePage.data().site
$destinationPage = $item.parents('.page:first')
destinationIsGhost = $destinationPage.hasClass('ghost')
moveWithinPage = equals($sourcePage, $destinationPage)
moveBetweenDuplicatePages = not moveWithinPage and \
not evt.shiftKey and \
$sourcePage.attr('id') == $destinationPage.attr('id')
removedTo = {
page: $destinationPage.data().data['title']
}
if destinationIsGhost or moveBetweenDuplicatePages
$(evt.target).sortable('cancel')
return
if moveWithinPage
order = getStoryItemOrder($item.parents('.story:first'))
if not _.isEqual(order, originalOrder)
$('.shadow-copy').remove()
$item.empty()
index = $(".item").index($item)
index = originalIndex if originalIndex < index
plugin.renderFrom index
pageHandler.put $destinationPage, {id: item.id, type: 'move', order: order}
return
copying = sourceIsReadOnly or evt.shiftKey
if copying
# If making a copy, update the temp clone so it becomes a true copy.
$('.shadow-copy').removeClass('shadow-copy')
.data($item.data()).attr({'data-id': $item.attr('data-id')})
else
pageHandler.put $sourcePage, {id: item.id, type: 'remove', removedTo: removedTo}
# Either way, record the add to the new page
$item.data 'pageElement', $destinationPage
$before = $item.prev('.item')
before = getItem($before)
item = aliasItem $destinationPage, $item, item
pageHandler.put $destinationPage,
{id: item.id, type: 'add', item, after: before?.id, attribution: dragAttribution }
$('.shadow-copy').remove()
$item.empty()
$before.after($item)
index = $(".item").index($item)
index = originalIndex if originalIndex < index
plugin.renderFrom index
changeMouseCursor = (e, ui) ->
$sourcePage = ui.item.data('pageElement')
sourceIsReadOnly = $sourcePage.hasClass('ghost') || $sourcePage.hasClass('remote')
$destinationPage = ui.placeholder.parents('.page:first')
destinationIsGhost = $destinationPage.hasClass('ghost')
moveWithinPage = equals($sourcePage, $destinationPage)
moveBetweenDuplicatePages = not moveWithinPage and \
$sourcePage.attr('id') == $destinationPage.attr('id')
copying = sourceIsReadOnly or (e.shiftKey and not moveWithinPage)
if destinationIsGhost or (moveBetweenDuplicatePages and not e.shiftKey)
$('body').css('cursor', 'no-drop')
$('.shadow-copy').hide()
else if copying
$('body').css('cursor', 'copy')
$('.shadow-copy').show()
else
$('body').css('cursor', 'move')
$('.shadow-copy').hide()
initDragging = ($page) ->
origCursor = $('body').css('cursor')
options =
connectWith: '.page .story'
placeholder: 'item-placeholder'
forcePlaceholderSize: true
delay: 150
$story = $page.find('.story')
originalOrder = null
originalIndex = null
dragCancelled = null
cancelDrag = (e) ->
if e.which == 27
dragCancelled = true
$story.sortable('cancel')
$story.sortable(options)
.on 'sortstart', (e, ui) ->
$item = ui.item
originalOrder = getStoryItemOrder($story)
originalIndex = $('.item').index($item)
dragCancelled = false
$('body').on('keydown', cancelDrag)
# Create a copy that we control since sortable removes theirs too early.
# Insert after the placeholder to prevent adding history when item not moved.
# Clear out the styling they add. Updates to jquery ui can affect this.
$item.clone().insertAfter(ui.placeholder).hide().addClass("shadow-copy")
.css(
width: ''
height: ''
position: ''
zIndex: ''
).removeAttr('data-id')
.on 'sort', changeMouseCursor
.on 'sortstop', (e, ui) ->
$('body').css('cursor', origCursor).off('keydown', cancelDrag)
handleDrop(e, ui, originalIndex, originalOrder) unless dragCancelled
$('.shadow-copy').remove()
getPageObject = ($journal) ->
$page = $($journal).parents('.page:first')
lineup.atKey $page.data('key')
handleMerging = (event, ui) ->
drag = getPageObject ui.draggable
drop = getPageObject event.target
pageEmitter.emit 'show', drop.merge drag
initMerging = ($page) ->
$journal = $page.find('.journal')
$journal.draggable
revert: true
appendTo: '.main'
scroll: false
helper: 'clone'
$journal.droppable
hoverClass: "ui-state-hover"
drop: handleMerging
accept: '.journal'
initAddButton = ($page) ->
$page.find(".add-factory").on("click", (evt) ->
return if $page.hasClass 'ghost'
evt.preventDefault()
createFactory($page))
createFactory = ($page) ->
item =
type: "factory"
id: random.itemId()
$item = $("<div />", class: "item factory").data('item',item).attr('data-id', item.id)
$item.data 'pageElement', $page
$page.find(".story").append($item)
plugin.do $item, item
$before = $item.prev('.item')
before = getItem($before)
pageHandler.put $page, {item: item, id: item.id, type: "add", after: before?.id}
handleHeaderClick = (e) ->
e.preventDefault()
lineup.debugSelfCheck ($(each).data('key') for each in $('.page'))
$page = $(e.target).parents('.page:first')
crumbs = lineup.crumbs $page.data('key'), location.host
[target, ] = crumbs
[prefix, ] = wiki.site(target).getDirectURL('').split('/')
if prefix is ''
prefix = window.location.protocol
newWindow = window.open "#{prefix}//#{crumbs.join '/'}", target
newWindow.focus()
emitHeader = ($header, $page, pageObject) ->
if pageObject.isRecycler()
remote = 'recycler'
else
remote = pageObject.getRemoteSite location.host
tooltip = pageObject.getRemoteSiteDetails location.host
$header.append """
<h1 title="#{tooltip}">
<span>
<a href="#{pageObject.siteLineup()}" target="#{remote}">
<img src="#{wiki.site(remote).flag()}" height="32px" class="favicon"></a>
#{resolve.escape pageObject.getTitle()}
</span>
</h1>
"""
$header.find('a').on 'click', handleHeaderClick
emitTimestamp = ($header, $page, pageObject) ->
if $page.attr('id').match /_rev/
$page.addClass('ghost')
$page.data('rev', pageObject.getRevision())
$header.append $ """
<h2 class="revision">
<span>
#{pageObject.getTimestamp()}
</span>
</h2>
"""
emitControls = ($journal) ->
$journal.append """
<div class="control-buttons">
<a href="#" class="button fork-page" title="fork this page">#{actionSymbols.fork}</a>
<a href="#" class="button add-factory" title="add paragraph">#{actionSymbols.add}</a>
</div>
"""
emitBacklinks = ($backlinks, pageObject) ->
slug = pageObject.getSlug()
backlinks = neighborhood.backLinks(slug)
if Object.keys(backlinks).length > 0
links = []
for linkSlug, backlink of backlinks
backlink.sites.sort (a,b) ->
(a.date || 0) < (b.date || 0)
flags = []
for site, i in backlink.sites
if i < 10
joint = if backlink.sites[i-1]?.date == site.date then "" else " "
flags.unshift joint
flags.unshift """
<img class="remote"
src="#{wiki.site(site.site).flag()}"
data-slug="#{linkSlug}"
data-site="#{site.site}"
data-id="#{site.itemId}"
title="#{site.site}\n#{wiki.util.formatElapsedTime site.date}">
"""
else if i == 10
flags.unshift ' ⋯ '
linkBack = resolve.resolveLinks("[[#{backlink.title}]]")
links.push """
<div style="clear: both;">
<div style="float: left;">#{linkBack}</div>
<div style="text-align: right;"> #{flags.join('')} </div>
</div>
"""
if links
$backlinks.append """
<details>
<summary>#{links.length} pages link here:</summary>
#{links.join "\n"}
</details>
"""
emitFooter = ($footer, pageObject) ->
host = pageObject.getRemoteSite(location.host)
slug = pageObject.getSlug()
$footer.append """
<a class="show-page-license" href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank">CC BY-SA 4.0</a> .
<a class="show-page-source" href="#{wiki.site(host).getDirectURL(slug)}.json" title="source">JSON</a> .
<a href= "#{wiki.site(host).getDirectURL(slug)}.html" date-slug="#{slug}" target="#{host}">#{host} </a> .
<a href= "#" class=search>search</a>
"""
editDate = (journal) ->
for action in (journal || []) by -1
return action.date if action.date and action.type != 'fork'
undefined
emitTwins = ($page) ->
page = $page.data 'data'
return unless page
site = $page.data('site') or window.location.host
site = window.location.host if site in ['view', 'origin']
slug = asSlug page.title
if viewing = editDate(page.journal)
bins = {newer:[], same:[], older:[]}
# {fed.wiki.org: [{slug: "happenings", title: "Happenings", date: 1358975303000, synopsis: "Changes here ..."}]}
for remoteSite, info of neighborhood.sites
if remoteSite != site and info.sitemap?
for item in info.sitemap
if item.slug == slug
bin = if item.date > viewing then bins.newer
else if item.date < viewing then bins.older
else bins.same
bin.push {remoteSite, item}
twins = []
# {newer:[remoteSite: "fed.wiki.org", item: {slug: ..., date: ...}, ...]}
for legend, bin of bins
continue unless bin.length
bin.sort (a,b) ->
a.item.date < b.item.date
flags = for {remoteSite, item}, i in bin
break if i >= 8
"""<img class="remote"
src="#{wiki.site(remoteSite).flag()}"
data-slug="#{slug}"
data-site="#{remoteSite}"
title="#{remoteSite}">
"""
twins.push "#{flags.join ' '} #{legend}"
$page.find('.twins').html """<p><span>#{twins.join ", "}</span></p>""" if twins
renderPageIntoPageElement = (pageObject, $page) ->
$page.data("data", pageObject.getRawPage())
$page.data("site", pageObject.getRemoteSite()) if pageObject.isRemote()
# console.log '.page keys ', ($(each).data('key') for each in $('.page'))
# console.log 'lineup keys', lineup.debugKeys()
resolve.resolutionContext = pageObject.getContext()
$page.empty()
$paper = $("<div class='paper' />")
$page.append($paper)
[$handleParent, $twins, $header, $story, $backlinks, $journal, $footer] = ['handle-parent', 'twins', 'header', 'story', 'backlinks', 'journal', 'footer'].map (className) ->
$('<div />').addClass(className).appendTo($paper) if className != 'journal' or $('.editEnable').is(':visible')
$pagehandle = $('<div />').addClass('page-handle').appendTo($handleParent)
emitHeader $header, $page, pageObject
emitTimestamp $header, $page, pageObject
promise = pageObject.seqItems (item, done) ->
$item = $ """<div class="item #{item.type}" data-id="#{item.id}">"""
$story.append $item
$item.data('item', item)
done()
promise = promise.then ->
index = $(".page").index($page[0])
itemIndex = $('.item').index($($('.page')[index]).find('.item'))
plugin.renderFrom itemIndex
.then ->
return $page
if $('.editEnable').is(':visible')
pageObject.seqActions (each, done) ->
addToJournal $journal, each.separator if each.separator
addToJournal $journal, each.action
done()
emitTwins $page
emitBacklinks $backlinks, pageObject
emitControls $journal if $('.editEnable').is(':visible')
emitFooter $footer, pageObject
$pagehandle.css({
height: "#{$story.position().top-$handleParent.position().top-5}px"
})
return promise
createMissingFlag = ($page, pageObject) ->
unless pageObject.isRemote()
$('img.favicon',$page).on('error', ->
plugin.get 'favicon', (favicon) ->
favicon.create())
rebuildPage = (pageObject, $page) ->
$page.addClass('local') if pageObject.isLocal()
$page.addClass('recycler') if pageObject.isRecycler()
$page.addClass('remote') if pageObject.isRemote()
$page.addClass('plugin') if pageObject.isPlugin()
promise = renderPageIntoPageElement pageObject, $page
createMissingFlag $page, pageObject
#STATE -- update url when adding new page, removing others
state.setUrl()
if $('.editEnable').is(':visible')
initDragging $page
initMerging $page
initAddButton $page
promise
buildPage = (pageObject, $page) ->
pageKey =lineup.addPage(pageObject)
$page.data('key', pageKey)
$page.get(0).dataset.key = pageKey
rebuildPage(pageObject, $page)
newFuturePage = (title, create) ->
slug = asSlug title
pageObject = newPage()
pageObject.setTitle(title)
hits = []
for site, info of neighborhood.sites
if info.sitemap?
result = _.find info.sitemap, (each) ->
each.slug == slug
if result?
hits.push
"type": "reference"
"site": site
"slug": slug
"title": result.title || slug
"text": result.synopsis || ''
if hits.length > 0
pageObject.addItem
'type': 'future'
'text': 'We could not find this page where it was expected.'
'title': title
'create': create
'context': pageHandler.context.filter((c) -> !['view', 'origin', 'local'].includes(c))
pageObject.addItem
'type': 'paragraph'
'text': "We did find possible duplicate in the current neighborhood."
pageObject.addItem hit for hit in hits
else
pageObject.addItem
'type': 'future'
'text': 'We could not find this page.'
'title': title
'create': create
'context': pageHandler.context.filter((c) -> !['view', 'origin', 'local'].includes(c))
pageObject
cycle = ($page) ->
promise = new Promise (resolve, _reject) ->
[slug, rev] = $page.attr('id').split('_rev')
title = $page.find('.header h1').text().trim()
pageInformation = {
slug: slug
rev: rev
site: $page.data('site')
}
whenNotGotten = ->
link = $("""a.internal[href="/#{slug}.html"]:last""")
title = title or link.text() or slug
key = link.parents('.page').data('key')
create = lineup.atKey(key)?.getCreate()
pageObject = newFuturePage(title)
promise = buildPage( pageObject, $page)
promise
.then ($page) ->
$page.addClass('ghost')
resolve promise
whenGotten = (pageObject) ->
promise = buildPage( pageObject, $page)
for site in pageObject.getNeighbors(location.host)
neighborhood.registerNeighbor site
resolve promise
pageHandler.get
whenGotten: whenGotten
whenNotGotten: whenNotGotten
pageInformation: pageInformation
return promise
module.exports = {cycle, emitTwins, buildPage, rebuildPage, newFuturePage}