-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathPublicLab.TitleModule.Related.js
85 lines (68 loc) · 2.89 KB
/
PublicLab.TitleModule.Related.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Displays related posts to associate this one with.
* Pass this a fetchRelated() method which runs show() with returned JSON data.
* Example:
```js
function fetchRelated(show) {
$.getJSON('/some/url', function(response) {
show(response);
});
}
```
Results should be in following JSON format:
[
{ id: 1, title: 'A related post', url: '/', author: 'eustatic'},
{ id: 2, title: 'Another related post', url: '/', author: 'stevie'},
{ id: 3, title: 'A third related post', url: '/', author: 'bsugar'}
]
*/
module.exports = function relatedNodes(module) {
var relatedEl;
build();
bindEvents();
// make an area for "related posts" to connect to
function build() {
module.el.find('.ple-module-content').append('<div style="display:none;" class="ple-title-related"></div>');
relatedEl = module.el.find('.ple-title-related');
relatedEl.append('<p class="ple-help">Does your work relate to one of these? Click to alert those contributors.</p><hr style="margin: 4px 0;" />');
}
// expects array of results in format:
// { id: 3, title: 'A third related post', url: '/', author: 'bsugar'}
function show(relatedResults) {
relatedEl.find('.result').remove();
relatedResults.slice(0, 8).forEach(function(result) {
relatedEl.append('<div class="result result-' + result.id + '" style="margin: 3px;"><a class="btn btn-xs btn-outline-secondary add-tag"><i class="fa fa-plus-circle"></i> Add</a> <a class="title"></a> by <a class="author"></a></div>');
relatedEl.find('.result-' + result.id + ' .title').html(result.title);
relatedEl.find('.result-' + result.id + ' .title').attr('href', result.url);
relatedEl.find('.result-' + result.id + ' .author').html('@' + result.author);
relatedEl.find('.result-' + result.id + ' .author').attr('href', '/profile/' + result.author);
$('.result-' + result.id + ' .add-tag').click(function() {
editor.tagsModule.el.find('input').tokenfield('createToken', 'response:' + result.id);
// pending https://github.com/publiclab/plots2/issues/646
// editor.tagsModule.el.find('input').tokenfield('createToken', 'notify:' + result.author);
$('.result-' + result.id).remove();
});
});
}
var fetchRelated = module.options.fetchRelated || function fetchRelated(show) {
// example
show([
{id: 1, title: 'A related post', url: '/', author: 'eustatic'},
{id: 2, title: 'Another related post', url: '/', author: 'stevie'},
{id: 3, title: 'A third related post', url: '/', author: 'bsugar'}
]);
};
function bindEvents() {
$(module.el).find('input').keydown(function(e) {
if (module.options.suggestRelated) {
relatedEl.fadeIn();
fetchRelated(show);
}
});
$(module.el).find('input').focusout(function(e) {
if (module.options.suggestRelated) {
relatedEl.fadeOut();
}
});
}
return relatedEl;
};