Commit | Line | Data |
---|---|---|
604b951e BA |
1 | <template lang="pug"> |
2 | main | |
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" | |
d85f259c | 26 | :id="'n' + n.id" |
910d631b BA |
27 | :class="{margintop:idx>0}" |
28 | ) | |
bd76b456 | 29 | span.ndt {{ formatDatetime(n.added) }} |
622e0fa8 | 30 | .dev-buttons(v-if="devs.includes(st.user.id)") |
604b951e BA |
31 | button(@click="editNews(n)") {{ st.tr["Edit"] }} |
32 | button(@click="deleteNews(n)") {{ st.tr["Delete"] }} | |
d85f259c BA |
33 | button(@click="gotoPrevNext(n, 1)") {{ st.tr["Previous_n"] }} |
34 | button(@click="gotoPrevNext(n, -1)") {{ st.tr["Next_n"] }} | |
622e0fa8 | 35 | .news-content(v-html="parseHtml(n.content)") |
b3ef5759 | 36 | button#loadMoreBtn( |
910d631b BA |
37 | v-if="hasMore" |
38 | @click="loadMore()" | |
39 | ) | |
604b951e BA |
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, | |
0234201f BA |
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, | |
6808d7a1 | 58 | curnews: { id: 0, content: "" }, |
604b951e | 59 | newsList: [], |
6808d7a1 | 60 | infoMsg: "" |
604b951e BA |
61 | }; |
62 | }, | |
63 | created: function() { | |
e57c4de4 BA |
64 | ajax( |
65 | "/news", | |
66 | "GET", | |
67 | { | |
68 | data: { cursor: this.cursor }, | |
69 | success: (res) => { | |
0234201f BA |
70 | // The returned list is sorted from most recent to oldest |
71 | this.newsList = res.newsList; | |
e57c4de4 | 72 | const L = res.newsList.length; |
0234201f | 73 | if (L > 0) this.cursor = res.newsList[L - 1].added; |
68e19a44 | 74 | else this.hasMore = false; |
e57c4de4 BA |
75 | } |
76 | } | |
77 | ); | |
604b951e BA |
78 | }, |
79 | mounted: function() { | |
d9a7a1e4 BA |
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"); | |
6808d7a1 BA |
84 | document |
85 | .getElementById("newnewsDiv") | |
86 | .addEventListener("click", processModalClick); | |
604b951e BA |
87 | }, |
88 | methods: { | |
89 | formatDatetime: function(dt) { | |
90 | const dtObj = new Date(dt); | |
bd76b456 BA |
91 | const timePart = getTime(dtObj); |
92 | // Show minutes but not seconds: | |
6808d7a1 BA |
93 | return ( |
94 | getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":")) | |
95 | ); | |
604b951e BA |
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"; | |
6808d7a1 | 106 | newsContent.style.height = 10 + newsContent.scrollHeight + "px"; |
604b951e BA |
107 | }, |
108 | resetCurnews: function() { | |
109 | this.curnews.id = 0; | |
110 | this.curnews.content = ""; | |
111 | // No need for added and uid fields: never updated | |
112 | }, | |
d85f259c BA |
113 | gotoPrevNext: function(n, dir) { |
114 | document.getElementById("n" + n.id)[ | |
115 | (dir < 0 ? "previous" : "next") + "ElementSibling"].scrollIntoView(); | |
116 | }, | |
604b951e BA |
117 | showModalNews: function() { |
118 | this.resetCurnews(); | |
6808d7a1 | 119 | window.doClick("modalNews"); |
604b951e BA |
120 | }, |
121 | sendNews: function() { | |
122 | const edit = this.curnews.id > 0; | |
123 | this.infoMsg = "Processing... Please wait"; | |
e57c4de4 BA |
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); | |
d85f259c | 132 | if (!!n) n.content = this.curnews.content; |
e57c4de4 BA |
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 | } | |
604b951e | 146 | } |
e57c4de4 | 147 | ); |
604b951e BA |
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 | |
6808d7a1 | 153 | window.doClick("modalNews"); |
604b951e BA |
154 | }, |
155 | deleteNews: function(n) { | |
6808d7a1 | 156 | if (confirm(this.st.tr["Are you sure?"])) { |
604b951e | 157 | this.infoMsg = "Processing... Please wait"; |
e57c4de4 BA |
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 | ); | |
604b951e BA |
171 | } |
172 | }, | |
173 | loadMore: function() { | |
e57c4de4 BA |
174 | ajax( |
175 | "/news", | |
176 | "GET", | |
177 | { | |
178 | data: { cursor: this.cursor }, | |
179 | success: (res) => { | |
68e19a44 BA |
180 | const L = res.newsList.length; |
181 | if (L > 0) { | |
e57c4de4 | 182 | this.newsList = this.newsList.concat(res.newsList); |
68e19a44 | 183 | this.cursor = res.newsList[L - 1].added; |
e57c4de4 BA |
184 | } else this.hasMore = false; |
185 | } | |
186 | } | |
187 | ); | |
6808d7a1 BA |
188 | } |
189 | } | |
604b951e BA |
190 | }; |
191 | </script> | |
192 | ||
9493b395 | 193 | <style lang="sass"> |
604b951e BA |
194 | [type="checkbox"].modal+div .card |
195 | max-width: 767px | |
196 | max-height: 100% | |
910d631b | 197 | |
604b951e | 198 | textarea#newsContent |
bd76b456 | 199 | margin: 0 |
604b951e BA |
200 | width: 100% |
201 | min-height: 200px | |
202 | max-height: 100% | |
910d631b | 203 | |
604b951e BA |
204 | #dialog |
205 | padding: 5px | |
206 | color: blue | |
910d631b | 207 | |
23e1fa07 BA |
208 | #writeNews |
209 | padding-top: 50px | |
210 | ||
b3ef5759 | 211 | button#writeNewsBtn, button#loadMoreBtn |
0c76fa56 BA |
212 | margin-top: 0 |
213 | margin-bottom: 0 | |
910d631b | 214 | |
bd76b456 BA |
215 | span.ndt |
216 | color: darkblue | |
feae89d3 | 217 | padding: 0 5px 0 var(--universal-margin) |
910d631b | 218 | |
feae89d3 BA |
219 | .news |
220 | padding-top: 10px | |
622e0fa8 | 221 | & > .dev-buttons |
bd76b456 | 222 | display: inline-block |
622e0fa8 | 223 | & > .news-content |
9493b395 BA |
224 | margin: var(--universal-margin) |
225 | & > p | |
226 | margin: 10px 0 | |
227 | & > br | |
228 | display: block | |
229 | margin-top: 10px | |
230 | content: " " | |
910d631b BA |
231 | |
232 | .margintop | |
233 | margin-top: 25px | |
234 | border-top: 1px solid grey | |
bd76b456 BA |
235 | @media screen and (max-width: 767px) |
236 | .margintop | |
237 | margin-top: 10px | |
604b951e | 238 | </style> |