-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathdiscourse-group-timezones.js
71 lines (58 loc) · 1.86 KB
/
discourse-group-timezones.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
import WidgetGlue from "discourse/widgets/glue";
import { getRegister } from "discourse-common/lib/get-owner";
import { withPluginApi } from "discourse/lib/plugin-api";
export default {
name: "discourse-group-timezones",
initialize() {
withPluginApi("0.8.7", (api) => {
let _glued = [];
function cleanUp() {
_glued.forEach((g) => g.cleanUp());
_glued = [];
}
function _attachWidget(container, options) {
const glue = new WidgetGlue(
"discourse-group-timezones",
getRegister(api),
options
);
glue.appendTo(container);
_glued.push(glue);
}
function _attachGroupTimezones($elem, post) {
const $groupTimezones = $(".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 timezones.";
}
const members = (post.get("group_timezones") || {})[group] || [];
_attachWidget(groupTimezone, {
id: `${post.id}-${idx}`,
members,
group,
usersOnHoliday:
api.container.lookup("service:site").users_on_holiday || [],
size: groupTimezone.getAttribute("data-size") || "medium",
});
});
}
function _attachPostWithGroupTimezones($elem, helper) {
if (helper) {
const post = helper.getModel();
if (post) {
api.preventCloak(post.id);
_attachGroupTimezones($elem, post);
}
}
}
api.decorateCooked(_attachPostWithGroupTimezones, {
id: "discourse-group-timezones",
});
api.cleanupStream(cleanUp);
});
},
};