Commit d24fe545 authored by Caleb James DeLisle's avatar Caleb James DeLisle

Fixed phantomjs test failures and conformed to c89 all-vars-at-beginning-of-function codestyle.

parent 58115408
......@@ -2,12 +2,12 @@
maxlen: 80,
sloppy: true,
nomen: true,
vars: true,
plusplus: true
*/
/*global
define: true,
jIO: true,
$: true,
jQuery: true,
XMLHttpRequest: true,
Blob: true,
FormData: true,
......@@ -17,10 +17,13 @@
* JIO XWiki Storage. Type = 'xwiki'.
* XWiki Document/Attachment storage.
*/
jIO.addStorageType('xwiki', function (spec, my) {
(function () {
var $, store;
store = function (spec, my) {
spec = spec || {};
var that, priv, xwikistorage;
that = my.basicStorage(spec, my);
priv = {};
......@@ -30,7 +33,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param id the document id.
* @return a map of { 'space':<Space>, 'page':<Page> }
*/
var getParts = function (id) {
priv.getParts = function (id) {
if (id.indexOf('/') === -1) {
return {
space: 'Main',
......@@ -49,17 +52,18 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param andThen function which is called with (formToken, err)
* as parameters.
*/
var doWithFormToken = function (andThen) {
priv.doWithFormToken = function (andThen) {
$.ajax({
url: priv.formTokenPath,
type: "GET",
async: true,
dataType: 'text',
success: function (html) {
var m, token;
// this is unreliable
//var token = $('meta[name=form_token]', html).attr("content");
var m = html.match(/<meta name="form_token" content="(\w*)"\/>/);
var token = (m && m[1]) || null;
m = html.match(/<meta name="form_token" content="(\w*)"\/>/);
token = (m && m[1]) || null;
if (!token) {
andThen(null, {
"status": 404,
......@@ -92,8 +96,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param docId the id of the document.
* @return the REST URL for accessing this document.
*/
var getDocRestURL = function (docId) {
var parts = getParts(docId);
priv.getDocRestURL = function (docId) {
var parts = priv.getParts(docId);
return priv.xwikiurl + '/rest/wikis/'
+ priv.wiki + '/spaces/' + parts.space + '/pages/' + parts.page;
};
......@@ -103,7 +107,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
* Equivilant to the `new Blob()` constructor.
* Will fall back on deprecated BlobBuilder if necessary.
*/
var makeBlob = function (contentArray, options) {
priv.makeBlob = function (contentArray, options) {
var i, bb, BB;
try {
// use the constructor if possible.
......@@ -120,6 +124,11 @@ jIO.addStorageType('xwiki', function (spec, my) {
}
};
priv.isBlob = function (potentialBlob) {
return typeof (potentialBlob) !== 'undefined' &&
potentialBlob.toString() === "[object Blob]";
};
/*
* Wrapper for the xwikistorage based on localstorage JiO store.
*/
......@@ -134,9 +143,10 @@ jIO.addStorageType('xwiki', function (spec, my) {
getItem: function (docId, andThen) {
var success = function (jqxhr) {
var out = {};
var out, xd;
out = {};
try {
var xd = $(jqxhr.responseText);
xd = $(jqxhr.responseText);
xd.find('modified').each(function () {
out._last_modified = Date.parse($(this).text());
});
......@@ -144,8 +154,12 @@ jIO.addStorageType('xwiki', function (spec, my) {
out._creation_date = Date.parse($(this).text());
});
xd.find('title').each(function () { out.title = $(this).text(); });
xd.find('parent').each(function () { out.parent = $(this).text(); });
xd.find('syntax').each(function () { out.syntax = $(this).text(); });
xd.find('parent').each(function () {
out.parent = $(this).text();
});
xd.find('syntax').each(function () {
out.syntax = $(this).text();
});
xd.find('content').each(function () {
out.content = $(this).text();
});
......@@ -163,7 +177,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
};
$.ajax({
url: getDocRestURL(docId),
url: priv.getDocRestURL(docId),
type: "GET",
async: true,
dataType: 'xml',
......@@ -198,13 +212,18 @@ jIO.addStorageType('xwiki', function (spec, my) {
* attachment blob and err being the error if any.
*/
getAttachment: function (docId, fileName, andThen) {
var xhr, parts, url;
// need to do this manually, jquery doesn't support returning blobs.
var xhr = new XMLHttpRequest();
var parts = getParts(docId);
var url = priv.xwikiurl + '/bin/download/' + parts.space +
"/" + parts.page + "/" + fileName;
xhr = new XMLHttpRequest();
parts = priv.getParts(docId);
url = priv.xwikiurl + '/bin/download/' + parts.space +
"/" + parts.page + "/" + fileName + '?cb=' + Math.random();
xhr.open('GET', url, true);
if (priv.useBlobs) {
xhr.responseType = 'blob';
} else {
xhr.responseType = 'text';
}
xhr.onload = function (e) {
if (xhr.status === 200) {
......@@ -212,11 +231,7 @@ jIO.addStorageType('xwiki', function (spec, my) {
if (contentType.indexOf(';') > -1) {
contentType = contentType.substring(0, contentType.indexOf(';'));
}
if (priv.useBlobs) {
andThen(makeBlob([xhr.response], {type: contentType}));
} else {
andThen(xhr.response);
}
} else {
andThen(null, {
"status": xhr.status,
......@@ -241,14 +256,15 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param andThen a callback taking (err), err being the error if any.
*/
setItem: function (id, doc, andThen) {
doWithFormToken(function (formToken, err) {
priv.doWithFormToken(function (formToken, err) {
if (err) {
that.error(err);
return;
}
var parts = getParts(id);
var parts = priv.getParts(id);
$.ajax({
url: priv.xwikiurl + "/bin/preview/" + parts.space + '/' + parts.page,
url: priv.xwikiurl + "/bin/preview/" +
parts.space + '/' + parts.page,
type: "POST",
async: true,
dataType: 'text',
......@@ -257,8 +273,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
title: doc.title || '',
xredirect: '',
language: 'en',
// RequiresHTMLConversion: 'content',
// content_syntax: doc.syntax || 'xwiki/2.1',
// RequiresHTMLConversion: 'content',
// content_syntax: doc.syntax || 'xwiki/2.1',
content: doc.content || '',
xeditaction: 'edit',
comment: 'Saved by JiO',
......@@ -292,21 +308,23 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param fileName the attachment file name.
* @param mimeType the MIME type of the attachment content.
* @param content the attachment content.
* @param andThen a callback taking one parameter which is the error if any.
* @param andThen a callback taking one parameter, the error if any.
*/
setAttachment: function (docId, fileName, mimeType, content, andThen) {
doWithFormToken(function (formToken, err) {
priv.doWithFormToken(function (formToken, err) {
var parts, blob, fd, xhr;
if (err) {
that.error(err);
return;
}
var parts = getParts(docId);
var blob = (content.constructor === "function Blob() { [native code] }")
? content : makeBlob([content], {type: mimeType});
var fd = new FormData();
parts = priv.getParts(docId);
blob = priv.isBlob(content)
? content
: priv.makeBlob([content], {type: mimeType});
fd = new FormData();
fd.append("filepath", blob, fileName);
fd.append("form_token", formToken);
var xhr = new XMLHttpRequest();
xhr = new XMLHttpRequest();
xhr.open('POST', priv.xwikiurl + "/bin/upload/" +
parts.space + '/' + parts.page, true);
xhr.onload = function (e) {
......@@ -328,14 +346,15 @@ jIO.addStorageType('xwiki', function (spec, my) {
},
removeItem: function (id, andThen) {
doWithFormToken(function (formToken, err) {
priv.doWithFormToken(function (formToken, err) {
if (err) {
that.error(err);
return;
}
var parts = getParts(id);
var parts = priv.getParts(id);
$.ajax({
url: priv.xwikiurl + "/bin/delete/" + parts.space + '/' + parts.page,
url: priv.xwikiurl + "/bin/delete/" +
parts.space + '/' + parts.page,
type: "POST",
async: true,
dataType: 'text',
......@@ -360,8 +379,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
},
removeAttachment: function (docId, fileName, andThen) {
var parts = getParts(docId);
doWithFormToken(function (formToken, err) {
var parts = priv.getParts(docId);
priv.doWithFormToken(function (formToken, err) {
if (err) {
that.error(err);
return;
......@@ -450,7 +469,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
// URL location of the wiki, unused since
// XWiki doesn't currently allow cross-domain requests.
priv.xwikiurl = spec.xwikiurl ||
window.location.href.replace(/\/xwiki\/bin\//, '/xwiki\n').split('\n')[0];
window.location.href.replace(/\/xwiki\/bin\//, '/xwiki\n')
.split('\n')[0];
// should be: s@/xwiki/bin/.*$@/xwiki@
// but jslint gets in the way.
......@@ -642,7 +662,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
* @param {object} command The JIO command
*/
that.remove = that.removeAttachment = function (command) {
var notFoundError = function (word) {
var notFoundError, objId, complete;
notFoundError = function (word) {
that.error({
"status": 404,
"statusText": "Not Found",
......@@ -652,8 +673,8 @@ jIO.addStorageType('xwiki', function (spec, my) {
});
};
var objId = command.getDocId();
var complete = function (err) {
objId = command.getDocId();
complete = function (err) {
if (err) {
that.error(err);
} else {
......@@ -691,4 +712,21 @@ jIO.addStorageType('xwiki', function (spec, my) {
};
return that;
});
};
if (typeof (define) === 'function' && define.amd) {
define(['jquery', 'jiobase', 'module'], function (jquery, j, mod) {
$ = jquery;
jIO.addStorageType('xwiki', store);
var conf = mod.config();
conf.type = 'xwiki';
return jIO.newJio(conf);
});
} else {
jIO.addStorageType('xwiki', store);
$ = jQuery;
}
}());
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment