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" | |
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> | |
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 | |
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() { | |
6808d7a1 BA |
73 | document |
74 | .getElementById("newnewsDiv") | |
75 | .addEventListener("click", processModalClick); | |
604b951e BA |
76 | }, |
77 | methods: { | |
78 | formatDatetime: function(dt) { | |
79 | const dtObj = new Date(dt); | |
bd76b456 BA |
80 | const timePart = getTime(dtObj); |
81 | // Show minutes but not seconds: | |
6808d7a1 BA |
82 | return ( |
83 | getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":")) | |
84 | ); | |
604b951e BA |
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"; | |
6808d7a1 | 95 | newsContent.style.height = 10 + newsContent.scrollHeight + "px"; |
604b951e BA |
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(); | |
6808d7a1 | 104 | window.doClick("modalNews"); |
604b951e BA |
105 | }, |
106 | sendNews: function() { | |
107 | const edit = this.curnews.id > 0; | |
108 | this.infoMsg = "Processing... Please wait"; | |
e57c4de4 BA |
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 | } | |
604b951e | 131 | } |
e57c4de4 | 132 | ); |
604b951e BA |
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 | |
6808d7a1 | 138 | window.doClick("modalNews"); |
604b951e BA |
139 | }, |
140 | deleteNews: function(n) { | |
6808d7a1 | 141 | if (confirm(this.st.tr["Are you sure?"])) { |
604b951e | 142 | this.infoMsg = "Processing... Please wait"; |
e57c4de4 BA |
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 | ); | |
604b951e BA |
156 | } |
157 | }, | |
158 | loadMore: function() { | |
e57c4de4 BA |
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 | ); | |
6808d7a1 BA |
173 | } |
174 | } | |
604b951e BA |
175 | }; |
176 | </script> | |
177 | ||
178 | <style lang="sass" scoped> | |
179 | [type="checkbox"].modal+div .card | |
180 | max-width: 767px | |
181 | max-height: 100% | |
910d631b | 182 | |
604b951e | 183 | textarea#newsContent |
bd76b456 | 184 | margin: 0 |
604b951e BA |
185 | width: 100% |
186 | min-height: 200px | |
187 | max-height: 100% | |
910d631b | 188 | |
604b951e BA |
189 | #dialog |
190 | padding: 5px | |
191 | color: blue | |
910d631b | 192 | |
23e1fa07 BA |
193 | #writeNews |
194 | padding-top: 50px | |
195 | ||
b3ef5759 | 196 | button#writeNewsBtn, button#loadMoreBtn |
0c76fa56 BA |
197 | margin-top: 0 |
198 | margin-bottom: 0 | |
910d631b | 199 | |
bd76b456 BA |
200 | span.ndt |
201 | color: darkblue | |
feae89d3 | 202 | padding: 0 5px 0 var(--universal-margin) |
910d631b | 203 | |
feae89d3 BA |
204 | .news |
205 | padding-top: 10px | |
bd76b456 BA |
206 | & > div |
207 | display: inline-block | |
910d631b BA |
208 | |
209 | .margintop | |
210 | margin-top: 25px | |
211 | border-top: 1px solid grey | |
bd76b456 BA |
212 | @media screen and (max-width: 767px) |
213 | .margintop | |
214 | margin-top: 10px | |
604b951e | 215 | </style> |