PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/client/src/collection.js

https://gitlab.com/johanlindberg/irvato-crm
JavaScript | 147 lines | 86 code | 34 blank | 27 comment | 19 complexity | 39de4aa46fe6551f76e70c31f00d38bb MD5 | raw file
  1. /************************************************************************
  2. * This file is part of EspoCRM.
  3. *
  4. * EspoCRM - Open Source CRM application.
  5. * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
  6. * Website: http://www.espocrm.com
  7. *
  8. * EspoCRM is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * EspoCRM is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
  20. *
  21. * The interactive user interfaces in modified source and object code versions
  22. * of this program must display Appropriate Legal Notices, as required under
  23. * Section 5 of the GNU General Public License version 3.
  24. *
  25. * In accordance with Section 7(b) of the GNU General Public License version 3,
  26. * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
  27. ************************************************************************/
  28. Espo.define('collection', [], function () {
  29. var Collection = Backbone.Collection.extend({
  30. name: null,
  31. total: 0,
  32. offset: 0,
  33. maxSize: 20,
  34. sortBy: 'id',
  35. asc: false,
  36. where: null,
  37. whereAdditional: null,
  38. _user: null,
  39. initialize: function (models, options) {
  40. options = options || {};
  41. this.name = options.name || this.name;
  42. this.urlRoot = this.urlRoot || this.name;
  43. this.url = this.url || this.urlRoot;
  44. this.sortBy = options.sortBy || this.sortBy;
  45. this.asc = ('asc' in options) ? options.asc : this.asc;
  46. this.data = {};
  47. Backbone.Collection.prototype.initialize.call(this);
  48. },
  49. _onModelEvent: function(event, model, collection, options) {
  50. if (event === 'sync' && collection !== this) return;
  51. Backbone.Collection.prototype._onModelEvent.apply(this, arguments);
  52. },
  53. sort: function (field, asc) {
  54. this.sortBy = field;
  55. this.asc = asc;
  56. this.fetch();
  57. },
  58. nextPage: function () {
  59. var offset = this.offset + this.maxSize;
  60. this.setOffset(offset);
  61. },
  62. previousPage: function () {
  63. var offset = this.offset - this.maxSize;
  64. this.setOffset(offset);
  65. },
  66. firstPage: function () {
  67. this.setOffset(0);
  68. },
  69. lastPage: function () {
  70. var offset = this.total - this.total % this.maxSize;
  71. this.setOffset(offset);
  72. },
  73. setOffset: function (offset) {
  74. if (offset < 0) {
  75. throw new RangeError('offset can not be less than 0');
  76. }
  77. if (offset > this.total) {
  78. throw new RangeError('offset can not be larger than total count');
  79. }
  80. this.offset = offset;
  81. this.fetch();
  82. },
  83. parse: function (response) {
  84. this.total = response.total;
  85. return response.list;
  86. },
  87. fetch: function (options) {
  88. var options = options || {};
  89. options.data = _.extend(options.data || {}, this.data);
  90. this.offset = options.offset || this.offset;
  91. this.sortBy = options.sortBy || this.sortBy;
  92. this.asc = options.asc || this.asc;
  93. this.where = options.where || this.where;
  94. if (!('maxSize' in options)) {
  95. options.data.maxSize = options.more ? this.maxSize : ((this.length > this.maxSize) ? this.length : this.maxSize);
  96. } else {
  97. options.data.maxSize = options.maxSize;
  98. }
  99. options.data.offset = options.more ? this.length : this.offset;
  100. options.data.sortBy = this.sortBy;
  101. options.data.asc = this.asc;
  102. options.data.where = this.getWhere();
  103. return Backbone.Collection.prototype.fetch.call(this, options);
  104. },
  105. getWhere: function () {
  106. return (this.where || []).concat(this.whereAdditional || []);
  107. },
  108. getUser: function () {
  109. return this._user;
  110. }
  111. });
  112. return Collection;
  113. });