Commit 0d76f7df authored by Tristan Cavelier's avatar Tristan Cavelier

erp5storage.js upgraded

parent ca3163aa
/* /*
* Copyright 2013, Nexedi SA * Copyright 2013, Nexedi SA
* Released under the LGPL license. * Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html * http://www.gnu.org/licenses/lgpl.html
*/ */
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global jIO: true, $: true, btoa: true */ /*global jIO: true, $: true */
// JIO Erp5 Storage Description : // JIO Erp5 Storage Description :
// { // {
// type: "erp5", // type: "erp5"
// url: {string} // url: {string}
// } // mode: {string} (optional)
// - "normal" (default)
// { // - "erp5_only"
// type: "erp5", //
// url: {string}, // with
// auth_type: {string}, (optional) //
// - "auto" (default) (not implemented) // auth_type: {string} (optional)
// - "basic" // - "none" (default)
// - "basic" (not implemented)
// - "digest" (not implemented) // - "digest" (not implemented)
// realm: {string}, (optional) // username: {string}
// - undefined (default) (not implemented) // password: {string} (optional)
// - "<string>" realm name (not implemented) // - no password (default)
// username: {string}, //
// password: {string} (optional) // or
// } //
// encoded_login: {string} (not implemented)
// { //
// type: "erp5", // or
// url: {string}, //
// encoded_login: {string}
// }
// {
// type: "erp5",
// url: {string},
// secured_login: {string} (not implemented) // secured_login: {string} (not implemented)
// } // }
jIO.addStorageType("erp5", function (spec, my) { jIO.addStorageType("erp5", function (spec, my) {
"use strict";
var priv = {}, that = my.basicStorage(spec, my), erp5 = {}; var priv = {}, that = my.basicStorage(spec, my), erp5 = {};
// ATTRIBUTES // // ATTRIBUTES //
priv.url = null; priv.url = null;
priv.mode = "normal";
priv.auth_type = "none";
priv.encoded_login = null; priv.encoded_login = null;
// CONSTRUCTOR // // CONSTRUCTOR //
...@@ -54,19 +51,21 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -54,19 +51,21 @@ jIO.addStorageType("erp5", function (spec, my) {
priv.__init__ = function (description) { priv.__init__ = function (description) {
priv.url = description.url || ""; priv.url = description.url || "";
priv.url = priv.removeSlashIfLast(priv.url); priv.url = priv.removeSlashIfLast(priv.url);
// if (description.secured_login) { if (description.mode === "erp5_only") {
// not implemented priv.mode = "erp5_only";
// } else }
if (description.encoded_login) { if (description.encoded_login) {
priv.encoded_login = description.encoded_login; priv.encoded_login = description.encoded_login;
} else if (description.auth_type) {
if (description.auth_type === "basic") {
priv.encoded_login = "Basic " +
btoa((description.username || "") + ":" +
(description.password || ""));
}
} else { } else {
priv.encoded_login = ""; if (description.username) {
priv.encoded_login =
"__ac_name=" + priv.convertToUrlParameter(description.username) +
"&" + (typeof description.password === "string" ?
"__ac_password=" +
priv.convertToUrlParameter(description.password) + "&" : "");
} else {
priv.encoded_login = "";
}
} }
}; };
...@@ -78,6 +77,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -78,6 +77,7 @@ jIO.addStorageType("erp5", function (spec, my) {
// encoded_login = decrypt(secured_login) // encoded_login = decrypt(secured_login)
return { return {
"url": priv.url, "url": priv.url,
"mode": priv.mode,
"encoded_login": priv.encoded_login "encoded_login": priv.encoded_login
}; };
}; };
...@@ -93,6 +93,37 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -93,6 +93,37 @@ jIO.addStorageType("erp5", function (spec, my) {
}; };
// TOOLS // // TOOLS //
/**
* Replace substrings to another strings
* @method recursiveReplace
* @param {string} string The string to do replacement
* @param {array} list_of_replacement An array of couple
* ["substring to select", "selected substring replaced by this string"].
* @return {string} The replaced string
*/
priv.recursiveReplace = function (string, list_of_replacement) {
var i, split_string = string.split(list_of_replacement[0][0]);
if (list_of_replacement[1]) {
for (i = 0; i < split_string.length; i += 1) {
split_string[i] = priv.recursiveReplace(
split_string[i],
list_of_replacement.slice(1)
);
}
}
return split_string.join(list_of_replacement[0][1]);
};
/**
* Changes & to %26
* @method convertToUrlParameter
* @param {string} parameter The parameter to convert
* @return {string} The converted parameter
*/
priv.convertToUrlParameter = function (parameter) {
return priv.recursiveReplace(parameter, [[" ", "%20"], ["&", "%26"]]);
};
/** /**
* Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c" * Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c"
* @method removeSlashIfLast * @method removeSlashIfLast
...@@ -110,23 +141,24 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -110,23 +141,24 @@ jIO.addStorageType("erp5", function (spec, my) {
* Modify an ajax object to add default values * Modify an ajax object to add default values
* @method makeAjaxObject * @method makeAjaxObject
* @param {object} json The JSON object * @param {object} json The JSON object
* @param {object} option The option object
* @param {string} method The erp5 request method * @param {string} method The erp5 request method
* @param {object} ajax_object The ajax object to override * @param {object} ajax_object The ajax object to override
* @return {object} A new ajax object with default values * @return {object} A new ajax object with default values
*/ */
priv.makeAjaxObject = function (json, method, ajax_object) { priv.makeAjaxObject = function (json, option, method, ajax_object) {
ajax_object.type = "POST"; ajax_object.type = "POST";
ajax_object.dataType = "json"; ajax_object.dataType = "json";
ajax_object.data = [{"name": "doc", "value": JSON.stringify(json)}]; ajax_object.data = [
{"name": "doc", "value": JSON.stringify(json)},
{"name": "option", "value": JSON.stringify(option)},
{"name": "mode", "value": priv.mode}
];
ajax_object.url = priv.url + "/JIO_" + method + ajax_object.url = priv.url + "/JIO_" + method +
"?_=" + Date.now(); "?" + priv.encoded_login + "_=" + Date.now();
ajax_object.async = ajax_object.async === false ? false : true; ajax_object.async = ajax_object.async === false ? false : true;
ajax_object.crossdomain = ajax_object.crossdomain === false ? false : true; ajax_object.crossdomain = ajax_object.crossdomain === false ? false : true;
ajax_object.headers = ajax_object.headers || {}; ajax_object.headers = ajax_object.headers || {};
if (ajax_object.headers.Authorization || priv.encoded_login) {
ajax_object.headers.Authorization = ajax_object.headers.Authorization ||
priv.encoded_login;
}
return ajax_object; return ajax_object;
}; };
...@@ -134,11 +166,12 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -134,11 +166,12 @@ jIO.addStorageType("erp5", function (spec, my) {
* Runs all ajax requests for erp5Storage * Runs all ajax requests for erp5Storage
* @method ajax * @method ajax
* @param {object} json The JSON object * @param {object} json The JSON object
* @param {object} option The option object
* @param {string} method The erp5 request method * @param {string} method The erp5 request method
* @param {object} ajax_object The request parameters (optional) * @param {object} ajax_object The request parameters (optional)
*/ */
priv.ajax = function (json, method, ajax_object) { priv.ajax = function (json, option, method, ajax_object) {
return $.ajax(priv.makeAjaxObject(json, method, ajax_object || {})); return $.ajax(priv.makeAjaxObject(json, option, method, ajax_object || {}));
//.always(then || function () {}); //.always(then || function () {});
}; };
...@@ -221,11 +254,12 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -221,11 +254,12 @@ jIO.addStorageType("erp5", function (spec, my) {
* Sends a request to ERP5 * Sends a request to ERP5
* @method erp5.genericRequest * @method erp5.genericRequest
* @param {object} doc The document object * @param {object} doc The document object
* @param {object} option The option object
* @param {string} method The ERP5 request method * @param {string} method The ERP5 request method
*/ */
erp5.genericRequest = function (json, method) { erp5.genericRequest = function (json, option, method) {
var jql = priv.makeJQLikeCallback(), error = null; var jql = priv.makeJQLikeCallback(), error = null;
priv.ajax(json, method).always(function (one, state, three) { priv.ajax(json, option, method).always(function (one, state, three) {
if (state === "parsererror") { if (state === "parsererror") {
return jql.respond(priv.createError( return jql.respond(priv.createError(
24, 24,
...@@ -256,12 +290,13 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -256,12 +290,13 @@ jIO.addStorageType("erp5", function (spec, my) {
/** /**
* The ERP5 storage generic command * The ERP5 storage generic command
* @method genericCommand * @method genericCommand
* @param {object} json The json to send * @param {object} command The JIO command object
* @param {string} method The ERP5 request method * @param {string} method The ERP5 request method
*/ */
priv.genericCommand = function (json, method) { priv.genericCommand = function (command, method) {
erp5.genericRequest( erp5.genericRequest(
json, command.cloneDoc(),
command.cloneOption(),
method method
).always(function (err, response) { ).always(function (err, response) {
if (err) { if (err) {
...@@ -277,7 +312,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -277,7 +312,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.post = function (command) { that.post = function (command) {
priv.genericCommand(command.cloneDoc(), "post"); priv.genericCommand(command, "post");
}; };
/** /**
...@@ -286,7 +321,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -286,7 +321,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.put = function (command) { that.put = function (command) {
priv.genericCommand(command.cloneDoc(), "put"); priv.genericCommand(command, "put");
}; };
/** /**
...@@ -295,7 +330,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -295,7 +330,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.putAttachment = function (command) { that.putAttachment = function (command) {
priv.genericCommand(command.cloneDoc(), "putAttachment"); priv.genericCommand(command, "putAttachment");
}; };
/** /**
...@@ -304,7 +339,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -304,7 +339,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.get = function (command) { that.get = function (command) {
priv.genericCommand(command.cloneDoc(), "get"); priv.genericCommand(command, "get");
}; };
/** /**
...@@ -313,7 +348,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -313,7 +348,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.getAttachment = function (command) { that.getAttachment = function (command) {
priv.genericCommand(command.cloneDoc(), "getAttachment"); priv.genericCommand(command, "getAttachment");
}; };
/** /**
...@@ -322,7 +357,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -322,7 +357,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.remove = function (command) { that.remove = function (command) {
priv.genericCommand(command.cloneDoc(), "remove"); priv.genericCommand(command, "remove");
}; };
/** /**
...@@ -331,7 +366,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -331,7 +366,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.removeAttachment = function (command) { that.removeAttachment = function (command) {
priv.genericCommand(command.cloneDoc(), "removeAttachment"); priv.genericCommand(command, "removeAttachment");
}; };
/** /**
...@@ -342,10 +377,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -342,10 +377,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.allDocs = function (command) { that.allDocs = function (command) {
priv.genericCommand({ priv.genericCommand(command, "allDocs");
"query": command.getOption("query"),
"include_docs": command.getOption("include_docs")
}, "allDocs");
}; };
/** /**
...@@ -354,7 +386,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -354,7 +386,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.check = function (command) { that.check = function (command) {
priv.genericCommand(command.cloneDoc(), "check"); priv.genericCommand(command, "check");
}; };
/** /**
...@@ -363,7 +395,7 @@ jIO.addStorageType("erp5", function (spec, my) { ...@@ -363,7 +395,7 @@ jIO.addStorageType("erp5", function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.repair = function (command) { that.repair = function (command) {
priv.genericCommand(command.cloneDoc(), "repair"); priv.genericCommand(command, "repair");
}; };
priv.__init__(spec); priv.__init__(spec);
......
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