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 @@ ...@@ -316,6 +316,8 @@
} }
priv.loading_object.getlist(); priv.loading_object.getlist();
priv.jio.getDocumentList({ priv.jio.getDocumentList({
'sort':{'lastModified':'descending',
'fileName':'ascending'},
'maxtries':3, 'maxtries':3,
'callback':function (result) { 'callback':function (result) {
if (result.status === 'done') { if (result.status === 'done') {
...@@ -333,6 +335,8 @@ ...@@ -333,6 +335,8 @@
// clone document list // clone document list
var array = $.extend(true,[],priv.data_object.documentList), i, var array = $.extend(true,[],priv.data_object.documentList), i,
lm, cd; 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) { for (i = 0; i < array.length; i += 1) {
lm = (new Date(array[i].lastModified)); lm = (new Date(array[i].lastModified));
cd = (new Date(array[i].creationDate)); 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 */ * Copyright (c) 2012 Nexedi; Licensed */
...@@ -707,6 +707,7 @@ var JIO = ...@@ -707,6 +707,7 @@ var JIO =
priv.callback = options.job.callback; priv.callback = options.job.callback;
priv.queue = options.queue; priv.queue = options.queue;
priv.res = {'status':'done','message':''}; priv.res = {'status':'done','message':''};
priv.sorted = false;
//// end Private attributes //// end Private attributes
//// Private Methods //// Private Methods
...@@ -744,10 +745,22 @@ var JIO = ...@@ -744,10 +745,22 @@ var JIO =
priv.res.message = 'Document list received.'; priv.res.message = 'Document list received.';
priv.res.return_value = documentlist; priv.res.return_value = documentlist;
for (i = 0; i < priv.res.return_value.length; i += 1) { for (i = 0; i < priv.res.return_value.length; i += 1) {
priv.res.return_value[i].lastModified = // transform current date format into ms since 1/1/1970
new Date(priv.res.return_value[i].lastModified).getTime(); // useful for easy comparison
priv.res.return_value[i].creationDate = if (typeof priv.res.return_value[i].lastModified !== 'number') {
new Date(priv.res.return_value[i].creationDate).getTime(); 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 () { priv.fail_removeDocument = function () {
...@@ -911,6 +924,23 @@ var JIO = ...@@ -911,6 +924,23 @@ var JIO =
statusText:'Undefined Method', statusText:'Undefined Method',
message:'This method must be redefined!'}); 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 //// end Public Methods
return that; return that;
}; };
......
This diff is collapsed.
...@@ -704,6 +704,7 @@ var JIO = ...@@ -704,6 +704,7 @@ var JIO =
priv.callback = options.job.callback; priv.callback = options.job.callback;
priv.queue = options.queue; priv.queue = options.queue;
priv.res = {'status':'done','message':''}; priv.res = {'status':'done','message':''};
priv.sorted = false;
//// end Private attributes //// end Private attributes
//// Private Methods //// Private Methods
...@@ -741,10 +742,22 @@ var JIO = ...@@ -741,10 +742,22 @@ var JIO =
priv.res.message = 'Document list received.'; priv.res.message = 'Document list received.';
priv.res.return_value = documentlist; priv.res.return_value = documentlist;
for (i = 0; i < priv.res.return_value.length; i += 1) { for (i = 0; i < priv.res.return_value.length; i += 1) {
priv.res.return_value[i].lastModified = // transform current date format into ms since 1/1/1970
new Date(priv.res.return_value[i].lastModified).getTime(); // useful for easy comparison
priv.res.return_value[i].creationDate = if (typeof priv.res.return_value[i].lastModified !== 'number') {
new Date(priv.res.return_value[i].creationDate).getTime(); 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 () { priv.fail_removeDocument = function () {
...@@ -908,6 +921,34 @@ var JIO = ...@@ -908,6 +921,34 @@ var JIO =
statusText:'Undefined Method', statusText:'Undefined Method',
message:'This method must be redefined!'}); 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 //// end Public Methods
return that; 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