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 sortedNewsList" :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;
52 const L = res.newsList.length;
54 this.cursor = res.newsList[L-1].id;
58 document.getElementById("newnewsDiv").addEventListener("click", processModalClick);
61 sortedNewsList: function() {
62 return this.newsList.sort( (n1,n2) => n1.added - n2.added );
66 formatDatetime: function(dt) {
67 const dtObj = new Date(dt);
68 const timePart = getTime(dtObj);
69 // Show minutes but not seconds:
70 return getDate(dtObj) + " " + timePart.substr(0,timePart.lastIndexOf(":"));
72 parseHtml: function(txt) {
73 return !txt.match(/<[/a-zA-Z]+>/)
74 ? txt.replace(/\n/g, "<br/>") //no HTML tag
77 adjustHeight: function() {
78 const newsContent = document.getElementById("newsContent");
79 // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length
80 newsContent.style.height = "1px";
81 newsContent.style.height = (10+newsContent.scrollHeight)+"px";
83 resetCurnews: function() {
85 this.curnews.content = "";
86 // No need for added and uid fields: never updated
88 showModalNews: function() {
92 sendNews: function() {
93 const edit = this.curnews.id > 0;
94 this.infoMsg = "Processing... Please wait";
97 edit ? "PUT" : "POST",
102 let n = this.newsList.find(n => n.id == this.curnews.id);
104 n.content = this.curnews.content;
109 content:this.curnews.content,
111 uid: this.st.user.id,
114 this.newsList = this.newsList.concat([newNews]);
116 document.getElementById("modalNews").checked = false;
122 editNews: function(n) {
123 this.curnews.content = n.content;
124 this.curnews.id = n.id;
125 // No need for added and uid fields: never updated
126 doClick('modalNews');
128 deleteNews: function(n) {
129 if (confirm(this.st.tr["Are you sure?"]))
131 this.infoMsg = "Processing... Please wait";
132 ajax("/news", "DELETE", {id:n.id}, () => {
133 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
134 this.newsList.splice(nIdx, 1);
136 document.getElementById("modalNews").checked = false;
140 loadMore: function() {
141 ajax("/news", "GET", {cursor:this.cursor}, (res) => {
142 if (res.newsList.length > 0)
144 this.newsList = this.newsList.concat(res.newsList);
145 const L = res.newsList.length;
147 this.cursor = res.newsList[L-1].id;
150 this.hasMore = false;
157 <style lang="sass" scoped>
158 [type="checkbox"].modal+div .card
171 padding: 0 5px 0 var(--universal-margin)
174 border-top: 1px solid grey
178 display: inline-block
179 @media screen and (max-width: 767px)