3 input#modalNews.modal(type="checkbox")
6 data-checkbox="modalNews"
9 label.modal-close(for="modalNews")
11 v-model="curnews.content"
12 :placeholder="st.tr['News go here']"
15 button(@click="sendNews()") {{ st.tr["Send"] }}
16 #dialog.text-center {{ st.tr[infoMsg] }}
18 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
20 v-if="devs.includes(st.user.id)"
21 @click="showModalNews"
23 | {{ st.tr["Write news"] }}
25 v-for="n,idx in newsList"
26 :class="{margintop:idx>0}"
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)")
37 | {{ st.tr["Load more"] }}
41 import { store } from "@/store";
42 import { ajax } from "@/utils/ajax";
43 import { getDate, getTime } from "@/utils/datetime";
44 import { processModalClick } from "@/utils/modalClick";
49 devs: [1], //for now the only dev is me
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: "" },
63 data: { cursor: this.cursor },
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;
73 // Mark that I've read the news:
74 localStorage.setItem("newsRead", Date.now());
75 if (this.st.user.id > 0) ajax("/newsread", "PUT");
76 document.getElementById("newsMenu").classList.remove("somenews");
78 .getElementById("newnewsDiv")
79 .addEventListener("click", processModalClick);
82 formatDatetime: function(dt) {
83 const dtObj = new Date(dt);
84 const timePart = getTime(dtObj);
85 // Show minutes but not seconds:
87 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
90 parseHtml: function(txt) {
91 return !txt.match(/<[/a-zA-Z]+>/)
92 ? txt.replace(/\n/g, "<br/>") //no HTML tag
95 adjustHeight: function() {
96 const newsContent = document.getElementById("newsContent");
97 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
98 newsContent.style.height = "1px";
99 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
101 resetCurnews: function() {
103 this.curnews.content = "";
104 // No need for added and uid fields: never updated
106 showModalNews: function() {
108 window.doClick("modalNews");
110 sendNews: function() {
111 const edit = this.curnews.id > 0;
112 this.infoMsg = "Processing... Please wait";
115 edit ? "PUT" : "POST",
117 data: { news: this.curnews },
120 let n = this.newsList.find(n => n.id == this.curnews.id);
121 if (n) n.content = this.curnews.content;
124 content: this.curnews.content,
126 uid: this.st.user.id,
129 this.newsList = [newNews].concat(this.newsList);
131 document.getElementById("modalNews").checked = false;
138 editNews: function(n) {
139 this.curnews.content = n.content;
140 this.curnews.id = n.id;
141 // No need for added and uid fields: never updated
142 window.doClick("modalNews");
144 deleteNews: function(n) {
145 if (confirm(this.st.tr["Are you sure?"])) {
146 this.infoMsg = "Processing... Please wait";
153 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
154 this.newsList.splice(nIdx, 1);
156 document.getElementById("modalNews").checked = false;
162 loadMore: function() {
167 data: { cursor: this.cursor },
169 if (res.newsList.length > 0) {
170 this.newsList = this.newsList.concat(res.newsList);
171 const L = res.newsList.length;
172 if (L > 0) this.cursor = res.newsList[L - 1].id;
173 } else this.hasMore = false;
182 <style lang="sass" scoped>
183 [type="checkbox"].modal+div .card
200 button#writeNewsBtn, button#loadMoreBtn
206 padding: 0 5px 0 var(--universal-margin)
211 display: inline-block
215 border-top: 1px solid grey
216 @media screen and (max-width: 767px)