Refactor models (merge Players in Games), add cursor to correspondance games. Finishe...
[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,
0234201f
BA
51 // timestamp of oldest showed news:
52 cursor: Number.MAX_SAFE_INTEGER,
53 // hasMore == TRUE: a priori there could be more news to load
54 hasMore: true,
6808d7a1 55 curnews: { id: 0, content: "" },
604b951e 56 newsList: [],
6808d7a1 57 infoMsg: ""
604b951e
BA
58 };
59 },
60 created: function() {
e57c4de4
BA
61 ajax(
62 "/news",
63 "GET",
64 {
65 data: { cursor: this.cursor },
66 success: (res) => {
0234201f
BA
67 // The returned list is sorted from most recent to oldest
68 this.newsList = res.newsList;
e57c4de4 69 const L = res.newsList.length;
0234201f 70 if (L > 0) this.cursor = res.newsList[L - 1].added;
e57c4de4
BA
71 }
72 }
73 );
604b951e
BA
74 },
75 mounted: function() {
d9a7a1e4
BA
76 // Mark that I've read the news:
77 localStorage.setItem("newsRead", Date.now());
78 if (this.st.user.id > 0) ajax("/newsread", "PUT");
79 document.getElementById("newsMenu").classList.remove("somenews");
6808d7a1
BA
80 document
81 .getElementById("newnewsDiv")
82 .addEventListener("click", processModalClick);
604b951e
BA
83 },
84 methods: {
85 formatDatetime: function(dt) {
86 const dtObj = new Date(dt);
bd76b456
BA
87 const timePart = getTime(dtObj);
88 // Show minutes but not seconds:
6808d7a1
BA
89 return (
90 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
91 );
604b951e
BA
92 },
93 parseHtml: function(txt) {
94 return !txt.match(/<[/a-zA-Z]+>/)
95 ? txt.replace(/\n/g, "<br/>") //no HTML tag
96 : txt;
97 },
98 adjustHeight: function() {
99 const newsContent = document.getElementById("newsContent");
100 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
101 newsContent.style.height = "1px";
6808d7a1 102 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
604b951e
BA
103 },
104 resetCurnews: function() {
105 this.curnews.id = 0;
106 this.curnews.content = "";
107 // No need for added and uid fields: never updated
108 },
109 showModalNews: function() {
110 this.resetCurnews();
6808d7a1 111 window.doClick("modalNews");
604b951e
BA
112 },
113 sendNews: function() {
114 const edit = this.curnews.id > 0;
115 this.infoMsg = "Processing... Please wait";
e57c4de4
BA
116 ajax(
117 "/news",
118 edit ? "PUT" : "POST",
119 {
120 data: { news: this.curnews },
121 success: (res) => {
122 if (edit) {
123 let n = this.newsList.find(n => n.id == this.curnews.id);
124 if (n) n.content = this.curnews.content;
125 } else {
126 const newNews = {
127 content: this.curnews.content,
128 added: Date.now(),
129 uid: this.st.user.id,
130 id: res.id
131 };
132 this.newsList = [newNews].concat(this.newsList);
133 }
134 document.getElementById("modalNews").checked = false;
135 this.infoMsg = "";
136 this.resetCurnews();
137 }
604b951e 138 }
e57c4de4 139 );
604b951e
BA
140 },
141 editNews: function(n) {
142 this.curnews.content = n.content;
143 this.curnews.id = n.id;
144 // No need for added and uid fields: never updated
6808d7a1 145 window.doClick("modalNews");
604b951e
BA
146 },
147 deleteNews: function(n) {
6808d7a1 148 if (confirm(this.st.tr["Are you sure?"])) {
604b951e 149 this.infoMsg = "Processing... Please wait";
e57c4de4
BA
150 ajax(
151 "/news",
152 "DELETE",
153 {
154 data: { id: n.id },
155 success: () => {
156 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
157 this.newsList.splice(nIdx, 1);
158 this.infoMsg = "";
159 document.getElementById("modalNews").checked = false;
160 }
161 }
162 );
604b951e
BA
163 }
164 },
165 loadMore: function() {
e57c4de4
BA
166 ajax(
167 "/news",
168 "GET",
169 {
170 data: { cursor: this.cursor },
171 success: (res) => {
172 if (res.newsList.length > 0) {
173 this.newsList = this.newsList.concat(res.newsList);
174 const L = res.newsList.length;
0234201f 175 if (L > 0) this.cursor = res.newsList[L - 1].added;
e57c4de4
BA
176 } else this.hasMore = false;
177 }
178 }
179 );
6808d7a1
BA
180 }
181 }
604b951e
BA
182};
183</script>
184
185<style lang="sass" scoped>
186[type="checkbox"].modal+div .card
187 max-width: 767px
188 max-height: 100%
910d631b 189
604b951e 190textarea#newsContent
bd76b456 191 margin: 0
604b951e
BA
192 width: 100%
193 min-height: 200px
194 max-height: 100%
910d631b 195
604b951e
BA
196#dialog
197 padding: 5px
198 color: blue
910d631b 199
23e1fa07
BA
200#writeNews
201 padding-top: 50px
202
b3ef5759 203button#writeNewsBtn, button#loadMoreBtn
0c76fa56
BA
204 margin-top: 0
205 margin-bottom: 0
910d631b 206
bd76b456
BA
207span.ndt
208 color: darkblue
feae89d3 209 padding: 0 5px 0 var(--universal-margin)
910d631b 210
feae89d3
BA
211.news
212 padding-top: 10px
bd76b456
BA
213 & > div
214 display: inline-block
910d631b
BA
215
216.margintop
217 margin-top: 25px
218 border-top: 1px solid grey
bd76b456
BA
219@media screen and (max-width: 767px)
220 .margintop
221 margin-top: 10px
604b951e 222</style>