This repository has been archived by the owner on Feb 14, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rearranged fetch some more, added tests.
- Loading branch information
Showing
7 changed files
with
294 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,43 @@ | ||
util = require('./util') | ||
|
||
module.exports = (params) -> | ||
{buildPage, putAction, pageElement} = params | ||
slug = $(pageElement).attr('id') | ||
site = $(pageElement).data('site') | ||
probe = (pageElement, callback, localContext) -> | ||
slug = pageElement.attr('id') | ||
site = pageElement.data('site') | ||
if pageElement.attr('data-server-generated') == 'true' | ||
callback null | ||
if wiki.useLocalStorage() and json = localStorage[slug] | ||
pageElement.addClass("local") | ||
callback JSON.parse(json) | ||
probe.context = ['origin'] unless probe.context.length > 0 | ||
localContext ?= (i for own i in probe.context) | ||
if typeof site is 'string' | ||
localContext = [site] | ||
site = localContext.shift() | ||
resource = if site=='origin' | ||
site = null | ||
slug | ||
else | ||
"remote/#{site}/#{slug}" | ||
$.ajax | ||
type: 'GET' | ||
dataType: 'json' | ||
url: "/#{resource}.json?random=#{util.randomBytes(4)}" | ||
success: (page) -> | ||
wiki.log 'fetch success', page, site || 'origin' | ||
$(pageElement).data('site', site) | ||
callback(page) | ||
error: (xhr, type, msg) -> | ||
if localContext.length > 0 | ||
probe pageElement, callback, localContext | ||
else | ||
site = null | ||
title = $("""a[href="/#{slug}.html"]""").html() | ||
title or= slug | ||
page = {title} | ||
wiki.putAction $(pageElement), {type: 'create', id: util.randomBytes(8), item: page} | ||
callback page | ||
|
||
probe = (slug, callback, localContext) -> | ||
wiki.fetchContext = ['origin'] unless wiki.fetchContext.length > 0 | ||
localContext ?= (i for own i in wiki.fetchContext) | ||
site = localContext.shift() | ||
resource = if site=='origin' | ||
site = null | ||
slug | ||
else | ||
"remote/#{site}/#{slug}" | ||
$.ajax | ||
type: 'GET' | ||
dataType: 'json' | ||
url: "/#{resource}.json?random=#{util.randomBytes(4)}" | ||
success: (page) -> | ||
wiki.log 'fetch success', page, site || 'origin' | ||
$(pageElement).data('site', site) | ||
callback(page) | ||
error: (xhr, type, msg) -> | ||
if localContext.length > 0 | ||
probe slug, callback, localContext | ||
else | ||
site = null | ||
callback(null) | ||
probe.context = [] | ||
|
||
create = (slug, callback) -> | ||
title = $("""a[href="/#{slug}.html"]""").html() | ||
title or= slug | ||
page = {title} | ||
putAction $(pageElement), {type: 'create', id: util.randomBytes(8), item: page} | ||
callback page | ||
module.exports = probe | ||
|
||
if $(pageElement).attr('data-server-generated') == 'true' | ||
buildPage null | ||
else | ||
if wiki.useLocalStorage() and json = localStorage[pageElement.attr("id")] | ||
pageElement.addClass("local") | ||
buildPage JSON.parse(json) | ||
else | ||
if site? | ||
$.get "/remote/#{site}/#{slug}.json?random=#{util.randomBytes(4)}", "", (page) -> | ||
buildPage page | ||
else | ||
probe slug, (page) -> | ||
if page? | ||
buildPage page | ||
else | ||
create slug, (page) -> | ||
buildPage page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
fetch = require '../lib/fetch.coffee' | ||
|
||
#Fakes for things still stuck in legacy.coffee | ||
wiki.useLocalStorage = -> false | ||
wiki.putAction = -> | ||
|
||
describe 'fetch', -> | ||
before -> | ||
$('<div id="fetch" data-site="foo" />').appendTo('body') | ||
|
||
describe 'ajax fails', -> | ||
it 'should have an empty context', -> | ||
expect(fetch.context).to.eql([]) | ||
|
||
it 'should create a page when it can not find it', (done) -> | ||
fetch $('#fetch'), (page) -> | ||
expect(page).to.eql({title: 'fetch'}) | ||
done() | ||
|
||
describe 'ajax, success', -> | ||
before -> | ||
sinon.stub(jQuery, "ajax").yieldsTo('success', 'test') | ||
|
||
it 'should fetch a page from specific site', (done) -> | ||
fetch $('#fetch'), (page) -> | ||
expect(jQuery.ajax.calledOnce).to.be.true | ||
expect(jQuery.ajax.args[0][0]).to.have.property('type', 'GET') | ||
expect(jQuery.ajax.args[0][0].url).to.match(///^/remote/foo/fetch\.json\?random=[a-z0-9]{8}$///) | ||
done() | ||
|
||
after -> | ||
jQuery.ajax.restore() | ||
|
||
describe 'ajax, search', -> | ||
before -> | ||
$('<div id="fetch2" />').appendTo('body') | ||
sinon.stub(jQuery, "ajax").yieldsTo('error') | ||
fetch.context = ['origin', 'example.com', 'asdf.test', 'foo.bar'] | ||
|
||
it 'should search through the context for a page', (done) -> | ||
fetch $('#fetch2'), (page) -> | ||
expect(jQuery.ajax.args[0][0].url).to.match(///^/fetch2\.json\?random=[a-z0-9]{8}$///) | ||
expect(jQuery.ajax.args[1][0].url).to.match(///^/remote/example.com/fetch2\.json\?random=[a-z0-9]{8}$///) | ||
expect(jQuery.ajax.args[2][0].url).to.match(///^/remote/asdf.test/fetch2\.json\?random=[a-z0-9]{8}$///) | ||
expect(jQuery.ajax.args[3][0].url).to.match(///^/remote/foo.bar/fetch2\.json\?random=[a-z0-9]{8}$///) | ||
done() | ||
|
||
after -> | ||
jQuery.ajax.restore() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.