Commit f32b249f authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

Events are no longer necessary to use JIO, and doesn't exist anymore (for now).

Publisher, Subscriber and Unsubscriber works.
We can now create most than one instance of JIO with "var j=JIO.createNew()".
Doc strings can realy help us to use JIO now.
Qunit tests for localStorage are not completly finished yet.
DavStorage have to be modified a little to make it complete.
ReplicateStorage does not exist yet.
parent 741a9b4f
...@@ -15,24 +15,28 @@ var globalID = setInterval (function (){ ...@@ -15,24 +15,28 @@ var globalID = setInterval (function (){
case 1: globalTestIndex = globalIndex; case 1: globalTestIndex = globalIndex;
test ( "Jio simple methods", function () { test ( "Jio simple methods", function () {
ok ( $.jio('addStorageType',{'type':'qunit','creator':function(){}}) var ca = $.Callbacks();
, "storage type qunit added."); console.log (ca);
var tmp = $.jio('isReady'); o.jio = JIO.createNew();
ok ( tmp === false, 'isReady === ' +tmp); ok ( o.jio, 'a new jio -> 1');
var tmp = $.jio({'storage':{'type':'local','userName':'myName'}, o.jio2 = JIO.createNew();
'applicant':{'ID':'myID'}}); ok ( o.jio2, 'another new jio -> 2');
ok ( tmp === true, 'initialized ? ' + tmp ); ok ( JIO.addStorageType('qunit', function(){}) ,
var tmp = $.jio('isReady'); "adding storage type.");
ok ( tmp === true, 'isReady === ' +tmp); deepEqual ( o.jio.isReady(), false, '1 must be not ready');
var tmp = $.jio('getApplicant'); deepEqual ( o.jio.start(), true, '1 must be ready now');
deepEqual ( tmp , {'ID':'myID'}, o.jio2.start();
'get the good applicant.' ); ok ( o.jio2.id !== o.jio.id, '1 and 2 must be different');
deepEqual ( o.jio.stop(), true, '1 must be stopped');
o.jio2.stop();
globalIndex++; globalIndex++;
}); });
break; break;
case 2: globalTestIndex = globalIndex; case 2: globalTestIndex = globalIndex;
o.local = {}; o.local = {};
o.local.jio = JIO.createNew();
o.local.jio.start();
test ( 'Jio Pub/Sub methods', function () { test ( 'Jio Pub/Sub methods', function () {
stop(); stop();
var i = 1, ti = 0, m = 10; var i = 1, ti = 0, m = 10;
...@@ -43,14 +47,12 @@ var globalID = setInterval (function (){ ...@@ -43,14 +47,12 @@ var globalID = setInterval (function (){
break; break;
case 1: // if(i===ti)return;ti=i; // force do only one time case 1: // if(i===ti)return;ti=i; // force do only one time
o.local.pubsub_test = false; o.local.pubsub_test = false;
$.jio('subscribe', o.local.pubsub_callback = o.local.jio.subscribe(
{'event':'pubsub_test', 'func': 'pubsub_test',function () {
function () { o.local.pubsub_test = true;
o.local.pubsub_test = true; });
}}); o.local.jio.publish('pubsub_test');
$.jio('publish',{'event':'pubsub_test'}); i++; break;
i++;
break;
case 5: // wait a little case 5: // wait a little
deepEqual (o.local.pubsub_test, true, deepEqual (o.local.pubsub_test, true,
'subscribe & publish'); 'subscribe & publish');
...@@ -63,16 +65,19 @@ var globalID = setInterval (function (){ ...@@ -63,16 +65,19 @@ var globalID = setInterval (function (){
break; break;
case 6: case 6:
o.local.pubsub_test = false; o.local.pubsub_test = false;
$.jio('unsubscribe',{'event':'pubsub_test'}); o.local.jio.unsubscribe('pubsub_test',
$.jio('publish',{'event':'pubsub_test'}); o.local.pubsub_callback);
o.local.jio.publish('pubsub_test');
i ++; i ++;
break; break;
case 9: // wait a little case 9: // wait a little
deepEqual (o.local.pubsub_test, false, deepEqual (o.local.pubsub_test, false,
'unsubscribe'); 'unsubscribe');
o.local.pubsub_test = !o.local.pubsub_test;
i++; i++;
break; break;
case m: case m:
o.local.jio.stop();
start(); start();
globalIndex++; globalIndex++;
clearInterval(id); clearInterval(id);
...@@ -84,199 +89,90 @@ var globalID = setInterval (function (){ ...@@ -84,199 +89,90 @@ var globalID = setInterval (function (){
case 3: globalTestIndex = globalIndex; case 3: globalTestIndex = globalIndex;
test ( 'LocalStorage' , function () { test ( 'LocalStorage' , function () {
stop(); stop();
var i = 1, ti = 0, m = 31; var i = 0, ti = -1, m = 20;
var id = setInterval(function (){ var id = setInterval(function (){
switch(i){ switch(i){
default: default:
i++; i++;
break; break;
case 1: case 0:
o.local.start_check = false; o.local.jio = JIO.createNew({
o.local.stop_check = false; 'type':'local',
o.local.ret_check = false; 'userName':'myName'
o.start_check_func = function () { },{'ID':'myApp'});
o.local.start_check = true; o.local.jio.start();
}; i++;
o.stop_check_func = function (e) { break;
o.local.ret_check = e.job.isAvailable; case 1:i++;
o.local.stop_check = true; //// test check name
}; o.local.check_test = null;
$.jio('subscribe',{'event':'start_checkingNameAvailability', o.local.jio.checkNameAvailability(
'func':function (e) { {'userName':'myName','callback': function (result) {
o.start_check_func(e); o.local.check_test = result.isAvailable;
}}) i = 3;
$.jio('subscribe',{'event':'stop_checkingNameAvailability', }});
'func':function (e) { break;
o.stop_check_func(e); case 2: break;
}}) case 3: i++;
$.jio('checkNameAvailability',{'userName':'myName'}); deepEqual (o.local.check_test, true,
i++; 'name must be available');
break; //// test save document
case 5: // wait a little o.local.jio.save_test = null;
deepEqual ( [o.local.start_check, o.local.jio.saveDocument(
o.local.stop_check, {'fileName':'file','fileContent':'content','callback':
o.local.ret_check], function (result) {
[true,true,true], o.local.save_test = result.isSaved;
'checking name availability'); i = 5;
i++; }});
break; break;
case 6: case 4: break;
o.local.start_save = false; case 5: i++;
o.local.stop_save = false; deepEqual (o.local.save_test, true,
o.local.ret_save = false; 'document must be saved');
o.start_save_func = function () { //// test check name
o.local.start_save = true; o.local.check_test = null;
}; o.local.jio.checkNameAvailability(
o.job_done_func = function (e) { {'userName':'myName','callback': function (result) {
o.local.ret_save = e.job.isSaved; o.local.check_test = result.isAvailable;
}; i = 7;
o.stop_save_func = function () { }});
o.local.stop_save = true; break;
}; case 6: break;
$.jio('subscribe',{'event':'start_saving', case 7: i++;
'func':function (e) { deepEqual (o.local.check_test, false,
o.start_save_func(e); 'name must be unavailable');
}}) //// test get list
$.jio('subscribe',{'event':'job_done', o.local.getlist_test = null;
'func':function(e){ o.local.jio.getDocumentList(
o.job_done_func(e);} {'callback': function (result) {
}); o.local.getlist_test = result.list;
$.jio('subscribe',{'event':'stop_saving', i = 9;
'func':function (e) { }});
o.stop_save_func(e); break;
}}) case 8: break;
$.jio('saveDocument',{'fileName':'file', case 9: i++;
'fileContent':'content'}); if (o.local.getlist_test) {
i++; delete o.local.getlist_test[0].lastModified;
break; delete o.local.getlist_test[0].creationDate;
case 10: // wait a little }
deepEqual ( [o.local.start_save, deepEqual (o.local.getlist_test,
o.local.stop_save, [{'fileName':'file'}],
o.local.ret_save], 'list must contain one file');
[true,true,true], //// test remove document
'saving'); o.local.remove_test = null;
i++; o.local.jio.removeDocument(
break; {'fileName':'file','callback': function (result) {
case 11: o.local.remove_test = result.isRemoved;
o.local.start_check = false; i = 11;
o.local.stop_check = false; }});
o.local.ret_check = true; break;
// already subscribed case 10: break;
$.jio('checkNameAvailability',{'userName':'myName'}); case 11: i++;
i++; deepEqual (o.local.remove_test, true,
break; 'file must be removed');
case 15: // wait a little
deepEqual ( [o.local.start_check,
o.local.stop_check,
o.local.ret_check],
[true,true,false],
'checking name availability');
i++;
break;
case 16:
o.local.start_load = false;
o.local.stop_load = false;
o.local.ret_load = null;
o.start_load_func = function () {
o.local.start_load = true;
};
o.job_done_func = function (e) {
o.local.ret_load = e.job.fileContent;
};
o.stop_load_func = function () {
o.local.stop_load = true;
};
$.jio('subscribe',{'event':'start_loading',
'func':function (e) {
o.start_load_func(e);
}})
$.jio('subscribe',{'event':'stop_loading',
'func':function (e) {
o.stop_load_func(e);
}})
$.jio('loadDocument',{'fileName':'file'});
i++;
break;
case 20: // wait a little
deepEqual ( [o.local.start_load,
o.local.stop_load,
o.local.ret_load],
[true,true,'content'],
'loading');
i++;
break;
case 21:
o.local.start_getlist = false;
o.local.stop_getlist = false;
o.local.ret_getlist_at_job = [];
o.local.ret_getlist_at_stop = [];
o.start_getlist_func = function () {
o.local.start_getlist = true;
};
o.job_done_func = function (e) {
o.local.ret_getlist_at_job = e.job.list;
};
o.stop_getlist_func = function (e) {
o.local.ret_getlist_at_stop = e.job.list;
o.local.stop_getlist = true;
};
$.jio('subscribe',{'event':'start_gettingList',
'func':function (e) {
o.start_getlist_func(e);
}})
$.jio('subscribe',{'event':'stop_gettingList',
'func':function (e) {
o.stop_getlist_func(e);
}})
$.jio('getDocumentList');
i++;
break;
case 25: // wait a little
var expected = JSON.parse(
localStorage['jio/local/myName/myID/file']);
delete expected.fileContent;
expected = [expected];
deepEqual ( [o.local.start_getlist,
o.local.stop_getlist,
o.local.ret_getlist_at_job,
o.local.ret_getlist_at_stop],
[true,true,expected,expected],
'get list');
i++;
break;
case 26:
o.local.start_remove = false;
o.local.stop_remove = false;
o.local.ret_remove = false;
o.start_remove_func = function () {
o.local.start_remove = true;
};
o.job_done_func = function (e) {
o.local.ret_remove = e.job.isRemoved;
};
o.stop_remove_func = function () {
o.local.stop_remove = true;
};
$.jio('subscribe',{'event':'start_removing',
'func':function (e) {
o.start_remove_func(e);
}})
$.jio('subscribe',{'event':'stop_removing',
'func':function (e) {
o.stop_remove_func(e);
}})
$.jio('removeDocument',{'fileName':'file'});
i++;
break;
case 30: // wait a little
deepEqual ( [o.local.start_remove,
o.local.stop_remove,
o.local.ret_remove],
[true,true,true],
'remove');
i++;
break; break;
case m: case m:
o.local.jio.stop();
start(); start();
globalIndex++; globalIndex++;
clearInterval(id); clearInterval(id);
......
;(function ( $ ) { ;var JIO =
(function () {
// check dependencies
var errorDependencies=function(){console.error('Cannot find jQuery.');};
try{if(!jQuery){
errorDependencies();return null;
}}catch(e){
errorDependencies();return null;}
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// constants // constants
var jioAttributeObject = { var jioConstObj = {
'jobMethodObject': { 'jobMethodObject': {
'checkNameAvailability': { 'checkNameAvailability': {
'start_event':'start_checkingNameAvailability', 'start_event':'start_checkingNameAvailability',
...@@ -25,25 +33,24 @@ ...@@ -25,25 +33,24 @@
'start_event':'start_removing', 'start_event':'start_removing',
'stop_event':'stop_removing', 'stop_event':'stop_removing',
'retvalue':'isRemoved' } // returns 'boolean' 'retvalue':'isRemoved' } // returns 'boolean'
}, }
};
// end constants
////////////////////////////////////////////////////////////////////////////
// jio globals
var jioGlobalObj = {
'localStorage': null, // where the browser stores data 'localStorage': null, // where the browser stores data
'tabid': 0, // this jio id 'queueID': 1,
'queue': null, // the job manager
'storage': null, // the storage given at init
'applicant': null, // the applicant given at init
'listener': null, // the job listener
'pubsub': null, // publisher subscriber
'isReady': false, // check if jio is ready
'storageTypeObject': {} // ex: {'type':'local','creator': fun ...} 'storageTypeObject': {} // ex: {'type':'local','creator': fun ...}
}; };
// end constants // end jio globals
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// cookies & localStorage // cookies & localStorage
var myLocalStorage = function () { var browserStorage = function () {
}; };
myLocalStorage.prototype = { browserStorage.prototype = {
getItem: function (name) { getItem: function (name) {
return JSON.parse(localStorage.getItem(name)); return JSON.parse(localStorage.getItem(name));
}, },
...@@ -102,13 +109,13 @@ ...@@ -102,13 +109,13 @@
// set good localStorage // set good localStorage
try { try {
if (localStorage.getItem) { if (localStorage.getItem) {
jioAttributeObject.localStorage = new myLocalStorage(); jioGlobalObj.localStorage = new browserStorage();
} else { } else {
jioAttributeObject.localStorage = new cookieStorage(); jioGlobalObj.localStorage = new cookieStorage();
} }
} }
catch (e) { catch (e) {
jioAttributeObject.localStorage = new cookieStorage(); jioGlobalObj.localStorage = new cookieStorage();
} }
// end cookies & localStorages // end cookies & localStorages
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
...@@ -118,49 +125,69 @@ ...@@ -118,49 +125,69 @@
var createStorageObject = function ( options ) { var createStorageObject = function ( options ) {
// Create a storage thanks to storages types set with 'addStorageType'. // Create a storage thanks to storages types set with 'addStorageType'.
if (!jioAttributeObject.storageTypeObject[ options.storage.type ]) if (!jioGlobalObj.storageTypeObject[ options.storage.type ])
return null; // error! return null; // error!
return jioAttributeObject.storageTypeObject[ return jioGlobalObj.storageTypeObject[
options.storage.type ](options); options.storage.type ](options);
}; };
var getNewQueueID = function () {
// Returns a new queueID
var localStor = jioGlobalObj.localStorage.getAll();
for (var k in localStor) {
var splitk = k.split('/');
if (splitk[0] === 'jio' &&
splitk[1] === 'id') {
if (JSON.parse(localStor[k]) < Date.now() - 10000) { // 10 sec ?
jioGlobalObj.localStorage.deleteItem(k);
}
if (JSON.parse(splitk[2]) >= jioGlobalObj.queueID) {
jioGlobalObj.queueID = JSON.parse(splitk[2]) + 1;
}
}
}
var id = jioGlobalObj.queueID;
jioGlobalObj.queueID ++;
return id;
};
// end Tools // end Tools
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Publisher Subcriber // Publisher Subcriber
var PubSub = function () { var PubSub = function () {
if (!this.eventAction) { var topics = {};
var topics = {}; this.eventAction = function (id) {
this.eventAction = function (id) { var callbacks;
var callbacks; var topic = id && topics[id];
var topic = id && topics[id]; if (!topic) {
if (!topic) { callbacks = $.Callbacks();
callbacks = $.Callbacks(); topic = {
topic = { publish: callbacks.fire,
publish: callbacks.fire, subscribe: callbacks.add,
subscribe: callbacks.add, unsubscribe: callbacks.remove
unsubscribe: callbacks.remove };
}; if (id) {
if (id) { topics[id] = topic;
topics[id] = topic;
}
} }
return topic; }
}; return topic;
} };
}; };
PubSub.prototype = { PubSub.prototype = {
publish: function (eventname,obj) { publish: function (eventname,obj) {
// publish an event
this.eventAction(eventname).publish(obj); this.eventAction(eventname).publish(obj);
}, },
subscribe: function (eventname,func) { subscribe: function (eventname,callback) {
this.eventAction(eventname).subscribe(func); // subscribe and return the callback function
this.eventAction(eventname).subscribe(callback);
return callback;
}, },
unsubscribe: function (eventname) { unsubscribe: function (eventname,callback) {
this.eventAction(eventname).unsubscribe(); // unsubscribe the callback from eventname
this.eventAction(eventname).unsubscribe(callback);
} }
}; };
jioAttributeObject.pubsub = new PubSub();
// end Publisher Subcriber // end Publisher Subcriber
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
...@@ -176,93 +203,44 @@ ...@@ -176,93 +203,44 @@
return job; return job;
}; };
var JobQueue = function () { var JobQueue = function ( publisher ) {
// JobQueue is a queue of jobs. It will regulary copy this queue // JobQueue is a queue of jobs. It will regulary copy this queue
// into localStorage to resume undone tasks. // into localStorage to resume undone tasks.
// pubsub: the publisher to use to send event
// 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 this.publisher = publisher;
console.log ('Queue');
this.jobid = 1; this.jobid = 1;
for (var id in this.jobObject) { this.jioID = 0;
if ( this.jobObject[id].id > this.jobid ) { this.jobObjectName = '';
this.jobid = this.jobObject[id].id; this.jobObject = {};
}
}
this.jobid++;
// reset all jobs' status to initial // reset all jobs' status to initial
this.resetAll(); this.resetAll();
var t = this;
var returnedJobAnalyse = function ( job ) {
// analyse the [job]
// if the job method does not exists, return false
if (!jioAttributeObject.jobMethodObject[job.method])
return false;
// 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 'checkNameAvailability':
// TODO merge name availability between storages
// TODO send job ?
$.jio('publish',{'event':
jioAttributeObject.jobMethodObject[
job.method].stop_event, 'job':job});
return;
case 'getDocumentList':
// 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':
jioAttributeObject.jobMethodObject[
job.method].stop_event , 'job':job});
///*, 'retvalue': merged list */});
// delete list
return;
default:
$.jio('publish',{'event':
jioAttributeObject.jobMethodObject[
job.method].stop_event});
return;
}
}
}; // end returnedJobAnalyse
//// sebscribe an event
var t = this;
$.jio('subscribe',{'event':'job_done','func':function (o) {
setTimeout (function () {
if (methods.isReady()) {
t.done(o.job);
returnedJobAnalyse (o.job);
t.copyJobQueueToLocalStorage();
}
},50);
}});
$.jio('subscribe',{'event':'job_fail','func':function (o) {
setTimeout (function () {
if (methods.isReady()) {
t.fail(o.job);
returnedJobAnalyse (o.job);
t.copyJobQueueToLocalStorage();
}
},50);
}});
//// end subscribing
}; };
JobQueue.prototype = { JobQueue.prototype = {
init: function ( options ) {
// initialize the JobQueue
// options.publisher : is the publisher to use to send events
// options.jioID : the jio ID
if (options.publisher)
this.publisher = publisher;
this.jioID = options.jioID;
this.jobObjectName = 'jio/jobObject/'+options.jioID;
this.jobObject = {};
this.copyJobQueueToLocalStorage();
},
close: function () {
// close the job queue.
// it also deletes from local storage only if the job list is
// empty.
if (JSON.stringify(this.jobObject) === '{}') {
jioGlobalObj.localStorage.deleteItem(this.jobObjectName);
}
},
isThereJobsWhere: function( func ) { isThereJobsWhere: function( func ) {
// Check if there is jobs, in the queue, // Check if there is jobs, in the queue,
// where [func](job) == true. // where [func](job) == true.
...@@ -278,28 +256,19 @@ ...@@ -278,28 +256,19 @@
copyJobQueueToLocalStorage: function () { copyJobQueueToLocalStorage: function () {
// Copy job queue into localStorage. // Copy job queue into localStorage.
return jioAttributeObject.localStorage.setItem( return jioGlobalObj.localStorage.setItem(
'jio/jobObject/' + jioAttributeObject.tabid, this.jobObjectName,this.jobObject);
this.jobObject);
}, },
addJob: function ( options ) { addJob: function ( job ) {
// Add a job to the queue // Add a job to the queue
// options.storage : the storage object // job : the job object
// options.job : the job object (may be a 'string')
console.log ('addJob'); console.log ('addJob');
// transforming job/storage to an object
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 // set job id
options.job.id = this.jobid; job.id = this.jobid;
this.jobid ++; this.jobid ++;
// save the new job into the queue // save the new job into the queue
this.jobObject[this.jobid] = options.job; this.jobObject[job.id] = job;
// save into localStorage // save into localStorage
this.copyJobQueueToLocalStorage(); this.copyJobQueueToLocalStorage();
}, // end addJob }, // end addJob
...@@ -367,45 +336,65 @@ ...@@ -367,45 +336,65 @@
invoke: function (job) { invoke: function (job) {
// Do a job invoking the good method in the good storage. // Do a job invoking the good method in the good storage.
console.log ('invoke'); console.log ('invoke ' + JSON.stringify(job));
//// analysing job method //// analysing job method
// if the method does not exist, do nothing // if the method does not exist, do nothing
if (!jioAttributeObject.jobMethodObject[job.method]) if (!jioConstObj.jobMethodObject[job.method])
return false; // suppose never happen return false; // suppose never happen
// test if a similar job is on going, in order to publish a start // test if a similar job is on going, in order to publish a start
// event if it is the first of his kind (method). // event if it is the first of his kind (method).
if (!this.isThereJobsWhere(function (testjob){ if (!this.isThereJobsWhere(function (testjob){
return (testjob.method === job.method && return (testjob.method === job.method &&
testjob.method === 'ongoing'); testjob.method === 'initial');
})) { })) {
job.status = 'ongoing'; job.status = 'ongoing';
$.jio('publish', this.publisher.publish(jioConstObj.jobMethodObject[
{'event': job.method].start_event);
jioAttributeObject.jobMethodObject[
job.method].start_event,
'job':job});
} else { } else {
job.status = 'ongoing'; job.status = 'ongoing';
} }
// Create a storage object and use it to save,load,...! // Create a storage object and use it to save,load,...!
var t = this;
createStorageObject( createStorageObject(
{'storage':job.storage, {'storage':job.storage,
'applicant':jioAttributeObject.applicant})[job.method](job); 'applicant':jioGlobalObj.applicant})[job.method](
job,function (endedjob){
t.ended(endedjob);
});
//// end method analyse //// end method analyse
}, },
done: function (job) { ended: function (job) {
// This job is supposed done, we can remove it from queue. console.log ('ended');
switch (job.status) {
this.removeJob ({'job':job}); case 'done':
}, // This job is supposed done, we can remove it from queue.
this.removeJob ({'job':job});
fail: function (job) { break;
// This job cannot be done, change its status. case 'fail':
// This job cannot be done.
// save to local storage
this.copyJobQueueToLocalStorage ();
break;
default:
break;
}
this.jobObject[job.id] = job; //// ended job analyse
// save to local storage // if the job method does not exists, return false
this.copyJobQueueToLocalStorage (); if (!jioConstObj.jobMethodObject[job.method])
return false;
// if there isn't some job to do, then send stop event
if (!this.isThereJobsWhere(function(testjob){
return (testjob.method === job.method &&
testjob.status !== 'fail');
})) {
this.publisher.publish(
jioConstObj.jobMethodObject[
job.method].stop_event);
return;
}
//// end returnedJobAnalyse
}, },
clean: function () { clean: function () {
// Clean the job list, removing all jobs that have failed. // Clean the job list, removing all jobs that have failed.
...@@ -422,12 +411,12 @@ ...@@ -422,12 +411,12 @@
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// jio job listener // jio job listener
var JobListener = function ( options ) { var JobListener = function ( queue ) {
// A little daemon which will start jobs from the joblist // A little daemon which will start jobs from the joblist
var settings = $.extend({'interval': 100},options); this.interval = 200;
this.interval = settings.interval;
this.id = null; this.id = null;
this.queue = queue;
}; };
JobListener.prototype = { JobListener.prototype = {
...@@ -439,14 +428,13 @@ ...@@ -439,14 +428,13 @@
start: function () { start: function () {
// Start the listener. It will always check if there are jobs in the // Start the listener. It will always check if there are jobs in the
// queue // queue
if (!this.id) { if (!this.id) {
var queue = this.queue;
this.id = setInterval (function () { this.id = setInterval (function () {
// if there are jobs // if there is jobs
if (jioAttributeObject.localStorage.getItem( if (JSON.stringify(queue.jobObject) !== '{}') {
'jio/jobObject/'+jioAttributeObject.tabid) && queue.invokeAll();
localStorage.joblist !== '{}') {
jioAttributeObject.queue.invokeAll();
} }
},this.interval); },this.interval);
console.log ('listener started'); console.log ('listener started');
...@@ -460,146 +448,153 @@ ...@@ -460,146 +448,153 @@
console.log ('listener stopped'); console.log ('listener stopped');
clearInterval (this.id); clearInterval (this.id);
this.id = null; this.id = null;
return true;
} }
return false;
} }
} }
// end jio job listener // end jio job listener
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// jio methods // Toucher
var methods = { var Toucher = function () {
init: function ( options ) { // The toucher is a little thread that show activity of this jio.
// Initialize jio. Create/Update all jio attributes and start this.interval = 400;
// listening to jobs. If jio is already initialized, arguments this.id = null;
// will be given to 'doMethod' which will treat them as a 'job'. };
Toucher.prototype = {
var settings = $.extend({},options); start: function (id) {
// if already initialized // start the toucher
if (methods.isReady()) { console.log ('start touching jio/id/'+id);
methods.doMethod(settings); if (!this.id) {
return false; this.touch(id);
} var t = this;
console.log ('initializing'); this.id = setInterval (function () {
// check settings t.touch(id);
if (!settings.storage || },this.interval);
!settings.applicant) { return true;
$.error ('Storage or applicant are not correctly set.'); } else {
return false;
}
// objectify settings if there are strings
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 jioAttributeObject.storageTypeObject
if (!settings.storage.type) {
$.error ('Storage incomplete.');
return false;
}
if(!jioAttributeObject.storageTypeObject[settings.storage.type]) {
$.error ('Unknown storage type "' +
settings.storage.type + '".');
return false; return false;
} }
// TODO set a tab id for THIS jio ?
// set a tab id to every jobs ?
// see todo from methods.close
// 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;
return true;
}, },
isReady: function () { stop: function () {
// Check if jio is ready to use. // stop the toucher
console.log ('stop touching');
return jioAttributeObject.isReady; if (this.id) {
}, clearInterval (this.id);
addStorageType: function ( options ) { this.id = null;
// Add a storage type to jio. Jio must have keys/types which are
// bound to a storage creation function. ex: 'local', will
// create a LocalStorage (in jio.storage.js).
// It can replace a older type with a newer creation function.
// options.type : the type of the storage.
// options.creator : the function to create a storage object.
var settings = $.extend({},options);
if (settings.type && settings.creator) {
jioAttributeObject.storageTypeObject[
settings.type] = settings.creator;
return true; return true;
} }
return false; return false;
}, },
getApplicant: function () { touch: function (id) {
// return applicant set at init of jio jioGlobalObj.localStorage.setItem ('jio/id/' + id,
Date.now() );
}
};
// end Toucher
////////////////////////////////////////////////////////////////////////////
return jioAttributeObject.applicant; ////////////////////////////////////////////////////////////////////////////
// JIO Constructor
var JioCons = function ( storage , applicant ) {
// JIO Constructor, create a new JIO object.
// It just initializes values.
// storage : the storage that contains {type:..,[storageinfos]}
// applicant : the applicant that contains {ID:...}
// these parameters are optional and may be 'string' or 'object'
// objectify storage and applicant
if(typeof storage === 'string')
storage = JSON.parse(options.storage);
if(typeof applicant === 'string')
applicant = JSON.parse(options.applicant);
// set init values
this['storage'] = storage;
this['applicant'] = applicant;
this['id'] = 0;
this['pubsub'] = new PubSub();
this['queue'] = new JobQueue(this.pubsub);
this['listener'] = new JobListener(this.queue);
this['toucher'] = new Toucher();
this['ready'] = false;
// check storage type
if (this.storage)
if (!jioGlobalObj.storageTypeObject[this.storage.type])
$.error('Unknown storage type "' + this.storage.type +'"');
};
// JIO Prototype
JioCons.prototype = {
start: function () {
// Start JIO: start listening to jobs and make it ready
// set a new jio id
this.id = getNewQueueID();
console.log (this.id);
// initializing objects
this.queue.init({'jioID':this.id});
// start touching
this.toucher.start(this.id);
// start listening
this.listener.start();
// is now ready
this.ready = true;
return this.isReady();
}, },
publish: function ( options ) { stop: function () {
// publish a jio event // Finish some job if possible and stop listening.
// options.event : contains the event name. // It can be restarted later
// options[*] : are the arguments sent with this event.
if (!methods.isReady()) return null; this.queue.close();
console.log ('publish ' + options.event); this.listener.stop();
var tmp = $.extend({},options); this.toucher.stop();
tmp.event = undefined; this.ready = false;
jioAttributeObject.pubsub.publish(options.event,tmp); return true;
}, },
subscribe: function ( options ) { kill: function () {
// subscribe to a jio event. We can subscribe to jio event even // kill this JIO, job listening and job operation (event if they
// if jio is not initialized. // are on going!)
// options.event : contains the event name.
// options.func : will call this function after receiving event.
console.log ('subscribe');
jioAttributeObject.pubsub.subscribe(options.event,options.func);
},
unsubscribe: function ( options ) {
// unsubscribe to a jio event.
// options.event : contains the event name.
// TODO I wonder if it works.. ?
console.log ('unsubscribe');
jioAttributeObject.pubsub.unsubscribe(options.event);
},
doMethod: function ( options ) {
// Do a method in jio. The method is set in [options.method],
// it can be everything but itself.
// $.jio({'fileName':'a','fileContent':'b','method':'save'} this.queue.close();
if (options.method) { this.listener.stop();
if (options.method === 'doMethod') { this.toucher.stop();
$.error ('Cannot do method "doMethod" recursively.'); // TODO
return null; this.ready = false;
} return true;
if (methods[options.method]) {
return methods[options.method]( options );
}
$.error ('Method ' + options.method + ' not found.')
}
return null;
}, },
isReady: function () {
// Check if Jio is ready to use.
return this.ready;
},
publish: function (eventname, obj) {
// publish an event on this jio
// eventname : the event name
// obj : is an object containing some parameters for example
if (!this.isReady()) return ;
console.log ('publish ' + eventname);
return this.pubsub.publish(eventname,obj);
},
subscribe: function (eventname, callback) {
// subscribe to an event on this jio. We can subscribe to jio event
// even if jio is not started. Returns the callback function in
// order to unsubscribe it.
// eventname : the event name.
// callback : called after receiving event.
console.log ('subscribe ' +eventname);
return this.pubsub.subscribe(eventname,callback);
},
unsubscribe: function (eventname,callback) {
// unsubscribe callback from an event
console.log ('unsubscribe ' +eventname);
return this.pubsub.unsubscribe(eventname,callback);
},
checkNameAvailability: function ( options ) { checkNameAvailability: function ( options ) {
// Check the user availability in the storage set in [options] // Check the user availability in the storage set in [options]
// or in the storage set at init. At the end of the job, // or in the storage set at init. At the end of the job,
...@@ -607,47 +602,57 @@ ...@@ -607,47 +602,57 @@
// return value. // return value.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to remove (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result.
// $.jio('checkNameAvailability',{'userName':'toto'}); // example :
console.log ('isAvailable'); // jio.checkNameAvailability({'userName':'myName','callback':
if (!methods.isReady()) return null; // function (result) { alert('is available? ' +
// result.isAvailable); }});
if (!this.isReady()) return null;
var settings = $.extend ({ var settings = $.extend ({
'userName': jioAttributeObject.storage.userName, 'userName': this.storage.userName,
'storage': jioAttributeObject.storage, 'storage': this.storage,
'applicant': jioAttributeObject.applicant, 'applicant': this.applicant,
'method': 'checkNameAvailability' 'method': 'checkNameAvailability',
'callback': function () {}
},options); },options);
// check dependencies // check dependencies
if (settings.userName) { if (settings.userName && settings.storage && settings.applicant) {
return jioAttributeObject.queue.addJob ( return this.queue.addJob ( new Job ( settings ) );
{'job':(new Job ( settings ))} ) ;
} }
return null; return null;
}, },
saveDocument: function ( options ) { saveDocument: function ( options ) {
// Load a document in the storage set in [options] // Load a document in the storage set in [options]
// or in the storage set at init. At the end of the job, // or in the storage set at init. At the end of the job,
// 'job_done' will be sent with this job. // 'job_done' will be sent with this job.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to remove (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result.
// jio.saveDocument({'fileName':'file','fileContent':'content',
// 'callback': function (result) { alert('saved?' +
// result.isSaved); }});
// $.jio('saveDocument',{'fileName':'a','fileContent':'b','options':{
// 'overwrite':false}}
console.log ('saveDocument'); console.log ('saveDocument');
if (!methods.isReady()) return null; if (!this.isReady()) return null;
var settings = $.extend({ var settings = $.extend({
'storage': jioAttributeObject.storage, 'storage': this.storage,
'applicant': jioAttributeObject.applicant,
'lastModified': Date.now(), 'lastModified': Date.now(),
'method':'saveDocument' 'method':'saveDocument',
'applicant': this.applicant,
'callback': function () {}
},options); },options);
// check dependencies // check dependencies
if (settings.fileName && settings.fileContent) { if (settings.fileName && settings.fileContent &&
return jioAttributeObject.queue.addJob ( settings.storage && settings.applicant) {
{'job':(new Job ( settings ))} ) ; return this.queue.addJob ( new Job ( settings ) );
} }
return null; return null;
}, },
loadDocument: function ( options ) { loadDocument: function ( options ) {
// Load a document in the storage set in [options] // Load a document in the storage set in [options]
// or in the storage set at init. At the end of the job, // or in the storage set at init. At the end of the job,
...@@ -655,98 +660,119 @@ ...@@ -655,98 +660,119 @@
// return value. // return value.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to remove (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result.
// jio.loadDocument({'fileName':'file','callback':
// function (result) { alert('content: '+
// result.fileContent); }});
// $.jio('loadDocument',{'fileName':'a'});
console.log ('load'); console.log ('load');
if (!methods.isReady()) return null; if (!this.isReady()) return null;
var settings = $.extend ({ var settings = $.extend ({
'storage':jioAttributeObject.storage, 'storage': this.storage,
'applicant':jioAttributeObject.applicant, 'applicant': this.applicant,
'method':'loadDocument' 'method':'loadDocument',
'callback': function(){}
},options); },options);
// check dependencies // check dependencies
if ( settings.fileName ) { if ( settings.fileName && settings.storage && settings.applicant) {
return jioAttributeObject.queue.addJob ( return jioGlobalObj.queue.addJob ( new Job ( settings ) );
{'job':(new Job ( settings ))} ) ;
} }
return null; return null;
}, },
getDocument: function ( options ) { getDocument: function ( options ) {
// TODO // TODO (don't forgot to add this method to constants)
}, },
getDocumentList: function ( options ) { getDocumentList: function ( options ) {
// Get a document list of the user in the storage set in [options] // Get a document list of the user in the storage set in [options]
// or in the storage set at init. At the end of the job, // or in the storage set at init.
// 'job_done' will be sent with this job and its 'list'
// return value.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to remove (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result.
// jio.getDocumentList({'callback':
// function (result) { alert('list: '+result.list); }});
// $.jio('getDocumentList');
console.log ('getList'); console.log ('getList');
if (!methods.isReady()) return null; if (!this.isReady()) return null;
var settings = $.extend ({ var settings = $.extend ({
'storage': jioAttributeObject.storage, 'storage': this.storage,
'applicant': jioAttributeObject.applicant, 'applicant': this.applicant,
'method':'getDocumentList' 'method':'getDocumentList',
'callback':function(){}
},options); },options);
return jioAttributeObject.queue.addJob ( // check dependencies
{'job':(new Job ( settings ))} ); if ( settings.storage && settings.applicant ) {
return this.queue.addJob ( new Job ( settings ) );
}
return null;
}, },
removeDocument: function ( options ) { removeDocument: function ( options ) {
// Remove a document in the storage set in [options] // Remove a document in the storage set in [options]
// or in the storage set at init. At the end of the job, // or in the storage set at init.
// 'job_done' will be sent with this job.
// options.storage : the storage where to remove (optional) // options.storage : the storage where to remove (optional)
// options.applicant : the applicant (optional) // options.applicant : the applicant (optional)
// options.callback(result) : called to get the result.
// $.jio('removeDocument',{'fileName':'a'}); // jio.removeDocument({'fileName':'file','callback':
// function (result) { alert('removed? '+result.isRemoved); }});
console.log ('removeDocument'); console.log ('removeDocument');
if (!methods.isReady()) return null; if (!this.isReady()) return null;
var settings = $.extend ({ var settings = $.extend ({
'storage': jioAttributeObject.storage, 'storage': this.storage,
'applicant': jioAttributeObject.applicant, 'applicant': this.applicant,
'method':'removeDocument' 'method':'removeDocument',
'callback':function (){}
},options); },options);
if ( settings.fileName ) { if ( settings.fileName && settings.storage && settings.applicant ) {
return jioAttributeObject.queue.addJob ( return this.queue.addJob ( new Job( settings ) );
{'job':(new Job( settings ))} );
} }
return null; return null;
},
start: function ( options ) {
// TODO
},
close: function ( options ) {
// Finish some job if possible and close jio.
// It can be re-init later.
// TODO if someone is managing the browser closure or go to previous
// page, $.jio('close') will close tab id (if any) to free its jobs,
// so that other tabs may do them.
jioAttributeObject.listener.stop();
jioAttributeObject.isReady = false;
return true;
},
getJioAttributes: function ( options ) {
return $.extend({},jioAttributeObject);
} }
}; };
// end JIO methods // end JIO Prototype
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// JIO arguments manager
$.jio = function ( method ) { ////////////////////////////////////////////////////////////////////////////
if ( methods[method] ) { // Jio creator
return methods[method].apply( this, Array.prototype.slice.call ( var Jio = function () {
arguments, 1)); // Jio creator object
} else if ( typeof method === 'object' || ! method ) { // this object permit to create jio object
return methods.init.apply( this, arguments ); };
} else { Jio.prototype = {
$.error ('Method ' + method + ' does not exists on JQuery.jio' ); createNew: function ( storage, applicant) {
// return a new instance of JIO
return new JioCons(storage,applicant);
},
addStorageType: function ( type, constructor ) {
// Add a storage type to jio. Jio must have keys/types which are
// bound to a storage creation function. ex: 'local', will
// create a LocalStorage (in jio.storage.js).
// It can replace a older type with a newer creation function.
// type : the type of the storage.
// constructor : the function which returns a new storage object.
if (type && constructor) {
jioGlobalObj.storageTypeObject[type] = constructor;
return true;
}
return false;
},
getGlobalObject: function () {
// Returns the global jio values
return jioGlobalObj;
},
getConstObject: function () {
// Returns a copy of the constants
return $.extend({},jioConstObj);
} }
}; };
// end JIO arguments manager return new Jio();
// end Jio Creator
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
})();
})( jQuery );
;(function ( $ ) { ;(function ( Jio ) {
// TODO do we realy need to check dependencies ? // check dependencies
// test dependencies var errorDependencies=function(){$.error('Cannot find Jio or Base64');};
try { try{if (!Jio || !Base64){
if ($.jio && Base64) {} errorDependencies();return;
else {return false;} }} catch (e){
} catch (e) { errorDependencies();return;}
$.error('jio.js and base64.js '+
' are required by jio.storage.js');
return false;
};
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// private vars // globals
var jioAttributeObject = $.jio('getJioAttributes'); var jioGlobalObj = Jio.getGlobalObject();
// end private vars // end globals
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Tools // Tools
// end Tools // end Tools
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
...@@ -33,7 +29,7 @@ ...@@ -33,7 +29,7 @@
this.userName = options.storage.userName; this.userName = options.storage.userName;
}; };
LocalStorage.prototype = { LocalStorage.prototype = {
checkNameAvailability: function ( job ) { checkNameAvailability: function ( job , jobendcallback) {
// checks the availability of the [job.userName]. // checks the availability of the [job.userName].
// if the name already exists, it is not available. // if the name already exists, it is not available.
// job: the job object // job: the job object
...@@ -42,31 +38,35 @@ ...@@ -42,31 +38,35 @@
// wait a little in order to simulate asynchronous operation // wait a little in order to simulate asynchronous operation
setTimeout(function () { setTimeout(function () {
var available = true; var available = true;
var localStor = jioAttributeObject.localStorage.getAll(); var localStor = jioGlobalObj.localStorage.getAll();
for (var k in localStor) { for (var k in localStor) {
var splitk = k.split('/'); var splitk = k.split('/');
if (splitk[0] === 'jio' && splitk[1] === job.userName) { if (splitk[0] === 'jio' &&
splitk[1] === 'local' &&
splitk[2] === job.userName) {
available = false; available = false;
break; break;
} }
} }
if (!available) { if (!available) {
job.status = 'done'; job.status = 'done';
job.message = ''+ job.userName + ' is not available.'; jobendcallback(job);
job.isAvailable = false; job.callback({'status':'done',
$.jio('publish',{'event': 'job_done', 'message':''+job.userName+
'job': job}); ' is not available.',
'isAvailable':false});
} else { } else {
job.status = 'done'; job.status = 'done';
job.message = ''+ job.userName + ' is available.'; jobendcallback(job);
job.isAvailable = true; job.callback({'status':'done',
$.jio('publish',{'event': 'job_done', 'message':''+job.userName+
'job': job}); ' is available.',
'isAvailable':true});
} }
}, 100); }, 100);
}, // end userNameAvailable }, // end userNameAvailable
saveDocument: function ( job ) { saveDocument: function ( job, jobendcallback ) {
// Save a document in the local storage // Save a document in the local storage
// job : the job object // job : the job object
// job.options : the save options object // job.options : the save options object
...@@ -79,8 +79,9 @@ ...@@ -79,8 +79,9 @@
var t = this; var t = this;
// wait a little in order to simulate asynchronous saving // wait a little in order to simulate asynchronous saving
setTimeout (function () { setTimeout (function () {
var res = {};
// reading // reading
var doc = jioAttributeObject.localStorage.getItem( var doc = jioGlobalObj.localStorage.getItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+ 'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName); job.fileName);
if (!doc) { // create document if (!doc) { // create document
...@@ -91,85 +92,91 @@ ...@@ -91,85 +92,91 @@
'lastModified': Date.now () 'lastModified': Date.now ()
} }
// writing // writing
jioAttributeObject.localStorage.setItem( jioGlobalObj.localStorage.setItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+ 'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName, doc); job.fileName, doc);
job.status = 'done'; // return
job.message = 'Document saved.'; res.status = job.status = 'done';
job.isSaved = true; res.message = 'Document saved.';
$.jio('publish',{'event':'job_done', res.isSaved = true;
'job':job}); jobendcallback(job);
return true; job.callback(res);
return;
} }
if ( settings.overwrite || settings.force ) { // overwrite if ( settings.overwrite || settings.force ) {
if ( ! settings.force ) { // force write // if it doesn't force writing
// checking modification date // checking modification date
if ( doc.lastModified >= job.lastModified ) { if ( ! settings.force &&
// date problem! doc.lastModified >= job.lastModified ) {
job.status = 'fail'; // date problem!
job.message = 'Modification date is earlier than ' + // return
'existing modification date.'; res.status = job.status = 'fail';
job.isSaved = false; res.message = 'Modification date is earlier than ' +
$.jio('publish',{'event':'job_fail', 'existing modification date.';
'job':job}); res.isSaved = false;
return false; jobendcallback(job);
} job.callback(res);
} return;
}
// overwriting
doc.lastModified = Date.now(); doc.lastModified = Date.now();
doc.fileContent = job.fileContent; doc.fileContent = job.fileContent;
// writing // writing
jioAttributeObject.localStorage.setItem( jioGlobalObj.localStorage.setItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+ 'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName, doc); job.fileName, doc);
job.status = 'done'; // return
job.message = 'Document saved'; res.status = job.status = 'done';
job.isSaved = true; res.message = 'Document saved';
$.jio('publish',{'event':'job_done', res.isSaved = true;
'job':job}); jobendcallback(job);
return true; job.callback(res);
return;
} }
// already exists // already exists
job.status = 'fail'; res.status = job.status = 'fail';
job.message = 'Document already exists.'; res.message = 'Document already exists.';
job.errno = 403; res.errno = 403;
job.isSaved = false; res.isSaved = false;
$.jio('publish',{'event':'job_fail', jobendcallback(job);
'job': job}); job.callback(res);
return false; return;
}, 100); }, 100);
}, // end saveDocument }, // end saveDocument
loadDocument: function ( job ) { loadDocument: function ( job, jobendcallback ) {
// load a document in the storage, copy the content into the job // load a document in the storage, copy the content into the job
// job : the job // job : the job
var t = this; var t = this;
// wait a little in order to simulate asynchronous operation // wait a little in order to simulate asynchronous operation
setTimeout(function () { setTimeout(function () {
var doc = jioAttributeObject.localStorage.getItem( var res = {};
var doc = jioGlobalObj.localStorage.getItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+ 'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName); job.fileName);
if (!doc) { if (!doc) {
job.status = 'fail'; res.status = job.status = 'fail';
job.errno = 404; res.errno = 404;
job.message = 'Document not found.'; res.message = 'Document not found.';
$.jio('publish',{'event':'job_fail', jobendcallback(job);
'job':job}); job.callback(res);
} else { } else {
job.status = 'done'; res.status = job.status = 'done';
job.message = 'Document loaded.'; res.message = 'Document loaded.';
job.fileContent = doc.fileContent; res.fileContent = doc.fileContent;
$.jio('publish',{'event':'job_done', jobendcallback(job);
'job':job}); job.callback(res);
} }
}, 100); }, 100);
}, // end loadDocument }, // end loadDocument
getDocumentList: function (job) { getDocumentList: function ( job, jobendcallback) {
var t = this; var t = this;
setTimeout(function () { setTimeout(function () {
var localStor = jioAttributeObject.localStorage.getAll(); var res = {};
job.list = []; var localStor = jioGlobalObj.localStorage.getAll();
res.list = [];
for (var k in localStor) { for (var k in localStor) {
var splitk = k.split('/'); var splitk = k.split('/');
if (splitk[0] === 'jio' && if (splitk[0] === 'jio' &&
...@@ -177,57 +184,54 @@ ...@@ -177,57 +184,54 @@
splitk[2] === job.storage.userName && splitk[2] === job.storage.userName &&
splitk[3] === job.applicant.ID) { splitk[3] === job.applicant.ID) {
fileObject = JSON.parse(localStor[k]); fileObject = JSON.parse(localStor[k]);
job.list.push ({ res.list.push ({
'fileName':fileObject.fileName, 'fileName':fileObject.fileName,
'creationDate':fileObject.creationDate, 'creationDate':fileObject.creationDate,
'lastModified':fileObject.lastModified}); 'lastModified':fileObject.lastModified});
} }
} }
console.log (JSON.stringify (job.list)); res.status = job.status = 'done';
job.status = 'done'; res.message = 'List received.';
job.message = 'List received.'; jobendcallback(job);
$.jio('publish',{'event':'job_done', job.callback(res);
'job':job});
}, 100); }, 100);
}, // end getDocumentList }, // end getDocumentList
removeDocument: function ( job ) { removeDocument: function ( job, jobendcallback ) {
var t = this; var t = this;
setTimeout (function () { setTimeout (function () {
var doc = jioAttributeObject.localStorage.getItem( var res = {};
var doc = jioGlobalObj.localStorage.getItem(
'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+ 'jio/local/'+job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName); job.fileName);
// already deleted
if (!doc) { if (!doc) {
// job.status = 'fail'; res.status = job.status = 'done';
// job.errno = 404; res.message = 'Document already removed.';
// job.message = 'Document not found.'; res.isRemoved = true;
// job.isRemoved = false; jobendcallback(job);
// $.jio('publish',{'event':'job_fail', job.callback(res);
// 'job':job}); return;
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});
} }
// deleting
jioGlobalObj.localStorage.deleteItem(
'jio/local/'+
job.storage.userName+'/'+job.applicant.ID+'/'+
job.fileName);
res.status = job.status = 'done';
res.message = 'Document removed.';
res.isRemoved = true;
jobendcallback(job);
job.callback(res);
return;
}, 100); }, 100);
} }
}; };
// add key to storageObject // add key to storageObject
$.jio('addStorageType',{'type':'local','creator':function (options) { Jio.addStorageType('local', function (options) {
return new LocalStorage(options); return new LocalStorage(options);
}}); });
// end Local Storage // end Local Storage
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
...@@ -527,11 +531,11 @@ ...@@ -527,11 +531,11 @@
}; };
// add key to storageObject // add key to storageObject
$.jio('addStorageType',{'type':'dav','creator':function (options) { Jio.addStorageType('dav', function (options) {
return new DAVStorage(options); return new DAVStorage(options);
}}); });
// end DAVStorage // end DAVStorage
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
})( jQuery ); })( 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