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

Commit

Permalink
Rearranged fetch some more, added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrn committed Apr 14, 2012
1 parent 2db73e1 commit 7b79c7a
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 144 deletions.
140 changes: 57 additions & 83 deletions client/client.js
Expand Up @@ -400,7 +400,6 @@ require.define("/lib/legacy.coffee", function (require, module, exports, __dirna
return null;
};
wiki.resolutionContext = [];
wiki.fetchContext = [];
resolveFrom = wiki.resolveFrom = function(addition, callback) {
wiki.resolutionContext.push(addition);
try {
Expand Down Expand Up @@ -656,11 +655,7 @@ require.define("/lib/legacy.coffee", function (require, module, exports, __dirna
initDragging(pageElement);
return initAddButton(pageElement);
};
return fetch({
buildPage: buildPage,
putAction: putAction,
pageElement: pageElement
});
return fetch(pageElement, buildPage);
};
LEFTARROW = 37;
RIGHTARROW = 39;
Expand Down Expand Up @@ -708,8 +703,8 @@ require.define("/lib/legacy.coffee", function (require, module, exports, __dirna
var name;
e.preventDefault();
name = $(e.target).data('pageName');
wiki.fetchContext = $(e.target).attr('title').split(' => ');
wiki.log('click', name, 'context', wiki.fetchContext);
fetch.context = $(e.target).attr('title').split(' => ');
wiki.log('click', name, 'context', fetch.context);
if (!e.shiftKey) $(e.target).parents('.page').nextAll().remove();
createPage(name).appendTo('.main').each(refresh);
return active.set($('.page').last());
Expand Down Expand Up @@ -789,91 +784,70 @@ require.define("/lib/util.coffee", function (require, module, exports, __dirname

require.define("/lib/fetch.coffee", function (require, module, exports, __dirname, __filename) {
(function() {
var util;
var probe, util;

util = require('./util');

module.exports = function(params) {
var buildPage, create, json, pageElement, probe, putAction, site, slug;
buildPage = params.buildPage, putAction = params.putAction, pageElement = params.pageElement;
slug = $(pageElement).attr('id');
site = $(pageElement).data('site');
probe = function(slug, callback, localContext) {
var i, resource;
if (!(wiki.fetchContext.length > 0)) wiki.fetchContext = ['origin'];
if (localContext == null) {
localContext = (function() {
var _i, _len, _ref, _results;
_ref = wiki.fetchContext;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
_results.push(i);
}
return _results;
})();
}
site = localContext.shift();
resource = site === 'origin' ? (site = null, slug) : "remote/" + site + "/" + slug;
return $.ajax({
type: 'GET',
dataType: 'json',
url: "/" + resource + ".json?random=" + (util.randomBytes(4)),
success: function(page) {
wiki.log('fetch success', page, site || 'origin');
$(pageElement).data('site', site);
return callback(page);
},
error: function(xhr, type, msg) {
if (localContext.length > 0) {
return probe(slug, callback, localContext);
} else {
site = null;
return callback(null);
}
probe = function(pageElement, callback, localContext) {
var i, json, resource, site, slug;
slug = pageElement.attr('id');
site = pageElement.data('site');
if (pageElement.attr('data-server-generated') === 'true') callback(null);
if (wiki.useLocalStorage() && (json = localStorage[slug])) {
pageElement.addClass("local");
callback(JSON.parse(json));
}
if (!(probe.context.length > 0)) probe.context = ['origin'];
if (localContext == null) {
localContext = (function() {
var _i, _len, _ref, _results;
_ref = probe.context;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
_results.push(i);
}
});
};
create = function(slug, callback) {
var page, title;
title = $("a[href=\"/" + slug + ".html\"]").html();
title || (title = slug);
page = {
title: title
};
putAction($(pageElement), {
type: 'create',
id: util.randomBytes(8),
item: page
});
return callback(page);
};
if ($(pageElement).attr('data-server-generated') === 'true') {
return buildPage(null);
} else {
if (wiki.useLocalStorage() && (json = localStorage[pageElement.attr("id")])) {
pageElement.addClass("local");
return buildPage(JSON.parse(json));
} else {
if (site != null) {
return $.get("/remote/" + site + "/" + slug + ".json?random=" + (util.randomBytes(4)), "", function(page) {
return buildPage(page);
});
return _results;
})();
}
if (typeof site === 'string') localContext = [site];
site = localContext.shift();
resource = site === 'origin' ? (site = null, slug) : "remote/" + site + "/" + slug;
return $.ajax({
type: 'GET',
dataType: 'json',
url: "/" + resource + ".json?random=" + (util.randomBytes(4)),
success: function(page) {
wiki.log('fetch success', page, site || 'origin');
$(pageElement).data('site', site);
return callback(page);
},
error: function(xhr, type, msg) {
var page, title;
if (localContext.length > 0) {
return probe(pageElement, callback, localContext);
} else {
return probe(slug, function(page) {
if (page != null) {
return buildPage(page);
} else {
return create(slug, function(page) {
return buildPage(page);
});
}
site = null;
title = $("a[href=\"/" + slug + ".html\"]").html();
title || (title = slug);
page = {
title: title
};
wiki.putAction($(pageElement), {
type: 'create',
id: util.randomBytes(8),
item: page
});
return callback(page);
}
}
}
});
};

probe.context = [];

module.exports = probe;

}).call(this);

});
Expand Down
88 changes: 38 additions & 50 deletions client/lib/fetch.coffee
@@ -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
12 changes: 4 additions & 8 deletions client/lib/legacy.coffee
Expand Up @@ -31,7 +31,6 @@ $ ->
null

wiki.resolutionContext = []
wiki.fetchContext = []
resolveFrom = wiki.resolveFrom = (addition, callback) ->
wiki.resolutionContext.push addition
try
Expand Down Expand Up @@ -252,11 +251,8 @@ $ ->
initDragging pageElement
initAddButton pageElement

fetch({
buildPage
putAction
pageElement
})
fetch pageElement, buildPage


LEFTARROW = 37
RIGHTARROW = 39
Expand Down Expand Up @@ -303,8 +299,8 @@ $ ->
.delegate '.internal', 'click', (e) ->
e.preventDefault()
name = $(e.target).data 'pageName'
wiki.fetchContext = $(e.target).attr('title').split(' => ')
wiki.log 'click', name, 'context', wiki.fetchContext
fetch.context = $(e.target).attr('title').split(' => ')
wiki.log 'click', name, 'context', fetch.context
$(e.target).parents('.page').nextAll().remove() unless e.shiftKey
createPage(name).appendTo('.main').each refresh
active.set($('.page').last())
Expand Down
51 changes: 51 additions & 0 deletions client/test/fetch.coffee
@@ -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()


2 changes: 1 addition & 1 deletion client/test/plugin.coffee
Expand Up @@ -23,5 +23,5 @@ describe 'plugin', ->
.be('<p>blah <a class="internal" href="/link.html" data-page-name="link" title="origin">Link</a> asdf</p>')

after ->
jQuery.ajax.restore()
jQuery.getScript.restore()

0 comments on commit 7b79c7a

Please sign in to comment.