Started code review + some fixes (unfinished)
[vchess.git] / client / src / views / News.vue
1 <template lang="pug">
2 main
3 input#modalNews.modal(type="checkbox")
4 div#newnewsDiv(role="dialog" data-checkbox="modalNews")
5 .card
6 label.modal-close(for="modalNews")
7 textarea#newsContent(
8 v-model="curnews.content"
9 :placeholder="st.tr['News go here']"
10 @input="adjustHeight"
11 )
12 button(@click="sendNews()") {{ st.tr["Send"] }}
13 #dialog.text-center {{ st.tr[infoMsg] }}
14 .row
15 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
16 button#writeNews(
17 v-if="devs.includes(st.user.id)"
18 @click="showModalNews"
19 )
20 | {{ st.tr["Write news"] }}
21 .news(v-for="n,idx in newsList" :class="{margintop:idx>0}")
22 span.ndt {{ formatDatetime(n.added) }}
23 div(v-if="devs.includes(st.user.id)")
24 button(@click="editNews(n)") {{ st.tr["Edit"] }}
25 button(@click="deleteNews(n)") {{ st.tr["Delete"] }}
26 p(v-html="parseHtml(n.content)")
27 button(v-if="hasMore" @click="loadMore()")
28 | {{ st.tr["Load more"] }}
29 </template>
30
31 <script>
32 import { store } from "@/store";
33 import { ajax } from "@/utils/ajax";
34 import { getDate, getTime } from "@/utils/datetime";
35 import { processModalClick } from "@/utils/modalClick";
36 export default {
37 name: "my-news",
38 data: function() {
39 return {
40 devs: [1], //for now the only dev is me
41 st: store.state,
42 cursor: 0, //ID of last showed news
43 hasMore: true, //a priori there could be more news to load
44 curnews: { id: 0, content: "" },
45 newsList: [],
46 infoMsg: ""
47 };
48 },
49 created: function() {
50 ajax("/news", "GET", { cursor: this.cursor }, res => {
51 this.newsList = res.newsList.sort((n1, n2) => n1.added - n2.added);
52 const L = res.newsList.length;
53 if (L > 0) this.cursor = this.newsList[0].id;
54 });
55 },
56 mounted: function() {
57 document
58 .getElementById("newnewsDiv")
59 .addEventListener("click", processModalClick);
60 },
61 methods: {
62 formatDatetime: function(dt) {
63 const dtObj = new Date(dt);
64 const timePart = getTime(dtObj);
65 // Show minutes but not seconds:
66 return (
67 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
68 );
69 },
70 parseHtml: function(txt) {
71 return !txt.match(/<[/a-zA-Z]+>/)
72 ? txt.replace(/\n/g, "<br/>") //no HTML tag
73 : txt;
74 },
75 adjustHeight: function() {
76 const newsContent = document.getElementById("newsContent");
77 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
78 newsContent.style.height = "1px";
79 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
80 },
81 resetCurnews: function() {
82 this.curnews.id = 0;
83 this.curnews.content = "";
84 // No need for added and uid fields: never updated
85 },
86 showModalNews: function() {
87 this.resetCurnews();
88 window.doClick("modalNews");
89 },
90 sendNews: function() {
91 const edit = this.curnews.id > 0;
92 this.infoMsg = "Processing... Please wait";
93 ajax("/news", edit ? "PUT" : "POST", { news: this.curnews }, res => {
94 if (edit) {
95 let n = this.newsList.find(n => n.id == this.curnews.id);
96 if (n) n.content = this.curnews.content;
97 } else {
98 const newNews = {
99 content: this.curnews.content,
100 added: Date.now(),
101 uid: this.st.user.id,
102 id: res.id
103 };
104 this.newsList = [newNews].concat(this.newsList);
105 }
106 document.getElementById("modalNews").checked = false;
107 this.infoMsg = "";
108 this.resetCurnews();
109 });
110 },
111 editNews: function(n) {
112 this.curnews.content = n.content;
113 this.curnews.id = n.id;
114 // No need for added and uid fields: never updated
115 window.doClick("modalNews");
116 },
117 deleteNews: function(n) {
118 if (confirm(this.st.tr["Are you sure?"])) {
119 this.infoMsg = "Processing... Please wait";
120 ajax("/news", "DELETE", { id: n.id }, () => {
121 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
122 this.newsList.splice(nIdx, 1);
123 this.infoMsg = "";
124 document.getElementById("modalNews").checked = false;
125 });
126 }
127 },
128 loadMore: function() {
129 ajax("/news", "GET", { cursor: this.cursor }, res => {
130 if (res.newsList.length > 0) {
131 this.newsList = this.newsList.concat(res.newsList);
132 const L = res.newsList.length;
133 if (L > 0) this.cursor = res.newsList[L - 1].id;
134 } else this.hasMore = false;
135 });
136 }
137 }
138 };
139 </script>
140
141 <style lang="sass" scoped>
142 [type="checkbox"].modal+div .card
143 max-width: 767px
144 max-height: 100%
145 textarea#newsContent
146 margin: 0
147 width: 100%
148 min-height: 200px
149 max-height: 100%
150 #dialog
151 padding: 5px
152 color: blue
153 button#writeNews
154 margin-top: 0
155 margin-bottom: 0
156 span.ndt
157 color: darkblue
158 padding: 0 5px 0 var(--universal-margin)
159 .margintop
160 margin-top: 25px
161 border-top: 1px solid grey
162 .news
163 padding-top: 10px
164 & > div
165 display: inline-block
166 @media screen and (max-width: 767px)
167 .margintop
168 margin-top: 10px
169 </style>