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: "" },
59 ajax("/news", "GET", { cursor: this.cursor }, res => {
60 this.newsList = res.newsList.sort((n1, n2) => n2.added - n1.added);
61 const L = res.newsList.length;
62 if (L > 0) this.cursor = this.newsList[0].id;
67 .getElementById("newnewsDiv")
68 .addEventListener("click", processModalClick);
71 formatDatetime: function(dt) {
72 const dtObj = new Date(dt);
73 const timePart = getTime(dtObj);
74 // Show minutes but not seconds:
76 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
79 parseHtml: function(txt) {
80 return !txt.match(/<[/a-zA-Z]+>/)
81 ? txt.replace(/\n/g, "<br/>") //no HTML tag
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";
90 resetCurnews: function() {
92 this.curnews.content = "";
93 // No need for added and uid fields: never updated
95 showModalNews: function() {
97 window.doClick("modalNews");
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 => {
104 let n = this.newsList.find(n => n.id == this.curnews.id);
105 if (n) n.content = this.curnews.content;
108 content: this.curnews.content,
110 uid: this.st.user.id,
113 this.newsList = [newNews].concat(this.newsList);
115 document.getElementById("modalNews").checked = false;
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");
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);
133 document.getElementById("modalNews").checked = false;
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;
150 <style lang="sass" scoped>
151 [type="checkbox"].modal+div .card
174 padding: 0 5px 0 var(--universal-margin)
179 display: inline-block
183 border-top: 1px solid grey
184 @media screen and (max-width: 767px)