From 179cf57cb97b8ba9901ccff7cd8e54c60e9b4d91 Mon Sep 17 00:00:00 2001 From: rr- Date: Tue, 2 Aug 2016 10:37:56 +0200 Subject: [PATCH] client/posts: add post featuring --- client/css/posts.styl | 4 ++++ client/html/post_edit_sidebar.tpl | 10 +++++++++ client/js/controllers/post_controller.js | 15 +++++++++++++ .../js/controls/post_edit_sidebar_control.js | 22 +++++++++++++++++++ client/js/models/post.js | 9 ++++++++ 5 files changed, 60 insertions(+) diff --git a/client/css/posts.styl b/client/css/posts.styl index 66335a56..23127080 100644 --- a/client/css/posts.styl +++ b/client/css/posts.styl @@ -316,6 +316,10 @@ $safety-unsafe = #F3985F flex-grow: 1 display: inline-block + .management + li + margin: 0 + label margin-bottom: 0.3em display: block diff --git a/client/html/post_edit_sidebar.tpl b/client/html/post_edit_sidebar.tpl index 83744d8d..e86eaad1 100644 --- a/client/html/post_edit_sidebar.tpl +++ b/client/html/post_edit_sidebar.tpl @@ -70,6 +70,16 @@ <% } %> + <% if (ctx.canFeaturePosts) { %> +
+ +
+ <% } %> +
diff --git a/client/js/controllers/post_controller.js b/client/js/controllers/post_controller.js index 6e5cb9b9..1fbe1cdb 100644 --- a/client/js/controllers/post_controller.js +++ b/client/js/controllers/post_controller.js @@ -59,6 +59,8 @@ class PostController { 'change', e => this._evtPostChange(e)); this._view.sidebarControl.addEventListener( 'submit', e => this._evtPostEdit(e)); + this._view.sidebarControl.addEventListener( + 'feature', e => this._evtPostFeature(e)); } if (this._view.commentFormControl) { this._view.commentFormControl.addEventListener( @@ -100,6 +102,19 @@ class PostController { settings.save(browsingSettings); } + _evtPostFeature(e) { + this._view.sidebarControl.disableForm(); + this._view.sidebarControl.clearMessages(); + e.detail.post.feature() + .then(() => { + this._view.sidebarControl.showSuccess('Post featured.'); + this._view.sidebarControl.enableForm(); + }, errorMessage => { + this._view.sidebarControl.showError(errorMessage); + this._view.sidebarControl.enableForm(); + }); + } + _evtPostEdit(e) { this._view.sidebarControl.disableForm(); this._view.sidebarControl.clearMessages(); diff --git a/client/js/controls/post_edit_sidebar_control.js b/client/js/controls/post_edit_sidebar_control.js index a3f13776..4827cbeb 100644 --- a/client/js/controls/post_edit_sidebar_control.js +++ b/client/js/controls/post_edit_sidebar_control.js @@ -42,6 +42,9 @@ class PostEditSidebarControl extends events.EventTarget { new ExpanderControl( 'Content', this._hostNode.querySelectorAll('.post-content, .post-thumbnail')); + new ExpanderControl( + 'Management', + this._hostNode.querySelectorAll('.management')); if (this._formNode) { this._formNode.addEventListener('submit', e => this._evtSubmit(e)); @@ -81,6 +84,11 @@ class PostEditSidebarControl extends events.EventTarget { this._post.hasCustomThumbnail ? 'block' : 'none'; } + if (this._featureLinkNode) { + this._featureLinkNode.addEventListener( + 'click', e => this._evtFeatureClick(e)); + } + this._post.addEventListener( 'changeContent', e => this._evtPostContentChange(e)); @@ -113,6 +121,16 @@ class PostEditSidebarControl extends events.EventTarget { this._thumbnailRemovalLinkNode.style.display = 'none'; } + _evtFeatureClick(e) { + if (confirm('Are you sure you want to feature this post?')) { + this.dispatchEvent(new CustomEvent('feature', { + detail: { + post: this._post, + }, + })); + } + } + _evtSubmit(e) { e.preventDefault(); this.dispatchEvent(new CustomEvent('submit', { @@ -185,6 +203,10 @@ class PostEditSidebarControl extends events.EventTarget { return this._formNode.querySelector('.post-thumbnail a'); } + get _featureLinkNode() { + return this._formNode.querySelector('.management .feature'); + } + enableForm() { views.enableForm(this._formNode); } diff --git a/client/js/models/post.js b/client/js/models/post.js index 468bac7a..c1280b3c 100644 --- a/client/js/models/post.js +++ b/client/js/models/post.js @@ -128,6 +128,15 @@ class Post extends events.EventTarget { }); } + feature() { + return api.post('/featured-post', {id: this._id}) + .then(response => { + return Promise.resolve(); + }, response => { + return Promise.reject(response.description); + }); + } + setScore(score) { return api.put('/post/' + this._id + '/score', {score: score}) .then(response => {