Skip to content
This repository was archived by the owner on Feb 14, 2018. It is now read-only.

Commit 77ca2aa

Browse files
committed
[merge:stores<--remotes/ward/master]
2 parents da7b979 + 4767721 commit 77ca2aa

File tree

15 files changed

+1072
-539
lines changed

15 files changed

+1072
-539
lines changed

client/client.js

Lines changed: 345 additions & 213 deletions
Large diffs are not rendered by default.

client/lib/fetch.coffee

Lines changed: 0 additions & 43 deletions
This file was deleted.

client/lib/legacy.coffee

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
window.wiki = {}
22
util = require('./util.coffee')
3-
fetch = require('./fetch.coffee')
3+
pageHandler = wiki.pageHandler = require('./pageHandler.coffee')
44
plugin = require('./plugin.coffee')
55
state = require('./state.coffee')
66
active = require('./active.coffee')
@@ -83,63 +83,20 @@ $ ->
8383
.attr("href", "//#{action.site}/#{pageElement.attr('id')}.html")
8484
.data("site", action.site)
8585
.data("slug", pageElement.attr('id'))
86-
else
87-
actionElement.on 'click', ->
88-
wiki.dialog "#{action.type} action", $('<pre/>').text(JSON.stringify(action, null, 2))
8986

9087
useLocalStorage = wiki.useLocalStorage = ->
9188
wiki.log 'useLocalStorage', $(".login").length > 0
9289
$(".login").length > 0
9390

94-
putAction = wiki.putAction = (pageElement, action) ->
95-
action.date = (new Date()).getTime()
96-
if (site = pageElement.data('site'))?
97-
action.fork = site
98-
pageElement.find('h1 img').attr('src', '/favicon.png')
99-
pageElement.find('h1 a').attr('href', '/')
100-
pageElement.data('site', null)
101-
state.setUrl()
102-
addToJournal pageElement.find('.journal'),
103-
type: 'fork'
104-
site: site
105-
date: action.date
106-
if useLocalStorage()
107-
pushToLocal(pageElement, action)
108-
pageElement.addClass("local")
109-
else
110-
pushToServer(pageElement, action)
111-
112-
pushToLocal = (pageElement, action) ->
113-
page = localStorage[pageElement.attr("id")]
114-
page = JSON.parse(page) if page
115-
page = action.item if action.type == 'create'
116-
page ||= pageElement.data("data")
117-
page.journal = [] unless page.journal?
118-
page.journal.concat(action)
119-
page.story = $(pageElement).find(".item").map(-> $(@).data("item")).get()
120-
localStorage[pageElement.attr("id")] = JSON.stringify(page)
121-
addToJournal pageElement.find('.journal'), action
122-
123-
pushToServer = (pageElement, action) ->
124-
$.ajax
125-
type: 'PUT'
126-
url: "/page/#{pageElement.attr('id')}/action"
127-
data:
128-
'action': JSON.stringify(action)
129-
success: () ->
130-
addToJournal pageElement.find('.journal'), action
131-
error: (xhr, type, msg) ->
132-
wiki.log "ajax error callback", type, msg
133-
13491
textEditor = wiki.textEditor = (div, item) ->
13592
textarea = $("<textarea>#{original = item.text ? ''}</textarea>")
13693
.focusout ->
13794
if item.text = textarea.val()
13895
plugin.do div.empty(), item
13996
return if item.text == original
140-
putAction div.parents('.page:first'), {type: 'edit', id: item.id, item: item}
97+
pageHandler.put div.parents('.page:first'), {type: 'edit', id: item.id, item: item}
14198
else
142-
putAction div.parents('.page:first'), {type: 'remove', id: item.id}
99+
pageHandler.put div.parents('.page:first'), {type: 'remove', id: item.id}
143100
div.remove()
144101
null
145102
.bind 'keydown', (e) ->
@@ -155,9 +112,23 @@ $ ->
155112
getItem = wiki.getItem = (element) ->
156113
$(element).data("item") or JSON.parse($(element).data('staticItem')) if $(element).length > 0
157114

158-
wiki.getData = ->
159-
who = $('.chart,.data,.calculator').last()
160-
if who? then who.data('item').data else {}
115+
wiki.getData = (vis) ->
116+
if vis
117+
idx = $('.item').index(vis)
118+
who = $(".item:lt(#{idx})").filter('.chart,.data,.calculator').last()
119+
if who? then who.data('item').data else {}
120+
else
121+
who = $('.chart,.data,.calculator').last()
122+
if who? then who.data('item').data else {}
123+
124+
wiki.getDataNodes = (vis) ->
125+
if vis
126+
idx = $('.item').index(vis)
127+
who = $(".item:lt(#{idx})").filter('.chart,.data,.calculator').toArray().reverse()
128+
$(who)
129+
else
130+
who = $('.chart,.data,.calculator').toArray().reverse()
131+
$(who)
161132

162133
doInternalLink = wiki.doInternalLink = (name, page) ->
163134
name = util.asSlug(name)
@@ -216,14 +187,34 @@ $ ->
216187

217188
.delegate '.internal', 'click', (e) ->
218189
name = $(e.target).data 'pageName'
219-
fetch.context = $(e.target).attr('title').split(' => ')
190+
pageHandler.context = $(e.target).attr('title').split(' => ')
220191
finishClick e, name
221192

222-
.delegate '.action.fork, .remote', 'click', (e) ->
193+
.delegate '.remote', 'click', (e) ->
223194
name = $(e.target).data('slug')
224-
fetch.context = [$(e.target).data('site')]
195+
pageHandler.context = [$(e.target).data('site')]
225196
finishClick e, name
226197

198+
.delegate '.action', 'click', (e) ->
199+
element = $(e.target)
200+
if element.is('.fork')
201+
name = $(e.target).data('slug')
202+
fetch.context = [$(e.target).data('site')]
203+
finishClick e, name
204+
else
205+
journalEntryIndex = $(this).parent().children().index(element)
206+
data = $(this).parent().parent().data('data')
207+
titleUrl = util.asSlug(data.title)
208+
revUrl = "#{titleUrl}_rev#{journalEntryIndex}"
209+
e.preventDefault()
210+
page = $(e.target).parents('.page') unless e.shiftKey
211+
$(page).nextAll().remove() if page?
212+
createPage(revUrl)
213+
.appendTo($('.main'))
214+
.each refresh
215+
active.set($('.page').last())
216+
217+
227218
.delegate '.action', 'hover', ->
228219
id = $(this).attr('data-id')
229220
$("[data-id=#{id}]").toggleClass('target')

client/lib/pageHandler.coffee

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
util = require('./util')
2+
state = require('./state')
3+
revision = require('./revision')
4+
5+
module.exports = pageHandler = {}
6+
7+
pageHandler.get = (pageElement, callback, localContext) ->
8+
# slug = pageElement.attr('id')
9+
pageAndRevisionStr = pageElement.attr('id')
10+
pageAndRevision = pageAndRevisionStr.split('_rev')
11+
slug = pageAndRevision[0]
12+
rev = pageAndRevision[1]
13+
14+
site = pageElement.data('site')
15+
if pageElement.attr('data-server-generated') == 'true'
16+
callback null
17+
if wiki.useLocalStorage() and json = localStorage[slug]
18+
pageElement.addClass("local")
19+
callback JSON.parse(json)
20+
pageHandler.context = ['origin'] unless pageHandler.context.length > 0
21+
if site
22+
localContext = []
23+
else
24+
localContext ?= (i for own i in pageHandler.context)
25+
site = localContext.shift()
26+
resource = if site=='origin'
27+
site = null
28+
slug
29+
else
30+
"remote/#{site}/#{slug}"
31+
32+
$.ajax
33+
type: 'GET'
34+
dataType: 'json'
35+
url: "/#{resource}.json?random=#{util.randomBytes(4)}"
36+
success: (page) ->
37+
wiki.log 'fetch success', page, site || 'origin'
38+
$(pageElement).data('site', site)
39+
page = revision.create rev, page if rev
40+
callback(page)
41+
error: (xhr, type, msg) ->
42+
if localContext.length > 0
43+
pageHandler.get pageElement, callback, localContext
44+
else
45+
site = null
46+
title = $("""a[href="/#{slug}.html"]""").html()
47+
title or= slug
48+
page = {title}
49+
pageHandler.put $(pageElement), {type: 'create', id: util.randomBytes(8), item: page}
50+
callback page
51+
52+
pageHandler.context = []
53+
54+
pushToLocal = (pageElement, action) ->
55+
page = localStorage[pageElement.attr("id")]
56+
page = JSON.parse(page) if page
57+
page = action.item if action.type == 'create'
58+
page ||= pageElement.data("data")
59+
page.journal = [] unless page.journal?
60+
page.journal.concat(action)
61+
page.story = $(pageElement).find(".item").map(-> $(@).data("item")).get()
62+
localStorage[pageElement.attr("id")] = JSON.stringify(page)
63+
wiki.addToJournal pageElement.find('.journal'), action
64+
65+
pushToServer = (pageElement, action) ->
66+
$.ajax
67+
type: 'PUT'
68+
url: "/page/#{pageElement.attr('id')}/action"
69+
data:
70+
'action': JSON.stringify(action)
71+
success: () ->
72+
wiki.addToJournal pageElement.find('.journal'), action
73+
error: (xhr, type, msg) ->
74+
wiki.log "ajax error callback", type, msg
75+
76+
pageHandler.put = (pageElement, action) ->
77+
action.date = (new Date()).getTime()
78+
if (site = pageElement.data('site'))?
79+
action.fork = site
80+
pageElement.find('h1 img').attr('src', '/favicon.png')
81+
pageElement.find('h1 a').attr('href', '/')
82+
pageElement.data('site', null)
83+
state.setUrl()
84+
wiki.addToJournal pageElement.find('.journal'),
85+
type: 'fork'
86+
site: site
87+
date: action.date
88+
if wiki.useLocalStorage()
89+
pushToLocal(pageElement, action)
90+
pageElement.addClass("local")
91+
else
92+
pushToServer(pageElement, action)
93+

client/lib/refresh.coffee

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
util = require('./util.coffee')
2-
fetch = require('./fetch.coffee')
2+
pageHandler = require('./pageHandler.coffee')
33
plugin = require('./plugin.coffee')
44
state = require('./state.coffee')
55

@@ -27,7 +27,7 @@ handleDragging = (evt, ui) ->
2727
before = wiki.getItem(beforeElement)
2828
{type: 'add', item: item, after: before?.id}
2929
action.id = item.id
30-
wiki.putAction thisPageElement, action
30+
pageHandler.put thisPageElement, action
3131

3232
initDragging = (pageElement) ->
3333
storyElement = pageElement.find('.story')
@@ -47,7 +47,7 @@ initAddButton = (pageElement) ->
4747
plugin.do itemElement, item
4848
beforeElement = itemElement.prev('.item')
4949
before = wiki.getItem(beforeElement)
50-
wiki.putAction pageElement, {item: item, id: item.id, type: "add", after: before?.id}
50+
pageHandler.put pageElement, {item: item, id: item.id, type: "add", after: before?.id}
5151

5252
emitHeader = (pageElement, page) ->
5353
site = $(pageElement).data('site')
@@ -114,5 +114,5 @@ module.exports = refresh = wiki.refresh = ->
114114
initDragging pageElement
115115
initAddButton pageElement
116116

117-
fetch pageElement, buildPage
117+
pageHandler.get pageElement, buildPage
118118

client/lib/revision.coffee

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# **revision.coffee**
2+
# This module generates a past revision of a data file and caches it in 'data/rev'.
3+
#
4+
# The saved file has the name of the id of the point in the journal's history
5+
# that the revision represents.
6+
7+
create = (revIndex, data) ->
8+
journal = data.journal
9+
revTitle = data.title
10+
revStory = []
11+
revJournal = []
12+
for journalEntry in journal.slice 0, revIndex+1
13+
itemSplicedIn = false
14+
itemEdited = false
15+
revJournal.push(journalEntry)
16+
switch journalEntry.type
17+
when 'create'
18+
if journalEntry.item.title?
19+
revTitle = journalEntry.item.title
20+
when 'add'
21+
if journalEntry.after?
22+
for storyItem, i in revStory
23+
if storyItem.id == journalEntry.after
24+
itemSplicedIn = true
25+
revStory.splice(i+1,0,journalEntry.item)
26+
break
27+
if !itemSplicedIn #defensive coding for if we don't have a story item to put this after
28+
revStory.push journalEntry.item
29+
else
30+
revStory.push journalEntry.item
31+
when 'edit'
32+
for storyItem, i in revStory
33+
if storyItem.id == journalEntry.id
34+
revStory[i] = journalEntry.item
35+
itemEdited = true
36+
break
37+
if !itemEdited #the first journal entry for welcome visitors is an edit
38+
revStory.push(journalEntry.item)
39+
when 'move'
40+
items = []
41+
for storyItem in revStory
42+
items[storyItem.id] = storyItem
43+
revStory = []
44+
for itemId in journalEntry.order
45+
revStory.push(items[itemId])
46+
when 'remove'
47+
removeId = journalEntry.id;
48+
for storyItem, i in revStory
49+
if storyItem.id == removeId
50+
revStory.splice(i,1)
51+
break
52+
#when 'fork' # do nothing when fork
53+
return {story: revStory, journal: revJournal, title: revTitle}
54+
55+
56+
exports.create = create

client/plugins/meta-factory.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ window.plugins.factory =
2020
plugin.bind div, item
2121
catch err
2222
div.append "<p class='error'>#{err}</p>"
23-
wiki.putAction pageElement, {type: 'edit', id: item.id, item: item}
23+
wiki.pageHandler.put pageElement, {type: 'edit', id: item.id, item: item}
2424

2525
div.dblclick ->
2626
div.removeClass('factory').addClass(item.type='paragraph')

client/plugins/meta-factory.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)