PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/app/scripts/collections/notes.js

https://gitlab.com/dannywillems/laverna
JavaScript | 143 lines | 96 code | 20 blank | 27 comment | 8 complexity | 180235b03a89d55ed79862beda17bd97 MD5 | raw file
  1. /*global define*/
  2. define([
  3. 'underscore',
  4. 'backbone',
  5. 'migrations/note',
  6. 'models/note',
  7. 'indexedDB'
  8. ], function (_, Backbone, NotesDB, Note) {
  9. 'use strict';
  10. var Notes = Backbone.Collection.extend({
  11. model: Note,
  12. database : NotesDB,
  13. storeName : 'notes',
  14. initialize: function () {
  15. },
  16. comparator: function (model) {
  17. return -model.get('created');
  18. },
  19. filterList: function (filter, query) {
  20. var res;
  21. switch (filter) {
  22. case 'favorite':
  23. res = this.getFavorites();
  24. break;
  25. case 'notebook':
  26. res = this.getNotebookNotes(query);
  27. break;
  28. case 'trashed':
  29. res = this.getTrashed();
  30. break;
  31. default:
  32. res = this.getActive();
  33. break;
  34. }
  35. return this.reset(res);
  36. },
  37. /**
  38. * Filter the list of all notes that are favorite
  39. */
  40. getFavorites: function () {
  41. return this.filter(function (note) {
  42. return note.get('isFavorite') === 1 && note.get('trash') === 0;
  43. });
  44. },
  45. /**
  46. * Only active notes
  47. */
  48. getActive: function () {
  49. return this.without.apply(this, this.getTrashed());
  50. },
  51. /**
  52. * Show only notebook's notes
  53. */
  54. getNotebookNotes: function ( notebookId ) {
  55. return this.filter(function (note) {
  56. var notebook = note.get('notebookId');
  57. if (notebook !== 0) {
  58. return notebook.get('id') === notebookId && note.get('trash') === 0;
  59. }
  60. });
  61. },
  62. /**
  63. * Show only tag's notes
  64. */
  65. getTagged: function ( tagName ) {
  66. return this.filter(function (note) {
  67. if (note.get('tags').length > 0) {
  68. return (_.indexOf(note.get('tags'), tagName) !== -1) && note.get('trash') === 0;
  69. }
  70. });
  71. },
  72. /**
  73. * Filter the list of notes that are removed to trash
  74. */
  75. getTrashed: function () {
  76. return this.filter(function (note) {
  77. return note.get('trash') === 1;
  78. });
  79. },
  80. /**
  81. * Filter: only unencrypted, JSON data probably encrypted data
  82. */
  83. getUnEncrypted: function () {
  84. return this.filter(function (note) {
  85. try {
  86. JSON.parse(note.get('title'));
  87. return false;
  88. } catch (e) {
  89. return true;
  90. }
  91. });
  92. },
  93. /**
  94. * Search
  95. */
  96. search : function(letters) {
  97. if (letters === '') {
  98. return this;
  99. }
  100. var pattern = new RegExp(letters, 'gim'),
  101. data;
  102. return this.filter(function(model) {
  103. data = model.decrypt();
  104. pattern.lastIndex = 0; // Reuse regexp
  105. return pattern.test(data.title) || pattern.test(data.content);
  106. });
  107. },
  108. /**
  109. * Pagination
  110. * @var int perPage
  111. * @var int page
  112. */
  113. pagination : function (page, perPage) {
  114. var collection = this;
  115. collection = _(collection.rest(page));
  116. collection = _(collection.first(perPage));
  117. return collection.map( function(model) {
  118. return model;
  119. });
  120. }
  121. });
  122. return Notes;
  123. });