becca_entities_branch.js.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: becca/entities/branch.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: becca/entities/branch.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. const Note = require('./note');
  21. const AbstractEntity = require("./abstract_entity");
  22. const sql = require("../../services/sql");
  23. const dateUtils = require("../../services/date_utils");
  24. /**
  25. * Branch represents a relationship between a child note and its parent note. Trilium allows a note to have multiple
  26. * parents.
  27. */
  28. class Branch extends AbstractEntity {
  29. static get entityName() { return "branches"; }
  30. static get primaryKeyName() { return "branchId"; }
  31. // notePosition is not part of hash because it would produce a lot of updates in case of reordering
  32. static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "prefix"]; }
  33. constructor(row) {
  34. super();
  35. if (!row) {
  36. return;
  37. }
  38. this.updateFromRow(row);
  39. this.init();
  40. }
  41. updateFromRow(row) {
  42. this.update([
  43. row.branchId,
  44. row.noteId,
  45. row.parentNoteId,
  46. row.prefix,
  47. row.notePosition,
  48. row.isExpanded,
  49. row.utcDateModified
  50. ]);
  51. }
  52. update([branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified]) {
  53. /** @type {string} */
  54. this.branchId = branchId;
  55. /** @type {string} */
  56. this.noteId = noteId;
  57. /** @type {string} */
  58. this.parentNoteId = parentNoteId;
  59. /** @type {string} */
  60. this.prefix = prefix;
  61. /** @type {int} */
  62. this.notePosition = notePosition;
  63. /** @type {boolean} */
  64. this.isExpanded = !!isExpanded;
  65. /** @type {string} */
  66. this.utcDateModified = utcDateModified;
  67. return this;
  68. }
  69. init() {
  70. if (this.branchId) {
  71. this.becca.branches[this.branchId] = this;
  72. }
  73. this.becca.childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this;
  74. if (this.branchId === 'root') {
  75. return;
  76. }
  77. const childNote = this.childNote;
  78. const parentNote = this.parentNote;
  79. if (!childNote.parents.includes(parentNote)) {
  80. childNote.parents.push(parentNote);
  81. }
  82. if (!childNote.parentBranches.includes(this)) {
  83. childNote.parentBranches.push(this);
  84. }
  85. if (!parentNote.children.includes(childNote)) {
  86. parentNote.children.push(childNote);
  87. }
  88. }
  89. /** @returns {Note} */
  90. get childNote() {
  91. if (!(this.noteId in this.becca.notes)) {
  92. // entities can come out of order in sync/import, create skeleton which will be filled later
  93. this.becca.addNote(this.noteId, new Note({noteId: this.noteId}));
  94. }
  95. return this.becca.notes[this.noteId];
  96. }
  97. getNote() {
  98. return this.childNote;
  99. }
  100. /** @returns {Note} */
  101. get parentNote() {
  102. if (!(this.parentNoteId in this.becca.notes)) {
  103. // entities can come out of order in sync/import, create skeleton which will be filled later
  104. this.becca.addNote(this.parentNoteId, new Note({noteId: this.parentNoteId}));
  105. }
  106. return this.becca.notes[this.parentNoteId];
  107. }
  108. get isDeleted() {
  109. return !(this.branchId in this.becca.branches);
  110. }
  111. beforeSaving() {
  112. if (this.notePosition === undefined || this.notePosition === null) {
  113. // TODO finding new position can be refactored into becca
  114. const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]);
  115. this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10;
  116. }
  117. if (!this.isExpanded) {
  118. this.isExpanded = false;
  119. }
  120. this.utcDateModified = dateUtils.utcNowDateTime();
  121. super.beforeSaving();
  122. this.becca.branches[this.branchId] = this;
  123. }
  124. getPojo() {
  125. return {
  126. branchId: this.branchId,
  127. noteId: this.noteId,
  128. parentNoteId: this.parentNoteId,
  129. prefix: this.prefix,
  130. notePosition: this.notePosition,
  131. isExpanded: this.isExpanded,
  132. isDeleted: false,
  133. utcDateModified: this.utcDateModified
  134. };
  135. }
  136. createClone(parentNoteId, notePosition) {
  137. return new Branch({
  138. noteId: this.noteId,
  139. parentNoteId: parentNoteId,
  140. notePosition: notePosition,
  141. prefix: this.prefix,
  142. isExpanded: this.isExpanded
  143. });
  144. }
  145. }
  146. module.exports = Branch;
  147. </code></pre>
  148. </article>
  149. </section>
  150. </div>
  151. <nav>
  152. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-sql.html">sql</a></li></ul><h3>Classes</h3><ul><li><a href="Attribute.html">Attribute</a></li><li><a href="BackendScriptApi.html">BackendScriptApi</a></li><li><a href="Branch.html">Branch</a></li><li><a href="EtapiToken.html">EtapiToken</a></li><li><a href="Note.html">Note</a></li><li><a href="NoteRevision.html">NoteRevision</a></li><li><a href="Option.html">Option</a></li><li><a href="RecentNote.html">RecentNote</a></li></ul><h3><a href="global.html">Global</a></h3>
  153. </nav>
  154. <br class="clear">
  155. <footer>
  156. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a>
  157. </footer>
  158. <script> prettyPrint(); </script>
  159. <script src="scripts/linenumber.js"> </script>
  160. </body>
  161. </html>