This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialize-discourse-group-timezones.js.es6
95 lines (81 loc) · 2.64 KB
/
initialize-discourse-group-timezones.js.es6
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
86
87
88
89
90
91
92
93
94
95
import WidgetGlue from "discourse/widgets/glue";
import { getRegister } from "discourse-common/lib/get-owner";
import { withPluginApi } from "discourse/lib/plugin-api";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
export default {
name: "discourse-group-timezones",
initialize() {
withPluginApi("0.8.7", api => {
let _glued = [];
function cleanUp() {
_glued.forEach(g => g.cleanUp());
_glued = [];
}
function _attachWidget(api, container, options) {
const glue = new WidgetGlue(
"discourse-group-timezones",
getRegister(api),
options
);
glue.appendTo(container);
_glued.push(glue);
}
function _loadGroupMembers(group) {
return ajax(`/groups/${group}/members.json?limit=50`, {
type: "GET",
cache: false
})
.then(groupResult => {
if (groupResult && groupResult.members) {
return groupResult.members;
}
})
.catch(popupAjaxError);
}
function _attachGroupTimezones($elem, id = 1) {
const $groupTimezones = $(".d-wrap[data-wrap=group-timezones]", $elem);
if (!$groupTimezones.length) {
return;
}
$groupTimezones.each((idx, groupTimezone) => {
const group = groupTimezone.getAttribute("data-group");
if (!group) {
throw "[group] attribute is necessary when using group-timezones.";
}
groupTimezone.innerHTML = "<div class='spinner'></div>";
_loadGroupMembers(group).then(members => {
_attachWidget(api, groupTimezone, {
id: `${id}-${idx}`,
members,
group,
usersOnHoliday:
api.container.lookup("site:main").users_on_holiday || [],
size: groupTimezone.getAttribute("data-size") || "medium"
});
});
});
}
function _attachPostWithGroupTimezones($elem, helper) {
if (helper) {
const post = helper.getModel();
api.preventCloak(post.id);
_attachGroupTimezones($elem, post.id);
}
}
api.decorateCooked(_attachPostWithGroupTimezones, {
id: "discourse-group-timezones"
});
api.onPageChange(url => {
const match = url.match(/\/g\/(\w+)/);
if (match && match.length && match[1]) {
const $elem = $(".group-bio");
if ($elem.length) {
_attachGroupTimezones($elem);
}
}
});
api.cleanupStream(cleanUp);
});
}
};