X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FNews.vue;h=0e3c3c7ae08d18811849d5ee6d0898d25ebd902e;hb=0234201fb338fc239d6f613c677fa932c7c3697c;hp=8a3de76d2a0f5ed6d51d72f6f9193b12207ad144;hpb=feae89d39c446311f78a24b597b6b1ffbf5e1018;p=vchess.git diff --git a/client/src/views/News.vue b/client/src/views/News.vue index 8a3de76d..0e3c3c7a 100644 --- a/client/src/views/News.vue +++ b/client/src/views/News.vue @@ -1,8 +1,11 @@ @@ -39,35 +48,47 @@ export default { return { devs: [1], //for now the only dev is me st: store.state, - cursor: 0, //ID of last showed news - hasMore: true, //a priori there could be more news to load - curnews: {id:0, content:""}, + // timestamp of oldest showed news: + cursor: Number.MAX_SAFE_INTEGER, + // hasMore == TRUE: a priori there could be more news to load + hasMore: true, + curnews: { id: 0, content: "" }, newsList: [], - infoMsg: "", + infoMsg: "" }; }, created: function() { - ajax("/news", "GET", {cursor:this.cursor}, (res) => { - this.newsList = res.newsList; - const L = res.newsList.length; - if (L > 0) - this.cursor = res.newsList[L-1].id; - }); + ajax( + "/news", + "GET", + { + data: { cursor: this.cursor }, + success: (res) => { + // The returned list is sorted from most recent to oldest + this.newsList = res.newsList; + const L = res.newsList.length; + if (L > 0) this.cursor = res.newsList[L - 1].added; + } + } + ); }, mounted: function() { - document.getElementById("newnewsDiv").addEventListener("click", processModalClick); - }, - computed: { - sortedNewsList: function() { - return this.newsList.sort( (n1,n2) => n1.added - n2.added ); - }, + // Mark that I've read the news: + localStorage.setItem("newsRead", Date.now()); + if (this.st.user.id > 0) ajax("/newsread", "PUT"); + document.getElementById("newsMenu").classList.remove("somenews"); + document + .getElementById("newnewsDiv") + .addEventListener("click", processModalClick); }, methods: { formatDatetime: function(dt) { const dtObj = new Date(dt); const timePart = getTime(dtObj); // Show minutes but not seconds: - return getDate(dtObj) + " " + timePart.substr(0,timePart.lastIndexOf(":")); + return ( + getDate(dtObj) + " " + timePart.substr(0, timePart.lastIndexOf(":")) + ); }, parseHtml: function(txt) { return !txt.match(/<[/a-zA-Z]+>/) @@ -78,7 +99,7 @@ export default { const newsContent = document.getElementById("newsContent"); // https://stackoverflow.com/questions/995168/textarea-to-resize-based-on-content-length newsContent.style.height = "1px"; - newsContent.style.height = (10+newsContent.scrollHeight)+"px"; + newsContent.style.height = 10 + newsContent.scrollHeight + "px"; }, resetCurnews: function() { this.curnews.id = 0; @@ -87,7 +108,7 @@ export default { }, showModalNews: function() { this.resetCurnews(); - doClick('modalNews'); + window.doClick("modalNews"); }, sendNews: function() { const edit = this.curnews.id > 0; @@ -95,27 +116,25 @@ export default { ajax( "/news", edit ? "PUT" : "POST", - {news: this.curnews}, - (res) => { - if (edit) - { - let n = this.newsList.find(n => n.id == this.curnews.id); - if (!!n) - n.content = this.curnews.content; - } - else - { - const newNews = { - content:this.curnews.content, - added:Date.now(), - uid: this.st.user.id, - id: res.id - }; - this.newsList = this.newsList.concat([newNews]); + { + data: { news: this.curnews }, + success: (res) => { + if (edit) { + let n = this.newsList.find(n => n.id == this.curnews.id); + if (n) n.content = this.curnews.content; + } else { + const newNews = { + content: this.curnews.content, + added: Date.now(), + uid: this.st.user.id, + id: res.id + }; + this.newsList = [newNews].concat(this.newsList); + } + document.getElementById("modalNews").checked = false; + this.infoMsg = ""; + this.resetCurnews(); } - document.getElementById("modalNews").checked = false; - this.infoMsg = ""; - this.resetCurnews(); } ); }, @@ -123,34 +142,43 @@ export default { this.curnews.content = n.content; this.curnews.id = n.id; // No need for added and uid fields: never updated - doClick('modalNews'); + window.doClick("modalNews"); }, deleteNews: function(n) { - if (confirm(this.st.tr["Are you sure?"])) - { + if (confirm(this.st.tr["Are you sure?"])) { this.infoMsg = "Processing... Please wait"; - ajax("/news", "DELETE", {id:n.id}, () => { - const nIdx = this.newsList.findIndex(nw => nw.id == n.id); - this.newsList.splice(nIdx, 1); - this.infoMsg = ""; - document.getElementById("modalNews").checked = false; - }); + ajax( + "/news", + "DELETE", + { + data: { id: n.id }, + success: () => { + const nIdx = this.newsList.findIndex(nw => nw.id == n.id); + this.newsList.splice(nIdx, 1); + this.infoMsg = ""; + document.getElementById("modalNews").checked = false; + } + } + ); } }, loadMore: function() { - ajax("/news", "GET", {cursor:this.cursor}, (res) => { - if (res.newsList.length > 0) + ajax( + "/news", + "GET", { - this.newsList = this.newsList.concat(res.newsList); - const L = res.newsList.length; - if (L > 0) - this.cursor = res.newsList[L-1].id; + data: { cursor: this.cursor }, + success: (res) => { + if (res.newsList.length > 0) { + this.newsList = this.newsList.concat(res.newsList); + const L = res.newsList.length; + if (L > 0) this.cursor = res.newsList[L - 1].added; + } else this.hasMore = false; + } } - else - this.hasMore = false; - }); - }, - }, + ); + } + } }; @@ -158,24 +186,36 @@ export default { [type="checkbox"].modal+div .card max-width: 767px max-height: 100% + textarea#newsContent margin: 0 width: 100% min-height: 200px max-height: 100% + #dialog padding: 5px color: blue + +#writeNews + padding-top: 50px + +button#writeNewsBtn, button#loadMoreBtn + margin-top: 0 + margin-bottom: 0 + span.ndt color: darkblue padding: 0 5px 0 var(--universal-margin) -.margintop - margin-top: 25px - border-top: 1px solid grey + .news padding-top: 10px & > div display: inline-block + +.margintop + margin-top: 25px + border-top: 1px solid grey @media screen and (max-width: 767px) .margintop margin-top: 10px