@lexho111/plainblog 0.5.28 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/Article.js +73 -4
  2. package/Blog.js +341 -140
  3. package/Formatter.js +2 -11
  4. package/README.md +1 -1
  5. package/{blog → blog_test_empty.db} +0 -0
  6. package/blog_test_load.db +0 -0
  7. package/build-scripts.js +54 -0
  8. package/coverage/clover.xml +1043 -0
  9. package/coverage/coverage-final.json +20 -0
  10. package/coverage/lcov-report/base.css +224 -0
  11. package/coverage/lcov-report/block-navigation.js +87 -0
  12. package/coverage/lcov-report/favicon.png +0 -0
  13. package/coverage/lcov-report/index.html +161 -0
  14. package/coverage/lcov-report/package/Article.js.html +406 -0
  15. package/coverage/lcov-report/package/Blog.js.html +2635 -0
  16. package/coverage/lcov-report/package/Formatter.js.html +379 -0
  17. package/coverage/lcov-report/package/build-scripts.js.html +247 -0
  18. package/coverage/lcov-report/package/build-styles.js.html +367 -0
  19. package/coverage/lcov-report/package/index.html +191 -0
  20. package/coverage/lcov-report/package/model/APIModel.js.html +190 -0
  21. package/coverage/lcov-report/package/model/ArrayList.js.html +382 -0
  22. package/coverage/lcov-report/package/model/ArrayListHashMap.js.html +379 -0
  23. package/coverage/lcov-report/package/model/BinarySearchTree.js.html +856 -0
  24. package/coverage/lcov-report/package/model/BinarySearchTreeHashMap.js.html +346 -0
  25. package/coverage/lcov-report/package/model/DataModel.js.html +307 -0
  26. package/coverage/lcov-report/package/model/DatabaseModel.js.html +232 -0
  27. package/coverage/lcov-report/package/model/FileAdapter.js.html +394 -0
  28. package/coverage/lcov-report/package/model/FileList.js.html +244 -0
  29. package/coverage/lcov-report/package/model/FileModel.js.html +358 -0
  30. package/coverage/lcov-report/package/model/SequelizeAdapter.js.html +538 -0
  31. package/coverage/lcov-report/package/model/SqliteAdapter.js.html +247 -0
  32. package/coverage/lcov-report/package/model/datastructures/ArrayList.js.html +439 -0
  33. package/coverage/lcov-report/package/model/datastructures/ArrayListHashMap.js.html +196 -0
  34. package/coverage/lcov-report/package/model/datastructures/BinarySearchTree.js.html +913 -0
  35. package/coverage/lcov-report/package/model/datastructures/BinarySearchTreeHashMap.js.html +346 -0
  36. package/coverage/lcov-report/package/model/datastructures/FileList.js.html +244 -0
  37. package/coverage/lcov-report/package/model/datastructures/index.html +176 -0
  38. package/coverage/lcov-report/package/model/index.html +206 -0
  39. package/coverage/lcov-report/package/utilities.js.html +511 -0
  40. package/coverage/lcov-report/prettify.css +1 -0
  41. package/coverage/lcov-report/prettify.js +2 -0
  42. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  43. package/coverage/lcov-report/sorter.js +210 -0
  44. package/coverage/lcov.info +2063 -0
  45. package/index.js +25 -1
  46. package/model/DataModel.js +79 -0
  47. package/model/DatabaseModel.js +20 -8
  48. package/model/FileAdapter.js +43 -4
  49. package/model/FileModel.js +47 -9
  50. package/model/SequelizeAdapter.js +11 -3
  51. package/model/datastructures/ArrayList.js +118 -0
  52. package/model/datastructures/ArrayListHashMap.js +37 -0
  53. package/model/datastructures/ArrayListHashMap.js.bk +90 -0
  54. package/model/datastructures/BinarySearchTree.js +276 -0
  55. package/model/datastructures/BinarySearchTreeHashMap.js +89 -0
  56. package/model/datastructures/BinarySearchTreeTest.js +16 -0
  57. package/model/datastructures/FileList.js +53 -0
  58. package/package.json +10 -2
  59. package/public/fetchData.js +0 -0
  60. package/public/scripts.min.js +2 -1
  61. package/public/styles.min.css +2 -1
  62. package/src/fetchData.js +150 -30
  63. package/src/styles.css +29 -0
  64. package/utilities.js +142 -0
package/Article.js CHANGED
@@ -1,14 +1,36 @@
1
1
  export default class Article {
2
- constructor(title, content, createdAt) {
2
+ constructor(id, title, content, createdAt) {
3
+ this.id = id;
3
4
  this.title = title;
4
5
  this.content = content;
5
- this.createdAt = createdAt;
6
+ this.createdAt = new Date(createdAt).getTime();
7
+ }
8
+
9
+ static createNew(title, content, createdAt = new Date()) {
10
+ const id = this.getNextID(); // Your existing ID generator
11
+ //const createdAt = new Date();
12
+ return new Article(id, title, content, createdAt);
13
+ }
14
+
15
+ static nextID = 100;
16
+ static getNextID() {
17
+ // TODO fix use nextID from BlogTreeSearch!
18
+ this.nextID++;
19
+ return Math.floor(Math.random() * 100000000);
6
20
  }
7
21
 
8
22
  getContent() {
9
23
  return this.content;
10
24
  }
11
25
 
26
+ getContentVeryShort() {
27
+ if (!this.content) return;
28
+ if (this.content.length < 50) return this.getContent();
29
+ let contentShort = this.content.substring(0, 50);
30
+ contentShort += "...";
31
+ return contentShort;
32
+ }
33
+
12
34
  getContentShort() {
13
35
  if (this.content.length < 400) return this.getContent();
14
36
  let contentShort = this.content.substring(0, 400);
@@ -26,13 +48,60 @@ export default class Article {
26
48
  return `${year}/${month}/${day} ${hours}:${minutes}`;
27
49
  }
28
50
 
29
- toHTML() {
51
+ toHTML(loggedin = false) {
52
+ if (this.content == null) return "";
30
53
  const moreButton = this.content.length > 400 ? `<a href="#">more</a>` : "";
31
- return ` <article>
54
+ const buttons = loggedin
55
+ ? `<div class="buttons"><div id="editButton${this.id}" class="button edit">edit</div><div id="deleteButton${this.id}" class="button delete">delete</div></div>`
56
+ : "";
57
+ const script = loggedin
58
+ ? `<script>
59
+ if (typeof window.handleAction !== 'function') {
60
+ window.handleAction = function(action, id) {
61
+ if (action === 'delete') {
62
+ if (confirm("Delete this article?")) {
63
+ fetch("/api/articles?id=" + id, { method: "DELETE" })
64
+ .then(res => { if(res.ok) window.location.reload(); else alert("Error: " + res.statusText); });
65
+ }
66
+ } else if (action === 'edit') {
67
+ const title = prompt("New Title");
68
+ const content = prompt("New Content");
69
+ if (title && content) {
70
+ fetch("/api/articles?id=" + id, {
71
+ method: "PUT",
72
+ body: JSON.stringify({ title, content })
73
+ }).then(res => { if(res.ok) window.location.reload(); else alert("Error: " + res.statusText); });
74
+ }
75
+ }
76
+ }
77
+ }
78
+ function clickListener(button) {
79
+ const mybutton = document.getElementById(button);
80
+ const action = button.includes("edit") ? "edit" : "delete";
81
+
82
+ // Mouse/Touch support
83
+ mybutton.addEventListener('click', () => window.handleAction(action, ${this.id}));
84
+
85
+ // Keyboard support
86
+ mybutton.addEventListener('keydown', (event) => {
87
+ if (event.key === 'Enter' || event.key === ' ') {
88
+ event.preventDefault(); // Prevents page scrolling on Space
89
+ window.handleAction(action, ${this.id});
90
+ }
91
+ });
92
+ }
93
+ clickListener("editButton${this.id}");
94
+ clickListener("deleteButton${this.id}");
95
+ </script>`
96
+ : "";
97
+ // prettier-ignore
98
+ return `<article data-id="${this.id}" data-date="${this.createdAt}">
99
+ ${buttons}
32
100
  <h2>${this.title}</h2>
33
101
  <span class="datetime">${this.getFormattedDate()}</span>
34
102
  <p>${this.getContentShort()}</p>
35
103
  ${moreButton}
104
+ ${script}
36
105
  </article>`;
37
106
  }
38
107
  }