3 input#modalNews.modal(type="checkbox")
4 div#newnewsDiv(role="dialog" data-checkbox="modalNews")
6 label.modal-close(for="modalNews")
8 v-model="curnews.content"
9 :placeholder="st.tr['News go here']"
12 button(@click="sendNews()") {{ st.tr["Send"] }}
13 #dialog.text-center {{ st.tr[infoMsg] }}
15 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
17 v-if="devs.includes(st.user.id)"
18 @click="showModalNews"
20 | {{ st.tr["Write news"] }}
21 .news(v-for="n,idx in newsList" :class="{margintop:idx>0}")
22 span.ndt {{ formatDatetime(n.added) }}
23 div(v-if="devs.includes(st.user.id)")
24 button(@click="editNews(n)") {{ st.tr["Edit"] }}
25 button(@click="deleteNews(n)") {{ st.tr["Delete"] }}
26 p(v-html="parseHtml(n.content)")
27 button(v-if="hasMore" @click="loadMore()")
28 | {{ st.tr["Load more"] }}
32 import { store } from "@/store";
33 import { ajax } from "@/utils/ajax";
34 import { getDate, getTime } from "@/utils/datetime";
35 import { processModalClick } from "@/utils/modalClick";
40 devs: [1], //for now the only dev is me
42 cursor: 0, //ID of last showed news
43 hasMore: true, //a priori there could be more news to load
44 curnews: { id: 0, content: "" },
50 ajax("/news", "GET", { cursor: this.cursor }, res => {
51 this.newsList = res.newsList.sort((n1, n2) => n1.added - n2.added);
52 const L = res.newsList.length;
53 if (L > 0) this.cursor = this.newsList[0].id;
58 .getElementById("newnewsDiv")
59 .addEventListener("click", processModalClick);
62 formatDatetime: function(dt) {
63 const dtObj = new Date(dt);
64 const timePart = getTime(dtObj);
65 // Show minutes but not seconds:
67 getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":"))
70 parseHtml: function(txt) {
71 return !txt.match(/<[/a-zA-Z]+>/)
72 ? txt.replace(/\n/g, "<br/>") //no HTML tag
75 adjustHeight: function() {
76 const newsContent = document.getElementById("newsContent");
77 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
78 newsContent.style.height = "1px";
79 newsContent.style.height = 10 + newsContent.scrollHeight + "px";
81 resetCurnews: function() {
83 this.curnews.content = "";
84 // No need for added and uid fields: never updated
86 showModalNews: function() {
88 window.doClick("modalNews");
90 sendNews: function() {
91 const edit = this.curnews.id > 0;
92 this.infoMsg = "Processing... Please wait";
93 ajax("/news", edit ? "PUT" : "POST", { news: this.curnews }, res => {
95 let n = this.newsList.find(n => n.id == this.curnews.id);
96 if (n) n.content = this.curnews.content;
99 content: this.curnews.content,
101 uid: this.st.user.id,
104 this.newsList = [newNews].concat(this.newsList);
106 document.getElementById("modalNews").checked = false;
111 editNews: function(n) {
112 this.curnews.content = n.content;
113 this.curnews.id = n.id;
114 // No need for added and uid fields: never updated
115 window.doClick("modalNews");
117 deleteNews: function(n) {
118 if (confirm(this.st.tr["Are you sure?"])) {
119 this.infoMsg = "Processing... Please wait";
120 ajax("/news", "DELETE", { id: n.id }, () => {
121 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
122 this.newsList.splice(nIdx, 1);
124 document.getElementById("modalNews").checked = false;
128 loadMore: function() {
129 ajax("/news", "GET", { cursor: this.cursor }, res => {
130 if (res.newsList.length > 0) {
131 this.newsList = this.newsList.concat(res.newsList);
132 const L = res.newsList.length;
133 if (L > 0) this.cursor = res.newsList[L - 1].id;
134 } else this.hasMore = false;
141 <style lang="sass" scoped>
142 [type="checkbox"].modal+div .card
158 padding: 0 5px 0 var(--universal-margin)
161 border-top: 1px solid grey
165 display: inline-block
166 @media screen and (max-width: 767px)