From 2ac8d8650ced43d63a3d784996c0c4c452ae5237 Mon Sep 17 00:00:00 2001 From: rr- Date: Mon, 13 Jun 2016 23:28:55 +0200 Subject: [PATCH] client/posts: introduce post model Now to replace all the api calls in views with event system... and then do the same for user, tag and comment --- client/js/controllers/post_controller.js | 7 +- client/js/controls/post_content_control.js | 3 - client/js/models/post.js | 89 ++++++++++++++++++++++ 3 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 client/js/models/post.js diff --git a/client/js/controllers/post_controller.js b/client/js/controllers/post_controller.js index 6899c38..2155750 100644 --- a/client/js/controllers/post_controller.js +++ b/client/js/controllers/post_controller.js @@ -4,6 +4,7 @@ const router = require('../router.js'); const api = require('../api.js'); const events = require('../events.js'); const settings = require('../settings.js'); +const Post = require('../models/post.js'); const TopNavigation = require('../models/top_navigation.js'); const PostView = require('../views/post_view.js'); const EmptyView = require('../views/empty_view.js'); @@ -26,13 +27,13 @@ class PostController { _showPostRoute(id, editMode) { TopNavigation.activate('posts'); Promise.all([ - api.get('/post/' + id), + Post.get(id), api.get(`/post/${id}/around?fields=id&query=` + this._decorateSearchQuery('')), ]).then(responses => { - const [postResponse, aroundResponse] = responses; + const [post, aroundResponse] = responses; this._postView.render({ - post: postResponse, + post: post, editMode: editMode, nextPostId: aroundResponse.next ? aroundResponse.next.id : null, prevPostId: aroundResponse.prev ? aroundResponse.prev.id : null, diff --git a/client/js/controls/post_content_control.js b/client/js/controls/post_content_control.js index 1144425..bd9d01e 100644 --- a/client/js/controls/post_content_control.js +++ b/client/js/controls/post_content_control.js @@ -6,9 +6,6 @@ const optimizedResize = require('../util/optimized_resize.js'); class PostContentControl { constructor(containerNode, post, viewportSizeCalculator) { - post.canvasWidth = post.canvasWidth || 800; - post.canvasHeight = post.canvasHeight || 450; - this._post = post; this._viewportSizeCalculator = viewportSizeCalculator; this._containerNode = containerNode; diff --git a/client/js/models/post.js b/client/js/models/post.js new file mode 100644 index 0000000..702f935 --- /dev/null +++ b/client/js/models/post.js @@ -0,0 +1,89 @@ +'use strict'; + +const api = require('../api.js'); +const events = require('../events.js'); + +class Post extends events.EventTarget { + constructor() { + super(); + this._id = null; + this._type = null; + this._mimeType = null; + this._creationTime = null; + this._user = null; + this._safety = null; + this._contentUrl = null; + this._thumbnailUrl = null; + this._canvasWidth = null; + this._canvasHeight = null; + this._fileSize = null; + + this._tags = []; + this._notes = []; + this._comments = []; + this._relations = []; + + this._score = null; + this._favoriteCount = null; + this._ownScore = null; + this._ownFavorite = null; + } + + // encapsulation - don't let set these casually + get id() { return this._id; } + get type() { return this._type; } + get mimeType() { return this._mimeType; } + get creationTime() { return this._creationTime; } + get user() { return this._user; } + get safety() { return this._safety; } + get contentUrl() { return this._contentUrl; } + get thumbnailUrl() { return this._thumbnailUrl; } + get canvasWidth() { return this._canvasWidth || 800; } + get canvasHeight() { return this._canvasHeight || 450; } + get fileSize() { return this._fileSize || 0; } + + get tags() { return this._tags; } + get notes() { return this._notes; } + get comments() { return this._comments; } + get relations() { return this._relations; } + + get score() { return this._score; } + get favoriteCount() { return this._favoriteCount; } + get ownFavorite() { return this._ownFavorite; } + get ownScore() { return this._ownScore; } + + static get(id) { + return new Promise((resolve, reject) => { + api.get('/post/' + id) + .then(response => { + const post = new Post(); + post._id = response.id; + post._type = response.type; + post._mimeType = response.mimeType; + post._creationTime = response.creationTime; + post._user = response.user; + post._safety = response.safety; + post._contentUrl = response.contentUrl; + post._thumbnailUrl = response.thumbnailUrl; + post._canvasWidth = response.canvasWidth; + post._canvasHeight = response.canvasHeight; + post._fileSize = response.fileSize; + + post._tags = response.tags; + post._notes = response.notes; + post._comments = response.comments; + post._relations = response.relations; + + post._score = response.score; + post._favoriteCount = response.favoriteCount; + post._ownScore = response.ownScore; + post._ownFavorite = response.ownFavorite; + resolve(post); + }, response => { + reject(response); + }); + }); + } +}; + +module.exports = Post;