Add pagination support for comments GUI

This commit is contained in:
Vincent Petry 2016-01-28 14:24:12 +01:00
parent d1518045ec
commit 29386eccf9
4 changed files with 77 additions and 37 deletions

View File

@ -10,9 +10,7 @@
(function(OC, OCA) { (function(OC, OCA) {
function filterFunction(model, term) { var NS_OWNCLOUD = 'http://owncloud.org/ns';
return model.get('name').substr(0, term.length) === term;
}
/** /**
* @class OCA.Comments.CommentsCollection * @class OCA.Comments.CommentsCollection
@ -32,7 +30,7 @@
_objectId: null, _objectId: null,
_endReached: false, _endReached: false,
_currentIndex: 0, _limit : 5,
initialize: function(models, options) { initialize: function(models, options) {
options = options || {}; options = options || {};
@ -58,22 +56,54 @@
return !this._endReached; return !this._endReached;
}, },
reset: function() {
this._endReached = false;
return OC.Backbone.Collection.prototype.reset.apply(this, arguments);
},
/** /**
* Fetch the next set of results * Fetch the next set of results
*/ */
fetchNext: function() { fetchNext: function(options) {
var self = this;
if (!this.hasMoreResults()) { if (!this.hasMoreResults()) {
return null; return null;
} }
if (this._currentIndex === 0) {
return this.fetch();
}
return this.fetch({remove: false});
},
reset: function() { var body = '<?xml version="1.0" encoding="utf-8" ?>\n' +
this._currentIndex = 0; '<D:report xmlns:D="DAV:" xmlns:oc="http://owncloud.org/ns">\n' +
OC.Backbone.Collection.prototype.reset.apply(this, arguments); ' <oc:limit>' + this._limit + '</oc:limit>\n';
if (this.length > 0) {
body += ' <oc:datetime>' + this.first().get('creationDateTime') + '</oc:datetime>\n';
}
body += '</D:report>\n';
var oldLength = this.length;
options = options || {};
var success = options.success;
options = _.extend({
remove: false,
data: body,
davProperties: CommentsCollection.prototype.model.prototype.davProperties,
success: function(resp) {
if (resp.length === oldLength) {
// no new entries, end reached
self._endReached = true;
}
if (!self.set(resp, options)) {
return false;
}
if (success) {
success.apply(null, arguments);
}
self.trigger('sync', 'REPORT', self, options);
}
}, options);
return this.sync('REPORT', this, options);
} }
}); });

View File

@ -19,10 +19,8 @@
' </ul>' + ' </ul>' +
'</div>' + '</div>' +
'<div class="empty hidden">{{emptyResultLabel}}</div>' + '<div class="empty hidden">{{emptyResultLabel}}</div>' +
/*
'<input type="button" class="showMore hidden" value="{{moreLabel}}"' + '<input type="button" class="showMore hidden" value="{{moreLabel}}"' +
' name="show-more" id="show-more" />' + ' name="show-more" id="show-more" />' +
*/
'<div class="loading hidden" style="height: 50px"></div>'; '<div class="loading hidden" style="height: 50px"></div>';
var COMMENT_TEMPLATE = var COMMENT_TEMPLATE =
@ -44,7 +42,8 @@
className: 'tab commentsTabView', className: 'tab commentsTabView',
events: { events: {
'submit .newCommentForm': '_onSubmitComment' 'submit .newCommentForm': '_onSubmitComment',
'click .showMore': '_onClickShowMore'
}, },
initialize: function() { initialize: function() {
@ -144,7 +143,7 @@
this.collection.fetchNext(); this.collection.fetchNext();
}, },
_onClickShowMoreVersions: function(ev) { _onClickShowMore: function(ev) {
ev.preventDefault(); ev.preventDefault();
this.nextPage(); this.nextPage();
}, },

View File

@ -76,6 +76,11 @@
* @param {Object} davProperties properties mapping * @param {Object} davProperties properties mapping
*/ */
function parsePropFindResult(result, davProperties) { function parsePropFindResult(result, davProperties) {
if (_.isArray(result)) {
return _.map(result, function(subResult) {
return parsePropFindResult(subResult, davProperties);
});
}
var props = { var props = {
href: result.href href: result.href
}; };
@ -151,15 +156,10 @@
if (isSuccessStatus(response.status)) { if (isSuccessStatus(response.status)) {
if (_.isFunction(options.success)) { if (_.isFunction(options.success)) {
var propsMapping = _.invert(options.davProperties); var propsMapping = _.invert(options.davProperties);
var results; var results = parsePropFindResult(response.body, propsMapping);
if (options.depth > 0) { if (options.depth > 0) {
results = _.map(response.body, function(data) {
return parsePropFindResult(data, propsMapping);
});
// discard root entry // discard root entry
results.shift(); results.shift();
} else {
results = parsePropFindResult(response.body, propsMapping);
} }
options.success(results); options.success(results);
@ -217,7 +217,13 @@
options.success(responseJson); options.success(responseJson);
return; return;
} }
options.success(result.body); // if multi-status, parse
if (result.status === 207) {
var propsMapping = _.invert(options.davProperties);
options.success(parsePropFindResult(result.body, propsMapping));
} else {
options.success(result.body);
}
} }
}); });
} }
@ -249,7 +255,7 @@
* DAV transport * DAV transport
*/ */
function davSync(method, model, options) { function davSync(method, model, options) {
var params = {type: methodMap[method]}; var params = {type: methodMap[method] || method};
var isCollection = (model instanceof Backbone.Collection); var isCollection = (model instanceof Backbone.Collection);
if (method === 'update' && (model.usePUT || (model.collection && model.collection.usePUT))) { if (method === 'update' && (model.usePUT || (model.collection && model.collection.usePUT))) {

View File

@ -1,17 +1,17 @@
if (typeof dav == 'undefined') { dav = {}; }; if (typeof dav == 'undefined') { dav = {}; };
dav._XML_CHAR_MAP = { dav._XML_CHAR_MAP = {
'<': '&lt;', '<': '&lt;',
'>': '&gt;', '>': '&gt;',
'&': '&amp;', '&': '&amp;',
'"': '&quot;', '"': '&quot;',
"'": '&apos;' "'": '&apos;'
}; };
dav._escapeXml = function(s) { dav._escapeXml = function(s) {
return s.replace(/[<>&"']/g, function (ch) { return s.replace(/[<>&"']/g, function (ch) {
return dav._XML_CHAR_MAP[ch]; return dav._XML_CHAR_MAP[ch];
}); });
}; };
dav.Client = function(options) { dav.Client = function(options) {
@ -79,17 +79,16 @@ dav.Client.prototype = {
return this.request('PROPFIND', url, headers, body).then( return this.request('PROPFIND', url, headers, body).then(
function(result) { function(result) {
var resultBody = this.parseMultiStatus(result.body);
if (depth===0) { if (depth===0) {
return { return {
status: result.status, status: result.status,
body: resultBody[0], body: result.body[0],
xhr: result.xhr xhr: result.xhr
}; };
} else { } else {
return { return {
status: result.status, status: result.status,
body: resultBody, body: result.body,
xhr: result.xhr xhr: result.xhr
}; };
} }
@ -161,6 +160,7 @@ dav.Client.prototype = {
*/ */
request : function(method, url, headers, body) { request : function(method, url, headers, body) {
var self = this;
var xhr = this.xhrProvider(); var xhr = this.xhrProvider();
if (this.userName) { if (this.userName) {
@ -182,8 +182,13 @@ dav.Client.prototype = {
return; return;
} }
var resultBody = xhr.response;
if (xhr.status === 207) {
resultBody = self.parseMultiStatus(xhr.response);
}
fulfill({ fulfill({
body: xhr.response, body: resultBody,
status: xhr.status, status: xhr.status,
xhr: xhr xhr: xhr
}); });