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