Skip to content

Instantly share code, notes, and snippets.

@jittagornp
Last active August 29, 2015 14:23
Show Gist options
  • Save jittagornp/608764591ccd704168b5 to your computer and use it in GitHub Desktop.
Save jittagornp/608764591ccd704168b5 to your computer and use it in GitHub Desktop.
angularjs model of pamarin.com
/**
* pamarin © 2015
*/
pamarin.factory('enum.Enumerate', function () {
return (function (list, fnc) {
/**
* @enum Enumerate
*/
var Enumerate = function (ctx) {
if (angular.isObject(ctx)) {
angular.forEach(ctx, function (val, key) {
this[key] = val;
}, this);
} else if (angular.isString(ctx)) {
this.name = ctx;
} else {
throw new Error('incorrect arguments.');
}
};
Enumerate.values = [];
/**
* @param {String | Object} ctx
* @returns {Enumerate}
*/
Enumerate.valueOf = function (ctx) {
if (!ctx || ctx == 'null') {
return null;
}
var obj = null;
angular.forEach(Enumerate.values, function (item) {
if (item.name === ctx) {
obj = item;
return false;
}
});
if (!obj) {
obj = new Enumerate(ctx);
Enumerate.values.push(obj);
}
return obj;
};
/**
* @param {String} protocol
* @returns {Enumerate}
*/
Enumerate.fromProtocol = function (protocol) {
try {
return Enumerate.valueOf(protocol);
} catch (ex) {
return null;
}
};
/**
* @returns {String}
*/
Enumerate.prototype.toProtocol = function () {
return this.name;
};
/**
* @returns {String}
*/
Enumerate.prototype.toString = function () {
return this.name;
};
fnc && angular.forEach(fnc, function (val, key) {
Enumerate.prototype[key] = val;
});
angular.forEach(list, function (item) {
Enumerate[
angular.isObject(item)
? item.name
: item
] = Enumerate.valueOf(item);
});
return Enumerate;
});
});
pamarin.factory('model.ProtocolMapper', [
function () {
/**
* @model ProtocolMapper
*/
var ProtocolMapper = function () {
};
/**
* for convert object to Object protocol
*
* @param {Array} arr
* @returns {Array}
*/
ProtocolMapper.toArray = function (arr) {
var protocols = [];
if (!arr) {
return protocols;
}
angular.forEach(arr, function (obj) {
protocols.push(obj.toProtocol());
});
return protocols;
};
/**
* for convert Object protocol to object
*
* @param {Array} arr
* @param {Function} model which has static method .fromProtocol(protocol)
* @returns {Array}
*/
ProtocolMapper.fromArray = function (arr, clazz) {
var objs = [];
if (!arr) {
return objs;
}
angular.forEach(arr, function (item) {
objs.push(clazz.fromProtocol(item));
});
return objs;
};
return ProtocolMapper;
}
]);
pamarin.factory('model.PageRequest', [
'enum.Enumerate',
function (Enumerate) {
/**
* @model PageRequest
*/
var PageRequest = function (number, size, sorts) {
this.number = number || 0;
this.size = size || 0;
this.sorts = sorts || [];
};
/**
* @model PageRequest.Sort
*
* @param {String} property
* @param {PageRequest.Sort.Direction} direction
*/
PageRequest.Sort = function (property, direction) {
this.property = property;
this.direction = direction;
};
/**
* @enum PageRequest.Sort.Direction
*/
PageRequest.Sort.Direction = Enumerate([
'ASC',
'DESC'
]);
function buildSorts(sorts) {
var sts = [];
angular.forEach(sorts, function (sort) {
sts.push(sort.property + ':' + sort.direction);
});
return sts.join(',');
}
/**
* @returns {String}
*/
PageRequest.prototype.toString = function () {
return 'page=' + this.number
+ '&size=' + this.size
+ (this.sorts.length ? ('&sort=' + buildSorts(this.sorts)) : '');
};
return PageRequest;
}
]);
pamarin.factory('PageResource', [
function () {
/**
* @model PageResource
*/
var PageResource = function () {
this.size = 0;
this.number = 0;
this.content = [];
this.sorts = [];
this.links = [];
this.totalElements = 0;
this.totalPages = 0;
this.numberOfElements = 0;
this.hasNextPage = false;
this.isOffsetLimit = false;
};
/**
* @param {Object} protocol
* @returns {PageResource}
*/
PageResource.fromProtocol = function (protocol) {
try {
var page = new PageResource();
page.size = protocol.size || 0;
page.number = protocol.number || 0;
page.content = protocol.content || [];
page.sorts = protocol.sorts || [];
page.links = protocol.links || [];
page.totalElements = protocol.total_elements || 0;
page.totalPages = protocol.total_pages || 0;
page.numberOfElements = protocol.number_of_elements || 0;
page.hasNextPage = protocol.has_next_page || false;
page.isOffsetLimit = protocol.is_offset_limit || false;
return page;
} catch (ex) {
return null;
}
};
return PageResource;
}
]);
pamarin.factory('model.PageData', [
function () {
/**
* @model PageData
*/
var PageData = function () {
this.title = null;
this.metadata = [];
this.content = null;
};
/**
* @param {Object} protocol
* @returns {PageData}
*/
PageData.fromProtocol = function (protocol) {
try {
var data = new PageData();
data.title = protocol.title;
data.metadata = protocol.metadata;
data.content = protocol.content;
return data;
} catch (ex) {
return null;
}
};
return PageData;
}
]);
pamarin.factory('model.Authority', [
function () {
/**
* @model Authority
*/
var Authority = function (id, desc) {
this.id = id || null;
this.description = desc || null;
};
Authority.ADMIN = new Authority('ADMIN', 'administrator');
/**
* @returns {Object}
*/
Authority.prototype.toProtocol = function () {
return {
auth_id: !!this.id ? this.id : null,
description: !!this.description ? this.description : null
};
};
/**
* @param {Object} protocol
* @returns {Authority}
*/
Authority.fromProtocol = function (protocol) {
try {
return new Authority(protocol.auth_id, protocol.description);
} catch (ex) {
return null;
}
};
return Authority;
}
]);
pamarin.factory('model.User', [
'messages',
'model.Link',
'enum.Enumerate',
'model.Authority',
'model.ProtocolMapper',
function (messages, Link, Enumerate, Authority, ProtocolMapper) {
/**
* @model User
*/
var User = function () {
this.id = null;
this.username = null;
this.firstName = null;
this.lastName = null;
this.name = null;
this.email = null;
this.gender = null; /* User.Gener */
this.locale = null;
this.timezone = null;
this.authorities = [];
this.picture = null;
this.social = null; /* User.Social */
this.socialId = null;
this.createDate = null;
this.updateDate = null;
this.aboutMe = null;
this.link = null; /* model.Link */
};
/**
* @enum User.Gender
*/
User.Gender = Enumerate([
{
name: 'UNKNOWN',
description: messages.UNKNOWN
},
{
name: 'MALE',
description: messages.MALE
},
{
name: 'FEMALE',
description: messages.FEMALE
}
]);
/**
* @enum User.Social
*/
User.Social = Enumerate([
'FACEBOOK',
'GOOGLE_PLUS',
'TWITTER'
]);
/**
* @param {Object} protocol
* @returns {User}
*/
User.fromProtocol = function (protocol) {
try {
var user = new User();
user.id = protocol.user_id;
user.username = protocol.username;
user.firstName = protocol.first_name;
user.lastName = protocol.last_name;
user.name = protocol.name;
user.email = protocol.email;
user.gender = User.Gender.valueOf(protocol.gender);
user.locale = protocol.locale;
user.timezone = protocol.timezone;
user.authorities = ProtocolMapper.fromArray(protocol.authorities, Authority);
user.picture = protocol.picture;
user.social = User.Social.valueOf(protocol.social);
user.socialId = protocol.social_id;
user.createDate = protocol.create_date;
user.updateDate = protocol.update_date;
user.aboutMe = protocol.about_me;
user.link = Link.fromProtocol(protocol.link);
return user;
} catch (ex) {
return null;
}
};
/**
* @returns {Object}
*/
User.prototype.toProtocol = function () {
return {
user_id: !!this.id ? this.id : null,
username: !!this.username ? this.username : null,
first_name: !!this.firstName ? this.firstName : null,
last_name: !!this.lastName ? this.lastName : null,
email: !!this.email ? this.email : null,
gender: !!this.gender ? this.gender.name : null,
locale: !!this.locale ? this.locale : null,
timezone: !!this.timezone ? this.timezone : null,
authorities: ProtocolMapper.toArray(this.authorities),
picture: !!this.picture ? this.picture : null,
social: !!this.social ? this.social.name : null,
social_id: !!this.socialId ? this.socialId : null,
create_date: !!this.createDate ? this.createDate : null,
update_date: !!this.updateDate ? this.updateDate : null,
about_me: !!this.aboutMe ? this.aboutMe : null,
link: !!this.link ? this.link.toProtocol() : null
};
};
return User;
}
]);
pamarin.factory('model.TripPageResource', [
'model.Trip',
'PageResource',
'model.ProtocolMapper',
function (Trip, PageResource, ProtocolMapper) {
/**
* @model TripPageResource
*/
var TripPageResource = function () {
};
/**
* @param {Object} protocol
* @returns {PageResource}
*/
TripPageResource.fromProtocol = function (protocol) {
try {
var page = PageResource.fromProtocol(protocol);
page.content = ProtocolMapper.fromArray(page.content, Trip);
return page;
} catch (ex) {
return null;
}
};
return TripPageResource;
}
]);
pamarin.factory('model.UserPageContent', [
'model.User',
'model.TripPageResource',
function (User, TripPageResource) {
/**
* @model UserPageContent
*/
var UserPageContent = function () {
this.user = null;
this.tripPage = null;
};
/**
* @param {Object} protocol
* @returns {UserPageContent}
*/
UserPageContent.fromProtocol = function (protocol) {
try {
var data = new UserPageContent();
data.user = User.fromProtocol(protocol.user);
data.tripPage = TripPageResource.fromProtocol(protocol.trip_page);
return data;
} catch (ex) {
return null;
}
};
return UserPageContent;
}
]);
pamarin.factory('model.AccountPageContent', [
'model.User',
function (User) {
/**
* @model AccountPageContent
*/
var AccountPageContent = function () {
this.user = null;
};
/**
* @param {Object} protocol
* @returns {AccountPageContent}
*/
AccountPageContent.fromProtocol = function (protocol) {
try {
var data = new AccountPageContent();
data.user = User.fromProtocol(protocol.user);
return data;
} catch (ex) {
return null;
}
};
return AccountPageContent;
}
]);
pamarin.factory('model.TagLookup', [
function () {
/**
* @model TagLookup
*/
var TagLookup = function () {
this.id = null;
this.name = null;
this.description = null;
};
/*
* @param {Object} protocol
* @returns {TagLookup}
*/
TagLookup.fromProtocol = function (protocol) {
try {
var lookup = new TagLookup();
lookup.id = protocol.lookup_id;
lookup.name = protocol.name;
lookup.description = protocol.description;
return lookup;
} catch (ex) {
return null;
}
};
/**
* @returns {Object}
*/
TagLookup.prototype.toProtocol = function () {
return {
lookup_id: !!this.id ? this.id : null,
name: !!this.name ? this.name : null
};
};
return TagLookup;
}
]);
pamarin.factory('model.Tag', [
'model.TagLookup',
function (TagLookup) {
/**
* @model Tag
*/
var Tag = function () {
this.id = {
tripId: null,
lookupId: null
};
this.lookup = null;
this.createDate = null;
};
/**
* @param {Object} protocol
* @returns {Tag}
*/
Tag.fromProtocol = function (protocol) {
try {
var tag = new Tag();
tag.id.tripId = protocol.id.trip_id;
tag.id.lookupId = protocol.id.lookup_id;
tag.lookup = TagLookup.fromProtocol(protocol.lookup);
tag.createDate = protocol.create_date;
return tag;
} catch (ex) {
return null;
}
};
/**
* @returns {Object}
*/
Tag.prototype.toProtocol = function () {
return {
id: {
trip_id: !!this.id ? this.id.tripId : null,
lookup_id: !!this.id ? this.id.lookupId : null
},
create_date: !!this.createDate ? this.createDate : null
};
};
return Tag;
}
]);
pamarin.factory('model.Tags', [
'model.Tag',
'model.ProtocolMapper',
function (Tag, ProtocolMapper) {
/**
* @model Tags
*/
var Tags = function () {
this.tags = null;
};
/**
* @param {Array<model.Tag>} tags
* @returns {Tags}
*/
Tags.fromTags = function (tags) {
var tgs = new Tags();
tgs.tags = tags;
return tgs;
};
/**
* @returns {Object}
*/
Tags.prototype.toProtocol = function () {
return {
tags: ProtocolMapper.toArray(this.tags, Tag)
};
};
return Tags;
}
]);
pamarin.factory('model.TagMapper', [
'model.Tag',
function (Tag) {
/**
* @model TagMapper
*/
var TagMapper = function () {
};
/**
* @param {model.TagLookup} lookup
* @param {String} tripId
* @returns {model.Tag}
*/
TagMapper.lookup2Tag = function (lookup, tripId) {
try {
var tag = new Tag();
tag.id.tripId = tripId;
tag.id.lookupId = lookup.id;
tag.lookup = lookup;
return tag;
} catch (ex) {
return null;
}
};
/**
* @param {Array<model.TagLookup>} lookups
* @param {type} tripId
* @returns {Array<model.Tag>}
*/
TagMapper.lookups2Tags = function (lookups, tripId) {
var tags = [];
angular.forEach(lookups, function (lookup) {
var tag = TagMapper.lookup2Tag(lookup, tripId);
if (tag) {
tags.push(tag);
}
});
return tags;
};
/**
* @param {model.Tag} tag
* @returns {mode.TagLookup}
*/
TagMapper.tag2Lookup = function (tag) {
if (!tag) {
return null;
}
return tag.lookup;
};
/**
* @param {Array<model.Tag>} tags
* @returns {Array<mode.TagLookup>}
*/
TagMapper.tags2Lookups = function (tags) {
var lookups = [];
angular.forEach(tags, function (tag) {
var lookup = TagMapper.tag2Lookup(tag);
if (lookup) {
lookups.push(lookup);
}
});
return lookups;
};
return TagMapper;
}
]);
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.Link', [
function () {
/**
* @model Link
*/
var Link = function () {
this.pattern = null;
this.mapping = null;
this.href = null;
};
/**
* @param {Object} protocol
* @returns {Link}
*/
Link.fromProtocol = function (protocol) {
try {
var link = new Link();
link.pattern = protocol.pattern;
link.mapping = protocol.mapping;
link.href = protocol.href;
return link;
} catch (ex) {
return null;
}
};
/**
* @returns {Object}
*/
Link.prototype.toProtocol = function () {
return {
pattern: !!this.pattern ? this.pattern : null,
mapping: !!this.mapping ? this.mapping : null,
href: !!this.href ? this.href : null
};
};
return Link;
}
]);
pamarin.factory('model.PostContent', [
function () {
/**
* @model PostContent
*/
var PostContent = function () {
this.id = null;
this.content = null;
};
/**
* @param {Object} protocol
* @returns {PostContent}
*/
PostContent.fromProtocol = function (protocol) {
try {
var content = new PostContent();
content.id = protocol.post_id;
content.content = protocol.content;
return content;
} catch (ex) {
return null;
}
};
/*
* @returns {Object}
*/
PostContent.prototype.toProtocol = function () {
return {
post_id: !!this.id ? this.id : null,
content: !!this.content ? this.content : null
};
};
return PostContent;
}
]);
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('model.Post', [
'enum.PostType',
'model.PostContent',
function (PostType, PostContent) {
/**
* @model Post
*/
var Post = function () {
this.id = null;
this.title = null;
this.referenceId = null;
this.createDate = null;
this.updateDate = null;
this.type = null;
this.sequence = null;
this.content = null;
};
/**
* @returns {Post}
*/
Post.newContent = function () {
var post = new Post();
post.type = PostType.CONTENT;
post.content = new PostContent();
post.editing = true;
return post;
};
/**
* @param {Object} protocol
* @returns {Post}
*/
Post.fromProtocol = function (protocol) {
try {
var post = new Post();
post.id = protocol.post_id;
post.title = protocol.title;
post.referenceId = protocol.reference_id;
post.createDate = protocol.create_date;
post.updateDate = protocol.update_date;
post.type = PostType.valueOf(protocol.type) || PostType.CONTENT;
post.sequence = protocol.post_sequence;
post.content = PostContent.fromProtocol(protocol.content);
return post;
} catch (ex) {
return null;
}
};
/**
* @returns {Object}
*/
Post.prototype.toProtocol = function () {
return {
post_id: !!this.id ? this.id : null,
title: !!this.title ? this.title : null,
reference_id: !!this.referenceId ? this.referenceId : null,
create_date: !!this.createDate ? this.createDate : null,
update_date: !!this.updateDate ? this.updateDate : null,
type: !!this.type ? this.type.name : PostType.CONTENT.name,
post_sequence: !!this.sequence ? this.sequence : null,
content: !!this.content ? this.content.toProtocol() : null
};
};
return Post;
}
]);
pamarin.factory('enum.LikeType', [
'enum.Enumerate',
function (Enumerate) {
/**
* @enum LikeType
*/
var LikeType = Enumerate([
'POST',
'COMMENT'
]);
return LikeType;
}
]);
pamarin.factory('model.Like', [
'model.User',
'enum.LikeType',
function (User, LikeType) {
/**
* @model Like
*/
var Like = function () {
this.id = {
referenceId: null,
userId: null,
referenceType: null
};
this.user = null;
};
/**
* @param {Object} protocol
* @returns {Like}
*/
Like.fromProtocol = function (protocol) {
try {
var like = new Like();
like.id.referenceId = protocol.id.reference_id;
like.id.userId = protocol.id.user_id;
like.id.referenceType = LikeType.valueOf(protocol.id.reference_type);
like.user = User.fromProtocol(protocol.user);
return like;
} catch (ex) {
return null;
}
};
return Like;
}
]);
pamarin.factory('model.Likes', [
'model.Like',
'model.ProtocolMapper',
function (Like, ProtocolMapper) {
/**
* @model Likes
*/
var Likes = function () {
this.total = 0;
this.liked = false;
this.likes = null;
};
/**
* @param {Object} protocol
* @returns {Likes}
*/
Likes.fromProtocol = function (protocol) {
try {
var likes = new Likes();
likes.total = protocol.total;
likes.liked = protocol.liked;
likes.likes = ProtocolMapper.fromArray(protocol.likes, Like);
return likes;
} catch (ex) {
return null;
}
};
return Likes;
}
]);
pamarin.factory('model.Comment', [
'util.Time',
'model.User',
function (Time, User) {
/**
* @model Comment
*/
var Comment = function () {
this.id = null;
this.message = null;
this.backupMessage = null;
this.createDate = null;
this.createDateDesc = null;
this.updateDate = null;
this.updateDateDesc = null;
this.referenceId = null;
this.userId = null;
this.user = null;
this.isOwner = null;
};
/**
* @param {Object} protocol
* @returns {Comment}
*/
Comment.fromProtocol = function (protocol) {
try {
var comment = new Comment();
comment.id = protocol.comment_id;
comment.message = protocol.message;
comment.backupMessage = protocol.message;
comment.createDate = protocol.create_date;
comment.createDateDesc = Time.fromNow(protocol.create_date);
comment.updateDate = protocol.update_date;
comment.updateDateDesc = Time.fromNow(protocol.update_date);
comment.referenceId = protocol.reference_id;
comment.userId = protocol.user_id;
comment.user = User.fromProtocol(protocol.user);
comment.isOwner = protocol.is_owner;
return comment;
} catch (ex) {
return null;
}
};
/**
* @returns {Object}
*/
Comment.prototype.toProtocol = function () {
return {
comment_id: !!this.id ? this.id : null,
message: !!this.message ? this.message : null,
create_date: !!this.createDate ? this.createDate : null,
update_date: !!this.updateDate ? this.updateDate : null,
reference_id: !!this.referenceId ? this.referenceId : null,
user_id: !!this.userId ? this.userId : null
};
};
return Comment;
}
]);
pamarin.factory('model.CommentPageResource', [
'PageResource',
'model.Comment',
'model.ProtocolMapper',
function (PageResource, Comment, ProtocolMapper) {
/**
* @model {CommentPageResource}
*/
var CommentPageResource = function () {
};
/*
* @param {Object} protocol
* @returns {model.PageResource}
*/
CommentPageResource.fromProtocol = function (protocol) {
var page = PageResource.fromProtocol(protocol);
page.content = ProtocolMapper.fromArray(page.content, Comment);
return page;
};
return CommentPageResource;
}
]);
pamarin.factory('model.TripCard', [
'model.Trip',
'model.Likes',
function (Trip, Likes) {
/**
* @model TripCard
*/
var TripCard = function () {
this.trip = null; /* model.Trip */
this.likes = null; /* model.Likes */
this.numberOfComments = null;
};
/**
* @param {Object} protocol
* @returns {TripCard}
*/
TripCard.fromProtocol = function (protocol) {
try {
var card = new TripCard();
card.trip = Trip.fromProtocol(protocol.trip);
card.likes = Likes.fromProtocol(protocol.likes);
card.numberOfComments = protocol.number_of_comments;
return card;
} catch (ex) {
return null;
}
};
return TripCard;
}
]);
pamarin.factory('model.Trip', [
'util.Time',
'model.Tag',
'model.Link',
'model.User',
'SecurityContext',
'enum.ArticleStatus',
'model.ProtocolMapper',
function (Time, Tag, Link, User, securityContext, 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.createDateDesc = null;
this.updateDate = null;
this.updateDateDesc = null;
this.status = null;
this.link = null;
this.isOwner = null;
};
Trip.newForWrite = function () {
var trip = new Trip();
trip.user = securityContext.user();
trip.userId = trip.user.id;
trip.status = ArticleStatus.DRAFF;
return trip;
};
/**
* @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.createDateDesc = Time.fromNow(protocol.create_date);
trip.updateDate = protocol.update_date;
trip.updateDateDesc = Time.fromNow(protocol.update_date);
trip.status = ArticleStatus.valueOf(protocol.status) || ArticleStatus.DRAFF;
trip.link = Link.fromProtocol(protocol.link);
trip.isOwner = protocol.is_owner;
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,
is_owner: !!this.isOwner ? this.isOwner : null
};
};
return Trip;
}
]);
pamarin.factory('model.PostPageResource', [
'PageResource',
'model.Post',
'model.ProtocolMapper',
function (PageResource, Post, ProtocolMapper) {
/**
* @model PostPageResource
*/
var PostPageResource = function () {
};
/*
* @param {Object} protocol
* @returns {model.PageResource}
*/
PostPageResource.fromProtocol = function (protocol) {
var page = PageResource.fromProtocol(protocol);
page.content = ProtocolMapper.fromArray(page.content, Post);
return page;
};
return PostPageResource;
}
]);
pamarin.factory('model.TripCardPageResource', [
'PageResource',
'model.TripCard',
'model.ProtocolMapper',
function (PageResource, TripCard, ProtocolMapper) {
/**
* @model TripCardPageResource
*/
var TripCardPageResource = function () {
};
/*
* @param {Object} protocol
* @returns {model.PageResource}
*/
TripCardPageResource.fromProtocol = function (protocol) {
var page = PageResource.fromProtocol(protocol);
page.content = ProtocolMapper.fromArray(page.content, TripCard);
return page;
};
return TripCardPageResource;
}
]);
pamarin.factory('model.HomePageContent', [
'model.TripCardPageResource',
function (TripCardPageResource) {
/**
* @model HomePageContent
*/
var HomePageContent = function () {
this.tripCardPage = null; /* model.TripCardPageResource */
};
/**
* @param {Object} protocol
* @returns {HomePageContent}
*/
HomePageContent.fromProtocol = function (protocol) {
try {
var content = new HomePageContent();
content.tripCardPage = TripCardPageResource.fromProtocol(protocol.trip_card_page);
return content;
} catch (ex) {
return null;
}
};
/*
* @param {PageResource} page
*/
HomePageContent.prototype.addTripCardPage = function (page) {
this.tripCardPage.number = page.number;
this.tripCardPage.hasNextPage = page.hasNextPage;
angular.forEach(page.content, function (card) {
this.tripCardPage.content.push(card);
}, this);
};
return HomePageContent;
}
]);
pamarin.factory('model.TripPageContent', [
'util.Time',
'model.Trip',
'SecurityContext',
'model.Likes',
'model.Comment',
'model.PostPageResource',
'model.CommentPageResource',
function (Time, Trip, securityContext, TripLikes, Comment, PostPageResource, CommentPageResource) {
/**
* @model TripPageContent
*/
var TripPageContent = function () {
this.trip = null;
this.postPage = null;
this.likes = null;
this.commentPage = null;
this.comment = null;
};
function translate2PostPage(protocol, content) {
content.postPage = PostPageResource.fromProtocol(protocol.post_page);
content.postPage.number = 0;
content.postPage.hasNextPage = content.postPage.hasNextPage || false;
angular.forEach(content.postPage.content, function (post) {
post.reference = content.trip;
});
}
function translate2CommentPage(protocol, content) {
content.commentPage = CommentPageResource.fromProtocol(protocol.comment_page);
content.commentPage.hasNextPage = content.commentPage.hasNextPage || false;
angular.forEach(content.commentPage.content, function (comment) {
comment.reference = content.trip;
});
}
/**
* @param {Object} protocol
* @returns {TripPageContent}
*/
TripPageContent.fromProtocol = function (protocol) {
try {
var content = new TripPageContent();
content.trip = Trip.fromProtocol(protocol.trip);
translate2PostPage(protocol, content);
translate2CommentPage(protocol, content);
content.likes = TripLikes.fromProtocol(protocol.likes);
return content;
} catch (ex) {
return null;
}
};
/**
* @param {model.Post} post
* @returns {model.Post}
*/
TripPageContent.prototype.setupPost = function (post) {
post.reference = this.trip;
post.referenceId = this.trip.id;
return post;
};
/**
* @param {model.Post} post
*/
TripPageContent.prototype.addPost = function (post) {
var posts = this.postPage.content;
if (!post.sequence) {
var SEQUENCE_INCREASE = 1000;
if (posts.length) {
var last = posts.slice(posts.length - 1)[0];
post.sequence = last.sequence + SEQUENCE_INCREASE;
} else {
post.sequence = SEQUENCE_INCREASE;
}
}
this.postPage.totalElements = this.postPage.totalElements + 1;
posts.push(this.setupPost(post));
};
/**
* @param {model.Post} post
* @param {Number} index
*/
TripPageContent.prototype.replacePost = function (post, index) {
this.trip.updateDateDesc = Time.fromNow(post.updateDate);
this.postPage.content.splice(index, 1, this.setupPost(post));
};
/**
* @param {model.Post} post
* @param {Number} index
*/
TripPageContent.prototype.insertPost = function (post, index) {
if (index < 0) {
return;
}
var posts = this.postPage.content;
var top = posts[index];
var bottom = posts[index + 1];
if (bottom) {
this.postPage.totalElements = this.postPage.totalElements + 1;
post.sequence = (top.sequence + bottom.sequence) / 2.0;
posts.splice(index + 1, 0, this.setupPost(post));
} else {
this.addPost(post);
}
};
/**
* @param {model.Post} post
* @returns {Number}
*/
TripPageContent.prototype.indexOfPost = function (post) {
return this.postPage.content.indexOf(post);
};
/**
* @param {model.Post} post
*/
TripPageContent.prototype.removePost = function (post) {
this.postPage.totalElements = this.postPage.totalElements - 1;
this.postPage.content.splice(this.indexOfPost(post), 1);
};
/*
* @param {PageResource} page
*/
TripPageContent.prototype.addPostPage = function (page) {
this.postPage.number = this.postPage.number + 1;
this.postPage.hasNextPage = page.hasNextPage;
angular.forEach(page.content, function (post) {
this.addPost(post);
}, this);
};
/**
* @param {Array<model.Tag>} tags
*/
TripPageContent.prototype.setTags = function (tags) {
if (tags.length) {
this.trip.updateDateDesc = Time.fromNow(tags[0].createDate);
}
this.trip.tags = tags;
};
/**
* @returns {model.Comment}
*/
TripPageContent.prototype.newComment = function () {
if (this.trip) {
var cmnt = new Comment();
cmnt.referenceId = this.trip.id;
cmnt.reference = this.trip;
cmnt.user = securityContext.user();
cmnt.userId = cmnt.user.id;
this.comment = cmnt;
return this.comment;
}
return null;
};
/**
* @param {model.Comment} comment
*/
TripPageContent.prototype.addComment = function (comment) {
this.commentPage.totalElements = this.commentPage.totalElements + 1;
this.commentPage.content.splice(0, 0, comment);
this.comment = this.newComment();
};
/**
* @param {model.Comment} comment
* @returns {Number}
*/
TripPageContent.prototype.indexOfComment = function (comment) {
return this.commentPage.content.indexOf(comment);
};
/**
* @param {model.Comment} comment
* @param {Number} index
*/
TripPageContent.prototype.replaceComment = function (comment, index) {
this.commentPage.content.splice(index, 1, comment);
};
/**
* @param {model.Comment} comment
*/
TripPageContent.prototype.removeComment = function (comment) {
this.commentPage.totalElements = this.commentPage.totalElements - 1;
this.commentPage.content.splice(this.indexOfComment(comment), 1);
};
/*
* @param {PageResource} page
*/
TripPageContent.prototype.addCommentPage = function (page) {
this.commentPage.number = page.number;
this.commentPage.hasNextPage = page.hasNextPage;
this.commentPage.totalElements = page.totalElements;
this.commentPage.numberOfElements += page.numberOfElements;
angular.forEach(page.content, function (comment) {
this.commentPage.content.push(comment);
}, this);
};
return TripPageContent;
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment