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 // timestamp of oldest showed news:
52 cursor: Number.MAX_SAFE_INTEGER,
53 // hasMore == TRUE: a priori there could be more news to load
55 curnews: { id: 0, content: "" },
65 data: { cursor: this.cursor },
67 // The returned list is sorted from most recent to oldest
68 this.newsList = res.newsList;
69 const L = res.newsList.length;
70 if (L > 0) this.cursor = res.newsList[L - 1].added;
76 // Mark that I've read the news:
77 localStorage.setItem("newsRead", Date.now());
78 if (this.st.user.id > 0) ajax("/newsread", "PUT");
79 document.getElementById("newsMenu").classList.remove("somenews");
81 .getElementById("newnewsDiv")
82 .addEventListener("click", processModalClick);
85 formatDatetime: function(dt) {
86 const dtObj = new Date(dt);
87 const timePart = getTime(dtObj);
88 // Show minutes but not seconds:
90 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
93 parseHtml: function(txt) {
94 return !txt.match(/<[/a-zA-Z]+>/)
95 ? txt.replace(/\n/g, "<br/>") //no HTML tag
98 adjustHeight: function() {
99 const newsContent = document.getElementById("newsContent");
100 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
101 newsContent.style.height = "1px";
102 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
104 resetCurnews: function() {
106 this.curnews.content = "";
107 // No need for added and uid fields: never updated
109 showModalNews: function() {
111 window.doClick("modalNews");
113 sendNews: function() {
114 const edit = this.curnews.id > 0;
115 this.infoMsg = "Processing... Please wait";
118 edit ? "PUT" : "POST",
120 data: { news: this.curnews },
123 let n = this.newsList.find(n => n.id == this.curnews.id);
124 if (n) n.content = this.curnews.content;
127 content: this.curnews.content,
129 uid: this.st.user.id,
132 this.newsList = [newNews].concat(this.newsList);
134 document.getElementById("modalNews").checked = false;
141 editNews: function(n) {
142 this.curnews.content = n.content;
143 this.curnews.id = n.id;
144 // No need for added and uid fields: never updated
145 window.doClick("modalNews");
147 deleteNews: function(n) {
148 if (confirm(this.st.tr["Are you sure?"])) {
149 this.infoMsg = "Processing... Please wait";
156 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
157 this.newsList.splice(nIdx, 1);
159 document.getElementById("modalNews").checked = false;
165 loadMore: function() {
170 data: { cursor: this.cursor },
172 if (res.newsList.length > 0) {
173 this.newsList = this.newsList.concat(res.newsList);
174 const L = res.newsList.length;
175 if (L > 0) this.cursor = res.newsList[L - 1].added;
176 } else this.hasMore = false;
185 <style lang="sass" scoped>
186 [type="checkbox"].modal+div .card
203 button#writeNewsBtn, button#loadMoreBtn
209 padding: 0 5px 0 var(--universal-margin)
214 display: inline-block
218 border-top: 1px solid grey
219 @media screen and (max-width: 767px)