Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 8a33b9c

Browse files
committed
update from liveblog repo.
added * `convertLinksWithRelativeProtocol` filter to fix https issues * `comments` services * main `services` for the theme. * fixed `resources` transformation.
1 parent 9be6f50 commit 8a33b9c

5 files changed

Lines changed: 248 additions & 20 deletions

File tree

liveblog-embed/comments.service.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
(function(angular) {
2+
'use strict';
3+
4+
CommentsManagerFactory.$inject = ['comments', 'items', '$q', 'config'];
5+
function CommentsManagerFactory(commentsService, itemsService, $q, config) {
6+
7+
function CommentsManager (max_results, sort) {
8+
9+
/**
10+
* Send a new comment to be approved.
11+
* @returns {promise}
12+
*/
13+
this.send = function(data) {
14+
var deferred = $q.defer();
15+
data.client_blog = config.blog._id;
16+
data.item_type = 'comment';
17+
itemsService.save(data).$promise.then(function(dataItem) {
18+
if (dataItem._status === 'ERR'){
19+
deferred.reject('Try again later!')
20+
return;
21+
}
22+
var comment = {
23+
"post_status": "comment",
24+
"client_blog": config.blog._id,
25+
"groups": [{
26+
"id": "root",
27+
"refs": [{"idRef":"main"}],
28+
"role": "grpRole:NEP"
29+
},{
30+
"id": "main",
31+
"refs": [{"residRef": dataItem._id}],
32+
"role":"grpRole:Main"}
33+
]
34+
};
35+
commentsService.save(comment).$promise.then(function(dataComment) {
36+
if (dataComment._status === 'ERR'){
37+
deferred.reject('Try again later!')
38+
return;
39+
}
40+
deferred.resolve(dataComment);
41+
});
42+
43+
}, function(error) {
44+
deferred.reject('Try again later!');
45+
});
46+
return deferred.promise;
47+
};
48+
}
49+
50+
// return the Comments Manager constructor
51+
return CommentsManager;
52+
}
53+
54+
angular.module('liveblog-embed')
55+
.factory('CommentsManager', CommentsManagerFactory)
56+
.directive('lbComments', ['CommentsManager', '$timeout', function(CommentsManager, $timeout) {
57+
var commentsManager = new CommentsManager();
58+
return {
59+
scope: {
60+
comment: '='
61+
},
62+
template: [
63+
'<div class="modal-backdrop ng-cloak" ng-show="modal"></div>',
64+
'<div class="modal ng-cloak" ng-show="modal">',
65+
' <div class="modal-dialog">',
66+
' <div ng-show="notify" class="notify">',
67+
' <div class="content">',
68+
' <div class="modal-header">',
69+
' <h3>Your comment was sent for approval.</h3>',
70+
' </div>',
71+
' </div>',
72+
' </div>',
73+
' <div ng-show="form">',
74+
' <form name="comment" novalidate ng-submit="send();">',
75+
' <div class="content">',
76+
' <div class="modal-header">',
77+
' <h2>Post a comment</h2>',
78+
' </div>',
79+
' <div class="modal-body">',
80+
' <fieldset>',
81+
' <div class="field">',
82+
' <label for="commenter">Name *</label>',
83+
' <input name="commenter" ng-model="commenter">',
84+
' <div role="alert">',
85+
' <span class="error" ng-show="commenter.length < 3">Please fill in your Name.</span>',
86+
' </div>',
87+
' </div>',
88+
' <div class="field">',
89+
' <label for="content">Comment *</label>',
90+
' <textarea name="content" ng-model="content"></textarea>',
91+
' <div role="alert">',
92+
' <span class="error" ng-show="content.length < 3">Please fill in your Comment.</span>',
93+
' </div>',
94+
' </div>',
95+
' </fieldset>',
96+
' </div>',
97+
' <div class="modal-footer">',
98+
' <button class="btn" ng-click="comment=false"><span>Cancel</span></button>',
99+
' <button type="submit" class="btn btn-primary"><span>Send</span></button>',
100+
' </div>',
101+
' </div>',
102+
' </form>',
103+
' </div>',
104+
' </div>',
105+
'</div>'
106+
].join(''),
107+
controller: function($scope) {
108+
var vm = $scope;
109+
angular.extend(vm, {
110+
modal: true,
111+
notify: false,
112+
form: true,
113+
reset: function() {
114+
if (!vm.form) {
115+
vm.commenter = undefined;
116+
vm.content = undefined;
117+
};
118+
},
119+
toggle: function(status) {
120+
if(vm.notify) {
121+
vm.notify = false;
122+
vm.form = false;
123+
vm.modal = false;
124+
vm.reset();
125+
} else {
126+
vm.modal = !vm.modal;
127+
vm.form = !vm.form;
128+
vm.reset();
129+
}
130+
},
131+
send: function() {
132+
if( !vm.commenter || vm.commenter.length < 3 || !vm.content || vm.content.length <3) {
133+
vm.commenter = (vm.commenter === undefined)? '' : vm.commenter;
134+
vm.content = (vm.content === undefined)? '' : vm.content;
135+
return false;
136+
}
137+
vm.notify = 'sended';
138+
vm.form = false;
139+
commentsManager.send({
140+
commenter: vm.commenter,
141+
text: vm.content
142+
}).then(function(){
143+
vm.reset();
144+
$timeout(function(){
145+
if(vm.notify) {
146+
vm.notify = false;
147+
vm.form = true;
148+
vm.comment = false;
149+
}
150+
}, 2500);
151+
});
152+
}
153+
});
154+
},
155+
link: function(scope, elem, attrs) {
156+
scope.comment = false;
157+
scope.$watch('comment', scope.toggle);
158+
}
159+
};
160+
}]);
161+
162+
})(angular);

liveblog-embed/filters.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
return match;
1616
});
1717
};
18-
});
19-
18+
})
19+
.filter('convertLinksWithRelativeProtocol', ['fixProtocol', function (fixProtocol) {
20+
return fixProtocol;
21+
}]);
2022
})(angular);

liveblog-embed/resources.service.js

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55
recycleFreq: 3600000, // 1h
66
storageMode: 'localStorage'
77
};
8-
Blogs.$inject = ['$resource', 'config', 'CacheFactory'];
9-
function Blogs($resource, config, CacheFactory) {
10-
if (!CacheFactory.get('blogsCache')) {
11-
CacheFactory.createCache('blogsCache', CACHE_OPTIONS);
8+
9+
transformBlog.$inject = ['fixProtocol']
10+
function transformBlog(fixProtocol) {
11+
return function(blog) {
12+
if (blog.picture_url) {
13+
var srcset = '';
14+
angular.forEach(blog.picture.renditions, function(value) {
15+
srcset += ', ' + fixProtocol(value.href) + ' ' + value.width + 'w';
16+
});
17+
blog.picture_srcset = srcset.substring(2);
18+
blog.picture_url = fixProtocol(blog.picture_url);
19+
}
20+
return blog;
1221
}
13-
return $resource(config.api_host + 'api/client_blogs/:blogId', {blogId: config.blog._id},{
22+
}
23+
24+
Blogs.$inject = ['$resource', 'config', 'transformBlog'];
25+
function Blogs($resource, config, transformBlog) {
26+
return $resource(config.api_host + 'api/client_blogs/:blogId?embedded={"picture":1}', {blogId: config.blog._id},{
1427
'get': {
1528
method:'GET',
16-
cache: CacheFactory.get('blogsCache'),
1729
transformResponse: function(blog) {
1830
blog = angular.fromJson(blog);
19-
var srcset = '';
20-
angular.forEach(blog.picture.renditions, function(value) {
21-
srcset += ', ' + value.href + ' ' + value.width + 'w';
22-
});
23-
blog.picture_srcset = srcset.substring(2);
24-
return blog;
31+
return transformBlog(blog);
2532
}
2633
}
2734
});
@@ -40,30 +47,71 @@
4047

4148
Posts.$inject = ['$resource', 'config', 'users'];
4249
function Posts($resource, config, users) {
50+
function _completeUser(obj) {
51+
if (obj.commenter) {
52+
obj.original_creator = {display_name: obj.commenter};
53+
} else if(obj.original_creator !== "" && obj.original_creator !== 'None'){
54+
users.get({userId: obj.original_creator}, function(user) {
55+
obj.original_creator = user._items? user._items[0] : user;
56+
});
57+
}
58+
return obj;
59+
}
4360
return $resource(config.api_host + 'api/client_blogs/:blogId/posts', {blogId: config.blog._id}, {
4461
get: {
4562
transformResponse: function(posts) {
4663
// decode json
4764
posts = angular.fromJson(posts);
4865
posts._items.forEach(function(post) {
66+
post.mainItem = _completeUser(post.groups[1].refs[0].item);
67+
// if an item has a commenter then that post hasComments.
68+
post.hasComments = _.reduce(post.groups[1].refs, function(is, val) {
69+
return is || _.isUndefined(val.item.commenter);
70+
}, false);
71+
// `fullDetails` is a business logic that can be compiled from other objects.
72+
post.fullDetails = post.hasComments;
73+
// special cases for comments.
74+
post.showUpdate = (post._updated !== post.published_date) &&
75+
!post.hasComments && (post.mainItem.item_type !== 'comment');
76+
4977
// add all the items directly in a `items` property
5078
if (angular.isDefined(post.groups[1])) {
51-
post.items = post.groups[1].refs.map(function(item) {return item.item;});
79+
post.items = post.groups[1].refs.map(function(value) {
80+
var item = value.item;
81+
if(post.fullDetails) {
82+
_completeUser(item);
83+
item.displayDate = (item.meta && item.meta._created) || item._created;
84+
} else {
85+
item.displayDate = post.published_date;
86+
}
87+
return item;
88+
});
5289
}
5390
// replace the creator id by the user object
54-
users.get({userId: post.original_creator}, function(user) {
55-
post.original_creator = user;
56-
});
91+
_completeUser(post);
5792
});
5893
return posts;
5994
}
6095
}
6196
});
6297
}
6398

99+
Comments.$inject = ['$resource', 'config'];
100+
function Comments($resource, config) {
101+
return $resource(config.api_host + 'api/client_comments/');
102+
}
103+
104+
Items.$inject = ['$resource', 'config'];
105+
function Items($resource, config) {
106+
return $resource(config.api_host + 'api/client_items/');
107+
}
108+
64109
angular.module('liveblog-embed')
65110
.service('users', Users)
66111
.service('posts', Posts)
67-
.service('blogs', Blogs);
112+
.service('blogs', Blogs)
113+
.service('comments', Comments)
114+
.service('items', Items)
115+
.factory('transformBlog',transformBlog);
68116

69117
})(angular);

liveblog-embed/services.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(function(angular) {
2+
'use strict';
3+
4+
angular.module('liveblog-embed')
5+
.factory('fixProtocol', ['config', function(config){
6+
return function(text) {
7+
var absoluteProtocol = RegExp(/http(s)?:\/\//ig);
8+
var serverpath = config.api_host.split('//').pop();
9+
config.api_host.replace(absoluteProtocol, '//');
10+
text.replace(absoluteProtocol, '//')
11+
return text.replace(absoluteProtocol, '//')
12+
};
13+
}]);
14+
})(angular);

theme.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"label": "Angular Liveblog Toolkit",
33
"name": "angular",
44
"abstract": true,
5-
"version": "1.4.0",
5+
"version": "1.4.4",
66
"author": "Edouard Richard <edouard.richard@sourcefabric.org>",
77
"repository": {
88
"type" : "git",
@@ -19,8 +19,10 @@
1919
"vendors/iframeResizer.contentWindow.min.js",
2020
"liveblog-embed/main.js",
2121
"liveblog-embed/resources.service.js",
22+
"liveblog-embed/comments.service.js",
2223
"liveblog-embed/pages-manager.service.js",
2324
"liveblog-embed/permalink.service.js",
25+
"liveblog-embed/services.js",
2426
"liveblog-embed/directives.js",
2527
"liveblog-embed/filters.js"
2628
],

0 commit comments

Comments
 (0)