PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/scripts/collections/notebooks.js

https://gitlab.com/dannywillems/laverna
JavaScript | 94 lines | 59 code | 16 blank | 19 comment | 3 complexity | 94e9d3f6285f683bf26d58cb0bf8efc5 MD5 | raw file
  1. /*global define*/
  2. define([
  3. 'underscore',
  4. 'backbone',
  5. 'migrations/note',
  6. 'models/notebook',
  7. 'indexedDB'
  8. ], function (_, Backbone, NotesDB, Notebook) {
  9. 'use strict';
  10. /**
  11. * Notebooks collection
  12. */
  13. var Notebooks = Backbone.Collection.extend({
  14. model: Notebook,
  15. database: NotesDB,
  16. storeName: 'notebooks',
  17. /**
  18. * Filter for tree structure
  19. */
  20. getTree: function (parents, tree) {
  21. var self = this,
  22. childs;
  23. parents = (parents || this.getRoots());
  24. tree = (tree || []);
  25. _.forEach(parents, function (model) {
  26. tree.push(model);
  27. childs = self.getChilds(model.get('id'));
  28. if (childs.length > 0) {
  29. childs = self.getTree(childs, tree);
  30. }
  31. });
  32. return tree;
  33. },
  34. getChilds: function (parentId) {
  35. return this.filter(function (model) {
  36. return model.get('parentId') === parentId;
  37. });
  38. },
  39. /**
  40. * Finds notebooks childrens
  41. */
  42. getChildrens: function () {
  43. return this.filter(function (notebook) {
  44. return notebook.get('parentId') !== '0';
  45. });
  46. },
  47. /**
  48. * Only root notebooks
  49. */
  50. getRoots: function () {
  51. return this.without.apply(this, this.getChildrens());
  52. },
  53. /**
  54. * Filter: only unencrypted, JSON data probably encrypted data
  55. */
  56. getUnEncrypted: function () {
  57. return this.filter(function (notebook) {
  58. try {
  59. JSON.parse(notebook.get('name'));
  60. return false;
  61. } catch (e) {
  62. return true;
  63. }
  64. });
  65. },
  66. /**
  67. * Decrypt all models in collection
  68. */
  69. decrypt: function () {
  70. var data = [];
  71. this.each(function (model) {
  72. data.push(model.decrypt());
  73. });
  74. return data;
  75. }
  76. });
  77. return Notebooks;
  78. });