Some more cleaning + fixes
[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
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#writeNews(
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(
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("/news", "GET", { cursor: this.cursor }, res => {
60 this.newsList = res.newsList.sort((n1, n2) => n1.added - n2.added);
61 const L = res.newsList.length;
62 if (L > 0) this.cursor = this.newsList[0].id;
63 });
64 },
65 mounted: function() {
66 document
67 .getElementById("newnewsDiv")
68 .addEventListener("click", processModalClick);
69 },
70 methods: {
71 formatDatetime: function(dt) {
72 const dtObj = new Date(dt);
73 const timePart = getTime(dtObj);
74 // Show minutes but not seconds:
75 return (
76 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
77 );
78 },
79 parseHtml: function(txt) {
80 return !txt.match(/<[/a-zA-Z]+>/)
81 ? txt.replace(/\n/g, "<br/>") //no HTML tag
82 : txt;
83 },
84 adjustHeight: function() {
85 const newsContent = document.getElementById("newsContent");
86 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
87 newsContent.style.height = "1px";
88 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
89 },
90 resetCurnews: function() {
91 this.curnews.id = 0;
92 this.curnews.content = "";
93 // No need for added and uid fields: never updated
94 },
95 showModalNews: function() {
96 this.resetCurnews();
97 window.doClick("modalNews");
98 },
99 sendNews: function() {
100 const edit = this.curnews.id > 0;
101 this.infoMsg = "Processing... Please wait";
102 ajax("/news", edit ? "PUT" : "POST", { news: this.curnews }, res => {
103 if (edit) {
104 let n = this.newsList.find(n => n.id == this.curnews.id);
105 if (n) n.content = this.curnews.content;
106 } else {
107 const newNews = {
108 content: this.curnews.content,
109 added: Date.now(),
110 uid: this.st.user.id,
111 id: res.id
112 };
113 this.newsList = [newNews].concat(this.newsList);
114 }
115 document.getElementById("modalNews").checked = false;
116 this.infoMsg = "";
117 this.resetCurnews();
118 });
119 },
120 editNews: function(n) {
121 this.curnews.content = n.content;
122 this.curnews.id = n.id;
123 // No need for added and uid fields: never updated
124 window.doClick("modalNews");
125 },
126 deleteNews: function(n) {
127 if (confirm(this.st.tr["Are you sure?"])) {
128 this.infoMsg = "Processing... Please wait";
129 ajax("/news", "DELETE", { id: n.id }, () => {
130 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
131 this.newsList.splice(nIdx, 1);
132 this.infoMsg = "";
133 document.getElementById("modalNews").checked = false;
134 });
135 }
136 },
137 loadMore: function() {
138 ajax("/news", "GET", { cursor: this.cursor }, res => {
139 if (res.newsList.length > 0) {
140 this.newsList = this.newsList.concat(res.newsList);
141 const L = res.newsList.length;
142 if (L > 0) this.cursor = res.newsList[L - 1].id;
143 } else this.hasMore = false;
144 });
145 }
146 }
147 };
148 </script>
149
150 <style lang="sass" scoped>
151 [type="checkbox"].modal+div .card
152 max-width: 767px
153 max-height: 100%
154
155 textarea#newsContent
156 margin: 0
157 width: 100%
158 min-height: 200px
159 max-height: 100%
160
161 #dialog
162 padding: 5px
163 color: blue
164
165 button#writeNews
166 margin-top: 0
167 margin-bottom: 0
168
169 span.ndt
170 color: darkblue
171 padding: 0 5px 0 var(--universal-margin)
172
173 .news
174 padding-top: 10px
175 & > div
176 display: inline-block
177
178 .margintop
179 margin-top: 25px
180 border-top: 1px solid grey
181 @media screen and (max-width: 767px)
182 .margintop
183 margin-top: 10px
184 </style>