dd3c84ef5d3f10030b1438f176e02011a26c50bd
[vchess.git] / client / src / views / News.vue
1 <template lang="pug">
2 main
3 input#modalNews.modal(type="checkbox")
4 div#newnewsDiv(
5 role="dialog"
6 data-checkbox="modalNews"
7 )
8 .card#writeNews
9 label.modal-close(for="modalNews")
10 textarea#newsContent(
11 v-model="curnews.content"
12 :placeholder="st.tr['News go here']"
13 @input="adjustHeight"
14 )
15 button(@click="sendNews()") {{ st.tr["Send"] }}
16 #dialog.text-center {{ st.tr[infoMsg] }}
17 .row
18 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
19 button#writeNewsBtn(
20 v-if="devs.includes(st.user.id)"
21 @click="showModalNews"
22 )
23 | {{ st.tr["Write news"] }}
24 .news(
25 v-for="n,idx in newsList"
26 :id="'n' + n.id"
27 :class="{margintop:idx>0}"
28 )
29 span.ndt {{ formatDatetime(n.added) }}
30 .dev-buttons(v-if="devs.includes(st.user.id)")
31 button(@click="editNews(n)") {{ st.tr["Edit"] }}
32 button(@click="deleteNews(n)") {{ st.tr["Delete"] }}
33 button(@click="gotoPrevNext(n, 1)") {{ st.tr["Previous_n"] }}
34 button(@click="gotoPrevNext(n, -1)") {{ st.tr["Next_n"] }}
35 .news-content(v-html="parseHtml(n.content)")
36 button#loadMoreBtn(
37 v-if="hasMore"
38 @click="loadMore()"
39 )
40 | {{ st.tr["Load more"] }}
41 </template>
42
43 <script>
44 import { store } from "@/store";
45 import { ajax } from "@/utils/ajax";
46 import { getDate, getTime } from "@/utils/datetime";
47 import { processModalClick } from "@/utils/modalClick";
48 export default {
49 name: "my-news",
50 data: function() {
51 return {
52 devs: [1], //for now the only dev is me
53 st: store.state,
54 // timestamp of oldest showed news:
55 cursor: Number.MAX_SAFE_INTEGER,
56 // hasMore == TRUE: a priori there could be more news to load
57 hasMore: true,
58 curnews: { id: 0, content: "" },
59 newsList: [],
60 infoMsg: ""
61 };
62 },
63 created: function() {
64 ajax(
65 "/news",
66 "GET",
67 {
68 data: { cursor: this.cursor },
69 success: (res) => {
70 // The returned list is sorted from most recent to oldest
71 this.newsList = res.newsList;
72 const L = res.newsList.length;
73 if (L > 0) this.cursor = res.newsList[L - 1].added;
74 else this.hasMore = false;
75 }
76 }
77 );
78 },
79 mounted: function() {
80 // Mark that I've read the news:
81 localStorage.setItem("newsRead", Date.now());
82 if (this.st.user.id > 0) ajax("/newsread", "PUT");
83 document.getElementById("newsMenu").classList.remove("somenews");
84 document
85 .getElementById("newnewsDiv")
86 .addEventListener("click", processModalClick);
87 },
88 methods: {
89 formatDatetime: function(dt) {
90 const dtObj = new Date(dt);
91 const timePart = getTime(dtObj);
92 // Show minutes but not seconds:
93 return (
94 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
95 );
96 },
97 parseHtml: function(txt) {
98 return !txt.match(/<[/a-zA-Z]+>/)
99 ? txt.replace(/\n/g, "<br/>") //no HTML tag
100 : txt;
101 },
102 adjustHeight: function() {
103 const newsContent = document.getElementById("newsContent");
104 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
105 newsContent.style.height = "1px";
106 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
107 },
108 resetCurnews: function() {
109 this.curnews.id = 0;
110 this.curnews.content = "";
111 // No need for added and uid fields: never updated
112 },
113 gotoPrevNext: function(n, dir) {
114 document.getElementById("n" + n.id)[
115 (dir < 0 ? "previous" : "next") + "ElementSibling"].scrollIntoView();
116 },
117 showModalNews: function() {
118 this.resetCurnews();
119 window.doClick("modalNews");
120 },
121 sendNews: function() {
122 const edit = this.curnews.id > 0;
123 this.infoMsg = "Processing... Please wait";
124 ajax(
125 "/news",
126 edit ? "PUT" : "POST",
127 {
128 data: { news: this.curnews },
129 success: (res) => {
130 if (edit) {
131 let n = this.newsList.find(n => n.id == this.curnews.id);
132 if (!!n) n.content = this.curnews.content;
133 } else {
134 const newNews = {
135 content: this.curnews.content,
136 added: Date.now(),
137 uid: this.st.user.id,
138 id: res.id
139 };
140 this.newsList = [newNews].concat(this.newsList);
141 }
142 document.getElementById("modalNews").checked = false;
143 this.infoMsg = "";
144 this.resetCurnews();
145 }
146 }
147 );
148 },
149 editNews: function(n) {
150 this.curnews.content = n.content;
151 this.curnews.id = n.id;
152 // No need for added and uid fields: never updated
153 window.doClick("modalNews");
154 },
155 deleteNews: function(n) {
156 if (confirm(this.st.tr["Are you sure?"])) {
157 this.infoMsg = "Processing... Please wait";
158 ajax(
159 "/news",
160 "DELETE",
161 {
162 data: { id: n.id },
163 success: () => {
164 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
165 this.newsList.splice(nIdx, 1);
166 this.infoMsg = "";
167 document.getElementById("modalNews").checked = false;
168 }
169 }
170 );
171 }
172 },
173 loadMore: function() {
174 ajax(
175 "/news",
176 "GET",
177 {
178 data: { cursor: this.cursor },
179 success: (res) => {
180 const L = res.newsList.length;
181 if (L > 0) {
182 this.newsList = this.newsList.concat(res.newsList);
183 this.cursor = res.newsList[L - 1].added;
184 } else this.hasMore = false;
185 }
186 }
187 );
188 }
189 }
190 };
191 </script>
192
193 <style lang="sass">
194 [type="checkbox"].modal+div .card
195 max-width: 767px
196 max-height: 100%
197
198 textarea#newsContent
199 margin: 0
200 width: 100%
201 min-height: 200px
202 max-height: 100%
203
204 #dialog
205 padding: 5px
206 color: blue
207
208 #writeNews
209 padding-top: 50px
210
211 button#writeNewsBtn, button#loadMoreBtn
212 margin-top: 0
213 margin-bottom: 0
214
215 span.ndt
216 color: darkblue
217 padding: 0 5px 0 var(--universal-margin)
218
219 .news
220 padding-top: 10px
221 & > .dev-buttons
222 display: inline-block
223 & > .news-content
224 margin: var(--universal-margin)
225 & > p
226 margin: 10px 0
227 & > br
228 display: block
229 margin-top: 10px
230 content: " "
231
232 .margintop
233 margin-top: 25px
234 border-top: 1px solid grey
235 @media screen and (max-width: 767px)
236 .margintop
237 margin-top: 10px
238 </style>