PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/files_versions/js/versioncollection.js

https://gitlab.com/wuhang2003/core
JavaScript | 97 lines | 64 code | 14 blank | 19 comment | 5 complexity | e21267661777961be61b06491d085dd3 MD5 | raw file
  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. };
  75. });
  76. this._endReached = result.data.endReached;
  77. this._currentIndex += results.length;
  78. return results;
  79. }
  80. });
  81. OCA.Versions = OCA.Versions || {};
  82. OCA.Versions.VersionCollection = VersionCollection;
  83. })();