versioncollection.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2015
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function() {
  11. /**
  12. * @memberof OCA.Versions
  13. */
  14. var VersionCollection = OC.Backbone.Collection.extend({
  15. model: OCA.Versions.VersionModel,
  16. /**
  17. * @var OCA.Files.FileInfoModel
  18. */
  19. _fileInfo: null,
  20. _endReached: false,
  21. _currentIndex: 0,
  22. url: function() {
  23. var url = OC.generateUrl('/apps/files_versions/ajax/getVersions.php');
  24. var query = {
  25. source: this._fileInfo.getFullPath(),
  26. start: this._currentIndex
  27. };
  28. return url + '?' + OC.buildQueryString(query);
  29. },
  30. setFileInfo: function(fileInfo) {
  31. this._fileInfo = fileInfo;
  32. // reset
  33. this._endReached = false;
  34. this._currentIndex = 0;
  35. },
  36. getFileInfo: function() {
  37. return this._fileInfo;
  38. },
  39. hasMoreResults: function() {
  40. return !this._endReached;
  41. },
  42. fetch: function(options) {
  43. if (!options || options.remove) {
  44. this._currentIndex = 0;
  45. }
  46. return OC.Backbone.Collection.prototype.fetch.apply(this, arguments);
  47. },
  48. /**
  49. * Fetch the next set of results
  50. */
  51. fetchNext: function() {
  52. if (!this.hasMoreResults()) {
  53. return null;
  54. }
  55. if (this._currentIndex === 0) {
  56. return this.fetch();
  57. }
  58. return this.fetch({remove: false});
  59. },
  60. reset: function() {
  61. this._currentIndex = 0;
  62. OC.Backbone.Collection.prototype.reset.apply(this, arguments);
  63. },
  64. parse: function(result) {
  65. var fullPath = this._fileInfo.getFullPath();
  66. var results = _.map(result.data.versions, function(version) {
  67. var revision = parseInt(version.version, 10);
  68. return {
  69. id: revision,
  70. name: version.name,
  71. fullPath: fullPath,
  72. timestamp: revision,
  73. size: version.size,
  74. mimetype: version.mimetype
  75. };
  76. });
  77. this._endReached = result.data.endReached;
  78. this._currentIndex += results.length;
  79. return results;
  80. }
  81. });
  82. OCA.Versions = OCA.Versions || {};
  83. OCA.Versions.VersionCollection = VersionCollection;
  84. })();