entities_note_short.js.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: entities/note_short.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: entities/note_short.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import server from '../services/server.js';
  20. import noteAttributeCache from "../services/note_attribute_cache.js";
  21. import ws from "../services/ws.js";
  22. import options from "../services/options.js";
  23. import froca from "../services/froca.js";
  24. const LABEL = 'label';
  25. const RELATION = 'relation';
  26. const NOTE_TYPE_ICONS = {
  27. "file": "bx bx-file",
  28. "image": "bx bx-image",
  29. "code": "bx bx-code",
  30. "render": "bx bx-extension",
  31. "search": "bx bx-file-find",
  32. "relation-map": "bx bx-map-alt",
  33. "book": "bx bx-book",
  34. "note-map": "bx bx-map-alt",
  35. "mermaid": "bx bx-selection"
  36. };
  37. /**
  38. * FIXME: since there's no "full note" anymore we can rename this to Note
  39. *
  40. * This note's representation is used in note tree and is kept in Froca.
  41. */
  42. class NoteShort {
  43. /**
  44. * @param {Froca} froca
  45. * @param {Object.&lt;string, Object>} row
  46. */
  47. constructor(froca, row) {
  48. this.froca = froca;
  49. /** @type {string[]} */
  50. this.attributes = [];
  51. /** @type {string[]} */
  52. this.targetRelations = [];
  53. /** @type {string[]} */
  54. this.parents = [];
  55. /** @type {string[]} */
  56. this.children = [];
  57. /** @type {Object.&lt;string, string>} */
  58. this.parentToBranch = {};
  59. /** @type {Object.&lt;string, string>} */
  60. this.childToBranch = {};
  61. this.update(row);
  62. }
  63. update(row) {
  64. /** @type {string} */
  65. this.noteId = row.noteId;
  66. /** @type {string} */
  67. this.title = row.title;
  68. /** @type {boolean} */
  69. this.isProtected = !!row.isProtected;
  70. /**
  71. * one of 'text', 'code', 'file' or 'render'
  72. * @type {string}
  73. */
  74. this.type = row.type;
  75. /**
  76. * content-type, e.g. "application/json"
  77. * @type {string}
  78. */
  79. this.mime = row.mime;
  80. }
  81. addParent(parentNoteId, branchId) {
  82. if (parentNoteId === 'none') {
  83. return;
  84. }
  85. if (!this.parents.includes(parentNoteId)) {
  86. this.parents.push(parentNoteId);
  87. }
  88. this.parentToBranch[parentNoteId] = branchId;
  89. }
  90. addChild(childNoteId, branchId, sort = true) {
  91. if (!(childNoteId in this.childToBranch)) {
  92. this.children.push(childNoteId);
  93. }
  94. this.childToBranch[childNoteId] = branchId;
  95. if (sort) {
  96. this.sortChildren();
  97. }
  98. }
  99. sortChildren() {
  100. const branchIdPos = {};
  101. for (const branchId of Object.values(this.childToBranch)) {
  102. branchIdPos[branchId] = this.froca.getBranch(branchId).notePosition;
  103. }
  104. this.children.sort((a, b) => branchIdPos[this.childToBranch[a]] &lt; branchIdPos[this.childToBranch[b]] ? -1 : 1);
  105. }
  106. /** @returns {boolean} */
  107. isJson() {
  108. return this.mime === "application/json";
  109. }
  110. async getContent() {
  111. // we're not caching content since these objects are in froca and as such pretty long lived
  112. const note = await server.get("notes/" + this.noteId);
  113. return note.content;
  114. }
  115. async getJsonContent() {
  116. const content = await this.getContent();
  117. try {
  118. return JSON.parse(content);
  119. }
  120. catch (e) {
  121. console.log(`Cannot parse content of note ${this.noteId}: `, e.message);
  122. return null;
  123. }
  124. }
  125. /** @returns {string[]} */
  126. getBranchIds() {
  127. return Object.values(this.parentToBranch);
  128. }
  129. /** @returns {Branch[]} */
  130. getBranches() {
  131. const branchIds = Object.values(this.parentToBranch);
  132. return this.froca.getBranches(branchIds);
  133. }
  134. /** @returns {boolean} */
  135. hasChildren() {
  136. return this.children.length > 0;
  137. }
  138. /** @returns {Branch[]} */
  139. getChildBranches() {
  140. // don't use Object.values() to guarantee order
  141. const branchIds = this.children.map(childNoteId => this.childToBranch[childNoteId]);
  142. return this.froca.getBranches(branchIds);
  143. }
  144. /** @returns {string[]} */
  145. getParentNoteIds() {
  146. return this.parents;
  147. }
  148. /** @returns {NoteShort[]} */
  149. getParentNotes() {
  150. return this.froca.getNotesFromCache(this.parents);
  151. }
  152. // will sort the parents so that non-search &amp; non-archived are first and archived at the end
  153. // this is done so that non-search &amp; non-archived paths are always explored as first when looking for note path
  154. resortParents() {
  155. this.parents.sort((aNoteId, bNoteId) => {
  156. const aBranchId = this.parentToBranch[aNoteId];
  157. if (aBranchId &amp;&amp; aBranchId.startsWith('virt-')) {
  158. return 1;
  159. }
  160. const aNote = this.froca.getNoteFromCache([aNoteId]);
  161. if (aNote.hasLabel('archived')) {
  162. return 1;
  163. }
  164. return -1;
  165. });
  166. }
  167. /** @returns {string[]} */
  168. getChildNoteIds() {
  169. return this.children;
  170. }
  171. /** @returns {Promise&lt;NoteShort[]>} */
  172. async getChildNotes() {
  173. return await this.froca.getNotes(this.children);
  174. }
  175. /**
  176. * @param {string} [type] - (optional) attribute type to filter
  177. * @param {string} [name] - (optional) attribute name to filter
  178. * @returns {Attribute[]} all note's attributes, including inherited ones
  179. */
  180. getOwnedAttributes(type, name) {
  181. const attrs = this.attributes
  182. .map(attributeId => this.froca.attributes[attributeId])
  183. .filter(Boolean); // filter out nulls;
  184. return this.__filterAttrs(attrs, type, name);
  185. }
  186. /**
  187. * @param {string} [type] - (optional) attribute type to filter
  188. * @param {string} [name] - (optional) attribute name to filter
  189. * @returns {Attribute[]} all note's attributes, including inherited ones
  190. */
  191. getAttributes(type, name) {
  192. return this.__filterAttrs(this.__getCachedAttributes([]), type, name);
  193. }
  194. __getCachedAttributes(path) {
  195. // notes/clones cannot form tree cycles, it is possible to create attribute inheritance cycle via templates
  196. // when template instance is a parent of template itself
  197. if (path.includes(this.noteId)) {
  198. return [];
  199. }
  200. if (!(this.noteId in noteAttributeCache.attributes)) {
  201. const newPath = [...path, this.noteId];
  202. const attrArrs = [ this.getOwnedAttributes() ];
  203. if (this.noteId !== 'root') {
  204. for (const parentNote of this.getParentNotes()) {
  205. // these virtual parent-child relationships are also loaded into froca
  206. if (parentNote.type !== 'search') {
  207. attrArrs.push(parentNote.__getInheritableAttributes(newPath));
  208. }
  209. }
  210. }
  211. for (const templateAttr of attrArrs.flat().filter(attr => attr.type === 'relation' &amp;&amp; attr.name === 'template')) {
  212. const templateNote = this.froca.notes[templateAttr.value];
  213. if (templateNote &amp;&amp; templateNote.noteId !== this.noteId) {
  214. attrArrs.push(templateNote.__getCachedAttributes(newPath));
  215. }
  216. }
  217. noteAttributeCache.attributes[this.noteId] = [];
  218. const addedAttributeIds = new Set();
  219. for (const attr of attrArrs.flat()) {
  220. if (!addedAttributeIds.has(attr.attributeId)) {
  221. addedAttributeIds.add(attr.attributeId);
  222. noteAttributeCache.attributes[this.noteId].push(attr);
  223. }
  224. }
  225. }
  226. return noteAttributeCache.attributes[this.noteId];
  227. }
  228. isRoot() {
  229. return this.noted
  230. }
  231. getAllNotePaths(encounteredNoteIds = null) {
  232. if (this.noteId === 'root') {
  233. return [['root']];
  234. }
  235. if (!encounteredNoteIds) {
  236. encounteredNoteIds = new Set();
  237. }
  238. encounteredNoteIds.add(this.noteId);
  239. const parentNotes = this.getParentNotes();
  240. let paths;
  241. if (parentNotes.length === 1) { // optimization for the most common case
  242. if (encounteredNoteIds.has(parentNotes[0].noteId)) {
  243. return [];
  244. }
  245. else {
  246. paths = parentNotes[0].getAllNotePaths(encounteredNoteIds);
  247. }
  248. }
  249. else {
  250. paths = [];
  251. for (const parentNote of parentNotes) {
  252. if (encounteredNoteIds.has(parentNote.noteId)) {
  253. continue;
  254. }
  255. const newSet = new Set(encounteredNoteIds);
  256. paths.push(...parentNote.getAllNotePaths(newSet));
  257. }
  258. }
  259. for (const path of paths) {
  260. path.push(this.noteId);
  261. }
  262. return paths;
  263. }
  264. getSortedNotePaths(hoistedNotePath = 'root') {
  265. const notePaths = this.getAllNotePaths().map(path => ({
  266. notePath: path,
  267. isInHoistedSubTree: path.includes(hoistedNotePath),
  268. isArchived: path.find(noteId => froca.notes[noteId].hasLabel('archived')),
  269. isSearch: path.find(noteId => froca.notes[noteId].type === 'search'),
  270. isHidden: path.includes("hidden")
  271. }));
  272. notePaths.sort((a, b) => {
  273. if (a.isInHoistedSubTree !== b.isInHoistedSubTree) {
  274. return a.isInHoistedSubTree ? -1 : 1;
  275. } else if (a.isSearch !== b.isSearch) {
  276. return a.isSearch ? 1 : -1;
  277. } else if (a.isArchived !== b.isArchived) {
  278. return a.isArchived ? 1 : -1;
  279. } else {
  280. return a.notePath.length - b.notePath.length;
  281. }
  282. });
  283. return notePaths;
  284. }
  285. __filterAttrs(attributes, type, name) {
  286. if (!type &amp;&amp; !name) {
  287. return attributes;
  288. } else if (type &amp;&amp; name) {
  289. return attributes.filter(attr => attr.type === type &amp;&amp; attr.name === name);
  290. } else if (type) {
  291. return attributes.filter(attr => attr.type === type);
  292. } else if (name) {
  293. return attributes.filter(attr => attr.name === name);
  294. }
  295. }
  296. __getInheritableAttributes(path) {
  297. const attrs = this.__getCachedAttributes(path);
  298. return attrs.filter(attr => attr.isInheritable);
  299. }
  300. /**
  301. * @param {string} [name] - label name to filter
  302. * @returns {Attribute[]} all note's labels (attributes with type label), including inherited ones
  303. */
  304. getOwnedLabels(name) {
  305. return this.getOwnedAttributes(LABEL, name);
  306. }
  307. /**
  308. * @param {string} [name] - label name to filter
  309. * @returns {Attribute[]} all note's labels (attributes with type label), including inherited ones
  310. */
  311. getLabels(name) {
  312. return this.getAttributes(LABEL, name);
  313. }
  314. getIcon() {
  315. const iconClassLabels = this.getLabels('iconClass');
  316. const workspaceIconClass = this.getWorkspaceIconClass();
  317. if (iconClassLabels.length > 0) {
  318. return iconClassLabels[0].value;
  319. }
  320. else if (workspaceIconClass) {
  321. return workspaceIconClass;
  322. }
  323. else if (this.noteId === 'root') {
  324. return "bx bx-chevrons-right";
  325. }
  326. else if (this.type === 'text') {
  327. if (this.isFolder()) {
  328. return "bx bx-folder";
  329. }
  330. else {
  331. return "bx bx-note";
  332. }
  333. }
  334. else if (this.type === 'code' &amp;&amp; this.mime.startsWith('text/x-sql')) {
  335. return "bx bx-data";
  336. }
  337. else {
  338. return NOTE_TYPE_ICONS[this.type];
  339. }
  340. }
  341. isFolder() {
  342. return this.type === 'search'
  343. || this.getFilteredChildBranches().length > 0;
  344. }
  345. getFilteredChildBranches() {
  346. let childBranches = this.getChildBranches();
  347. if (!childBranches) {
  348. ws.logError(`No children for ${parentNote}. This shouldn't happen.`);
  349. return;
  350. }
  351. if (options.is("hideIncludedImages_main")) {
  352. const imageLinks = this.getRelations('imageLink');
  353. // image is already visible in the parent note so no need to display it separately in the book
  354. childBranches = childBranches.filter(branch => !imageLinks.find(rel => rel.value === branch.noteId));
  355. }
  356. // we're not checking hideArchivedNotes since that would mean we need to lazy load the child notes
  357. // which would seriously slow down everything.
  358. // we check this flag only once user chooses to expand the parent. This has the negative consequence that
  359. // note may appear as folder but not contain any children when all of them are archived
  360. return childBranches;
  361. }
  362. /**
  363. * @param {string} [name] - relation name to filter
  364. * @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
  365. */
  366. getOwnedRelations(name) {
  367. return this.getOwnedAttributes(RELATION, name);
  368. }
  369. /**
  370. * @param {string} [name] - relation name to filter
  371. * @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
  372. */
  373. getRelations(name) {
  374. return this.getAttributes(RELATION, name);
  375. }
  376. /**
  377. * @param {string} type - attribute type (label, relation, etc.)
  378. * @param {string} name - attribute name
  379. * @returns {boolean} true if note has an attribute with given type and name (including inherited)
  380. */
  381. hasAttribute(type, name) {
  382. return !!this.getAttribute(type, name);
  383. }
  384. /**
  385. * @param {string} type - attribute type (label, relation, etc.)
  386. * @param {string} name - attribute name
  387. * @returns {boolean} true if note has an attribute with given type and name (including inherited)
  388. */
  389. hasOwnedAttribute(type, name) {
  390. return !!this.getOwnedAttribute(type, name);
  391. }
  392. /**
  393. * @param {string} type - attribute type (label, relation, etc.)
  394. * @param {string} name - attribute name
  395. * @returns {Attribute} attribute of given type and name. If there's more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
  396. */
  397. getOwnedAttribute(type, name) {
  398. const attributes = this.getOwnedAttributes(type, name);
  399. return attributes.length > 0 ? attributes[0] : 0;
  400. }
  401. /**
  402. * @param {string} type - attribute type (label, relation, etc.)
  403. * @param {string} name - attribute name
  404. * @returns {Attribute} attribute of given type and name. If there's more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
  405. */
  406. getAttribute(type, name) {
  407. const attributes = this.getAttributes(type, name);
  408. return attributes.length > 0 ? attributes[0] : null;
  409. }
  410. /**
  411. * @param {string} type - attribute type (label, relation, etc.)
  412. * @param {string} name - attribute name
  413. * @returns {string} attribute value of given type and name or null if no such attribute exists.
  414. */
  415. getOwnedAttributeValue(type, name) {
  416. const attr = this.getOwnedAttribute(type, name);
  417. return attr ? attr.value : null;
  418. }
  419. /**
  420. * @param {string} type - attribute type (label, relation, etc.)
  421. * @param {string} name - attribute name
  422. * @returns {string} attribute value of given type and name or null if no such attribute exists.
  423. */
  424. getAttributeValue(type, name) {
  425. const attr = this.getAttribute(type, name);
  426. return attr ? attr.value : null;
  427. }
  428. /**
  429. * @param {string} name - label name
  430. * @returns {boolean} true if label exists (excluding inherited)
  431. */
  432. hasOwnedLabel(name) { return this.hasOwnedAttribute(LABEL, name); }
  433. /**
  434. * @param {string} name - label name
  435. * @returns {boolean} true if label exists (including inherited)
  436. */
  437. hasLabel(name) { return this.hasAttribute(LABEL, name); }
  438. /**
  439. * @param {string} name - relation name
  440. * @returns {boolean} true if relation exists (excluding inherited)
  441. */
  442. hasOwnedRelation(name) { return this.hasOwnedAttribute(RELATION, name); }
  443. /**
  444. * @param {string} name - relation name
  445. * @returns {boolean} true if relation exists (including inherited)
  446. */
  447. hasRelation(name) { return this.hasAttribute(RELATION, name); }
  448. /**
  449. * @param {string} name - label name
  450. * @returns {Attribute} label if it exists, null otherwise
  451. */
  452. getOwnedLabel(name) { return this.getOwnedAttribute(LABEL, name); }
  453. /**
  454. * @param {string} name - label name
  455. * @returns {Attribute} label if it exists, null otherwise
  456. */
  457. getLabel(name) { return this.getAttribute(LABEL, name); }
  458. /**
  459. * @param {string} name - relation name
  460. * @returns {Attribute} relation if it exists, null otherwise
  461. */
  462. getOwnedRelation(name) { return this.getOwnedAttribute(RELATION, name); }
  463. /**
  464. * @param {string} name - relation name
  465. * @returns {Attribute} relation if it exists, null otherwise
  466. */
  467. getRelation(name) { return this.getAttribute(RELATION, name); }
  468. /**
  469. * @param {string} name - label name
  470. * @returns {string} label value if label exists, null otherwise
  471. */
  472. getOwnedLabelValue(name) { return this.getOwnedAttributeValue(LABEL, name); }
  473. /**
  474. * @param {string} name - label name
  475. * @returns {string} label value if label exists, null otherwise
  476. */
  477. getLabelValue(name) { return this.getAttributeValue(LABEL, name); }
  478. /**
  479. * @param {string} name - relation name
  480. * @returns {string} relation value if relation exists, null otherwise
  481. */
  482. getOwnedRelationValue(name) { return this.getOwnedAttributeValue(RELATION, name); }
  483. /**
  484. * @param {string} name - relation name
  485. * @returns {string} relation value if relation exists, null otherwise
  486. */
  487. getRelationValue(name) { return this.getAttributeValue(RELATION, name); }
  488. /**
  489. * @param {string} name
  490. * @returns {Promise&lt;NoteShort>|null} target note of the relation or null (if target is empty or note was not found)
  491. */
  492. async getRelationTarget(name) {
  493. const targets = await this.getRelationTargets(name);
  494. return targets.length > 0 ? targets[0] : null;
  495. }
  496. /**
  497. * @param {string} [name] - relation name to filter
  498. * @returns {Promise&lt;NoteShort[]>}
  499. */
  500. async getRelationTargets(name) {
  501. const relations = this.getRelations(name);
  502. const targets = [];
  503. for (const relation of relations) {
  504. targets.push(await this.froca.getNote(relation.value));
  505. }
  506. return targets;
  507. }
  508. /**
  509. * @returns {NoteShort[]}
  510. */
  511. getTemplateNotes() {
  512. const relations = this.getRelations('template');
  513. return relations.map(rel => this.froca.notes[rel.value]);
  514. }
  515. getPromotedDefinitionAttributes() {
  516. if (this.hasLabel('hidePromotedAttributes')) {
  517. return [];
  518. }
  519. return this.getAttributes()
  520. .filter(attr => attr.isDefinition())
  521. .filter(attr => {
  522. const def = attr.getDefinition();
  523. return def &amp;&amp; def.isPromoted;
  524. });
  525. }
  526. hasAncestor(ancestorNote, visitedNoteIds = null) {
  527. if (this.noteId === ancestorNote.noteId) {
  528. return true;
  529. }
  530. if (!visitedNoteIds) {
  531. visitedNoteIds = new Set();
  532. } else if (visitedNoteIds.has(this.noteId)) {
  533. // to avoid infinite cycle when template is descendent of the instance
  534. return false;
  535. }
  536. visitedNoteIds.add(this.noteId);
  537. for (const templateNote of this.getTemplateNotes()) {
  538. if (templateNote.hasAncestor(ancestorNote, visitedNoteIds)) {
  539. return true;
  540. }
  541. }
  542. for (const parentNote of this.getParentNotes()) {
  543. if (parentNote.hasAncestor(ancestorNote, visitedNoteIds)) {
  544. return true;
  545. }
  546. }
  547. return false;
  548. }
  549. /**
  550. * @deprecated NOOP
  551. */
  552. invalidateAttributeCache() {}
  553. /**
  554. * Get relations which target this note
  555. *
  556. * @returns {Attribute[]}
  557. */
  558. getTargetRelations() {
  559. return this.targetRelations
  560. .map(attributeId => this.froca.attributes[attributeId]);
  561. }
  562. /**
  563. * Get relations which target this note
  564. *
  565. * @returns {NoteShort[]}
  566. */
  567. async getTargetRelationSourceNotes() {
  568. const targetRelations = this.getTargetRelations();
  569. return await this.froca.getNotes(targetRelations.map(tr => tr.noteId));
  570. }
  571. /**
  572. * Return note complement which is most importantly note's content
  573. *
  574. * @return {Promise&lt;NoteComplement>}
  575. */
  576. async getNoteComplement() {
  577. return await this.froca.getNoteComplement(this.noteId);
  578. }
  579. toString() {
  580. return `Note(noteId=${this.noteId}, title=${this.title})`;
  581. }
  582. get dto() {
  583. const dto = Object.assign({}, this);
  584. delete dto.froca;
  585. return dto;
  586. }
  587. getCssClass() {
  588. const labels = this.getLabels('cssClass');
  589. return labels.map(l => l.value).join(' ');
  590. }
  591. getWorkspaceIconClass() {
  592. const labels = this.getLabels('workspaceIconClass');
  593. return labels.length > 0 ? labels[0].value : "";
  594. }
  595. getWorkspaceTabBackgroundColor() {
  596. const labels = this.getLabels('workspaceTabBackgroundColor');
  597. return labels.length > 0 ? labels[0].value : "";
  598. }
  599. /** @returns {boolean} true if this note is JavaScript (code or attachment) */
  600. isJavaScript() {
  601. return (this.type === "code" || this.type === "file")
  602. &amp;&amp; (this.mime.startsWith("application/javascript")
  603. || this.mime === "application/x-javascript"
  604. || this.mime === "text/javascript");
  605. }
  606. /** @returns {boolean} true if this note is HTML */
  607. isHtml() {
  608. return (this.type === "code" || this.type === "file" || this.type === "render") &amp;&amp; this.mime === "text/html";
  609. }
  610. /** @returns {string|null} JS script environment - either "frontend" or "backend" */
  611. getScriptEnv() {
  612. if (this.isHtml() || (this.isJavaScript() &amp;&amp; this.mime.endsWith('env=frontend'))) {
  613. return "frontend";
  614. }
  615. if (this.type === 'render') {
  616. return "frontend";
  617. }
  618. if (this.isJavaScript() &amp;&amp; this.mime.endsWith('env=backend')) {
  619. return "backend";
  620. }
  621. return null;
  622. }
  623. async executeScript() {
  624. if (!this.isJavaScript()) {
  625. throw new Error(`Note ${this.noteId} is of type ${this.type} and mime ${this.mime} and thus cannot be executed`);
  626. }
  627. const env = this.getScriptEnv();
  628. if (env === "frontend") {
  629. const bundleService = (await import("../services/bundle.js")).default;
  630. await bundleService.getAndExecuteBundle(this.noteId);
  631. }
  632. else if (env === "backend") {
  633. await server.post('script/run/' + this.noteId);
  634. }
  635. else {
  636. throw new Error(`Unrecognized env type ${env} for note ${this.noteId}`);
  637. }
  638. }
  639. }
  640. export default NoteShort;
  641. </code></pre>
  642. </article>
  643. </section>
  644. </div>
  645. <nav>
  646. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Attribute.html">Attribute</a></li><li><a href="Branch.html">Branch</a></li><li><a href="FrontendScriptApi.html">FrontendScriptApi</a></li><li><a href="NoteComplement.html">NoteComplement</a></li><li><a href="NoteShort.html">NoteShort</a></li></ul><h3>Global</h3><ul><li><a href="global.html#doRenderBody">doRenderBody</a></li></ul>
  647. </nav>
  648. <br class="clear">
  649. <footer>
  650. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a>
  651. </footer>
  652. <script> prettyPrint(); </script>
  653. <script src="scripts/linenumber.js"> </script>
  654. </body>
  655. </html>