server/apps/comments/js/commentcollection.js

168 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-01-27 17:28:55 +00:00
/*
* Copyright (c) 2016
*
* This file is licensed under the Affero General Public License version 3
* or later.
*
* See the COPYING-README file.
*
*/
(function(OC, OCA) {
/**
2016-02-02 14:13:45 +00:00
* @class OCA.Comments.CommentCollection
2016-01-27 17:28:55 +00:00
* @classdesc
*
* Collection of comments assigned to a file
*
*/
2016-02-02 14:13:45 +00:00
var CommentCollection = OC.Backbone.Collection.extend(
/** @lends OCA.Comments.CommentCollection.prototype */ {
2016-01-27 17:28:55 +00:00
sync: OC.Backbone.davSync,
model: OCA.Comments.CommentModel,
/**
* Object type
*
* @type string
*/
2016-01-27 17:28:55 +00:00
_objectType: 'files',
/**
* Object id
*
* @type string
*/
2016-01-27 17:28:55 +00:00
_objectId: null,
/**
* True if there are no more page results left to fetch
*
* @type bool
*/
2016-01-27 17:28:55 +00:00
_endReached: false,
/**
* Number of comments to fetch per page
*
* @type int
*/
2016-02-01 15:54:18 +00:00
_limit : 20,
2016-01-27 17:28:55 +00:00
/**
* Initializes the collection
*
* @param {string} [options.objectType] object type
* @param {string} [options.objectId] object id
*/
2016-01-27 17:28:55 +00:00
initialize: function(models, options) {
options = options || {};
if (options.objectType) {
this._objectType = options.objectType;
}
if (options.objectId) {
this._objectId = options.objectId;
}
},
url: function() {
return OC.linkToRemote('dav') + '/comments/' +
encodeURIComponent(this._objectType) + '/' +
encodeURIComponent(this._objectId) + '/';
},
setObjectId: function(objectId) {
this._objectId = objectId;
},
hasMoreResults: function() {
return !this._endReached;
},
reset: function() {
this._endReached = false;
this._summaryModel = null;
return OC.Backbone.Collection.prototype.reset.apply(this, arguments);
},
2016-01-27 17:28:55 +00:00
/**
* Fetch the next set of results
*/
fetchNext: function(options) {
var self = this;
2016-01-27 17:28:55 +00:00
if (!this.hasMoreResults()) {
return null;
}
var body = '<?xml version="1.0" encoding="utf-8" ?>\n' +
2016-02-02 17:06:16 +00:00
'<oc:filter-comments xmlns:D="DAV:" xmlns:oc="http://owncloud.org/ns">\n' +
2016-02-01 15:54:18 +00:00
// load one more so we know there is more
' <oc:limit>' + (this._limit + 1) + '</oc:limit>\n' +
' <oc:offset>' + this.length + '</oc:offset>\n' +
2016-02-02 17:06:16 +00:00
'</oc:filter-comments>\n';
options = options || {};
var success = options.success;
options = _.extend({
remove: false,
parse: true,
data: body,
2016-02-02 14:13:45 +00:00
davProperties: CommentCollection.prototype.model.prototype.davProperties,
success: function(resp) {
2016-02-01 15:54:18 +00:00
if (resp.length <= self._limit) {
// no new entries, end reached
self._endReached = true;
2016-02-01 15:54:18 +00:00
} else {
// remove last entry, for next page load
resp = _.initial(resp);
}
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);
},
/**
* Returns the matching summary model
*
* @return {OCA.Comments.CommentSummaryModel} summary model
*/
getSummaryModel: function() {
if (!this._summaryModel) {
this._summaryModel = new OCA.Comments.CommentSummaryModel({
id: this._objectId,
objectType: this._objectType
});
}
return this._summaryModel;
},
/**
* Updates the read marker for this comment thread
*
* @param {Date} [date] optional date, defaults to now
* @param {Object} [options] backbone options
*/
updateReadMarker: function(date, options) {
options = options || {};
return this.getSummaryModel().save({
readMarker: (date || new Date()).toUTCString()
}, options);
2016-01-27 17:28:55 +00:00
}
});
2016-02-02 14:13:45 +00:00
OCA.Comments.CommentCollection = CommentCollection;
2016-01-27 17:28:55 +00:00
})(OC, OCA);