Commit 3c5348b8 authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

Add localStorage or Cookie support.

localStorage job list is a rescue job list now.
job list and LocalStorage are inserted to localStorage differently.
...
parent a1f3c2e9
......@@ -5,6 +5,8 @@
<title>JIO</title>
<script type="text/javascript" src="js/jquery/jquery.js"></script>
<script type="text/javascript" src="unhosted/jio.js"></script>
<script type="text/javascript" src="unhosted/jquery.storage.js"></script>
<script type="text/javascript" src="unhosted/base64.js"></script>
<script type="text/javascript" src="unhosted/jio.storage.js"></script>
<!-- <script type="text/javascript" src="unhosted/sjcl.js"></script> -->
......@@ -70,25 +72,30 @@ $.jio('subscribe',{'event':'start_gettingList', 'func': function ( o ) {
}});
$.jio('subscribe',{'event':'stop_gettingList', 'func': function ( o ) {
$('#divmessage').html($('#divmessage').html() + '<br />' +
'Getting list ended: ' + toString(o.job.list) );
'Getting list ended: ' + toString(o) );
}});
// $.jio({'storage':{'type':'local','userName':'tristan'},
// 'applicant':{'ID':'www.ungproject.org'}});
$.jio({'storage':{'type':'dav','userName':'tristan','password':'test',
'location':'https://ca-davstorage:8080','provider':'Unknown'},
$.jio({'storage':{'type':'local','userName':'tristan'},
'applicant':{'ID':'www.ungproject.org'}});
// $.jio({'storage':{'type':'dav','userName':'tristan','password':'test',
// 'location':'https://ca-davstorage:8080','provider':'Unknown'},
// 'applicant':{'ID':'www.ungproject.org'}});
// TODO
// doc string
// localStorage.status
// $.jio('isAvailable',{'userName':'toto'});
// $.jio('isAvailable',{'userName':'tristan'});
// $.jio('save',{'fileName':'SonNom','fileContent':'SonContenu'});
// $.jio({'fileName':'SonNom','fileContent':'SonContenu','method':'save'});
// $.jio({'fileName':'SonNom2','fileContent':'SonContenu2','method':'save'});
// $.jio('load',{'fileName':'SonNom'});
$.jio('getList');
// $.jio('saveDocument',{'fileName':'SonNom','fileContent':'SonContenu'});
// $.jio({'fileName':'SonNom','fileContent':'SonContenu','method':'saveDocument'});
// $.jio({'fileName':'SonNom2','fileContent':'SonContenu2','method':'saveDocument'});
// $.jio('loadDocument',{'fileName':'SonNom'});
// $.jio('removeDocument',{'fileName':'SonNom'});
// $.jio('getDocumentList');
......
;(function ( $ ) {
////////////////////////////////////////////////////////////////////////////
// private vars
var attr = {
var jioAttributeObject = {
'jobMethodObject': {
'isAvailable': {
'start_event':'start_checkingNameAvailability',
'stop_event':'stop_checkingNameAvailability',
'func':'checkNameAvailability',
'retvalue':'isAvailable' }, // returns 'boolean'
'save': {
'saveDocument': {
'start_event':'start_saving',
'stop_event':'stop_saving',
'func':'saveDocument',
'retvalue':'isSaved' }, // returns 'boolean'
'load': {
'loadDocument': {
'start_event':'start_loading',
'stop_event':'stop_loading',
'func':'loadDocument',
'retvalue':'fileContent' }, // returns the file content 'string'
'getList': {
'getDocumentList': {
'start_event':'start_gettingList',
'stop_event':'stop_gettingList',
'func':'getDocumentList',
'retvalue':'list' }, // returns the document list 'array'
'remove': {
'removeDocument': {
'start_event':'start_removing',
'stop_event':'stop_removing',
'func':'removeDocument',
'retvalue':'isRemoved' } // returns 'boolean'
},
'localStorage': null,
'tabid': 0,
'queue': null, // the job manager
'storage': null, // the storage given at init
'applicant': null, // the applicant given at init
......@@ -43,64 +42,89 @@
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Tools
var objectDump = function (o) {
// TODO DEBUG we can remove this function
console.log (JSON.stringify(o));
};
var toString = function (o) {
// TODO DEBUG we can remove this function
return (JSON.stringify(o));
};
var supportLocalStorage = function () {
// Modernizr function
try { return !!localStorage.getItem; }
catch (e) { return false; }
// cookies & localStorage
var myLocalStorage = function () {
};
var ifRemainingJobs = function(method) {
// check if it remains [method] jobs (save,load,...)
// method : can me 'string' or 'undefined'
// undefined -> test if there is at least one job
// string -> test if there is at least one [method] job
if (!method) return (localStorage.joblist !== '[]');
var joba = getJobArrayFromLocalStorage();
for (var k in joba) {
if (joba[k].method === method &&
joba[k].status !== 'fail') return true;
myLocalStorage.prototype = {
getItem: function (name) {
return JSON.parse(localStorage.getItem(name));
},
setItem: function (name,value) {
if (name)
return localStorage.setItem(name,JSON.stringify(value));
},
getAll: function() {
return localStorage;
},
deleteItem: function (name) {
if (name)
delete localStorage[name];
}
return false;
};
var getStatusObjectFromLocalStorage = function () {
var tmp = {};
if (localStorage.status) {
tmp = JSON.parse (localStorage.status);
} else { // if it does not exists, create it
for (var method in attr.jobMethodObject) {
tmp[method] = 'done';
var cookieStorage = function () {
};
cookieStorage.prototype = {
getItem: function (name) {
var cookies = document.cookie.split(';');
for (var i in cookies) {
var x = cookies[i].substr(0, cookies[i].indexOf('='));
var y = cookies[i].substr(cookies[i].indexOf('=')+1);
x = x.replace(/^\s+|\s+$/g,"");
if( x == name ) return unescape(y);
}
return null;
},
setItem: function (name,value) {
// function to store into cookies
if (value != undefined) {
document.cookie = name+'='+JSON.stringify(value)+';domain='+
window.location.hostname+
';path='+window.location.pathname;
return true;
}
return false;
},
getAll: function() {
var retObject = {};
var cookies = document.cookie.split(':');
for (var i in cookies) {
var x = cookies[i].substr(0, cookies[i].indexOf('='));
var y = cookies[i].substr(cookies[i].indexOf('=')+1);
x = x.replace(/^\s+|\s+$/g,"");
retObject[x] = unescape(y);
}
return retObject;
},
deleteItem: function (name) {
document.cookie = name+'=null;domain='+window.location.hostname+
';path='+window.location.pathname+
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
return tmp;
};
var setStatusObjectToLocalStorage = function (statusobject) {
localStorage.status = JSON.stringify(statusobject);
};
var getJobArrayFromLocalStorage = function () {
var localjoblist = [];
if (localStorage.joblist)
localjoblist = JSON.parse(localStorage.joblist);
return localjoblist;
};
var setJobArrayToLocalStorage = function (jobarray) {
localStorage.joblist = JSON.stringify(jobarray);
};
// set good localStorage
try {
if (localStorage.getItem) {
jioAttributeObject.localStorage = new myLocalStorage();
} else {
jioAttributeObject.localStorage = new cookieStorage();
}
}
catch (e) {
jioAttributeObject.localStorage = new cookieStorage();
}
// end cookies & localStorages
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Tools
var createStorageObject = function ( options ) {
// create a storage thanks to attr.storageTypeObject
// create a storage thanks to jioAttributeObject.storageTypeObject
if (!attr.storageTypeObject[ options.storage.type])
if (!jioAttributeObject.storageTypeObject[ options.storage.type ])
return null; // error!
return attr.storageTypeObject[ options.storage.type ](options);
}
return jioAttributeObject.storageTypeObject[
options.storage.type ](options);
};
// end Tools
////////////////////////////////////////////////////////////////////////////
......@@ -138,7 +162,7 @@
$.jiopubsub(eventname).unsubscribe();
}
};
attr.pubsub = new PubSub();
jioAttributeObject.pubsub = new PubSub();
// end Publisher Subcriber
////////////////////////////////////////////////////////////////////////////
......@@ -154,93 +178,127 @@
};
var JobQueue = function () {
// restore job list from local storage, if it exists
this.jobObject = jioAttributeObject.localStorage.getItem(
'jio/jobObject/' + jioAttributeObject.tabid);
if (!this.jobObject) this.jobObject = {};
//// set first job ID
console.log ('Queue');
this.jobid = 1;
var localjoblist = getJobArrayFromLocalStorage();
if (localjoblist.length > 0) {
this.jobid = localjoblist[localjoblist.length - 1].id + 1;
};
localjoblist = undefined; // it is not necessary to fill memory with
for (var id in this.jobObject) {
if ( this.jobObject[id].id > this.jobid ) {
this.jobid = this.jobObject[id].id;
}
}
this.jobid++;
// reset all jobs' status to initial
this.resetAll();
var t = this;
var returnedJobAnalyse = function ( job ) {
// analyse the [o.job]
// analyse the [job]
//// analysing job method
if (!attr.jobMethodObject[job.method])
// if the job method does not exists, return false
if (!jioAttributeObject.jobMethodObject[job.method])
return false;
if (!ifRemainingJobs(job.method)) {
var statusobject = getStatusObjectFromLocalStorage();
statusobject[job.method] = 'done';
setStatusObjectToLocalStorage(statusobject);
// if there isn't some job to do, then send stop event
if (!t.isThereJobsWhere(function(testjob){
return (testjob.method === job.method &&
testjob.status !== 'fail');
})) {
switch (job.method) {
// TODO merging is done with jio? or by the user's scripts?
case 'isAvailable':
// TODO merge name availability between storages
// TODO send job ?
$.jio('publish',{'event': attr.jobMethodObject[
job.method].stop_event, 'job':job});
$.jio('publish',{'event':
jioAttributeObject.jobMethodObject[
job.method].stop_event, 'job':job});
return;
case 'getList':
// TODO merge list if necessary (we can create
// this.getList, deleted after sending the event,
// filled with done event) -> merge here or show
// possible actions (manually, auto, ...)
$.jio('publish',{'event': attr.jobMethodObject[
job.method].stop_event , 'job':job});
$.jio('publish',{'event':
jioAttributeObject.jobMethodObject[
job.method].stop_event , 'job':job});
///*, 'retvalue': merged list */});
// delete list
return;
default:
$.jio('publish',{'event': attr.jobMethodObject[
job.method].stop_event});
$.jio('publish',{'event':
jioAttributeObject.jobMethodObject[
job.method].stop_event});
return;
}
}
//// end analyse
};
}; // end returnedJobAnalyse
//// sebscribe an event
var t = this;
$.jio('subscribe',{'event':'job_done','func':function (o) {
if (methods.isReady()) {
t.done(o.job);
returnedJobAnalyse (o.job);
}
setTimeout (function () {
if (methods.isReady()) {
t.done(o.job);
returnedJobAnalyse (o.job);
t.setJobObjectToLocalStorage();
}
},50);
}});
$.jio('subscribe',{'event':'job_fail','func':function (o) {
if (methods.isReady()) {
t.fail(o.job);
returnedJobAnalyse (o.job);
}
setTimeout (function () {
if (methods.isReady()) {
t.fail(o.job);
returnedJobAnalyse (o.job);
t.setJobObjectToLocalStorage();
}
},50);
}});
//// end subscribing
};
JobQueue.prototype = {
addJob: function ( o ) {
isThereJobsWhere: function( func ) {
// check if there is jobs where [func](job) == true
if (!func)
return true;
for (var id in this.jobObject) {
if (func(this.jobObject[id]))
return true;
}
return false;
},
setJobObjectToLocalStorage: function () {
// set the jobs to the local storage
return jioAttributeObject.localStorage.setItem(
'jio/jobObject/' + jioAttributeObject.tabid,
this.jobObject);
},
addJob: function ( options ) {
// Add a job to the Job list
// o.storage : the storage object
// o.job : the job object (may be a 'string')
// options.storage : the storage object
// options.job : the job object (may be a 'string')
console.log ('addJob');
//// Adding job to the list in localStorage
// transforming job/storage to an object
try { o.job = JSON.parse(o.job); } catch (e) {};
try { o.storage = JSON.parse(o.storage); } catch (e) {};
// create joblist in local storage if unset
if (!localStorage.joblist) localStorage.joblist = "[]";
if (typeof options.job === 'string')
options.job = JSON.parse(options.job);
if (typeof options.storage === 'string')
options.storage=JSON.parse(options.storage);
// set job id
o.job.id = this.jobid;
options.job.id = this.jobid;
this.jobid ++;
// save the new job
var localjoblist = getJobArrayFromLocalStorage();
localjoblist.push(o.job);
setJobArrayToLocalStorage (localjoblist);
//// job added
this.jobObject[this.jobid] = options.job;
// save into localStorage
this.setJobObjectToLocalStorage();
}, // end addJob
removeJob: function ( options ) {
......@@ -250,34 +308,30 @@
console.log ('removeJob');
//// set tests functions
var settings = $.extend ({'where':function (j) {return true;}},
var settings = $.extend ({'where':function (job) {return true;}},
options);
var andwhere ;
if (settings.job) {
andwhere = function (j) {return (j.id === settings.job.id);};
andwhere = function (job) {return (job.id===settings.job.id);};
} else {
andwhere = function (j) {return true;};
andwhere = function (job) {return true;};
}
//// end set tests functions
//// modify the job list
var jobarray = getJobArrayFromLocalStorage();
var newjobarray = [];
var found = false;
// fill now jobarray with the
for (var k in jobarray) {
if ( settings.where(jobarray[k]) && andwhere(jobarray[k]) ) {
for (var k in this.jobObject) {
if (settings.where(this.jobObject[k]) &&
andwhere(this.jobObject[k]) ) {
delete this.jobObject[k];
found = true;
} else {
newjobarray.push ( jobarray[k] );
}
}
if (found) {
setJobArrayToLocalStorage (newjobarray);
} else {
$.error ('Job not found, when trying to remove one.');
if (!found) {
$.error ('No jobs was found, when trying to remove some.');
}
//// end modifying
this.setJobObjectToLocalStorage();
console.log ('end removeJob');
},
......@@ -285,27 +339,21 @@
resetAll: function () {
// reset All job to 'initial'
console.log ('resetAll');
var jobarray = getJobArrayFromLocalStorage();
for (var i in jobarray) {
jobarray[i].status = 'initial';
for (var id in this.jobObject) {
this.jobObject[id].status = 'initial';
}
setJobArrayToLocalStorage(jobarray);
console.log ('end resetAll');
this.setJobObjectToLocalStorage();
},
invokeAll: function () {
// Do all jobs in the list
//// do All jobs
var jobarray = getJobArrayFromLocalStorage();
for (var i in jobarray) {
if (jobarray[i].status === 'initial') {
jobarray[i].status = 'ongoing';
this.invoke(jobarray[i]);
for (var i in this.jobObject) {
if (this.jobObject[i].status === 'initial') {
this.invoke(this.jobObject[i]);
}
}
setJobArrayToLocalStorage(jobarray);
this.setJobObjectToLocalStorage();
//// end, continue doing jobs asynchronously
},
......@@ -315,22 +363,26 @@
console.log ('invoke');
//// analysing job method
// browsing methods
if (!attr.jobMethodObject[job.method])
if (!jioAttributeObject.jobMethodObject[job.method])
// TODO do an appropriate error reaction ? unknown job method
return false;
var statusobject = getStatusObjectFromLocalStorage ();
if (statusobject[job.method] === 'done') {
statusobject[job.method] = 'ongoing';
setStatusObjectToLocalStorage(statusobject);
if (!this.isThereJobsWhere(function (testjob){
return (testjob.method === job.method &&
testjob.method === 'ongoing');
})) {
job.status = 'ongoing';
$.jio('publish',
{'event':attr.jobMethodObject[job.method].start_event,
{'event':
jioAttributeObject.jobMethodObject[
job.method].start_event,
'job':job});
} else {
job.status = 'ongoing';
}
// create an object and use it to save,load,...!
createStorageObject({'storage':job.storage,
'applicant':attr.applicant})[
attr.jobMethodObject[job.method].func
](job);
createStorageObject(
{'storage':job.storage,
'applicant':jioAttributeObject.applicant})[job.method](job);
//// end method analyse
console.log ('end invoke');
},
......@@ -344,27 +396,18 @@
fail: function (job) {
// This job cannot be done, change its status into localStorage.
var jobarray = getJobArrayFromLocalStorage ();
//// find this unique job inside the list
for (var i in jobarray) {
if (jobarray[i].id === job.id) {
jobarray[i] = job;
break;
}
}
this.jobObject[job.id] = job;
// save to local storage
setJobArrayToLocalStorage (jobarray);
this.setJobObjectToLocalStorage ();
},
clean: function () {
// Clean the job list, removing all jobs that have failed.
// It also change the localStorage Job list
this.removeJob (undefined,
{'where':
function (j) {
return (j.status === 'fail');
} });
{'where':function (job) {
return (job.status === 'fail');
} });
}
};
// end Job & JobQueue
......@@ -389,9 +432,10 @@
if (!this.id) {
this.id = setInterval (function () {
// if there are jobs
if (localStorage.joblist &&
localStorage.joblist !== '[]') {
attr.queue.invokeAll();
if (jioAttributeObject.localStorage.getItem(
'jio/jobObject/'+jioAttributeObject.tabid) &&
localStorage.joblist !== '{}') {
jioAttributeObject.queue.invokeAll();
}
},this.interval);
console.log ('listener started');
......@@ -422,13 +466,6 @@
return false;
}
console.log ('initializing');
// check dependencies
if (!supportLocalStorage()) {
alert ('Your browser does not support local storage. '+
'$.jio cannot be initialized.');
$.error ('localStorage not supported');
return false;
}
// check settings
if (!settings.storage ||
!settings.applicant) {
......@@ -436,18 +473,19 @@
return false;
}
// objectify settings if there are strings
try { attr.storage = JSON.parse(settings.storage); }
catch (e) { attr.storage = settings.storage; }
try { attr.applicant = JSON.parse(settings.applicant); }
catch (e) { attr.applicant = settings.applicant; }
try {jioAttributeObject.storage=JSON.parse(settings.storage);}
catch(e){jioAttributeObject.storage=settings.storage;}
try{jioAttributeObject.applicant=JSON.parse(settings.applicant);}
catch(e){jioAttributeObject.applicant=settings.applicant;}
// check if key exists in attr.storageTypeObject
// check if key exists in jioAttributeObject.storageTypeObject
if (!settings.storage.type) {
$.error ('Storage incomplete.');
return false;
}
if (!attr.storageTypeObject[settings.storage.type]) {
$.error ('Unknown storage type "' + settings.storage.type + '".');
if(!jioAttributeObject.storageTypeObject[settings.storage.type]) {
$.error ('Unknown storage type "' +
settings.storage.type + '".');
return false;
}
......@@ -455,47 +493,61 @@
// set a tab id to every jobs ?
// see todo from methods.close
//attr.pubsub = new PubSub();
if (!attr.queue)
attr.queue = new JobQueue();
if (!attr.listener)
attr.listener = new JobListener();
attr.listener.start();
attr.isReady = true;
// set tab id
var localStor = jioAttributeObject.localStorage.getAll();
for (var key in localStor) {
var splitedkey = key.split('/');
if (splitedkey[0] === 'jio' && splitedkey[1] === 'tab' &&
JSON.parse(splitedkey[2]) > jioAttributeObject.tabid) {
jioAttributeObject.tabid = JSON.parse(splitedkey[2]);
}
}
jioAttributeObject.tabid ++;
//jioAttributeObject.pubsub = new PubSub();
if (!jioAttributeObject.queue)
jioAttributeObject.queue = new JobQueue();
if (!jioAttributeObject.listener)
jioAttributeObject.listener = new JobListener();
jioAttributeObject.listener.start();
jioAttributeObject.isReady = true;
},
isReady: function () {
// check if jio is ready
return attr.isReady;
return jioAttributeObject.isReady;
},
addStorageType: function ( options ) {
var settings = $.extend({},options);
// add LocalStorage to storage object
if (settings.type && settings.creator) {
attr.storageTypeObject [settings.type] = settings.creator;
jioAttributeObject.storageTypeObject[
settings.type] = settings.creator;
return true;
}
return false;
},
getApplicant: function () {
// return applicant
return attr.applicant;
return jioAttributeObject.applicant;
},
publish: function ( options ) {
if (!methods.isReady()) return null;
console.log ('publish ' + options.event);
var tmp = $.extend({},options);
tmp.event = undefined;
attr.pubsub.publish(options.event,tmp);
jioAttributeObject.pubsub.publish(options.event,tmp);
},
subscribe: function ( options ) {
console.log ('subscribe');
attr.pubsub.subscribe(options.event,options.func);
jioAttributeObject.pubsub.subscribe(options.event,options.func);
},
unsubscribe: function ( options ) {
console.log ('unsubscribe');
attr.pubsub.unsubscribe(options.event);
jioAttributeObject.pubsub.unsubscribe(options.event);
},
doMethod: function ( options ) {
// $.jio({'fileName':'a','fileContent':'b','method':'save'}
if (options.method) {
if (methods[options.method]) {
......@@ -505,83 +557,88 @@
}
}
},
isAvailable: function ( options ) {
// $.jio('isAvailable',{'userName':'toto'});
checkUserNameAvailability: function ( options ) {
// $.jio('checkUserNameAvailability',{'userName':'toto'});
console.log ('isAvailable');
if (!methods.isReady()) return null;
var settings = $.extend ({
'userName': attr.storage.userName,
'storage': attr.storage,
'applicant': attr.applicant,
'method': 'isAvailable'
'userName': jioAttributeObject.storage.userName,
'storage': jioAttributeObject.storage,
'applicant': jioAttributeObject.applicant,
'method': 'checkUserNameAvailability'
},options);
// check dependencies
if (settings.userName) {
return attr.queue.addJob ( {'job':(new Job ( settings ))} ) ;
return jioAttributeObject.queue.addJob (
{'job':(new Job ( settings ))} ) ;
}
return null;
},
save: function ( options ) {
// $.jio('save',{'fileName':'a','fileContent':'b','options':{
saveDocument: function ( options ) {
// $.jio('saveDocument',{'fileName':'a','fileContent':'b','options':{
// 'overwrite':false}}
console.log ('save');
console.log ('saveDocument');
if (!methods.isReady()) return null;
var settings = $.extend({
'storage': attr.storage,
'applicant': attr.applicant,
'storage': jioAttributeObject.storage,
'applicant': jioAttributeObject.applicant,
'lastModified': Date.now(),
'method':'save'
'method':'saveDocument'
},options);
// check dependencies
if (settings.fileName && settings.fileContent) {
return attr.queue.addJob ( {'job':(new Job ( settings ))} ) ;
return jioAttributeObject.queue.addJob (
{'job':(new Job ( settings ))} ) ;
}
return null;
},
load: function ( options ) {
// $.jio('load',{'fileName':'a'});
loadDocument: function ( options ) {
// $.jio('loadDocument',{'fileName':'a'});
console.log ('load');
if (!methods.isReady()) return null;
var settings = $.extend ({
'storage':attr.storage,
'applicant':attr.applicant,
'method':'load'
'storage':jioAttributeObject.storage,
'applicant':jioAttributeObject.applicant,
'method':'loadDocument'
},options);
// check dependencies
if ( settings.fileName ) {
return attr.queue.addJob ( {'job':(new Job ( settings ))} ) ;
return jioAttributeObject.queue.addJob (
{'job':(new Job ( settings ))} ) ;
}
return null;
},
getList: function ( options ) {
// $.jio('getList');
getDocumentList: function ( options ) {
// $.jio('getDocumentList');
console.log ('getList');
if (!methods.isReady()) return null;
var settings = $.extend ({
'storage': attr.storage,
'applicant': attr.applicant,
'method':'getList'
'storage': jioAttributeObject.storage,
'applicant': jioAttributeObject.applicant,
'method':'getDocumentList'
},options);
return attr.queue.addJob ( {'job':(new Job ( settings ))} );
return jioAttributeObject.queue.addJob (
{'job':(new Job ( settings ))} );
},
remove: function ( options ) {
// $.jio('remove',{'fileName':'a'});
console.log ('remove');
removeDocument: function ( options ) {
// $.jio('removeDocument',{'fileName':'a'});
console.log ('removeDocument');
if (!methods.isReady()) return null;
var settings = $.extend ({
'storage': attr.storage,
'applicant': attr.applicant,
'method':'remove'
'storage': jioAttributeObject.storage,
'applicant': jioAttributeObject.applicant,
'method':'removeDocument'
},options);
if ( settings.fileName ) {
return attr.queue.addJob ( {'job':(new Job( settings ))} );
return jioAttributeObject.queue.addJob (
{'job':(new Job( settings ))} );
}
return null;
},
clean: function ( options ) {
// clean the job, removing all job that have failed.
attr.queue.clean();
},
close: function ( options ) {
// finish some job if possible and close jio.
// it can be re-init later.
......@@ -590,8 +647,11 @@
// page, $.jio('close') will close tab id (if any) to free its jobs,
// so that other tabs may do them.
attr.listener.stop();
attr.isReady = false;
jioAttributeObject.listener.stop();
jioAttributeObject.isReady = false;
},
getJioAttributes: function ( options ) {
return $.extend({},jioAttributeObject);
}
};
// end JIO methods
......@@ -610,183 +670,4 @@
// end JIO arguments manager
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Local Storage
var LocalStorage = function ( options ) {
this.userName = options.storage.userName;
// check user's localStorage
if(!localStorage[this.userName]) {
localStorage[this.userName] = '{}'; // create user
}
};
LocalStorage.prototype = {
readDocumentsFromLocalStorage: function () {
return JSON.parse (localStorage[this.userName]);
},
writeDocumentsToLocalStorage: function (documents) {
localStorage[this.userName] = JSON.stringify(documents);
},
checkNameAvailability: function ( job ) {
// wait a little in order to simulate asynchronous operation
setTimeout(function () {
if (localStorage[job.userName]) {
job.status = 'done';
job.message = ''+ job.userName + ' is not available.';
job.isAvailable = false;
$.jio('publish',{'event': 'job_done',
'job': job});
} else {
job.status = 'done';
job.message = ''+ job.userName + ' is available.';
job.isAvailable = true;
$.jio('publish',{'event': 'job_done',
'job': job});
}
}, 100);
}, // end userNameAvailable
saveDocument: function ( job ) {
// Save a document in the local storage
// job : the job object
// job.options : the save options
// job.options.overwrite : true -> overwrite
// job.options.force : true -> save even if jobdate < existingdate
// or overwrite: false
var settings = $.extend({'overwrite':true,
'force':false},job.options);
var t = this;
// wait a little in order to simulate asynchronous saving
setTimeout (function () {
var documents = t.readDocumentsFromLocalStorage();
if (!documents[job.fileName]) { // create document
documents[job.fileName] = {
'fileName': job.fileName,
'fileContent': job.fileContent,
'creationDate': Date.now (),
'lastModified': Date.now ()
}
t.writeDocumentsToLocalStorage(documents);
job.status = 'done';
job.message = 'Document saved.';
job.isSaved = true;
$.jio('publish',{'event':'job_done',
'job':job});
return true;
}
if ( settings.overwrite || settings.force ) { // overwrite
if ( ! settings.force ) { // force write
// checking modification date
if ( ! documents[
job.fileName].lastModified < job.lastModified ) {
// date problem!
job.status = 'fail';
job.message = 'Modification date is earlier than ' +
'existing modification date.';
job.isSaved = false;
$.jio('publish',{'event':'job_done',
'job':job});
return false;
}
}
documents[job.fileName].lastModified = Date.now();
documents[job.fileName].fileContent = job.fileContent;
t.writeDocumentsToLocalStorage(documents);
job.status = 'done';
job.message = 'Document saved';
job.isSaved = true;
$.jio('publish',{'event':'job_done',
'job':job});
return true;
}
// already exists
job.status = 'fail';
job.message = 'Document already exists.';
job.errno = 403;
job.isSaved = false;
$.jio('publish',{'event':'job_fail',
'job': job});
return false;
}, 100);
}, // end saveDocument
loadDocument: function ( job ) {
// load a document in the storage, copy the content into the job
// job : the job
var t = this;
// wait a little in order to simulate asynchronous operation
setTimeout(function () {
var documents = t.readDocumentsFromLocalStorage();
if (!documents[job.fileName]) {
job.status = 'fail';
job.errno = 404;
job.message = 'Document not found.';
$.jio('publish',{'event':'job_fail',
'job':job});
} else {
job.status = 'done';
job.message = 'Document loaded.';
job.fileContent = documents[job.fileName].fileContent;
$.jio('publish',{'event':'job_done',
'job':job});
}
}, 100);
}, // end loadDocument
getDocumentList: function (job) {
var t = this;
setTimeout(function () {
var documents = t.readDocumentsFromLocalStorage();
job.list = [];
for (var k in documents) {
job.list.push ({
'fileName':documents[k].fileName,
'creationDate':documents[k].creationDate,
'lastModified':documents[k].lastModified});
}
job.status = 'done';
job.message = 'List received.';
$.jio('publish',{'event':'job_done',
'job':job});
}, 100);
}, // end getDocumentList
removeDocument: function () {
var t = this;
setTimeout (function () {
var documents = t.readDocumentsFromLocalStorage();
if (!documents[job.fileName]) {
// job.status = 'fail';
// job.errno = 404;
// job.message = 'Document not found.';
// job.isRemoved = false;
// $.jio('publish',{'event':'job_fail',
// 'job':job});
job.status = 'done';
job.message = 'Document already removed.';
job.isRemoved = true;
$.jio('publish',{'event':'job_done',
'job':job});
} else {
delete documents[job.fileName];
t.writeToLocalStorage(documents);
job.status = 'done';
job.message = 'Document removed.';
job.isRemoved = true;
$.jio('publish',{'event':'job_done',
'job':job});
}
}, 100);
}
};
// add key to storageObject
methods.addStorageType({'type':'local','creator':function (o) {
return new LocalStorage(o);
}});
// end Local Storage
////////////////////////////////////////////////////////////////////////////
})( jQuery );
;(function ( $ ) {
// TODO do we realy need to check dependencies ?
// test dependencies
try {
if (Base64) {}
if ($.jio && Base64) {}
else {return false;}
} catch (e) {
$.error('Base64 is required by jio.storage.js');
$.error('jio.js and base64.js '+
' are required by jio.storage.js');
return false;
};
////////////////////////////////////////////////////////////////////////////
// private vars
var attr = {
'jobMethodObject': {
'available': {
'start_event':'start_testingNameAvailability',
'stop_event':'stop_testingNameAvailability',
'func':'checkNameAvailability',
'retvalue':'isAvailable' }, // returns 'boolean'
'save': {
'start_event':'start_saving',
'stop_event':'stop_saving',
'func':'saveDocument',
'retvalue':'isSaved' }, // returns 'boolean'
'load': {
'start_event':'start_loading',
'stop_event':'stop_loading',
'func':'loadDocument',
'retvalue':'fileContent' }, // returns the file content 'string'
'getList': {
'start_event':'start_gettingList',
'stop_event':'stop_gettingList',
'func':'getDocumentList',
'retvalue':'list' }, // returns the document list 'array'
'remove': {
'start_event':'start_removing',
'stop_event':'stop_removing',
'func':'removeDocument',
'retvalue':'isRemoved' } // returns 'boolean'
}
};
var jioAttributeObject = $.jio('getJioAttributes');
// end private vars
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Tools
var objectDump = function (o) {
// TODO DEBUG we can remove this function
console.log (JSON.stringify(o));
// end Tools
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Local Storage
var LocalStorage = function ( options ) {
// LocalStorage constructor
// initializes the local storage for jio and create user if necessary.
this.userName = options.storage.userName;
};
var toString = function (o) {
// TODO DEBUG we can remove this function
return (JSON.stringify(o));
LocalStorage.prototype = {
checkNameAvailability: function ( job ) {
// checks the availability of the [job.userName].
// if the name already exists, it is not available.
// job: the job object
// job.userName: the name we want to check.
// wait a little in order to simulate asynchronous operation
setTimeout(function () {
var available = true;
var localStor = jioAttributeObject.localStorage.getAll();
for (var k in localStor) {
var splitk = k.split('/');
if (splitk[0] === 'jio' && splitk[1] === job.userName) {
available = false;
break;
}
}
if (!available) {
job.status = 'done';
job.message = ''+ job.userName + ' is not available.';
job.isAvailable = false;
$.jio('publish',{'event': 'job_done',
'job': job});
} else {
job.status = 'done';
job.message = ''+ job.userName + ' is available.';
job.isAvailable = true;
$.jio('publish',{'event': 'job_done',
'job': job});
}
}, 100);
}, // end userNameAvailable
saveDocument: function ( job ) {
// Save a document in the local storage
// job : the job object
// job.options : the save options object
// job.options.overwrite : true -> overwrite
// job.options.force : true -> save even if jobdate < existingdate
// or overwrite: false
var settings = $.extend({'overwrite':true,
'force':false},job.options);
var t = this;
// wait a little in order to simulate asynchronous saving
setTimeout (function () {
// reading
var doc = jioAttributeObject.localStorage.getItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName);
if (!doc) { // create document
doc = {
'fileName': job.fileName,
'fileContent': job.fileContent,
'creationDate': Date.now (),
'lastModified': Date.now ()
}
// writing
jioAttributeObject.localStorage.setItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName, doc);
job.status = 'done';
job.message = 'Document saved.';
job.isSaved = true;
$.jio('publish',{'event':'job_done',
'job':job});
return true;
}
if ( settings.overwrite || settings.force ) { // overwrite
if ( ! settings.force ) { // force write
// checking modification date
if ( doc.lastModified >= job.lastModified ) {
// date problem!
job.status = 'fail';
job.message = 'Modification date is earlier than ' +
'existing modification date.';
job.isSaved = false;
$.jio('publish',{'event':'job_fail',
'job':job});
return false;
}
}
doc.lastModified = Date.now();
doc.fileContent = job.fileContent;
// writing
jioAttributeObject.localStorage.setItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName, doc);
job.status = 'done';
job.message = 'Document saved';
job.isSaved = true;
$.jio('publish',{'event':'job_done',
'job':job});
return true;
}
// already exists
job.status = 'fail';
job.message = 'Document already exists.';
job.errno = 403;
job.isSaved = false;
$.jio('publish',{'event':'job_fail',
'job': job});
return false;
}, 100);
}, // end saveDocument
loadDocument: function ( job ) {
// load a document in the storage, copy the content into the job
// job : the job
var t = this;
// wait a little in order to simulate asynchronous operation
setTimeout(function () {
var doc = jioAttributeObject.localStorage.getItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName);
if (!doc) {
job.status = 'fail';
job.errno = 404;
job.message = 'Document not found.';
$.jio('publish',{'event':'job_fail',
'job':job});
} else {
job.status = 'done';
job.message = 'Document loaded.';
job.fileContent = doc.fileContent;
$.jio('publish',{'event':'job_done',
'job':job});
}
}, 100);
}, // end loadDocument
getDocumentList: function (job) {
var t = this;
setTimeout(function () {
var localStor = jioAttributeObject.localStorage.getAll();
job.list = [];
for (var k in localStor) {
var splitk = k.split('/');
if (splitk[0] === 'jio' &&
splitk[1] === 'local' &&
splitk[2] === job.storage.userName &&
splitk[3] === job.applicant.ID) {
// TODO error here
console.log (JSON.stringify (localStor[k]));
job.list.push ({
'fileName':localStor[k].fileName,
'creationDate':localStor[k].creationDate,
'lastModified':localStor[k].lastModified});
}
}
console.log (JSON.stringify (job.list));
job.status = 'done';
job.message = 'List received.';
$.jio('publish',{'event':'job_done',
'job':job});
}, 100);
}, // end getDocumentList
removeDocument: function ( job ) {
var t = this;
setTimeout (function () {
var doc = jioAttributeObject.localStorage.getItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName);
if (!doc) {
// job.status = 'fail';
// job.errno = 404;
// job.message = 'Document not found.';
// job.isRemoved = false;
// $.jio('publish',{'event':'job_fail',
// 'job':job});
job.status = 'done';
job.message = 'Document already removed.';
job.isRemoved = true;
$.jio('publish',{'event':'job_done',
'job':job});
} else {
jioAttributeObject.localStorage.deleteItem(
'jio/local/'+
job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName);
job.status = 'done';
job.message = 'Document removed.';
job.isRemoved = true;
$.jio('publish',{'event':'job_done',
'job':job});
}
}, 100);
}
};
// end Tools
// add key to storageObject
$.jio('addStorageType',{'type':'local','creator':function (options) {
return new LocalStorage(options);
}});
// end Local Storage
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
......@@ -280,11 +456,11 @@
$("D\\:response",xmlData).each(function(i,data) {
if(i>0) { // exclude parent folder
var file = {};
var nt = ($($("D\\:href",
var pathArray = ($($("D\\:href",
xmlData).get(i)).text()).split('/');
file.fileName = (nt[nt.length-1] ?
nt[nt.length-1] :
nt[nt.length-2]+'/');
file.fileName = (pathArray[pathArray.length-1] ?
pathArray[pathArray.length-1] :
pathArray[pathArray.length-2]+'/');
if (file.fileName === '.htaccess' ||
file.fileName === '.htpasswd')
return;
......@@ -352,8 +528,8 @@
};
// add key to storageObject
$.jio('addStorageType',{'type':'dav','creator':function (o) {
return new DAVStorage(o);
$.jio('addStorageType',{'type':'dav','creator':function (options) {
return new DAVStorage(options);
}});
// end DAVStorage
......
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