Commit d4da072a authored by Tristan Cavelier's avatar Tristan Cavelier

Grunt conflictmanagerstorage.js (jio.storage.js concat, lint, min)

parent a2d7a860
/*! JIO Storage - v0.1.0 - 2012-06-22
/*! JIO Storage - v0.1.0 - 2012-06-26
* Copyright (c) 2012 Nexedi; Licensed */
(function(LocalOrCookieStorage, $, Base64, sjcl, MD5, Jio) {
(function(LocalOrCookieStorage, $, Base64, sjcl, hex_sha256, Jio) {
var newLocalStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'base' ), priv = {};
......@@ -1103,11 +1103,12 @@ var newConflictManagerStorage = function ( spec, my ) {
spec = spec || {};
my = my || {};
var local_namespace = 'jio/conflictmanager/';
priv.username = spec.username || '';
var storage_exists = (spec.storage?true:false);
priv.secondstorage_spec = spec.storage || {type:'base'};
priv.secondstorage_string = JSON.stringify (priv.secondstorage_spec);
var local_namespace = 'jio/conflictmanager/'+priv.secondstorage_string+'/';
var super_serialized = that.serialized;
that.serialized = function () {
......@@ -1118,8 +1119,7 @@ var newConflictManagerStorage = function ( spec, my ) {
that.validateState = function () {
if (!priv.username || storage_exists) {
return 'Need at least two parameter: "owner" and "storage" '+
'.';
return 'Need at least two parameter: "owner" and "storage".';
}
return '';
};
......@@ -1134,6 +1134,38 @@ var newConflictManagerStorage = function ( spec, my ) {
return newarray;
};
var super_fail = that.fail;
that.fail = function (command,error) {
command.setMaxRetry(1);
super_fail(error);
};
priv.loadMetadataFromDistant = function (command,path,onDone,onFail) {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = onFail;
cloned_option.onDone = onDone;
var newcommand = that.newCommand(
'loadDocument',{path:path,
option:cloned_option});
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
};
priv.saveMetadataToDistant = function (command,path,content,onDone,onFail) {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = onFail;
cloned_option.onDone = onDone;
var newcommand = that.newCommand(
'saveDocument',{path:path,
content:JSON.stringify (content),
option:cloned_option});
newcommand.setMaxRetry (0); // inf
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
};
/**
* Save a document and can manage conflicts.
* @method saveDocument
......@@ -1144,9 +1176,8 @@ var newConflictManagerStorage = function ( spec, my ) {
local_metadata_file_name = local_namespace + metadata_file_name,
local_file_metadata = {}, // local file.metadata
command_file_metadata = {}, // distant file.metadata
run_index = 0,
run_index = 0, previous_revision = 0,
end = false, is_a_new_file = false,
previous_revision = 0,
local_file_hash = hex_sha256 (command.getContent()),
run = function (index) {
switch (index) {
......@@ -1181,37 +1212,35 @@ var newConflictManagerStorage = function ( spec, my ) {
run_index ++; run (run_index);
break;
case 2: // load metadata from distant
(function () {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = function (error) {
priv.loadMetadataFromDistant (
command,metadata_file_name,
function (result) {
command_file_metadata = JSON.parse (result.content);
run_index ++; run (run_index);
},function (error) {
if (error.status === 404) {
command_file_metadata = local_file_metadata;
is_a_new_file = true;
run_index ++; run (run_index);
} else {
run_index = (-10);
end = true;
that.fail(error);
that.fail(command,error);
}
};
cloned_option.onDone = function (result) {
command_file_metadata = JSON.parse (result.content);
run_index ++; run (run_index);
};
var newcommand = that.newCommand(
'loadDocument',{path:metadata_file_name,
option:cloned_option});
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
}());
});
break;
case 5: // check conflicts
var updateMetadataCommon = function () {
var original_creation_date;
if (is_a_new_file || !command_file_metadata.owner[
command_file_metadata.winner.owner]) {
original_creation_date = now.getTime();
} else {
original_creation_date = command_file_metadata.owner[
command_file_metadata.winner.owner].
creation_date || now.getTime();
}
if (command_file_metadata.owner[priv.username]) {
previous_revision = command_file_metadata.owner[
......@@ -1284,9 +1313,10 @@ var newConflictManagerStorage = function ( spec, my ) {
// if known conflict
if (known_conflict_list[i].hash ===
conflict_hash) {
command_file_metadata.conflict_list =
priv.removeValuesFromArrayWhere(
command_file_metadata.conflict_list,
compare_fun); // FIXME : must remove something!
compare_fun);
updateCommandMetadataNotOnConflict();
run_index = 98;
run (6);
......@@ -1301,31 +1331,20 @@ var newConflictManagerStorage = function ( spec, my ) {
end = true;
run (6); // save metadata
run (7); // save document revision
that.fail(); // TODO
that.fail(command); // TODO
command.getOption('onConflict')(conflict_object);
}
break;
case 6: // save metadata
(function () {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = function (error) {
priv.saveMetadataToDistant (
command,metadata_file_name,command_file_metadata,
function () {
run_index ++; run (run_index);
},function (error) {
run_index = (-10);
end = true;
that.fail(error);
};
cloned_option.onDone = function () {
run_index ++; run (run_index);
};
var newcommand = that.newCommand(
'saveDocument',{path:metadata_file_name,
content:JSON.stringify (
command_file_metadata),
option:cloned_option});
newcommand.setMaxRetry (0); // inf
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
}());
that.fail(command,error);
});
break;
case 7: // save document revision
(function () {
......@@ -1334,7 +1353,7 @@ var newConflictManagerStorage = function ( spec, my ) {
cloned_option.onFail = function (error) {
run_index = (-10);
end = true;
that.fail(error);
that.fail(command,error);
};
cloned_option.onDone = function () {
run (8);
......@@ -1353,13 +1372,17 @@ var newConflictManagerStorage = function ( spec, my ) {
break;
case 8:
(function () {
if ( previous_revision !== 0 ) {
if ( previous_revision !== 0 && (
!command_file_metadata.owner[priv.username] ||
previous_revision !==
command_file_metadata.owner[
priv.username].revision ) ) {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = function (error) {
run_index = (-10);
end = true;
that.fail(error);
that.fail(command,error);
};
cloned_option.onDone = function () {
run_index ++; run (run_index);
......@@ -1389,7 +1412,6 @@ var newConflictManagerStorage = function ( spec, my ) {
}
};
run (0);
command.setMaxRetry (1);
};
/**
......@@ -1406,15 +1428,9 @@ var newConflictManagerStorage = function ( spec, my ) {
run = function (index) {
switch (index) {
case 0: // load metadata file from distant
(function () {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = function (error) {
run_index = (-10);
end = true;
that.fail(error);
};
cloned_option.onDone = function (result) {
priv.loadMetadataFromDistant (
command,metadata_file_name,
function (result) {
command_file_metadata = JSON.parse (result.content);
owner = command.getOption('owner');
run_index = 98;
......@@ -1426,15 +1442,11 @@ var newConflictManagerStorage = function ( spec, my ) {
run (2);
}
run (1);
};
var newcommand = that.newCommand(
'loadDocument',
{path:metadata_file_name,
option:cloned_option});
newcommand.setMaxRetry (0); // inf
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
}());
},function (error) {
run_index = (-10);
end = true;
that.fail(command,error);
});
break;
case 1: // update local metadata
LocalOrCookieStorage.setItem (local_metadata_file_name,
......@@ -1448,10 +1460,11 @@ var newConflictManagerStorage = function ( spec, my ) {
cloned_option.onFail = function (error) {
run_index = (-10);
end = true;
that.fail(error);
that.fail(command,error);
};
cloned_option.onDone = function (result) {
loaded_file = result;
loaded_file.name = command.getPath();
run_index ++; run (run_index);
};
var newcommand = that.newCommand(
......@@ -1460,7 +1473,6 @@ var newConflictManagerStorage = function ( spec, my ) {
command_file_metadata.winner.revision +
'.' + command_file_metadata.winner.owner,
option:cloned_option});
newcommand.setMaxRetry (0); // inf
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
}());
......@@ -1472,10 +1484,11 @@ var newConflictManagerStorage = function ( spec, my ) {
cloned_option.onFail = function (error) {
run_index = (-10);
end = true;
that.fail(error);
that.fail(command,error);
};
cloned_option.onDone = function (result) {
loaded_file = result;
loaded_file.name = command.getPath();
run_index ++; run (run_index);
};
if (!command_file_metadata.owner[owner]) {
......@@ -1490,7 +1503,6 @@ var newConflictManagerStorage = function ( spec, my ) {
command_file_metadata.owner[owner].revision +
'.' + owner,
option:cloned_option});
newcommand.setMaxRetry (0); // inf
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
}());
......@@ -1514,7 +1526,87 @@ var newConflictManagerStorage = function ( spec, my ) {
* @method getDocumentList
*/
that.getDocumentList = function (command) {
that.fail({message:'NIY'});
var command_file_metadata_list = [], // distant files metadata
result_list = [],
end = false, nb_loaded_file = 0,
_1 = function () {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = function (error) {
that.fail(command,error);
};
cloned_option.onDone = function (result) {
var i;
for (i = 0; i < result.length; i+= 1) {
var splitname = result[i].name.split('.') || [];
var content_object;
var doc = {};
if (splitname[splitname.length-1] === 'metadata') {
try {
content_object = JSON.parse (result[i].content);
} catch (e) {
continue;
}
result_list.push(content_object);
splitname.length --;
doc.name = splitname.join('.');
doc.creation_date = content_object.owner[
content_object.winner.owner].creation_date;
doc.last_modified = content_object.owner[
content_object.winner.owner].last_modified;
command_file_metadata_list.push(doc);
}
}
if (command.getOption('metadata_only')) {
that.done(command_file_metadata_list);
} else {
if (result.length === 0) {
return that.done([]);
}
for (i = 0; i < command_file_metadata_list.length; i+= 1) {
LocalOrCookieStorage.setItem (
command_file_metadata_list[i].name + '.metadata',
result_list[i]);
loadFile(command_file_metadata_list[i],
result_list[i].winner.revision,
result_list[i].winner.owner);
}
that.end();
}
};
var newcommand = that.newCommand(
'getDocumentList',
{path:command.getPath(),
option:cloned_option});
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
}, loadFile = function (doc,revision,owner) {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = function (error) {
if (!end) {
end = true;
that.fail(command,error);
}
};
cloned_option.onDone = function (result) {
if (!end) {
doc.content = result.content;
nb_loaded_file ++;
if (command_file_metadata_list.length === nb_loaded_file) {
end = true;
that.done(command_file_metadata_list);
}
}
};
var newcommand = that.newCommand(
'loadDocument',
{path:doc.name + '.' + revision + '.' + owner,
option:cloned_option});
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
};
_1();
};
/**
......@@ -1522,11 +1614,198 @@ var newConflictManagerStorage = function ( spec, my ) {
* @method removeDocument
*/
that.removeDocument = function (command) {
that.fail({message:'NIY'});
var metadata_file_name = command.getPath() + '.metadata',
local_metadata_file_name = local_namespace + metadata_file_name,
local_file_metadata = {},
command_file_metadata = {}, // distant file.metadata
run_index = 0, previous_revision = 0,
end = false, is_a_new_file = false,
run = function (index) {
switch (index) {
case 0:
run_index = 3;
run (2);
run (1);
break;
case 1: // update local metadata
var new_owner_object = {revision:0,hash:'',
last_modified:0,
creation_date:0};
local_file_metadata =
LocalOrCookieStorage.getItem (local_metadata_file_name);
if ( local_file_metadata ) {
// if metadata already exists
if ( !local_file_metadata.owner[priv.username] ) {
local_file_metadata.owner[priv.username] =
new_owner_object;
}
} else {
local_file_metadata = {
winner: {},
owner: {},
conflict_list: []
};
local_file_metadata.winner = {
revision:0,owner:priv.username,hash:''};
local_file_metadata.owner[priv.username] =
new_owner_object;
}
run_index ++; run (run_index);
break;
case 2: // load metadata from distant
priv.loadMetadataFromDistant (
command,metadata_file_name,
function (result) {
command_file_metadata = JSON.parse (result.content);
run_index++; run (run_index);
},function (error) {
if (error.status === 404) {
command_file_metadata = local_file_metadata;
is_a_new_file = true;
run_index++; run (run_index);
return;
}
run_index = (-10);
end = true;
that.fail(command,error);
});
break;
case 5:
var updateMetadataCommon = function () {
if (command_file_metadata.owner[priv.username]) {
previous_revision = command_file_metadata.owner[
priv.username].revision;
delete command_file_metadata.owner[priv.username];
}
};
var updateCommandMetadataNotOnConflict = function () {
updateMetadataCommon();
command_file_metadata.winner.owner = priv.username;
command_file_metadata.winner.revision = 0;
command_file_metadata.winner.hash = '';
};
var updateCommandMetadataOnConflict = function () {
updateMetadataCommon ();
};
// if this is a new file
if (is_a_new_file) {
LocalOrCookieStorage.deleteItem (local_metadata_file_name);
return that.done();
}
// if no conflict
if (local_file_metadata.winner.revision ===
command_file_metadata.winner.revision &&
local_file_metadata.winner.hash ===
command_file_metadata.winner.hash) {
// OK! Now, update distant metadata, store them and remove
updateCommandMetadataNotOnConflict();
LocalOrCookieStorage.setItem (local_metadata_file_name,
command_file_metadata);
run_index = 98;
run (6); // save metadata
run (7); // remove document revision
} else {
// if conflict
var conflict_object = {
label: 'revision',
path: command.getPath(),
conflict_owner: {
name: command_file_metadata.winner.owner,
revision: command_file_metadata.winner.revision,
hash: command_file_metadata.winner.hash}
},
// gen hash
conflict_hash = hex_sha256 (JSON.stringify (
conflict_object));
conflict_object.hash = conflict_hash;
// browse known conflict list
var i, known_conflict_list =
command.getOption('known_conflict_list') || [];
var compare_fun = function (v) {
return (v.hash === conflict_hash);
};
for (i = 0; i < known_conflict_list.length; i+= 1) {
// if known conflict
if (known_conflict_list[i].hash ===
conflict_hash) {
command_file_metadata.conflict_list =
priv.removeValuesFromArrayWhere(
command_file_metadata.conflict_list,
compare_fun);
updateCommandMetadataNotOnConflict();
run_index = 98;
run (6);
run (7);
return;
}
}
updateCommandMetadataOnConflict();
// if unknown conflict
command_file_metadata.conflict_list.push (conflict_object);
run_index = (-10);
end = true;
run (6); // save metadata
run (7); // remove document revision
that.fail(command); // TODO
command.getOption('onConflict')(conflict_object);
}
break;
case 6:
priv.saveMetadataToDistant (
command,metadata_file_name,command_file_metadata,
function () {
run_index ++; run (run_index);
},function (error) {
run_index = (-10);
end = true;
that.fail(command,error);
});
break;
case 7:
(function () {
if ( previous_revision !== 0 && (
!command_file_metadata.owner[priv.username] ||
previous_revision !==
command_file_metadata.owner[
priv.username].revision ) ) {
var cloned_option = command.cloneOption ();
cloned_option.onResponse = function () {};
cloned_option.onFail = function (error) {
run_index = (-10);
end = true;
that.fail(command,error);
};
cloned_option.onDone = function () {
run_index ++; run (run_index);
};
var newcommand = that.newCommand(
'removeDocument',
{path:command.getPath() + '.' +
previous_revision + '.' + priv.username,
option:cloned_option});
newcommand.setMaxRetry (0); // inf
that.addJob ( that.newStorage (priv.secondstorage_spec),
newcommand );
} else {
run_index ++; run (run_index);
}
}());
break;
case 100:
if (!end) {
end = true;
that.done();
return;
}
break;
default: break;
}
};
run (0);
};
return that;
};
Jio.addStorageType('replicate', newReplicateStorage);
Jio.addStorageType('conflictmanager', newConflictManagerStorage);
}( LocalOrCookieStorage, jQuery, Base64, sjcl, MD5, jio ));
}( LocalOrCookieStorage, jQuery, Base64, sjcl, hex_sha256, jio ));
/*! JIO Storage - v0.1.0 - 2012-06-22
/*! JIO Storage - v0.1.0 - 2012-06-26
* Copyright (c) 2012 Nexedi; Licensed */
(function(a,b,c,d,e,f){var g=function(b,c){var d=f.storage(b,c,"base"),e={};e.username=b.username||"",e.applicationname=b.applicationname||"untitled";var g="jio/local_user_array",h="jio/local_file_name_array/"+e.username+"/"+e.applicationname,i=d.serialized;return d.serialized=function(){var a=i();return a.applicationname=e.applicationname,a.username=e.username,a},d.validateState=function(){return e.username?"":'Need at least one parameter: "username".'},e.getUserArray=function(){return a.getItem(g)||[]},e.addUser=function(b){var c=e.getUserArray();c.push(b),a.setItem(g,c)},e.userExists=function(a){var b=e.getUserArray(),c,d;for(c=0,d=b.length;c<d;c+=1)if(b[c]===a)return!0;return!1},e.getFileNameArray=function(){return a.getItem(h)||[]},e.addFileName=function(b){var c=e.getFileNameArray();c.push(b),a.setItem(h,c)},e.removeFileName=function(b){var c,d,f=e.getFileNameArray(),g=[];for(c=0,d=f.length;c<d;c+=1)f[c]!==b&&g.push(f[c]);a.setItem(h,g)},d.saveDocument=function(b){setTimeout(function(){var c=null,f="jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath();c=a.getItem(f),c?(c.last_modified=Date.now(),c.content=b.getContent()):(c={name:b.getPath(),content:b.getContent(),creation_date:Date.now(),last_modified:Date.now()},e.userExists(e.username)||e.addUser(e.username),e.addFileName(b.getPath())),a.setItem(f,c),d.done()},100)},d.loadDocument=function(b){setTimeout(function(){var c=null;c=a.getItem("jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath()),c?(b.getOption("metadata_only")&&delete c.content,d.done(c)):d.fail(b,{status:404,statusText:"Not Found.",message:'Document "'+b.getPath()+'" not found in localStorage.'})},100)},d.getDocumentList=function(b){setTimeout(function(){var c=[],f=[],g,h,i="key",j="jio/local/"+e.username+"/"+e.applicationname,k={};f=e.getFileNameArray();for(g=0,h=f.length;g<h;g+=1)k=a.getItem(j+"/"+f[g]),k&&(b.getOption("metadata_only")?c.push({name:k.name,creation_date:k.creation_date,last_modified:k.last_modified}):c.push({name:k.name,content:k.content,creation_date:k.creation_date,last_modified:k.last_modified}));d.done(c)},100)},d.removeDocument=function(b){setTimeout(function(){var c="jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath();a.deleteItem(c),e.removeFileName(b.getPath()),d.done()},100)},d};f.addStorageType("local",g);var h=function(a,d){var e=f.storage(a,d,"base"),g={};g.username=a.username||"",g.applicationname=a.applicationname||"untitled",g.url=a.url||"",g.password=a.password||"";var h=e.serialized;return e.serialized=function(){var a=h();return a.username=g.username,a.applicationname=g.applicationname,a.url=g.url,a.password=g.password,a},e.validateState=function(){return g.username&&g.url?"":'Need at least 2 parameters: "username" and "url".'},e.saveDocument=function(a){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"PUT",data:a.getContent(),async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(){e.done()},error:function(b){b.message='Cannot save "'+a.getPath()+'" into DAVStorage.',e.fail(b)}})},e.loadDocument=function(a){var d={},f=function(){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"GET",async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(a){d.content=a,e.done(d)},error:function(b){b.status===404?b.message='Document "'+a.getPath()+'" not found in localStorage.':b.message='Cannot load "'+a.getPath()+'" from DAVStorage.',e.fail(b)}})};d.name=a.getPath(),b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"PROPFIND",async:!0,dataType:"xml",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(c){b(c).find("lp1\\:getlastmodified, getlastmodified").each(function(){d.last_modified=b(this).text()}),b(c).find("lp1\\:creationdate, creationdate").each(function(){d.creation_date=b(this).text()}),a.getOption("metadata_only")?e.done(d):f()},error:function(b){b.message='Cannot load "'+a.getPath()+'" informations from DAVStorage.',e.fail(b)}})},e.getDocumentList=function(a){var d=[],f={},h=[];b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/",async:!0,type:"PROPFIND",dataType:"xml",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password),Depth:"1"},success:function(a){b(a).find("D\\:response, response").each(function(a,c){if(a>0){f={},b(c).find("D\\:href, href").each(function(){h=b(this).text().split("/"),f.name=h[h.length-1]?h[h.length-1]:h[h.length-2]+"/"});if(f.name===".htaccess"||f.name===".htpasswd")return;b(c).find("lp1\\:getlastmodified, getlastmodified").each(function(){f.last_modified=b(this).text()}),b(c).find("lp1\\:creationdate, creationdate").each(function(){f.creation_date=b(this).text()}),d.push(f)}}),e.done(d)},error:function(a){a.message="Cannot get a document list from DAVStorage.",e.fail(a)}})},e.removeDocument=function(a){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"DELETE",async:!0,headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(){e.done()},error:function(a){a.status===404?e.done():(a.message='Cannot remove "'+e.getFileName()+'" from DAVStorage.',e.fail(a))}})},e};f.addStorageType("dav",h);var i=function(a,b){var c=f.storage(a,b,"handler"),d={};d.return_value_array=[],d.storagelist=a.storagelist||[],d.nb_storage=d.storagelist.length;var e=c.serialized;return c.serialized=function(){var a=e();return a.storagelist=d.storagelist,a},c.validateState=function(){return d.storagelist.length===0?'Need at least one parameter: "storagelist" containing at least one storage.':""},d.isTheLast=function(){return d.return_value_array.length===d.nb_storage},d.doJob=function(a,b){var e=!1,f=[],g,h=function(a){d.return_value_array.push(a)},i=function(a){e||(f.push(a),d.isTheLast()&&c.fail({status:207,statusText:"Multi-Status",message:b,array:f}))},j=function(a){e||(e=!0,c.done(a))};for(g=0;g<d.nb_storage;g+=1){var k=a.clone(),l=c.newStorage(d.storagelist[g]);k.onResponseDo(h),k.onFailDo(i),k.onDoneDo(j),c.addJob(l,k)}a.setMaxRetry(1)},c.saveDocument=function(a){d.doJob(a,'All save "'+a.getPath()+'" requests have failed.'),c.end()},c.loadDocument=function(a){d.doJob(a,'All load "'+a.getPath()+'" requests have failed.'),c.end()},c.getDocumentList=function(a){d.doJob(a,"All get document list requests have failed."),c.end()},c.removeDocument=function(a){d.doJob(a,'All remove "'+a.getPath()+'" requests have failed.'),c.end()},c};f.addStorageType("replicate",i);var j=function(b,c){var d=f.storage(b,c,"handler"),e={},g=b.storage||!1;e.secondstorage_spec=b.storage||{type:"base"},e.secondstorage_string=JSON.stringify(e.secondstorage_spec);var h="jio/indexed_storage_array",i="jio/indexed_file_array/"+e.secondstorage_string,j=d.serialized;return d.serialized=function(){var a=j();return a.storage=e.secondstorage_spec,a},d.validateState=function(){return g?"":'Need at least one parameter: "storage" containing storage specifications.'},e.isStorageArrayIndexed=function(){return a.getItem(h)?!0:!1},e.getIndexedStorageArray=function(){return a.getItem(h)||[]},e.indexStorage=function(b){var c=e.getIndexedStorageArray();c.push(typeof b=="string"?b:JSON.stringify(b)),a.setItem(h,c)},e.isAnIndexedStorage=function(a){var b=typeof a=="string"?a:JSON.stringify(a),c,d,f=e.getIndexedStorageArray();for(c=0,d=f.length;c<d;c+=1)if(JSON.stringify(f[c])===b)return!0;return!1},e.fileArrayExists=function(){return a.getItem(i)?!0:!1},e.getFileArray=function(){return a.getItem(i)||[]},e.setFileArray=function(b){return a.setItem(i,b)},e.isFileIndexed=function(a){var b,c,d=e.getFileArray();for(b=0,c=d.length;b<c;b+=1)if(d[b].name===a)return!0;return!1},e.addFile=function(b){var c=e.getFileArray();c.push(b),a.setItem(i,c)},e.removeFile=function(b){var c,d,f=e.getFileArray(),g=[];for(c=0,d=f.length;c<d;c+=1)f[c].name!==b&&g.push(f[c]);a.setItem(i,g)},e.update=function(){var a=function(a){e.isAnIndexedStorage(e.secondstorage_string)||e.indexStorage(e.secondstorage_string),e.setFileArray(a)};d.addJob(d.newStorage(e.secondstorage_spec),d.newCommand("getDocumentList",{path:".",option:{onDone:a,max_retry:3}}))},d.saveDocument=function(a){var b=a.clone();b.onResponseDo(function(){}),b.onDoneDo(function(b){e.isFileIndexed(a.getPath())||e.addFile({name:a.getPath(),last_modified:0,creation_date:0}),e.update(),d.done()}),b.onFailDo(function(a){d.fail(a)}),d.addJob(d.newStorage(e.secondstorage_spec),b)},d.loadDocument=function(a){var b,c,f,g,h=function(a){d.done(a)},i=function(a){d.fail(a)},j=function(){var b=a.clone();b.onResponseDo(function(){}),b.onFailDo(i),b.onDoneDo(h),d.addJob(d.newStorage(e.secondstorage_spec),b)};e.update(),a.getOption("metadata_only")?setTimeout(function(){if(e.fileArrayExists()){b=e.getFileArray();for(c=0,f=b.length;c<f;c+=1)if(b[c].name===a.getPath())return d.done(b[c])}else j()},100):j()},d.getDocumentList=function(a){var b,c,f=!1;e.update(),a.getOption("metadata_only")?(b=setInterval(function(){f&&(d.fail({status:0,statusText:"Timeout",message:"The request has timed out."}),clearInterval(b)),e.fileArrayExists()&&(d.done(e.getFileArray()),clearInterval(b))},100),setTimeout(function(){f=!0},1e4)):(c=a.clone(),c.onDoneDo(function(a){d.done(a)}),c.onFailDo(function(a){d.fail(a)}),d.addJob(d.newStorage(e.secondstorage_spec),c))},d.removeDocument=function(a){var b=a.clone();b.onResponseDo(function(){}),b.onDoneDo(function(b){e.removeFile(a.getPath()),e.update(),d.done()}),b.onFailDo(function(a){d.fail(a)}),d.addJob(d.newStorage(e.secondstorage_spec),b)},d};f.addStorageType("indexed",j);var k=function(a,c){var e=f.storage(a,c,"handler"),g={};g.username=a.username||"",g.password=a.password||"",g.secondstorage_spec=a.storage||{type:"base"};var h=e.serialized;return e.serialized=function(){var a=h();return a.username=g.username,a.password=g.password,a},e.validateState=function(){return g.username&&JSON.stringify(g.secondstorage_spec)===JSON.stringify({type:"base"})?"":'Need at least two parameters: "username" and "storage".'},g.encrypt_param_object={iv:"kaprWwY/Ucr7pumXoTHbpA",v:1,iter:1e3,ks:256,ts:128,mode:"ccm",adata:"",cipher:"aes",salt:"K4bmZG9d704"},g.decrypt_param_object={iv:"kaprWwY/Ucr7pumXoTHbpA",ks:256,ts:128,salt:"K4bmZG9d704"},g.encrypt=function(a,b,c){var f=d.encrypt(e.getStorageUserName()+":"+e.getStoragePassword(),a,g.encrypt_param_object);b(JSON.parse(f).ct,c)},g.decrypt=function(a,c,f,h){var i,j=b.extend(!0,{},g.decrypt_param_object);j.ct=a||"",j=JSON.stringify(j);try{i=d.decrypt(e.getStorageUserName()+":"+e.getStoragePassword(),j)}catch(k){c({status:0,statusText:"Decrypt Fail",message:"Unable to decrypt."},f,h);return}c(i,f,h)},e.saveDocument=function(a){var b,c,d=function(){g.encrypt(a.getPath(),function(a){b=a,f()})},f=function(){g.encrypt(a.getContent(),function(a){c=a,h()})},h=function(){var a=e.cloneOption(),d,f;a.onResponse=function(){},a.onDone=function(){e.done()},a.onFail=function(a){e.fail(a)},d=e.newCommand({path:b,content:c,option:a}),f=e.newStorage(g.secondstorage_spec),e.addJob(f,d)};d()},e.loadDocument=function(a){var b,c,d=function(){g.encrypt(a.getPath(),function(a){b=a,f()})},f=function(){var c=a.cloneOption(),d,f;c.onResponse=function(){},c.onFail=i,c.onDone=h,d=e.newCommand({path:b,option:c}),f=e.newStorage(g.secondstorage_spec),e.addJob(f,d)},h=function(b){b.name=a.getPath(),a.getOption("metadata_only")?e.done(b):g.decrypt(b.content,function(a){typeof a=="object"?e.fail({status:0,statusText:"Decrypt Fail",message:"Unable to decrypt"}):(b.content=a,e.done(b))})},i=function(a){e.fail(a)};d()},e.getDocumentList=function(a){var b,c,d,f=0,h,i=!0,j=function(){var c=a.clone(),d=e.newStorage(g.secondstorage_spec);c.onResponseDo(k),c.onDoneDo(function(){}),c.onFailDo(function(){}),e.addJob(b)},k=function(a){if(a.status.isDone()){h=a.return_value;for(c=0,d=h.length;c<d;c+=1)g.decrypt(h[c].name,l,c,"name")}else e.fail(a.error)},l=function(a,b,c){var g;f++;if(typeof a=="object"){i&&e.fail({status:0,statusText:"Decrypt Fail",message:"Unable to decrypt."}),i=!1;return}h[b][c]=a,f===d&&i&&e.done(h)};j()},e.removeDocument=function(){var a,b,c=function(){g.encrypt(e.getFileName(),function(a){b=a,d()})},d=function(){a=e.cloneJob(),a.name=b,a.storage=e.getSecondStorage(),a.onResponse=f,e.addJob(a)},f=function(a){a.status==="done"?e.done():e.fail(a.error)};c()},e};f.addStorageType("crypt",k);var l=function(b,c){var d=f.storage(b,c,"handler"),e={};b=b||{},c=c||{};var g="jio/conflictmanager/";e.username=b.username||"";var h=b.storage?!0:!1;e.secondstorage_spec=b.storage||{type:"base"};var i=d.serialized;return d.serialized=function(){var a=i();return a.storage=e.secondstorage_spec,a},d.validateState=function(){return!e.username||h?'Need at least two parameter: "owner" and "storage" .':""},e.removeValuesFromArrayWhere=function(a,b){var c,d=[];for(c=0;c<a.length;c+=1)b(a[c])||d.push(a[c]);return d},d.saveDocument=function(b){var c=b.getPath()+".metadata",f=new Date,h=g+c,i={},j={},k=0,l=!1,m=!1,n=0,o=hex_sha256(b.getContent()),p=function(g){switch(g){case 0:k=3,p(2),p(1);break;case 1:var q={revision:0,hash:"",last_modified:0,creation_date:f.getTime()};i=a.getItem(h),i?i.owner[e.username]||(i.owner[e.username]=q):(i={winner:{},owner:{},conflict_list:[]},i.winner={revision:0,owner:e.username,hash:""},i.owner[e.username]=q),k++,p(k);break;case 2:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){a.status===404?(j=i,k++,p(k)):(k=-10,l=!0,d.fail(a))},a.onDone=function(a){j=JSON.parse(a.content),k++,p(k)};var f=d.newCommand("loadDocument",{path:c,option:a});d.addJob(d.newStorage(e.secondstorage_spec),f)})();break;case 5:var r=function(){var a;a=j.owner[j.winner.owner].creation_date||f.getTime(),j.owner[e.username]?n=j.owner[e.username].revision:j.owner[e.username]={},j.owner[e.username].last_modified=f.getTime(),j.owner[e.username].creation_date=a,j.owner[e.username].hash=o},s=function(){r(),j.winner.owner=e.username,j.winner.revision++,j.winner.hash=o,j.owner[e.username].revision=j.winner.revision},t=function(){r(),j.owner[e.username].revision++};if(m){s(),a.setItem(h,j),k=98,p(6),p(7);break}if(i.winner.revision===j.winner.revision&&i.winner.hash===j.winner.hash)s(),a.setItem(h,j),k=98,p(6),p(7);else{var u={label:"revision",path:b.getPath(),conflict_owner:{name:j.winner.owner,revision:j.winner.revision,hash:j.winner.hash}},v=hex_sha256(JSON.stringify(u));u.hash=v;var w,x=b.getOption("known_conflict_list")||[],y=function(a){return a.hash===v};for(w=0;w<x.length;w+=1)if(x[w].hash===v){e.removeValuesFromArrayWhere(j.conflict_list,y),s(),k=98,p(6),p(7);return}t(),j.conflict_list.push(u),k=-10,l=!0,p(6),p(7),d.fail(),b.getOption("onConflict")(u)}break;case 6:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){k=-10,l=!0,d.fail(a)},a.onDone=function(){k++,p(k)};var f=d.newCommand("saveDocument",{path:c,content:JSON.stringify(j),option:a});f.setMaxRetry(0),d.addJob(d.newStorage(e.secondstorage_spec),f)})();break;case 7:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){k=-10,l=!0,d.fail(a)},a.onDone=function(){p(8)};var c=d.newCommand("saveDocument",{path:b.getPath()+"."+j.owner[e.username].revision+"."+e.username,content:b.getContent(),option:a});c.setMaxRetry(0),d.addJob(d.newStorage(e.secondstorage_spec),c)})();break;case 8:(function(){if(n!==0){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){k=-10,l=!0,d.fail(a)},a.onDone=function(){k++,p(k)};var c=d.newCommand("removeDocument",{path:b.getPath()+"."+n+"."+e.username,option:a});c.setMaxRetry(0),d.addJob(d.newStorage(e.secondstorage_spec),c)}else k++,p(k)})();break;case 100:if(!l){l=!0,d.done();return}break;default:}};p(0),b.setMaxRetry(1)},d.loadDocument=function(b){var c=b.getPath()+".metadata",f=g+c,h={},i=0,j=!1,k="",l,m=function(g){switch(g){case 0:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){i=-10,j=!0,d.fail(a)},a.onDone=function(a){h=JSON.parse(a.content),k=b.getOption("owner"),i=98,k?m(3):m(2),m(1)};var f=d.newCommand("loadDocument",{path:c,option:a});f.setMaxRetry(0),d.addJob(d.newStorage(e.secondstorage_spec),f)})();break;case 1:a.setItem(f,h),i++,m(i);break;case 2:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){i=-10,j=!0,d.fail(a)},a.onDone=function(a){l=a,i++,m(i)};var c=d.newCommand("loadDocument",{path:b.getPath()+"."+h.winner.revision+"."+h.winner.owner,option:a});c.setMaxRetry(0),d.addJob(d.newStorage(e.secondstorage_spec),c)})();break;case 3:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){i=-10,j=!0,d.fail(a)},a.onDone=function(a){l=a,i++,m(i)};if(!h.owner[k]){a.onFail({status:404,statusText:"Not Found",message:"Document not found."});return}var c=d.newCommand("loadDocument",{path:b.getPath()+"."+h.owner[k].revision+"."+k,option:a});c.setMaxRetry(0),d.addJob(d.newStorage(e.secondstorage_spec),c)})();break;case 100:if(!j){j=!0,d.done(l);return}break;default:}};m(0)},d.getDocumentList=function(a){d.fail({message:"NIY"})},d.removeDocument=function(a){d.fail({message:"NIY"})},d};f.addStorageType("replicate",i)})(LocalOrCookieStorage,jQuery,Base64,sjcl,MD5,jio);
\ No newline at end of file
(function(a,b,c,d,e,f){var g=function(b,c){var d=f.storage(b,c,"base"),e={};e.username=b.username||"",e.applicationname=b.applicationname||"untitled";var g="jio/local_user_array",h="jio/local_file_name_array/"+e.username+"/"+e.applicationname,i=d.serialized;return d.serialized=function(){var a=i();return a.applicationname=e.applicationname,a.username=e.username,a},d.validateState=function(){return e.username?"":'Need at least one parameter: "username".'},e.getUserArray=function(){return a.getItem(g)||[]},e.addUser=function(b){var c=e.getUserArray();c.push(b),a.setItem(g,c)},e.userExists=function(a){var b=e.getUserArray(),c,d;for(c=0,d=b.length;c<d;c+=1)if(b[c]===a)return!0;return!1},e.getFileNameArray=function(){return a.getItem(h)||[]},e.addFileName=function(b){var c=e.getFileNameArray();c.push(b),a.setItem(h,c)},e.removeFileName=function(b){var c,d,f=e.getFileNameArray(),g=[];for(c=0,d=f.length;c<d;c+=1)f[c]!==b&&g.push(f[c]);a.setItem(h,g)},d.saveDocument=function(b){setTimeout(function(){var c=null,f="jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath();c=a.getItem(f),c?(c.last_modified=Date.now(),c.content=b.getContent()):(c={name:b.getPath(),content:b.getContent(),creation_date:Date.now(),last_modified:Date.now()},e.userExists(e.username)||e.addUser(e.username),e.addFileName(b.getPath())),a.setItem(f,c),d.done()},100)},d.loadDocument=function(b){setTimeout(function(){var c=null;c=a.getItem("jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath()),c?(b.getOption("metadata_only")&&delete c.content,d.done(c)):d.fail(b,{status:404,statusText:"Not Found.",message:'Document "'+b.getPath()+'" not found in localStorage.'})},100)},d.getDocumentList=function(b){setTimeout(function(){var c=[],f=[],g,h,i="key",j="jio/local/"+e.username+"/"+e.applicationname,k={};f=e.getFileNameArray();for(g=0,h=f.length;g<h;g+=1)k=a.getItem(j+"/"+f[g]),k&&(b.getOption("metadata_only")?c.push({name:k.name,creation_date:k.creation_date,last_modified:k.last_modified}):c.push({name:k.name,content:k.content,creation_date:k.creation_date,last_modified:k.last_modified}));d.done(c)},100)},d.removeDocument=function(b){setTimeout(function(){var c="jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath();a.deleteItem(c),e.removeFileName(b.getPath()),d.done()},100)},d};f.addStorageType("local",g);var h=function(a,d){var e=f.storage(a,d,"base"),g={};g.username=a.username||"",g.applicationname=a.applicationname||"untitled",g.url=a.url||"",g.password=a.password||"";var h=e.serialized;return e.serialized=function(){var a=h();return a.username=g.username,a.applicationname=g.applicationname,a.url=g.url,a.password=g.password,a},e.validateState=function(){return g.username&&g.url?"":'Need at least 2 parameters: "username" and "url".'},e.saveDocument=function(a){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"PUT",data:a.getContent(),async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(){e.done()},error:function(b){b.message='Cannot save "'+a.getPath()+'" into DAVStorage.',e.fail(b)}})},e.loadDocument=function(a){var d={},f=function(){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"GET",async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(a){d.content=a,e.done(d)},error:function(b){b.status===404?b.message='Document "'+a.getPath()+'" not found in localStorage.':b.message='Cannot load "'+a.getPath()+'" from DAVStorage.',e.fail(b)}})};d.name=a.getPath(),b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"PROPFIND",async:!0,dataType:"xml",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(c){b(c).find("lp1\\:getlastmodified, getlastmodified").each(function(){d.last_modified=b(this).text()}),b(c).find("lp1\\:creationdate, creationdate").each(function(){d.creation_date=b(this).text()}),a.getOption("metadata_only")?e.done(d):f()},error:function(b){b.message='Cannot load "'+a.getPath()+'" informations from DAVStorage.',e.fail(b)}})},e.getDocumentList=function(a){var d=[],f={},h=[];b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/",async:!0,type:"PROPFIND",dataType:"xml",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password),Depth:"1"},success:function(a){b(a).find("D\\:response, response").each(function(a,c){if(a>0){f={},b(c).find("D\\:href, href").each(function(){h=b(this).text().split("/"),f.name=h[h.length-1]?h[h.length-1]:h[h.length-2]+"/"});if(f.name===".htaccess"||f.name===".htpasswd")return;b(c).find("lp1\\:getlastmodified, getlastmodified").each(function(){f.last_modified=b(this).text()}),b(c).find("lp1\\:creationdate, creationdate").each(function(){f.creation_date=b(this).text()}),d.push(f)}}),e.done(d)},error:function(a){a.message="Cannot get a document list from DAVStorage.",e.fail(a)}})},e.removeDocument=function(a){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"DELETE",async:!0,headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(){e.done()},error:function(a){a.status===404?e.done():(a.message='Cannot remove "'+e.getFileName()+'" from DAVStorage.',e.fail(a))}})},e};f.addStorageType("dav",h);var i=function(a,b){var c=f.storage(a,b,"handler"),d={};d.return_value_array=[],d.storagelist=a.storagelist||[],d.nb_storage=d.storagelist.length;var e=c.serialized;return c.serialized=function(){var a=e();return a.storagelist=d.storagelist,a},c.validateState=function(){return d.storagelist.length===0?'Need at least one parameter: "storagelist" containing at least one storage.':""},d.isTheLast=function(){return d.return_value_array.length===d.nb_storage},d.doJob=function(a,b){var e=!1,f=[],g,h=function(a){d.return_value_array.push(a)},i=function(a){e||(f.push(a),d.isTheLast()&&c.fail({status:207,statusText:"Multi-Status",message:b,array:f}))},j=function(a){e||(e=!0,c.done(a))};for(g=0;g<d.nb_storage;g+=1){var k=a.clone(),l=c.newStorage(d.storagelist[g]);k.onResponseDo(h),k.onFailDo(i),k.onDoneDo(j),c.addJob(l,k)}a.setMaxRetry(1)},c.saveDocument=function(a){d.doJob(a,'All save "'+a.getPath()+'" requests have failed.'),c.end()},c.loadDocument=function(a){d.doJob(a,'All load "'+a.getPath()+'" requests have failed.'),c.end()},c.getDocumentList=function(a){d.doJob(a,"All get document list requests have failed."),c.end()},c.removeDocument=function(a){d.doJob(a,'All remove "'+a.getPath()+'" requests have failed.'),c.end()},c};f.addStorageType("replicate",i);var j=function(b,c){var d=f.storage(b,c,"handler"),e={},g=b.storage||!1;e.secondstorage_spec=b.storage||{type:"base"},e.secondstorage_string=JSON.stringify(e.secondstorage_spec);var h="jio/indexed_storage_array",i="jio/indexed_file_array/"+e.secondstorage_string,j=d.serialized;return d.serialized=function(){var a=j();return a.storage=e.secondstorage_spec,a},d.validateState=function(){return g?"":'Need at least one parameter: "storage" containing storage specifications.'},e.isStorageArrayIndexed=function(){return a.getItem(h)?!0:!1},e.getIndexedStorageArray=function(){return a.getItem(h)||[]},e.indexStorage=function(b){var c=e.getIndexedStorageArray();c.push(typeof b=="string"?b:JSON.stringify(b)),a.setItem(h,c)},e.isAnIndexedStorage=function(a){var b=typeof a=="string"?a:JSON.stringify(a),c,d,f=e.getIndexedStorageArray();for(c=0,d=f.length;c<d;c+=1)if(JSON.stringify(f[c])===b)return!0;return!1},e.fileArrayExists=function(){return a.getItem(i)?!0:!1},e.getFileArray=function(){return a.getItem(i)||[]},e.setFileArray=function(b){return a.setItem(i,b)},e.isFileIndexed=function(a){var b,c,d=e.getFileArray();for(b=0,c=d.length;b<c;b+=1)if(d[b].name===a)return!0;return!1},e.addFile=function(b){var c=e.getFileArray();c.push(b),a.setItem(i,c)},e.removeFile=function(b){var c,d,f=e.getFileArray(),g=[];for(c=0,d=f.length;c<d;c+=1)f[c].name!==b&&g.push(f[c]);a.setItem(i,g)},e.update=function(){var a=function(a){e.isAnIndexedStorage(e.secondstorage_string)||e.indexStorage(e.secondstorage_string),e.setFileArray(a)};d.addJob(d.newStorage(e.secondstorage_spec),d.newCommand("getDocumentList",{path:".",option:{onDone:a,max_retry:3}}))},d.saveDocument=function(a){var b=a.clone();b.onResponseDo(function(){}),b.onDoneDo(function(b){e.isFileIndexed(a.getPath())||e.addFile({name:a.getPath(),last_modified:0,creation_date:0}),e.update(),d.done()}),b.onFailDo(function(a){d.fail(a)}),d.addJob(d.newStorage(e.secondstorage_spec),b)},d.loadDocument=function(a){var b,c,f,g,h=function(a){d.done(a)},i=function(a){d.fail(a)},j=function(){var b=a.clone();b.onResponseDo(function(){}),b.onFailDo(i),b.onDoneDo(h),d.addJob(d.newStorage(e.secondstorage_spec),b)};e.update(),a.getOption("metadata_only")?setTimeout(function(){if(e.fileArrayExists()){b=e.getFileArray();for(c=0,f=b.length;c<f;c+=1)if(b[c].name===a.getPath())return d.done(b[c])}else j()},100):j()},d.getDocumentList=function(a){var b,c,f=!1;e.update(),a.getOption("metadata_only")?(b=setInterval(function(){f&&(d.fail({status:0,statusText:"Timeout",message:"The request has timed out."}),clearInterval(b)),e.fileArrayExists()&&(d.done(e.getFileArray()),clearInterval(b))},100),setTimeout(function(){f=!0},1e4)):(c=a.clone(),c.onDoneDo(function(a){d.done(a)}),c.onFailDo(function(a){d.fail(a)}),d.addJob(d.newStorage(e.secondstorage_spec),c))},d.removeDocument=function(a){var b=a.clone();b.onResponseDo(function(){}),b.onDoneDo(function(b){e.removeFile(a.getPath()),e.update(),d.done()}),b.onFailDo(function(a){d.fail(a)}),d.addJob(d.newStorage(e.secondstorage_spec),b)},d};f.addStorageType("indexed",j);var k=function(a,c){var e=f.storage(a,c,"handler"),g={};g.username=a.username||"",g.password=a.password||"",g.secondstorage_spec=a.storage||{type:"base"};var h=e.serialized;return e.serialized=function(){var a=h();return a.username=g.username,a.password=g.password,a},e.validateState=function(){return g.username&&JSON.stringify(g.secondstorage_spec)===JSON.stringify({type:"base"})?"":'Need at least two parameters: "username" and "storage".'},g.encrypt_param_object={iv:"kaprWwY/Ucr7pumXoTHbpA",v:1,iter:1e3,ks:256,ts:128,mode:"ccm",adata:"",cipher:"aes",salt:"K4bmZG9d704"},g.decrypt_param_object={iv:"kaprWwY/Ucr7pumXoTHbpA",ks:256,ts:128,salt:"K4bmZG9d704"},g.encrypt=function(a,b,c){var f=d.encrypt(e.getStorageUserName()+":"+e.getStoragePassword(),a,g.encrypt_param_object);b(JSON.parse(f).ct,c)},g.decrypt=function(a,c,f,h){var i,j=b.extend(!0,{},g.decrypt_param_object);j.ct=a||"",j=JSON.stringify(j);try{i=d.decrypt(e.getStorageUserName()+":"+e.getStoragePassword(),j)}catch(k){c({status:0,statusText:"Decrypt Fail",message:"Unable to decrypt."},f,h);return}c(i,f,h)},e.saveDocument=function(a){var b,c,d=function(){g.encrypt(a.getPath(),function(a){b=a,f()})},f=function(){g.encrypt(a.getContent(),function(a){c=a,h()})},h=function(){var a=e.cloneOption(),d,f;a.onResponse=function(){},a.onDone=function(){e.done()},a.onFail=function(a){e.fail(a)},d=e.newCommand({path:b,content:c,option:a}),f=e.newStorage(g.secondstorage_spec),e.addJob(f,d)};d()},e.loadDocument=function(a){var b,c,d=function(){g.encrypt(a.getPath(),function(a){b=a,f()})},f=function(){var c=a.cloneOption(),d,f;c.onResponse=function(){},c.onFail=i,c.onDone=h,d=e.newCommand({path:b,option:c}),f=e.newStorage(g.secondstorage_spec),e.addJob(f,d)},h=function(b){b.name=a.getPath(),a.getOption("metadata_only")?e.done(b):g.decrypt(b.content,function(a){typeof a=="object"?e.fail({status:0,statusText:"Decrypt Fail",message:"Unable to decrypt"}):(b.content=a,e.done(b))})},i=function(a){e.fail(a)};d()},e.getDocumentList=function(a){var b,c,d,f=0,h,i=!0,j=function(){var c=a.clone(),d=e.newStorage(g.secondstorage_spec);c.onResponseDo(k),c.onDoneDo(function(){}),c.onFailDo(function(){}),e.addJob(b)},k=function(a){if(a.status.isDone()){h=a.return_value;for(c=0,d=h.length;c<d;c+=1)g.decrypt(h[c].name,l,c,"name")}else e.fail(a.error)},l=function(a,b,c){var g;f++;if(typeof a=="object"){i&&e.fail({status:0,statusText:"Decrypt Fail",message:"Unable to decrypt."}),i=!1;return}h[b][c]=a,f===d&&i&&e.done(h)};j()},e.removeDocument=function(){var a,b,c=function(){g.encrypt(e.getFileName(),function(a){b=a,d()})},d=function(){a=e.cloneJob(),a.name=b,a.storage=e.getSecondStorage(),a.onResponse=f,e.addJob(a)},f=function(a){a.status==="done"?e.done():e.fail(a.error)};c()},e};f.addStorageType("crypt",k);var l=function(b,c){var d=f.storage(b,c,"handler"),g={};b=b||{},c=c||{},g.username=b.username||"";var h=b.storage?!0:!1;g.secondstorage_spec=b.storage||{type:"base"},g.secondstorage_string=JSON.stringify(g.secondstorage_spec);var i="jio/conflictmanager/"+g.secondstorage_string+"/",j=d.serialized;d.serialized=function(){var a=j();return a.storage=g.secondstorage_spec,a},d.validateState=function(){return!g.username||h?'Need at least two parameter: "owner" and "storage".':""},g.removeValuesFromArrayWhere=function(a,b){var c,d=[];for(c=0;c<a.length;c+=1)b(a[c])||d.push(a[c]);return d};var k=d.fail;return d.fail=function(a,b){a.setMaxRetry(1),k(b)},g.loadMetadataFromDistant=function(a,b,c,e){var f=a.cloneOption();f.onResponse=function(){},f.onFail=e,f.onDone=c;var h=d.newCommand("loadDocument",{path:b,option:f});d.addJob(d.newStorage(g.secondstorage_spec),h)},g.saveMetadataToDistant=function(a,b,c,e,f){var h=a.cloneOption();h.onResponse=function(){},h.onFail=f,h.onDone=e;var i=d.newCommand("saveDocument",{path:b,content:JSON.stringify(c),option:h});i.setMaxRetry(0),d.addJob(d.newStorage(g.secondstorage_spec),i)},d.saveDocument=function(b){var c=b.getPath()+".metadata",f=new Date,h=i+c,j={},k={},l=0,m=0,n=!1,o=!1,p=e(b.getContent()),q=function(i){switch(i){case 0:l=3,q(2),q(1);break;case 1:var r={revision:0,hash:"",last_modified:0,creation_date:f.getTime()};j=a.getItem(h),j?j.owner[g.username]||(j.owner[g.username]=r):(j={winner:{},owner:{},conflict_list:[]},j.winner={revision:0,owner:g.username,hash:""},j.owner[g.username]=r),l++,q(l);break;case 2:g.loadMetadataFromDistant(b,c,function(a){k=JSON.parse(a.content),l++,q(l)},function(a){a.status===404?(k=j,o=!0,l++,q(l)):(l=-10,n=!0,d.fail(b,a))});break;case 5:var s=function(){var a;o||!k.owner[k.winner.owner]?a=f.getTime():a=k.owner[k.winner.owner].creation_date||f.getTime(),k.owner[g.username]?m=k.owner[g.username].revision:k.owner[g.username]={},k.owner[g.username].last_modified=f.getTime(),k.owner[g.username].creation_date=a,k.owner[g.username].hash=p},t=function(){s(),k.winner.owner=g.username,k.winner.revision++,k.winner.hash=p,k.owner[g.username].revision=k.winner.revision},u=function(){s(),k.owner[g.username].revision++};if(o){t(),a.setItem(h,k),l=98,q(6),q(7);break}if(j.winner.revision===k.winner.revision&&j.winner.hash===k.winner.hash)t(),a.setItem(h,k),l=98,q(6),q(7);else{var v={label:"revision",path:b.getPath(),conflict_owner:{name:k.winner.owner,revision:k.winner.revision,hash:k.winner.hash}},w=e(JSON.stringify(v));v.hash=w;var x,y=b.getOption("known_conflict_list")||[],z=function(a){return a.hash===w};for(x=0;x<y.length;x+=1)if(y[x].hash===w){k.conflict_list=g.removeValuesFromArrayWhere(k.conflict_list,z),t(),l=98,q(6),q(7);return}u(),k.conflict_list.push(v),l=-10,n=!0,q(6),q(7),d.fail(b),b.getOption("onConflict")(v)}break;case 6:g.saveMetadataToDistant(b,c,k,function(){l++,q(l)},function(a){l=-10,n=!0,d.fail(b,a)});break;case 7:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){l=-10,n=!0,d.fail(b,a)},a.onDone=function(){q(8)};var c=d.newCommand("saveDocument",{path:b.getPath()+"."+k.owner[g.username].revision+"."+g.username,content:b.getContent(),option:a});c.setMaxRetry(0),d.addJob(d.newStorage(g.secondstorage_spec),c)})();break;case 8:(function(){if(m===0||!!k.owner[g.username]&&m===k.owner[g.username].revision)l++,q(l);else{var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){l=-10,n=!0,d.fail(b,a)},a.onDone=function(){l++,q(l)};var c=d.newCommand("removeDocument",{path:b.getPath()+"."+m+"."+g.username,option:a});c.setMaxRetry(0),d.addJob(d.newStorage(g.secondstorage_spec),c)}})();break;case 100:if(!n){n=!0,d.done();return}break;default:}};q(0)},d.loadDocument=function(b){var c=b.getPath()+".metadata",e=i+c,f={},h=0,j=!1,k="",l,m=function(i){switch(i){case 0:g.loadMetadataFromDistant(b,c,function(a){f=JSON.parse(a.content),k=b.getOption("owner"),h=98,k?m(3):m(2),m(1)},function(a){h=-10,j=!0,d.fail(b,a)});break;case 1:a.setItem(e,f),h++,m(h);break;case 2:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){h=-10,j=!0,d.fail(b,a)},a.onDone=function(a){l=a,l.name=b.getPath(),h++,m(h)};var c=d.newCommand("loadDocument",{path:b.getPath()+"."+f.winner.revision+"."+f.winner.owner,option:a});d.addJob(d.newStorage(g.secondstorage_spec),c)})();break;case 3:(function(){var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){h=-10,j=!0,d.fail(b,a)},a.onDone=function(a){l=a,l.name=b.getPath(),h++,m(h)};if(!f.owner[k]){a.onFail({status:404,statusText:"Not Found",message:"Document not found."});return}var c=d.newCommand("loadDocument",{path:b.getPath()+"."+f.owner[k].revision+"."+k,option:a});d.addJob(d.newStorage(g.secondstorage_spec),c)})();break;case 100:if(!j){j=!0,d.done(l);return}break;default:}};m(0)},d.getDocumentList=function(b){var c=[],e=[],f=!1,h=0,i=function(){var f=b.cloneOption();f.onResponse=function(){},f.onFail=function(a){d.fail(b,a)},f.onDone=function(f){var g;for(g=0;g<f.length;g+=1){var h=f[g].name.split(".")||[],i,k={};if(h[h.length-1]==="metadata"){try{i=JSON.parse(f[g].content)}catch(l){continue}e.push(i),h.length--,k.name=h.join("."),k.creation_date=i.owner[i.winner.owner].creation_date,k.last_modified=i.owner[i.winner.owner].last_modified,c.push(k)}}if(b.getOption("metadata_only"))d.done(c);else{if(f.length===0)return d.done([]);for(g=0;g<c.length;g+=1)a.setItem(c[g].name+".metadata",e[g]),j(c[g],e[g].winner.revision,e[g].winner.owner);d.end()}};var h=d.newCommand("getDocumentList",{path:b.getPath(),option:f});d.addJob(d.newStorage(g.secondstorage_spec),h)},j=function(a,e,i){var j=b.cloneOption();j.onResponse=function(){},j.onFail=function(a){f||(f=!0,d.fail(b,a))},j.onDone=function(b){f||(a.content=b.content,h++,c.length===h&&(f=!0,d.done(c)))};var k=d.newCommand("loadDocument",{path:a.name+"."+e+"."+i,option:j});d.addJob(d.newStorage(g.secondstorage_spec),k)};i()},d.removeDocument=function(b){var c=b.getPath()+".metadata",f=i+c,h={},j={},k=0,l=0,m=!1,n=!1,o=function(i){switch(i){case 0:k=3,o(2),o(1);break;case 1:var p={revision:0,hash:"",last_modified:0,creation_date:0};h=a.getItem(f),h?h.owner[g.username]||(h.owner[g.username]=p):(h={winner:{},owner:{},conflict_list:[]},h.winner={revision:0,owner:g.username,hash:""},h.owner[g.username]=p),k++,o(k);break;case 2:g.loadMetadataFromDistant(b,c,function(a){j=JSON.parse(a.content),k++,o(k)},function(a){if(a.status===404){j=h,n=!0,k++,o(k);return}k=-10,m=!0,d.fail(b,a)});break;case 5:var q=function(){j.owner[g.username]&&(l=j.owner[g.username].revision,delete j.owner[g.username])},r=function(){q(),j.winner.owner=g.username,j.winner.revision=0,j.winner.hash=""},s=function(){q()};if(n)return a.deleteItem(f),d.done();if(h.winner.revision===j.winner.revision&&h.winner.hash===j.winner.hash)r(),a.setItem(f,j),k=98,o(6),o(7);else{var t={label:"revision",path:b.getPath(),conflict_owner:{name:j.winner.owner,revision:j.winner.revision,hash:j.winner.hash}},u=e(JSON.stringify(t));t.hash=u;var v,w=b.getOption("known_conflict_list")||[],x=function(a){return a.hash===u};for(v=0;v<w.length;v+=1)if(w[v].hash===u){j.conflict_list=g.removeValuesFromArrayWhere(j.conflict_list,x),r(),k=98,o(6),o(7);return}s(),j.conflict_list.push(t),k=-10,m=!0,o(6),o(7),d.fail(b),b.getOption("onConflict")(t)}break;case 6:g.saveMetadataToDistant(b,c,j,function(){k++,o(k)},function(a){k=-10,m=!0,d.fail(b,a)});break;case 7:(function(){if(l===0||!!j.owner[g.username]&&l===j.owner[g.username].revision)k++,o(k);else{var a=b.cloneOption();a.onResponse=function(){},a.onFail=function(a){k=-10,m=!0,d.fail(b,a)},a.onDone=function(){k++,o(k)};var c=d.newCommand("removeDocument",{path:b.getPath()+"."+l+"."+g.username,option:a});c.setMaxRetry(0),d.addJob(d.newStorage(g.secondstorage_spec),c)}})();break;case 100:if(!m){m=!0,d.done();return}break;default:}};o(0)},d};f.addStorageType("conflictmanager",l)})(LocalOrCookieStorage,jQuery,Base64,sjcl,hex_sha256,jio);
\ No newline at end of file
......@@ -6,7 +6,7 @@ var newConflictManagerStorage = function ( spec, my ) {
priv.username = spec.username || '';
var storage_exists = (spec.storage?true:false);
priv.secondstorage_spec = spec.storage || {type:'base'};
priv.secondstorage_string = JSON.stringify (priv.secondstorage_spec)
priv.secondstorage_string = JSON.stringify (priv.secondstorage_spec);
var local_namespace = 'jio/conflictmanager/'+priv.secondstorage_string+'/';
......@@ -462,7 +462,7 @@ var newConflictManagerStorage = function ( spec, my ) {
} else {
if (result.length === 0) {
return that.done([]);
};
}
for (i = 0; i < command_file_metadata_list.length; i+= 1) {
LocalOrCookieStorage.setItem (
command_file_metadata_list[i].name + '.metadata',
......@@ -516,6 +516,7 @@ var newConflictManagerStorage = function ( spec, my ) {
that.removeDocument = function (command) {
var metadata_file_name = command.getPath() + '.metadata',
local_metadata_file_name = local_namespace + metadata_file_name,
local_file_metadata = {},
command_file_metadata = {}, // distant file.metadata
run_index = 0, previous_revision = 0,
end = false, is_a_new_file = false,
......@@ -705,4 +706,4 @@ var newConflictManagerStorage = function ( spec, my ) {
return that;
};
Jio.addStorageType('replicate', newReplicateStorage);
Jio.addStorageType('conflictmanager', newConflictManagerStorage);
......@@ -9,4 +9,4 @@
*
* @module JIOStorages
*/
(function(LocalOrCookieStorage, $, Base64, sjcl, MD5, Jio) {
(function(LocalOrCookieStorage, $, Base64, sjcl, hex_sha256, Jio) {
}( LocalOrCookieStorage, jQuery, Base64, sjcl, MD5, jio ));
}( LocalOrCookieStorage, jQuery, Base64, sjcl, hex_sha256, jio ));
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