Last active
August 29, 2015 14:24
-
-
Save jittagornp/75cf2453df6dd4957cce to your computer and use it in GitHub Desktop.
flow control of trip page pamarin.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pamarin.factory('model.Trip', [ | |
| 'model.User', | |
| 'model.Tag', | |
| 'model.Link', | |
| 'enum.ArticleStatus', | |
| 'model.ProtocolMapper', | |
| function (User, Tag, Link, ArticleStatus, ProtocolMapper) { | |
| /** | |
| * @model Trip | |
| */ | |
| var Trip = function () { | |
| this.id = null; | |
| this.tripname = null; | |
| this.name = null; | |
| this.userId = null; | |
| this.user = null; | |
| this.tags = null; | |
| this.createDate = null; | |
| this.updateDate = null; | |
| this.status = null; | |
| this.link = null; | |
| }; | |
| /** | |
| * @param {Object} protocol | |
| * @returns {model.Trip} | |
| */ | |
| Trip.fromProtocol = function (protocol) { | |
| try { | |
| var trip = new Trip(); | |
| trip.id = protocol.trip_id; | |
| trip.tripname = protocol.tripname; | |
| trip.name = protocol.name; | |
| trip.userId = protocol.user_id; | |
| trip.user = User.fromProtocol(protocol.user); | |
| trip.tags = ProtocolMapper.fromArray(protocol.tags, Tag); | |
| trip.createDate = protocol.create_date; | |
| trip.updateDate = protocol.update_date; | |
| trip.status = ArticleStatus.valueOf(protocol.status) || ArticleStatus.DRAFF; | |
| trip.link = Link.fromProtocol(protocol.link); | |
| return trip; | |
| } catch (ex) { | |
| return null; | |
| } | |
| }; | |
| /** | |
| * @returns {Object} | |
| */ | |
| Trip.prototype.toProtocol = function () { | |
| return { | |
| trip_id: !!this.id ? this.id : null, | |
| tripname: !!this.tripname ? this.tripname : null, | |
| name: !!this.name ? this.name : null, | |
| user_id: !!this.userId ? this.userId : null, | |
| tags: ProtocolMapper.toArray(this.tags), | |
| create_date: !!this.createDate ? this.createDate : null, | |
| update_date: !!this.updateDate ? this.updateDate : null, | |
| status: !!this.status ? this.status.name : ArticleStatus.DRAFF.name, | |
| link: !!this.link ? this.link.toProtocol() : null | |
| }; | |
| }; | |
| return Trip; | |
| } | |
| ]); | |
| pamarin.factory('model.TripPost', [ | |
| 'enum.PostType', | |
| 'model.TripPostContent', | |
| function (PostType, TripPostContent) { | |
| /** | |
| * @model TripPost | |
| */ | |
| var TripPost = function () { | |
| this.id = null; | |
| this.title = null; | |
| this.tripId = null; | |
| this.trip = null; | |
| this.userId = null; | |
| this.createDate = null; | |
| this.updateDate = null; | |
| this.type = null; | |
| this.content = null; | |
| }; | |
| /** | |
| * @returns {TripPost} | |
| */ | |
| TripPost.newContent = function () { | |
| var post = new TripPost(); | |
| post.type = PostType.CONTENT; | |
| post.content = new TripPostContent(); | |
| return post; | |
| }; | |
| /** | |
| * @param {Object} protocol | |
| * @returns {TripPost} | |
| */ | |
| TripPost.fromProtocol = function (protocol) { | |
| try { | |
| var post = new TripPost(); | |
| post.id = protocol.trip_post_id; | |
| post.title = protocol.title; | |
| post.tripId = protocol.trip_id; | |
| post.userId = protocol.user_id; | |
| post.createDate = protocol.create_date; | |
| post.updateDate = protocol.update_date; | |
| post.type = PostType.valueOf(protocol.type) || PostType.CONTENT; | |
| post.content = TripPostContent.fromProtocol(protocol.content); | |
| return post; | |
| } catch (ex) { | |
| return null; | |
| } | |
| }; | |
| /** | |
| * @returns {Object} | |
| */ | |
| TripPost.prototype.toProtocol = function () { | |
| return { | |
| trip_post_id: !!this.id ? this.id : null, | |
| title: !!this.title ? this.title : null, | |
| trip_id: !!this.tripId ? this.tripId : null, | |
| user_id: !!this.userId ? this.userId : null, | |
| create_date: !!this.createDate ? this.createDate : null, | |
| update_date: !!this.updateDate ? this.updateDate : null, | |
| type: !!this.type ? this.type.name : PostType.CONTENT.name, | |
| content: !!this.content ? this.content.toProtocol() : null | |
| }; | |
| }; | |
| return TripPost; | |
| } | |
| ]); | |
| pamarin.factory('model.TripPostContent', [ | |
| function () { | |
| /** | |
| * @model TripPostContent | |
| */ | |
| var TripPostContent = function () { | |
| this.id = null; | |
| this.content = null; | |
| }; | |
| /** | |
| * @param {Object} protocol | |
| * @returns {TripPostContent} | |
| */ | |
| TripPostContent.fromProtocol = function (protocol) { | |
| try { | |
| var content = new TripPostContent(); | |
| content.id = protocol.trip_post_id; | |
| content.content = protocol.content; | |
| return content; | |
| } catch (ex) { | |
| return null; | |
| } | |
| }; | |
| /* | |
| * @returns {Object} | |
| */ | |
| TripPostContent.prototype.toProtocol = function () { | |
| return { | |
| trip_post_id: !!this.id ? this.id : null, | |
| content: !!this.content ? this.content : null | |
| }; | |
| }; | |
| return TripPostContent; | |
| } | |
| ]); | |
| pamarin.factory('enum.PostType', [ | |
| 'messages', | |
| 'enum.Enumerate', | |
| function (messages, Enumerate) { | |
| /** | |
| * @enum PostType | |
| */ | |
| var PostType = Enumerate([ | |
| { | |
| name: 'CONTENT', | |
| description: messages.CONTENT | |
| }, | |
| { | |
| name: 'PHOTO', | |
| description: messages.PHOTO | |
| } | |
| ]); | |
| return PostType; | |
| } | |
| ]); | |
| pamarin.factory('enum.ArticleStatus', [ | |
| 'messages', | |
| 'enum.Enumerate', | |
| function (messages, Enumerate) { | |
| /** | |
| * @enum ArticleStatus | |
| */ | |
| var ArticleStatus = Enumerate([ | |
| { | |
| name: 'PUBLIC', | |
| description: messages.ARTICLE_PUBLIC | |
| }, | |
| { | |
| name: 'DRAFF', | |
| description: messages.ARTICLE_DRAFF | |
| } | |
| ]); | |
| return ArticleStatus; | |
| } | |
| ]); | |
| pamarin.factory('model.TripPostPageResource', [ | |
| 'PageResource', | |
| 'model.TripPost', | |
| 'model.ProtocolMapper', | |
| function (PageResource, TripPost, ProtocolMapper) { | |
| /** | |
| * @model {TripPostPageResource} | |
| */ | |
| var TripPostPageResource = function () { | |
| }; | |
| /* | |
| * @param {Object} protocol | |
| * @returns {model.PageResource} | |
| */ | |
| TripPostPageResource.fromProtocol = function (protocol) { | |
| var page = PageResource.fromProtocol(protocol); | |
| page.content = ProtocolMapper.fromArray(page.content, TripPost); | |
| return page; | |
| }; | |
| return TripPostPageResource; | |
| } | |
| ]); | |
| pamarin.factory('model.TripPageContent', [ | |
| 'model.Trip', | |
| 'model.TripPostPageResource', | |
| function (Trip, TripPostPageResource) { | |
| /** | |
| * @model TripPageContent | |
| */ | |
| var TripPageContent = function () { | |
| this.trip = null; | |
| this.hasNextPage = false; | |
| this.pageNumber = 0; | |
| this.postPage = null; | |
| }; | |
| /** | |
| * @param {Object} protocol | |
| * @returns {TripPageContent} | |
| */ | |
| TripPageContent.fromProtocol = function (protocol) { | |
| try { | |
| var data = new TripPageContent(); | |
| data.trip = Trip.fromProtocol(protocol.trip); | |
| data.postPage = TripPostPageResource.fromProtocol(protocol.post_page); | |
| angular.forEach(data.postPage.content, function (post) { | |
| post.trip = data.trip; | |
| }); | |
| data.hasNextPage = data.postPage.hasNextPage || false; | |
| return data; | |
| } catch (ex) { | |
| return null; | |
| } | |
| }; | |
| /** | |
| * @param {model.TripPost} post | |
| * @returns {model.TripPost} | |
| */ | |
| TripPageContent.prototype.setupPost = function (post) { | |
| post.trip = this.trip; | |
| post.tripId = this.trip.id; | |
| post.userId = this.trip.userId; | |
| return post; | |
| }; | |
| /** | |
| * @param {model.TripPost} post | |
| */ | |
| TripPageContent.prototype.addPost = function (post) { | |
| this.postPage.content.push(this.setupPost(post)); | |
| }; | |
| TripPageContent.prototype.replacePost = function (post, index) { | |
| this.postPage.content.splice(index, 1, this.setupPost(post)); | |
| }; | |
| TripPageContent.prototype.indexOfPost = function (post) { | |
| return this.postPage.content.indexOf(post); | |
| }; | |
| /** | |
| * @param {model.TripPost} post | |
| */ | |
| TripPageContent.prototype.removePost = function (post) { | |
| this.postPage.content.splice(this.indexOfPost(post), 1); | |
| }; | |
| /* | |
| * @param {PageResource} page | |
| */ | |
| TripPageContent.prototype.addPage = function (page) { | |
| this.pageNumber = page.number; | |
| this.hasNextPage = page.hasNextPage; | |
| angular.forEach(page.content, function (post) { | |
| this.addPost(post); | |
| }, this); | |
| }; | |
| return TripPageContent; | |
| } | |
| ]); | |
| pamarin.service('TripService', [ | |
| 'config', | |
| 'util.Http', | |
| 'model.Trip', | |
| 'PageContext', | |
| 'model.PageData', | |
| 'util.ResponseMapper', | |
| 'model.TripPageContent', | |
| function (config, Http, Trip, pageContext, PageData, ResponseMapper, TripPageContent) { | |
| var serviceUrl = { | |
| insert: config.apiBase + '/trips', | |
| update: config.apiBase + '/trips', | |
| findPageData: config.apiBase + '/users/:username/trips/:tripname/pagedata' | |
| }; | |
| this.loadPageDataByUsernameAndTripname = function (username, tripname, callback) { | |
| ResponseMapper.fromHttp(Http.get( | |
| serviceUrl.findPageData | |
| .replace(':username', username) | |
| .replace(':tripname', tripname) | |
| )) | |
| .map2Model(PageData) | |
| .filter(function (data) { | |
| data.content = TripPageContent.fromProtocol(data.content); | |
| pageContext.data(data); | |
| return data.content; | |
| }) | |
| .returnOne(callback); | |
| }; | |
| this.save = function (trip, callback) { | |
| if (trip.id) { | |
| ResponseMapper.fromHttp(Http.put( | |
| serviceUrl.update, | |
| trip.toProtocol() | |
| )) | |
| .map2Model(Trip) | |
| .returnOne(callback); | |
| } else { | |
| ResponseMapper.fromHttp(Http.post( | |
| serviceUrl.insert, | |
| trip.toProtocol() | |
| )) | |
| .map2Model(Trip) | |
| .returnOne(callback); | |
| } | |
| }; | |
| } | |
| ]); | |
| pamarin.service('TripPostService', [ | |
| 'config', | |
| 'util.Http', | |
| 'model.TripPost', | |
| 'util.ResponseMapper', | |
| function (config, Http, TripPost, ResponseMapper) { | |
| var serviceUrl = { | |
| findAll: config.apiBase + '/users/:username/trips/:tripname/posts', | |
| insert: config.apiBase + '/users/:username/trips/:tripname/posts', | |
| update: config.apiBase + '/users/:username/trips/:tripname/posts', | |
| delete: config.apiBase + '/users/:username/trips/:tripname/posts/:postId' | |
| }; | |
| this.findByTrip = function (trip, pageable, callback) { | |
| ResponseMapper.fromHttp(Http.get( | |
| serviceUrl.findAll | |
| .replace(':username', trip.user.username) | |
| .replace(':tripname', trip.tripname) | |
| + '?' + pageable | |
| )) | |
| .map2Model(TripPost) | |
| .returnPage(callback); | |
| }; | |
| this.delete = function (post, callback) { | |
| ResponseMapper.fromHttp(Http.delete( | |
| serviceUrl.delete | |
| .replace(':username', post.trip.user.username) | |
| .replace(':tripname', post.trip.tripname) | |
| .replace(':postId', post.id) | |
| )) | |
| .returnOne(callback); | |
| }; | |
| this.save = function (post, callback) { | |
| if (post.id) { | |
| ResponseMapper.fromHttp(Http.put( | |
| serviceUrl.update | |
| .replace(':username', post.trip.user.username) | |
| .replace(':tripname', post.trip.tripname), | |
| post.toProtocol() | |
| )) | |
| .map2Model(TripPost) | |
| .returnOne(callback); | |
| } else { | |
| ResponseMapper.fromHttp(Http.post( | |
| serviceUrl.insert | |
| .replace(':username', post.trip.user.username) | |
| .replace(':tripname', post.trip.tripname), | |
| post.toProtocol() | |
| )) | |
| .map2Model(TripPost) | |
| .returnOne(callback); | |
| } | |
| }; | |
| } | |
| ]); | |
| pamarin.controller('TripCtrl', [ | |
| 'data', | |
| '$state', | |
| '$scope', | |
| 'TripService', | |
| 'PageContext', | |
| '$stateParams', | |
| 'TripPostService', | |
| 'model.TripPost', | |
| 'model.PageRequest', | |
| 'model.TripPageContent', | |
| function (data, $state, $scope, service, pageContext, $stateParams, postService, TripPost, PageRequest, TripPageContent) { | |
| $scope.data = TripPageContent.fromProtocol(data.init); | |
| pageContext.lazy(function () { | |
| service.loadPageDataByUsernameAndTripname( | |
| $stateParams.username, | |
| $stateParams.tripname, | |
| function (data) { | |
| $scope.data = data; | |
| } | |
| ); | |
| }); | |
| $scope.saveTrip = function () { | |
| service.save($scope.data.trip, function (trip) { | |
| $scope.data.trip = trip; | |
| $state.go('trips', { | |
| username: trip.user.username, | |
| tripname: trip.tripname | |
| }); | |
| }); | |
| }; | |
| $scope.cancelPost = function (post) { | |
| if (post.id) { | |
| } else { | |
| $scope.data.removePost(post); | |
| } | |
| }; | |
| $scope.addPost = function () { | |
| $scope.data.addPost(TripPost.newContent()); | |
| }; | |
| $scope.savePost = function (post) { | |
| var index = $scope.data.indexOfPost(post); | |
| postService.save(post, function (post) { | |
| $scope.data.replacePost(post, index); | |
| }); | |
| }; | |
| $scope.deletePost = function (post) { | |
| postService.delete(post, function (status) { | |
| if (status) { | |
| $scope.data.removePost(post); | |
| } | |
| }); | |
| }; | |
| $scope.loadMorePost = function () { | |
| if ($scope.data.hasNextPage) { | |
| var request = new PageRequest($scope.data.pageNumber + 1, 5); | |
| postService.findByTrip( | |
| $scope.data.trip, | |
| request, | |
| function (page) { | |
| $scope.data.addPage(page); | |
| } | |
| ); | |
| } | |
| }; | |
| } | |
| ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment