summaryrefslogtreecommitdiffstats
path: root/apps/comments/js/commentcollection.js
blob: 1fda4a4c709e713b8c68d8a3acd3d5d11a6e3948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * 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) {

	var NS_OWNCLOUD = 'http://owncloud.org/ns';

	/**
	 * @class OCA.Comments.CommentsCollection
	 * @classdesc
	 *
	 * Collection of comments assigned to a file
	 *
	 */
	var CommentsCollection = OC.Backbone.Collection.extend(
		/** @lends OCA.Comments.CommentsCollection.prototype */ {

		sync: OC.Backbone.davSync,

		model: OCA.Comments.CommentModel,

		_objectType: 'files',
		_objectId: null,

		_endReached: false,
		_limit : 5,

		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;
			return OC.Backbone.Collection.prototype.reset.apply(this, arguments);
		},

		/**
		 * Fetch the next set of results
		 */
		fetchNext: function(options) {
			var self = this;
			if (!this.hasMoreResults()) {
				return null;
			}

			var body = '<?xml version="1.0" encoding="utf-8" ?>\n' +
				'<D:report xmlns:D="DAV:" xmlns:oc="http://owncloud.org/ns">\n' +
				'   <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);
		}
	});

	OCA.Comments.CommentsCollection = CommentsCollection;
})(OC, OCA);