Replaced AJAX by fetch: not everything tested yet, but seems fine
[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 :class="{margintop:idx>0}"
27 )
28 span.ndt {{ formatDatetime(n.added) }}
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"] }}
32 p(v-html="parseHtml(n.content)")
33 button#loadMoreBtn(
34 v-if="hasMore"
35 @click="loadMore()"
36 )
37 | {{ st.tr["Load more"] }}
38 </template>
39
40 <script>
41 import { store } from "@/store";
42 import { ajax } from "@/utils/ajax";
43 import { getDate, getTime } from "@/utils/datetime";
44 import { processModalClick } from "@/utils/modalClick";
45 export 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
53 curnews: { id: 0, content: "" },
54 newsList: [],
55 infoMsg: ""
56 };
57 },
58 created: function() {
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 );
71 },
72 mounted: function() {
73 document
74 .getElementById("newnewsDiv")
75 .addEventListener("click", processModalClick);
76 },
77 methods: {
78 formatDatetime: function(dt) {
79 const dtObj = new Date(dt);
80 const timePart = getTime(dtObj);
81 // Show minutes but not seconds:
82 return (
83 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
84 );
85 },
86 parseHtml: function(txt) {
87 return !txt.match(/<[/a-zA-Z]+>/)
88 ? txt.replace(/\n/g, "<br/>") //no HTML tag
89 : txt;
90 },
91 adjustHeight: function() {
92 const newsContent = document.getElementById("newsContent");
93 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
94 newsContent.style.height = "1px";
95 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
96 },
97 resetCurnews: function() {
98 this.curnews.id = 0;
99 this.curnews.content = "";
100 // No need for added and uid fields: never updated
101 },
102 showModalNews: function() {
103 this.resetCurnews();
104 window.doClick("modalNews");
105 },
106 sendNews: function() {
107 const edit = this.curnews.id > 0;
108 this.infoMsg = "Processing... Please wait";
109 ajax(
110 "/news",
111 edit ? "PUT" : "POST",
112 {
113 data: { news: this.curnews },
114 success: (res) => {
115 if (edit) {
116 let n = this.newsList.find(n => n.id == this.curnews.id);
117 if (n) n.content = this.curnews.content;
118 } else {
119 const newNews = {
120 content: this.curnews.content,
121 added: Date.now(),
122 uid: this.st.user.id,
123 id: res.id
124 };
125 this.newsList = [newNews].concat(this.newsList);
126 }
127 document.getElementById("modalNews").checked = false;
128 this.infoMsg = "";
129 this.resetCurnews();
130 }
131 }
132 );
133 },
134 editNews: function(n) {
135 this.curnews.content = n.content;
136 this.curnews.id = n.id;
137 // No need for added and uid fields: never updated
138 window.doClick("modalNews");
139 },
140 deleteNews: function(n) {
141 if (confirm(this.st.tr["Are you sure?"])) {
142 this.infoMsg = "Processing... Please wait";
143 ajax(
144 "/news",
145 "DELETE",
146 {
147 data: { id: n.id },
148 success: () => {
149 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
150 this.newsList.splice(nIdx, 1);
151 this.infoMsg = "";
152 document.getElementById("modalNews").checked = false;
153 }
154 }
155 );
156 }
157 },
158 loadMore: function() {
159 ajax(
160 "/news",
161 "GET",
162 {
163 data: { cursor: this.cursor },
164 success: (res) => {
165 if (res.newsList.length > 0) {
166 this.newsList = this.newsList.concat(res.newsList);
167 const L = res.newsList.length;
168 if (L > 0) this.cursor = res.newsList[L - 1].id;
169 } else this.hasMore = false;
170 }
171 }
172 );
173 }
174 }
175 };
176 </script>
177
178 <style lang="sass" scoped>
179 [type="checkbox"].modal+div .card
180 max-width: 767px
181 max-height: 100%
182
183 textarea#newsContent
184 margin: 0
185 width: 100%
186 min-height: 200px
187 max-height: 100%
188
189 #dialog
190 padding: 5px
191 color: blue
192
193 #writeNews
194 padding-top: 50px
195
196 button#writeNewsBtn, button#loadMoreBtn
197 margin-top: 0
198 margin-bottom: 0
199
200 span.ndt
201 color: darkblue
202 padding: 0 5px 0 var(--universal-margin)
203
204 .news
205 padding-top: 10px
206 & > div
207 display: inline-block
208
209 .margintop
210 margin-top: 25px
211 border-top: 1px solid grey
212 @media screen and (max-width: 767px)
213 .margintop
214 margin-top: 10px
215 </style>