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 in sortedNewsList")
22 h4 {{ formatDatetime(n.added) }}
23 p(v-html="parseHtml(n.content)")
24 div(v-if="devs.includes(st.user.id)")
25 button(@click="editNews(n)") {{ st.tr["Edit"] }}
26 button(@click="deleteNews(n)") {{ st.tr["Delete"] }}
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 return getDate(dtObj) + " " + getTime(dtObj);
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() {
90 sendNews: function() {
91 const edit = this.curnews.id > 0;
92 this.infoMsg = "Processing... Please wait";
95 edit ? "PUT" : "POST",
100 let n = this.newsList.find(n => n.id == this.curnews.id);
102 n.content = this.curnews.content;
107 content:this.curnews.content,
109 uid: this.st.user.id,
112 this.newsList = this.newsList.concat([newNews]);
114 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 doClick('modalNews');
126 deleteNews: function(n) {
127 if (confirm(this.st.tr["Are you sure?"]))
129 this.infoMsg = "Processing... Please wait";
130 ajax("/news", "DELETE", {id:n.id}, () => {
131 const nIdx = this.newsList.findIndex(nw => nw.id == n.id);
132 this.newsList.splice(nIdx, 1);
134 document.getElementById("modalNews").checked = false;
138 loadMore: function() {
139 ajax("/news", "GET", {cursor:this.cursor}, (res) => {
140 if (res.newsList.length > 0)
142 this.newsList = this.newsList.concat(res.newsList);
143 const L = res.newsList.length;
145 this.cursor = res.newsList[L-1].id;
148 this.hasMore = false;
155 <style lang="sass" scoped>
156 [type="checkbox"].modal+div .card