becca_entities_battribute.js.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: becca/entities/battribute.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/battribute.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>"use strict";
  20. const BNote = require('./bnote');
  21. const AbstractBeccaEntity = require("./abstract_becca_entity");
  22. const sql = require("../../services/sql");
  23. const dateUtils = require("../../services/date_utils");
  24. const promotedAttributeDefinitionParser = require("../../services/promoted_attribute_definition_parser");
  25. const {sanitizeAttributeName} = require("../../services/sanitize_attribute_name");
  26. /**
  27. * There are currently only two types of attributes, labels or relations.
  28. * @typedef {"label" | "relation"} AttributeType
  29. */
  30. /**
  31. * Attribute is an abstract concept which has two real uses - label (key - value pair)
  32. * and relation (representing named relationship between source and target note)
  33. *
  34. * @extends AbstractBeccaEntity
  35. */
  36. class BAttribute extends AbstractBeccaEntity {
  37. static get entityName() { return "attributes"; }
  38. static get primaryKeyName() { return "attributeId"; }
  39. static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable"]; }
  40. constructor(row) {
  41. super();
  42. if (!row) {
  43. return;
  44. }
  45. this.updateFromRow(row);
  46. this.init();
  47. }
  48. updateFromRow(row) {
  49. this.update([
  50. row.attributeId,
  51. row.noteId,
  52. row.type,
  53. row.name,
  54. row.value,
  55. row.isInheritable,
  56. row.position,
  57. row.utcDateModified
  58. ]);
  59. }
  60. update([attributeId, noteId, type, name, value, isInheritable, position, utcDateModified]) {
  61. /** @type {string} */
  62. this.attributeId = attributeId;
  63. /** @type {string} */
  64. this.noteId = noteId;
  65. /** @type {AttributeType} */
  66. this.type = type;
  67. /** @type {string} */
  68. this.name = name;
  69. /** @type {int} */
  70. this.position = position;
  71. /** @type {string} */
  72. this.value = value || "";
  73. /** @type {boolean} */
  74. this.isInheritable = !!isInheritable;
  75. /** @type {string} */
  76. this.utcDateModified = utcDateModified;
  77. return this;
  78. }
  79. init() {
  80. if (this.attributeId) {
  81. this.becca.attributes[this.attributeId] = this;
  82. }
  83. if (!(this.noteId in this.becca.notes)) {
  84. // entities can come out of order in sync, create skeleton which will be filled later
  85. this.becca.addNote(this.noteId, new BNote({noteId: this.noteId}));
  86. }
  87. this.becca.notes[this.noteId].ownedAttributes.push(this);
  88. const key = `${this.type}-${this.name.toLowerCase()}`;
  89. this.becca.attributeIndex[key] = this.becca.attributeIndex[key] || [];
  90. this.becca.attributeIndex[key].push(this);
  91. const targetNote = this.targetNote;
  92. if (targetNote) {
  93. targetNote.targetRelations.push(this);
  94. }
  95. }
  96. validate() {
  97. if (!["label", "relation"].includes(this.type)) {
  98. throw new Error(`Invalid attribute type '${this.type}' in attribute '${this.attributeId}' of note '${this.noteId}'`);
  99. }
  100. if (!this.name?.trim()) {
  101. throw new Error(`Invalid empty name in attribute '${this.attributeId}' of note '${this.noteId}'`);
  102. }
  103. if (this.type === 'relation' &amp;&amp; !(this.value in this.becca.notes)) {
  104. throw new Error(`Cannot save relation '${this.name}' of note '${this.noteId}' since it targets not existing note '${this.value}'.`);
  105. }
  106. }
  107. get isAffectingSubtree() {
  108. return this.isInheritable
  109. || (this.type === 'relation' &amp;&amp; ['template', 'inherit'].includes(this.name));
  110. }
  111. get targetNoteId() { // alias
  112. return this.type === 'relation' ? this.value : undefined;
  113. }
  114. isAutoLink() {
  115. return this.type === 'relation' &amp;&amp; ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
  116. }
  117. get note() {
  118. return this.becca.notes[this.noteId];
  119. }
  120. get targetNote() {
  121. if (this.type === 'relation') {
  122. return this.becca.notes[this.value];
  123. }
  124. }
  125. /**
  126. * @returns {BNote|null}
  127. */
  128. getNote() {
  129. const note = this.becca.getNote(this.noteId);
  130. if (!note) {
  131. throw new Error(`Note '${this.noteId}' of attribute '${this.attributeId}', type '${this.type}', name '${this.name}' does not exist.`);
  132. }
  133. return note;
  134. }
  135. /**
  136. * @returns {BNote|null}
  137. */
  138. getTargetNote() {
  139. if (this.type !== 'relation') {
  140. throw new Error(`Attribute '${this.attributeId}' is not a relation.`);
  141. }
  142. if (!this.value) {
  143. return null;
  144. }
  145. return this.becca.getNote(this.value);
  146. }
  147. /**
  148. * @returns {boolean}
  149. */
  150. isDefinition() {
  151. return this.type === 'label' &amp;&amp; (this.name.startsWith('label:') || this.name.startsWith('relation:'));
  152. }
  153. getDefinition() {
  154. return promotedAttributeDefinitionParser.parse(this.value);
  155. }
  156. getDefinedName() {
  157. if (this.type === 'label' &amp;&amp; this.name.startsWith('label:')) {
  158. return this.name.substr(6);
  159. } else if (this.type === 'label' &amp;&amp; this.name.startsWith('relation:')) {
  160. return this.name.substr(9);
  161. } else {
  162. return this.name;
  163. }
  164. }
  165. get isDeleted() {
  166. return !(this.attributeId in this.becca.attributes);
  167. }
  168. beforeSaving(opts = {}) {
  169. if (!opts.skipValidation) {
  170. this.validate();
  171. }
  172. this.name = sanitizeAttributeName(this.name);
  173. if (!this.value) {
  174. // null value isn't allowed
  175. this.value = "";
  176. }
  177. if (this.position === undefined || this.position === null) {
  178. const maxExistingPosition = this.getNote().getAttributes()
  179. .reduce((maxPosition, attr) => Math.max(maxPosition, attr.position || 0), 0);
  180. this.position = maxExistingPosition + 10;
  181. }
  182. if (!this.isInheritable) {
  183. this.isInheritable = false;
  184. }
  185. this.utcDateModified = dateUtils.utcNowDateTime();
  186. super.beforeSaving();
  187. this.becca.attributes[this.attributeId] = this;
  188. }
  189. getPojo() {
  190. return {
  191. attributeId: this.attributeId,
  192. noteId: this.noteId,
  193. type: this.type,
  194. name: this.name,
  195. position: this.position,
  196. value: this.value,
  197. isInheritable: this.isInheritable,
  198. utcDateModified: this.utcDateModified,
  199. isDeleted: false
  200. };
  201. }
  202. createClone(type, name, value, isInheritable) {
  203. return new BAttribute({
  204. noteId: this.noteId,
  205. type: type,
  206. name: name,
  207. value: value,
  208. position: this.position,
  209. isInheritable: isInheritable,
  210. utcDateModified: this.utcDateModified
  211. });
  212. }
  213. }
  214. module.exports = BAttribute;
  215. </code></pre>
  216. </article>
  217. </section>
  218. </div>
  219. <nav>
  220. <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="AbstractBeccaEntity.html">AbstractBeccaEntity</a></li><li><a href="BAttachment.html">BAttachment</a></li><li><a href="BAttribute.html">BAttribute</a></li><li><a href="BBranch.html">BBranch</a></li><li><a href="BEtapiToken.html">BEtapiToken</a></li><li><a href="BNote.html">BNote</a></li><li><a href="BOption.html">BOption</a></li><li><a href="BRecentNote.html">BRecentNote</a></li><li><a href="BRevision.html">BRevision</a></li><li><a href="BackendScriptApi.html">BackendScriptApi</a></li></ul><h3>Global</h3><ul><li><a href="global.html#api">api</a></li></ul>
  221. </nav>
  222. <br class="clear">
  223. <footer>
  224. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a>
  225. </footer>
  226. <script> prettyPrint(); </script>
  227. <script src="scripts/linenumber.js"> </script>
  228. </body>
  229. </html>