Commit 2ec1169b authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

JIO provides now a sort method for jio storages.

If the storage does not sort document list by itself,
JIO will sort it anyway.
The storage should use the method sortDone() to tell to JIO that
the document list is already sorted (avoid double sorting).
If the storage can not support sorting, it can also use
sortDocumentArray() provided by JIO (and no need to use sortDone()).
parent 78f133e6
......@@ -316,6 +316,8 @@
}
priv.loading_object.getlist();
priv.jio.getDocumentList({
'sort':{'lastModified':'descending',
'fileName':'ascending'},
'maxtries':3,
'callback':function (result) {
if (result.status === 'done') {
......@@ -333,6 +335,8 @@
// clone document list
var array = $.extend(true,[],priv.data_object.documentList), i,
lm, cd;
// FIXME : we can have 2012/1/1 12:5
// we should have 2012/01/01 12:05
for (i = 0; i < array.length; i += 1) {
lm = (new Date(array[i].lastModified));
cd = (new Date(array[i].creationDate));
......
/*! JIO - v0.1.0 - 2012-05-24
/*! JIO - v0.1.0 - 2012-05-29
* Copyright (c) 2012 Nexedi; Licensed */
......@@ -707,6 +707,7 @@ var JIO =
priv.callback = options.job.callback;
priv.queue = options.queue;
priv.res = {'status':'done','message':''};
priv.sorted = false;
//// end Private attributes
//// Private Methods
......@@ -744,10 +745,22 @@ var JIO =
priv.res.message = 'Document list received.';
priv.res.return_value = documentlist;
for (i = 0; i < priv.res.return_value.length; i += 1) {
priv.res.return_value[i].lastModified =
new Date(priv.res.return_value[i].lastModified).getTime();
priv.res.return_value[i].creationDate =
new Date(priv.res.return_value[i].creationDate).getTime();
// transform current date format into ms since 1/1/1970
// useful for easy comparison
if (typeof priv.res.return_value[i].lastModified !== 'number') {
priv.res.return_value[i].lastModified =
new Date(priv.res.return_value[i].lastModified).
getTime();
}
if (typeof priv.res.return_value[i].creationDate !== 'number') {
priv.res.return_value[i].creationDate =
new Date(priv.res.return_value[i].creationDate).
getTime();
}
}
// check for sorting
if (typeof priv.job.sort !== 'undefined' && !priv.sorted) {
that.sortDocumentArray(priv.res.return_value);
}
};
priv.fail_removeDocument = function () {
......@@ -911,6 +924,23 @@ var JIO =
statusText:'Undefined Method',
message:'This method must be redefined!'});
};
that.sortDocumentArray = function (documentarray) {
documentarray.sort(function (row1,row2) {
var k, res;
for (k in priv.job.sort) {
var sign = (priv.job.sort[k] === 'descending' ? -1 : 1);
if (row1[k] === row2[k]) { continue; }
return (row1[k] > row2[k] ? sign : -sign);
}
return 0;
});
that.sortDone();
};
that.sortDone = function () {
priv.sorted = true;
};
//// end Public Methods
return that;
};
......
/*! JIO - v0.1.0 - 2012-05-24
/*! JIO - v0.1.0 - 2012-05-29
* Copyright (c) 2012 Nexedi; Licensed */
var JIO=function(){var a=function(a,b){var c={job_method_object:{checkNameAvailability:{},saveDocument:{},loadDocument:{},getDocumentList:{},removeDocument:{}}},d={job_managing_method:{canSelect:function(a,b){return JSON.stringify(a.storage)===JSON.stringify(b.storage)&&JSON.stringify(a.applicant)===JSON.stringify(b.applicant)&&a.fileName===b.fileName?!0:!1},canRemoveFailOrDone:function(a,b){return a.status==="fail"||a.status==="done"?!0:!1},canEliminate:function(a,b){return a.status!=="ongoing"&&(a.method==="removeDocument"&&b.method==="saveDocument"||a.method==="saveDocument"&&b.method==="removeDocument")?!0:!1},canReplace:function(a,b){return a.status!=="ongoing"&&a.method===b.method&&a.date<b.date?!0:!1},cannotAccept:function(a,b){if(a.status!=="ongoing"){if(a.method==="removeDocument"&&b.method==="loadDocument")return!0}else{if(a.method===b.method==="loadDocument")return!0;if(a.method==="removeDocument"&&(b.method==="loadDocument"||b.method==="removeDocument"))return!0;if(a.method===b.method==="saveDocument"&&a.fileContent===b.fileContent)return!0;if(a.method===b.method==="getDocumentList"||a.method===b.method==="checkNameAvailability")return!0}return!1},mustWait:function(a,b){return a.method==="getDocumentList"||a.method==="checkNameAvailability"||b.method==="getDocumentList"||b.method==="checkNameAvailability"?!1:!0}},queue_id:1,storage_type_object:{},max_wait_time:1e4},e=function(a,b){var c;for(c in b)a[c]=b[c];return a},f,g,h,i,j,k,l,m;return f=function(a,c){var d={},e={},f={},g,h;return e.eventAction=function(a){return h=a&&f[a],h||(g=b.Callbacks(),h={publish:g.fire,subscribe:g.add,unsubscribe:g.remove},a&&(f[a]=h)),h},d.publish=function(a,b){e.eventAction(a).publish(b)},d.subscribe=function(a,b){return e.eventAction(a).subscribe(b),b},d.unsubscribe=function(a,b){e.eventAction(a).unsubscribe(b)},d},g=function(a,b){var c=e({},a);return c.id=0,c.status="initial",c.date=Date.now(),c},h=function(b,f){var h={},i={},j="jio/idarray";return h.init=function(c){var d,e=function(){},f;i.use_local_storage&&(f=a.getItem(j)||[],b.publisher&&(i.publisher=b.publisher),i.jio_id=c.jio_id,i.job_object_name="jio/jobobject/"+i.jio_id,f.push(i.jio_id),a.setItem(j,f)),i.job_object={},h.copyJobQueueToLocalStorage();for(d in i.recovered_job_object)i.recovered_job_object[d].callback=e,h.addJob(i.recovered_job_object[d])},h.close=function(){JSON.stringify(i.job_object)==="{}"&&a.deleteItem(i.job_object_name)},h.getNewQueueID=function(){var b=null,c=0,e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)e[b]>=d.queue_id&&(d.queue_id=e[b]+1);return c=d.queue_id,d.queue_id++,c},h.recoverOlderJobObject=function(){var b=null,c=[],d=!1,e;if(i.use_local_storage){e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)a.getItem("jio/id/"+e[b])<Date.now()-1e4?(a.deleteItem("jio/id/"+e[b]),i.recovered_job_object=a.getItem("jio/jobobject/"+e[b]),a.deleteItem("jio/jobobject/"+e[b]),d=!0):c.push(e[b]);d&&a.setItem(j,c)}},h.isThereJobsWhere=function(a){var b="id";if(!a)return!0;for(b in i.job_object)if(a(i.job_object[b]))return!0;return!1},h.copyJobQueueToLocalStorage=function(){return i.use_local_storage?a.setItem(i.job_object_name,i.job_object):!1},h.createJob=function(a){return h.addJob(g(a))},h.addJob=function(a){var b=!0,c=[],e=[],f=[],g=null,j="id";for(j in i.job_object){if(d.job_managing_method.canRemoveFailOrDone(i.job_object[j],a)){f.push(j);continue}if(d.job_managing_method.canSelect(i.job_object[j],a)){if(d.job_managing_method.canEliminate(i.job_object[j],a)){c.push(j);continue}if(d.job_managing_method.canReplace(i.job_object[j],a)){g=k({queue:h,job:i.job_object[j]}),g.replace(a),b=!1;break}if(d.job_managing_method.cannotAccept(i.job_object[j],a))return!1;if(d.job_managing_method.mustWait(i.job_object[j],a)){e.push(j);continue}}}if(b){for(j=0;j<c.length;j+=1)g=k({queue:h,job:i.job_object[c[j]]}),g.eliminate();if(e.length>0){a.status="wait",a.waitingFor={jobIdArray:e};for(j=0;j<e.length;j+=1)i.job_object[e[j]]&&(i.job_object[e[j]].maxtries=1)}for(j=0;j<f.length;j+=1)h.removeJob(i.job_object[f[j]]);a.id=i.job_id,a.tries=0,i.job_id++,i.job_object[a.id]=a}return h.copyJobQueueToLocalStorage(),!0},h.removeJob=function(a){var b=e({where:function(a){return!0}},a),c,d=!1,f="key";if(b.job)i.job_object[b.job.id]&&b.where(i.job_object[b.job.id])&&(delete i.job_object[b.job.id],d=!0);else for(f in i.job_object)b.where(i.job_object[f])&&(delete i.job_object[f],d=!0);d||console.error("No jobs was found, when trying to remove some."),h.copyJobQueueToLocalStorage()},h.resetAll=function(){var a="id";for(a in i.job_object)i.job_object[a].status="initial";h.copyJobQueueToLocalStorage()},h.invokeAll=function(){var a="id",b,c;for(a in i.job_object){c=!1;if(i.job_object[a].status==="initial")h.invoke(i.job_object[a]);else if(i.job_object[a].status==="wait"){c=!0;if(i.job_object[a].waitingFor.jobIdArray)for(b=0;b<i.job_object[a].waitingFor.jobIdArray.length;b+=1)if(i.job_object[i.job_object[a].waitingFor.jobIdArray[b]]){c=!1;break}i.job_object[a].waitingFor.time&&i.job_object[a].waitingFor.time>Date.now()&&(c=!1),c&&h.invoke(i.job_object[a])}}this.copyJobQueueToLocalStorage()},h.invoke=function(a){var b;if(!c.job_method_object[a.method])return!1;h.isThereJobsWhere(function(b){return b.method===a.method&&b.method==="initial"})?a.status="ongoing":(a.status="ongoing",i.publisher.publish(c.job_method_object[a.method]["start_"+a.method])),b=k({queue:this,job:a}),b.execute()},h.ended=function(a){var b=e({},a);h.removeJob({job:b});if(!c.job_method_object[b.method])return!1;if(!h.isThereJobsWhere(function(a){return a.method===b.method&&a.status==="ongoing"||a.status==="initial"})){i.publisher.publish(c.job_method_object[b.method]["stop_"+b.method]);return}},h.clean=function(){h.removeJob(undefined,{where:function(a){return a.status==="fail"}})},i.use_local_storage=b.options.use_local_storage,i.publisher=b.publisher,i.job_id=1,i.jio_id=0,i.job_object_name="",i.job_object={},i.recovered_job_object={},h},i=function(a,b){var c={},d={};return d.interval=200,d.id=null,d.queue=a.queue,c.setIntervalDelay=function(a){d.interval=a},c.start=function(){return d.id?!1:(d.id=setInterval(function(){d.queue.recoverOlderJobObject(),d.queue.invokeAll()},d.interval),!0)},c.stop=function(){return d.id?(clearInterval(d.id),d.id=null,!0):!1},c},j=function(){var b={},c={};return c.interval=400,c.id=null,b.start=function(a){return c.id?!1:(b.touch(a),c.id=setInterval(function(){b.touch(a)},c.interval),!0)},b.stop=function(){return c.id?(clearInterval(c.id),c.id=null,!0):!1},b.touch=function(b){a.setItem("jio/id/"+b,Date.now())},b},k=function(a){var b={},c={};return c.job=a.job,c.callback=a.job.callback,c.queue=a.queue,c.res={status:"done",message:""},c.fail_checkNameAvailability=function(){c.res.message="Unable to check name availability."},c.done_checkNameAvailability=function(a){c.res.message=c.job.userName+" is "+(a?"":"not ")+"available.",c.res.return_value=a},c.fail_loadDocument=function(){c.res.message="Unable to load document."},c.done_loadDocument=function(a){c.res.message="Document loaded.",c.res.return_value=a,c.res.return_value.lastModified=(new Date(c.res.return_value.lastModified)).getTime(),c.res.return_value.creationDate=(new Date(c.res.return_value.creationDate)).getTime()},c.fail_saveDocument=function(){c.res.message="Unable to save document."},c.done_saveDocument=function(){c.res.message="Document saved."},c.fail_getDocumentList=function(){c.res.message="Unable to retrieve document list."},c.done_getDocumentList=function(a){var b;c.res.message="Document list received.",c.res.return_value=a;for(b=0;b<c.res.return_value.length;b+=1)c.res.return_value[b].lastModified=(new Date(c.res.return_value[b].lastModified)).getTime(),c.res.return_value[b].creationDate=(new Date(c.res.return_value[b].creationDate)).getTime()},c.fail_removeDocument=function(){c.res.message="Unable to removed document."},c.done_removeDocument=function(){c.res.message="Document removed."},c.retryLater=function(){var a=c.job.tries*c.job.tries*1e3;a>d.max_wait_time&&(a=d.max_wait_time),c.job.status="wait",c.job.waitingFor={time:Date.now()+a}},b.cloneJob=function(){return e({},c.job)},b.getUserName=function(){return c.job.userName||""},b.getApplicantID=function(){return c.job.applicant.ID||""},b.getStorageUserName=function(){return c.job.storage.userName||""},b.getStoragePassword=function(){return c.job.storage.password||""},b.getStorageLocation=function(){return c.job.storage.location||""},b.getStorageArray=function(){return c.job.storage.storageArray||[]},b.getFileName=function(){return c.job.fileName||""},b.getFileContent=function(){return c.job.fileContent||""},b.cloneOptionObject=function(){return e({},c.job.options)},b.getMaxTries=function(){return c.job.maxtries},b.getTries=function(){return c.job.tries||0},b.setMaxTries=function(a){c.job.maxtries=a},b.addJob=function(a){return c.queue.createJob(a)},b.eliminate=function(){c.job.maxtries=1,c.job.tries=1,b.fail("Job Stopped!",0)},b.replace=function(a){c.job.tries=0,c.job.date=a.date,c.job.callback=a.callback,c.res.status="fail",c.res.message="Job Stopped!",c.res.error={},c.res.error.status=0,c.res.error.statusText="Replaced",c.res.error.message="The job was replaced by a newer one.",c["fail_"+c.job.method](),c.callback(c.res)},b.fail=function(a){c.res.status="fail",c.res.error=a,c.res.error.status=c.res.error.status||0,c.res.error.statusText=c.res.error.statusText||"Unknown Error",c.res.error.array=c.res.error.array||[],c.res.error.message=c.res.error.message||"",!c.job.maxtries||c.job.tries<c.job.maxtries?c.retryLater():(c.job.status="fail",c["fail_"+c.job.method](),c.queue.ended(c.job),c.callback(c.res))},b.done=function(a){c.job.status="done",c["done_"+c.job.method](a),c.queue.ended(c.job),c.callback(c.res)},b.execute=function(){return c.job.tries=b.getTries()+1,d.storage_type_object[c.job.storage.type]?d.storage_type_object[c.job.storage.type]({job:c.job,queue:c.queue})[c.job.method]():null},b.checkNameAvailability=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.loadDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.saveDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.getDocumentList=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.removeDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b},l=function(a,b){var c={},g={};g.wrongParametersError=function(a){var b="Method: "+a.method+", One or some parameters are undefined.";return console.error(b),a.callback({status:"fail",error:{status:0,statusText:"Undefined Parameter",message:b}}),null},c.getID=function(){return g.id},c.start=function(){return g.id!==0?!1:(g.id=g.queue.getNewQueueID(),g.queue.init({jio_id:g.id}),g.updater&&g.updater.start(g.id),g.listener.start(),g.ready=!0,c.isReady())},c.stop=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,g.id=0,!0},c.kill=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,!0},c.isReady=function(){return g.ready},c.publish=function(a,b){if(!c.isReady())return;return g.pubsub.publish(a,b)},c.subscribe=function(a,b){return g.pubsub.subscribe(a,b)},c.unsubscribe=function(a,b){return g.pubsub.unsubscribe(a,b)},c.checkNameAvailability=function(a){var b=e({userName:g.storage.userName,storage:g.storage,applicant:g.applicant,method:"checkNameAvailability",callback:function(){}},a);return c.isReady()&&b.userName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.saveDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,fileContent:"",method:"saveDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.loadDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"loadDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.getDocumentList=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"getDocumentList",callback:function(){}},a);return c.isReady()&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.removeDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"removeDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)};var k=e({use_local_storage:!0},a.options);return typeof a.storage=="string"&&(a.storage=JSON.parse(a.storage)),typeof a.applicant=="string"&&(a.applicant=JSON.parse(a.applicant)),g.storage=a.storage,g.applicant=a.applicant,g.id=0,g.pubsub=f({options:k}),g.queue=h({publisher:g.pubsub,options:k}),g.listener=i({queue:g.queue,options:k}),g.ready=!1,k.use_local_storage?g.updater=j({options:k}):g.updater=null,g.storage&&!d.storage_type_object[g.storage.type]&&console.error('Unknown storage type "'+g.storage.type+'"'),c.start(),c},m=function(a,b){var f={};return f.createNew=function(a,b,c){var d=e({use_local_storage:!0},c);return l({storage:a,applicant:b,options:d})},f.newBaseStorage=function(a,b){return k(a,b)},f.addStorageType=function(a,b){return a&&b?(d.storage_type_object[a]=b,!0):!1},f.getGlobalObject=function(){return d},f.getConstObject=function(){return e({},c)},f},m()};return window.requirejs?(define("JIO",["LocalOrCookieStorage","jQuery"],a),undefined):a(LocalOrCookieStorage,jQuery)}();
\ No newline at end of file
var JIO=function(){var a=function(a,b){var c={job_method_object:{checkNameAvailability:{},saveDocument:{},loadDocument:{},getDocumentList:{},removeDocument:{}}},d={job_managing_method:{canSelect:function(a,b){return JSON.stringify(a.storage)===JSON.stringify(b.storage)&&JSON.stringify(a.applicant)===JSON.stringify(b.applicant)&&a.fileName===b.fileName?!0:!1},canRemoveFailOrDone:function(a,b){return a.status==="fail"||a.status==="done"?!0:!1},canEliminate:function(a,b){return a.status!=="ongoing"&&(a.method==="removeDocument"&&b.method==="saveDocument"||a.method==="saveDocument"&&b.method==="removeDocument")?!0:!1},canReplace:function(a,b){return a.status!=="ongoing"&&a.method===b.method&&a.date<b.date?!0:!1},cannotAccept:function(a,b){if(a.status!=="ongoing"){if(a.method==="removeDocument"&&b.method==="loadDocument")return!0}else{if(a.method===b.method==="loadDocument")return!0;if(a.method==="removeDocument"&&(b.method==="loadDocument"||b.method==="removeDocument"))return!0;if(a.method===b.method==="saveDocument"&&a.fileContent===b.fileContent)return!0;if(a.method===b.method==="getDocumentList"||a.method===b.method==="checkNameAvailability")return!0}return!1},mustWait:function(a,b){return a.method==="getDocumentList"||a.method==="checkNameAvailability"||b.method==="getDocumentList"||b.method==="checkNameAvailability"?!1:!0}},queue_id:1,storage_type_object:{},max_wait_time:1e4},e=function(a,b){var c;for(c in b)a[c]=b[c];return a},f,g,h,i,j,k,l,m;return f=function(a,c){var d={},e={},f={},g,h;return e.eventAction=function(a){return h=a&&f[a],h||(g=b.Callbacks(),h={publish:g.fire,subscribe:g.add,unsubscribe:g.remove},a&&(f[a]=h)),h},d.publish=function(a,b){e.eventAction(a).publish(b)},d.subscribe=function(a,b){return e.eventAction(a).subscribe(b),b},d.unsubscribe=function(a,b){e.eventAction(a).unsubscribe(b)},d},g=function(a,b){var c=e({},a);return c.id=0,c.status="initial",c.date=Date.now(),c},h=function(b,f){var h={},i={},j="jio/idarray";return h.init=function(c){var d,e=function(){},f;i.use_local_storage&&(f=a.getItem(j)||[],b.publisher&&(i.publisher=b.publisher),i.jio_id=c.jio_id,i.job_object_name="jio/jobobject/"+i.jio_id,f.push(i.jio_id),a.setItem(j,f)),i.job_object={},h.copyJobQueueToLocalStorage();for(d in i.recovered_job_object)i.recovered_job_object[d].callback=e,h.addJob(i.recovered_job_object[d])},h.close=function(){JSON.stringify(i.job_object)==="{}"&&a.deleteItem(i.job_object_name)},h.getNewQueueID=function(){var b=null,c=0,e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)e[b]>=d.queue_id&&(d.queue_id=e[b]+1);return c=d.queue_id,d.queue_id++,c},h.recoverOlderJobObject=function(){var b=null,c=[],d=!1,e;if(i.use_local_storage){e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)a.getItem("jio/id/"+e[b])<Date.now()-1e4?(a.deleteItem("jio/id/"+e[b]),i.recovered_job_object=a.getItem("jio/jobobject/"+e[b]),a.deleteItem("jio/jobobject/"+e[b]),d=!0):c.push(e[b]);d&&a.setItem(j,c)}},h.isThereJobsWhere=function(a){var b="id";if(!a)return!0;for(b in i.job_object)if(a(i.job_object[b]))return!0;return!1},h.copyJobQueueToLocalStorage=function(){return i.use_local_storage?a.setItem(i.job_object_name,i.job_object):!1},h.createJob=function(a){return h.addJob(g(a))},h.addJob=function(a){var b=!0,c=[],e=[],f=[],g=null,j="id";for(j in i.job_object){if(d.job_managing_method.canRemoveFailOrDone(i.job_object[j],a)){f.push(j);continue}if(d.job_managing_method.canSelect(i.job_object[j],a)){if(d.job_managing_method.canEliminate(i.job_object[j],a)){c.push(j);continue}if(d.job_managing_method.canReplace(i.job_object[j],a)){g=k({queue:h,job:i.job_object[j]}),g.replace(a),b=!1;break}if(d.job_managing_method.cannotAccept(i.job_object[j],a))return!1;if(d.job_managing_method.mustWait(i.job_object[j],a)){e.push(j);continue}}}if(b){for(j=0;j<c.length;j+=1)g=k({queue:h,job:i.job_object[c[j]]}),g.eliminate();if(e.length>0){a.status="wait",a.waitingFor={jobIdArray:e};for(j=0;j<e.length;j+=1)i.job_object[e[j]]&&(i.job_object[e[j]].maxtries=1)}for(j=0;j<f.length;j+=1)h.removeJob(i.job_object[f[j]]);a.id=i.job_id,a.tries=0,i.job_id++,i.job_object[a.id]=a}return h.copyJobQueueToLocalStorage(),!0},h.removeJob=function(a){var b=e({where:function(a){return!0}},a),c,d=!1,f="key";if(b.job)i.job_object[b.job.id]&&b.where(i.job_object[b.job.id])&&(delete i.job_object[b.job.id],d=!0);else for(f in i.job_object)b.where(i.job_object[f])&&(delete i.job_object[f],d=!0);d||console.error("No jobs was found, when trying to remove some."),h.copyJobQueueToLocalStorage()},h.resetAll=function(){var a="id";for(a in i.job_object)i.job_object[a].status="initial";h.copyJobQueueToLocalStorage()},h.invokeAll=function(){var a="id",b,c;for(a in i.job_object){c=!1;if(i.job_object[a].status==="initial")h.invoke(i.job_object[a]);else if(i.job_object[a].status==="wait"){c=!0;if(i.job_object[a].waitingFor.jobIdArray)for(b=0;b<i.job_object[a].waitingFor.jobIdArray.length;b+=1)if(i.job_object[i.job_object[a].waitingFor.jobIdArray[b]]){c=!1;break}i.job_object[a].waitingFor.time&&i.job_object[a].waitingFor.time>Date.now()&&(c=!1),c&&h.invoke(i.job_object[a])}}this.copyJobQueueToLocalStorage()},h.invoke=function(a){var b;if(!c.job_method_object[a.method])return!1;h.isThereJobsWhere(function(b){return b.method===a.method&&b.method==="initial"})?a.status="ongoing":(a.status="ongoing",i.publisher.publish(c.job_method_object[a.method]["start_"+a.method])),b=k({queue:this,job:a}),b.execute()},h.ended=function(a){var b=e({},a);h.removeJob({job:b});if(!c.job_method_object[b.method])return!1;if(!h.isThereJobsWhere(function(a){return a.method===b.method&&a.status==="ongoing"||a.status==="initial"})){i.publisher.publish(c.job_method_object[b.method]["stop_"+b.method]);return}},h.clean=function(){h.removeJob(undefined,{where:function(a){return a.status==="fail"}})},i.use_local_storage=b.options.use_local_storage,i.publisher=b.publisher,i.job_id=1,i.jio_id=0,i.job_object_name="",i.job_object={},i.recovered_job_object={},h},i=function(a,b){var c={},d={};return d.interval=200,d.id=null,d.queue=a.queue,c.setIntervalDelay=function(a){d.interval=a},c.start=function(){return d.id?!1:(d.id=setInterval(function(){d.queue.recoverOlderJobObject(),d.queue.invokeAll()},d.interval),!0)},c.stop=function(){return d.id?(clearInterval(d.id),d.id=null,!0):!1},c},j=function(){var b={},c={};return c.interval=400,c.id=null,b.start=function(a){return c.id?!1:(b.touch(a),c.id=setInterval(function(){b.touch(a)},c.interval),!0)},b.stop=function(){return c.id?(clearInterval(c.id),c.id=null,!0):!1},b.touch=function(b){a.setItem("jio/id/"+b,Date.now())},b},k=function(a){var b={},c={};return c.job=a.job,c.callback=a.job.callback,c.queue=a.queue,c.res={status:"done",message:""},c.sorted=!1,c.fail_checkNameAvailability=function(){c.res.message="Unable to check name availability."},c.done_checkNameAvailability=function(a){c.res.message=c.job.userName+" is "+(a?"":"not ")+"available.",c.res.return_value=a},c.fail_loadDocument=function(){c.res.message="Unable to load document."},c.done_loadDocument=function(a){c.res.message="Document loaded.",c.res.return_value=a,c.res.return_value.lastModified=(new Date(c.res.return_value.lastModified)).getTime(),c.res.return_value.creationDate=(new Date(c.res.return_value.creationDate)).getTime()},c.fail_saveDocument=function(){c.res.message="Unable to save document."},c.done_saveDocument=function(){c.res.message="Document saved."},c.fail_getDocumentList=function(){c.res.message="Unable to retrieve document list."},c.done_getDocumentList=function(a){var d;c.res.message="Document list received.",c.res.return_value=a;for(d=0;d<c.res.return_value.length;d+=1)typeof c.res.return_value[d].lastModified!="number"&&(c.res.return_value[d].lastModified=(new Date(c.res.return_value[d].lastModified)).getTime()),typeof c.res.return_value[d].creationDate!="number"&&(c.res.return_value[d].creationDate=(new Date(c.res.return_value[d].creationDate)).getTime());typeof c.job.sort!="undefined"&&!c.sorted&&b.sortDocumentArray(c.res.return_value)},c.fail_removeDocument=function(){c.res.message="Unable to removed document."},c.done_removeDocument=function(){c.res.message="Document removed."},c.retryLater=function(){var a=c.job.tries*c.job.tries*1e3;a>d.max_wait_time&&(a=d.max_wait_time),c.job.status="wait",c.job.waitingFor={time:Date.now()+a}},b.cloneJob=function(){return e({},c.job)},b.getUserName=function(){return c.job.userName||""},b.getApplicantID=function(){return c.job.applicant.ID||""},b.getStorageUserName=function(){return c.job.storage.userName||""},b.getStoragePassword=function(){return c.job.storage.password||""},b.getStorageLocation=function(){return c.job.storage.location||""},b.getStorageArray=function(){return c.job.storage.storageArray||[]},b.getFileName=function(){return c.job.fileName||""},b.getFileContent=function(){return c.job.fileContent||""},b.cloneOptionObject=function(){return e({},c.job.options)},b.getMaxTries=function(){return c.job.maxtries},b.getTries=function(){return c.job.tries||0},b.setMaxTries=function(a){c.job.maxtries=a},b.addJob=function(a){return c.queue.createJob(a)},b.eliminate=function(){c.job.maxtries=1,c.job.tries=1,b.fail("Job Stopped!",0)},b.replace=function(a){c.job.tries=0,c.job.date=a.date,c.job.callback=a.callback,c.res.status="fail",c.res.message="Job Stopped!",c.res.error={},c.res.error.status=0,c.res.error.statusText="Replaced",c.res.error.message="The job was replaced by a newer one.",c["fail_"+c.job.method](),c.callback(c.res)},b.fail=function(a){c.res.status="fail",c.res.error=a,c.res.error.status=c.res.error.status||0,c.res.error.statusText=c.res.error.statusText||"Unknown Error",c.res.error.array=c.res.error.array||[],c.res.error.message=c.res.error.message||"",!c.job.maxtries||c.job.tries<c.job.maxtries?c.retryLater():(c.job.status="fail",c["fail_"+c.job.method](),c.queue.ended(c.job),c.callback(c.res))},b.done=function(a){c.job.status="done",c["done_"+c.job.method](a),c.queue.ended(c.job),c.callback(c.res)},b.execute=function(){return c.job.tries=b.getTries()+1,d.storage_type_object[c.job.storage.type]?d.storage_type_object[c.job.storage.type]({job:c.job,queue:c.queue})[c.job.method]():null},b.checkNameAvailability=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.loadDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.saveDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.getDocumentList=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.removeDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.sortDocumentArray=function(a){a.sort(function(a,b){var d,e;for(d in c.job.sort){var f=c.job.sort[d]==="descending"?-1:1;if(a[d]===b[d])continue;return a[d]>b[d]?f:-f}return 0}),b.sortDone()},b.sortDone=function(){c.sorted=!0},b},l=function(a,b){var c={},g={};g.wrongParametersError=function(a){var b="Method: "+a.method+", One or some parameters are undefined.";return console.error(b),a.callback({status:"fail",error:{status:0,statusText:"Undefined Parameter",message:b}}),null},c.getID=function(){return g.id},c.start=function(){return g.id!==0?!1:(g.id=g.queue.getNewQueueID(),g.queue.init({jio_id:g.id}),g.updater&&g.updater.start(g.id),g.listener.start(),g.ready=!0,c.isReady())},c.stop=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,g.id=0,!0},c.kill=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,!0},c.isReady=function(){return g.ready},c.publish=function(a,b){if(!c.isReady())return;return g.pubsub.publish(a,b)},c.subscribe=function(a,b){return g.pubsub.subscribe(a,b)},c.unsubscribe=function(a,b){return g.pubsub.unsubscribe(a,b)},c.checkNameAvailability=function(a){var b=e({userName:g.storage.userName,storage:g.storage,applicant:g.applicant,method:"checkNameAvailability",callback:function(){}},a);return c.isReady()&&b.userName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.saveDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,fileContent:"",method:"saveDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.loadDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"loadDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.getDocumentList=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"getDocumentList",callback:function(){}},a);return c.isReady()&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.removeDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"removeDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)};var k=e({use_local_storage:!0},a.options);return typeof a.storage=="string"&&(a.storage=JSON.parse(a.storage)),typeof a.applicant=="string"&&(a.applicant=JSON.parse(a.applicant)),g.storage=a.storage,g.applicant=a.applicant,g.id=0,g.pubsub=f({options:k}),g.queue=h({publisher:g.pubsub,options:k}),g.listener=i({queue:g.queue,options:k}),g.ready=!1,k.use_local_storage?g.updater=j({options:k}):g.updater=null,g.storage&&!d.storage_type_object[g.storage.type]&&console.error('Unknown storage type "'+g.storage.type+'"'),c.start(),c},m=function(a,b){var f={};return f.createNew=function(a,b,c){var d=e({use_local_storage:!0},c);return l({storage:a,applicant:b,options:d})},f.newBaseStorage=function(a,b){return k(a,b)},f.addStorageType=function(a,b){return a&&b?(d.storage_type_object[a]=b,!0):!1},f.getGlobalObject=function(){return d},f.getConstObject=function(){return e({},c)},f},m()};return window.requirejs?(define("JIO",["LocalOrCookieStorage","jQuery"],a),undefined):a(LocalOrCookieStorage,jQuery)}();
\ No newline at end of file
......@@ -704,6 +704,7 @@ var JIO =
priv.callback = options.job.callback;
priv.queue = options.queue;
priv.res = {'status':'done','message':''};
priv.sorted = false;
//// end Private attributes
//// Private Methods
......@@ -741,10 +742,22 @@ var JIO =
priv.res.message = 'Document list received.';
priv.res.return_value = documentlist;
for (i = 0; i < priv.res.return_value.length; i += 1) {
priv.res.return_value[i].lastModified =
new Date(priv.res.return_value[i].lastModified).getTime();
priv.res.return_value[i].creationDate =
new Date(priv.res.return_value[i].creationDate).getTime();
// transform current date format into ms since 1/1/1970
// useful for easy comparison
if (typeof priv.res.return_value[i].lastModified !== 'number') {
priv.res.return_value[i].lastModified =
new Date(priv.res.return_value[i].lastModified).
getTime();
}
if (typeof priv.res.return_value[i].creationDate !== 'number') {
priv.res.return_value[i].creationDate =
new Date(priv.res.return_value[i].creationDate).
getTime();
}
}
// check for sorting
if (!priv.sorted && typeof priv.job.sort !== 'undefined') {
that.sortDocumentArray({documentarray:priv.res.return_value});
}
};
priv.fail_removeDocument = function () {
......@@ -908,6 +921,34 @@ var JIO =
statusText:'Undefined Method',
message:'This method must be redefined!'});
};
/**
* Sorts a document list using sort parameters set in the job.
* @method sortDocumentArray
* @param {object} o
* - o.documentarray {array} the array we want to sort.
*/
that.sortDocumentArray = function (o) {
o.documentarray.sort(function (row1,row2) {
var k, res;
for (k in priv.job.sort) {
var sign = (priv.job.sort[k] === 'descending' ? -1 : 1);
if (row1[k] === row2[k]) { continue; }
return (row1[k] > row2[k] ? sign : -sign);
}
return 0;
});
that.sortDone();
};
/**
* Tells to this storage that the sorting process is already done.
* @method sortDone
*/
that.sortDone = function () {
priv.sorted = true;
};
//// end Public Methods
return that;
};
......
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