forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotspots.js
291 lines (257 loc) · 7.86 KB
/
hotspots.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"use strict";
const _ = require("lodash");
const render_hotspot_overlay = require("../templates/hotspot_overlay.hbs");
const render_intro_reply_hotspot = require("../templates/intro_reply_hotspot.hbs");
// popover orientations
const TOP = "top";
const LEFT = "left";
const RIGHT = "right";
const BOTTOM = "bottom";
const LEFT_BOTTOM = "left_bottom";
const VIEWPORT_CENTER = "viewport_center";
// popover orientation can optionally be fixed here (property: popover),
// otherwise popovers.compute_placement is used to compute orientation
const HOTSPOT_LOCATIONS = new Map([
[
"intro_reply",
{
element: ".selected_message .messagebox-content",
offset_x: 0.85,
offset_y: 0.7,
popover: BOTTOM,
},
],
[
"intro_streams",
{
element: "#streams_header .sidebar-title",
offset_x: 1.35,
offset_y: 0.39,
},
],
[
"intro_topics",
{
element: ".topic-name",
offset_x: 0.8,
offset_y: 0.39,
},
],
[
"intro_gear",
{
element: "#settings-dropdown",
offset_x: -0.4,
offset_y: 1.2,
popover: LEFT_BOTTOM,
},
],
[
"intro_compose",
{
element: "#left_bar_compose_stream_button_big",
offset_x: 0,
offset_y: 0,
},
],
]);
// popover illustration url(s)
const WHALE = "/static/images/hotspots/whale.svg";
exports.post_hotspot_as_read = function (hotspot_name) {
channel.post({
url: "/json/users/me/hotspots",
data: {hotspot: JSON.stringify(hotspot_name)},
error(err) {
blueslip.error(err.responseText);
},
});
};
function place_icon(hotspot) {
const element = $(hotspot.location.element);
const icon = $("#hotspot_" + hotspot.name + "_icon");
if (
element.length === 0 ||
element.css("display") === "none" ||
!element.is(":visible") ||
element.is(":hidden")
) {
icon.css("display", "none");
return false;
}
const offset = {
top: element.outerHeight() * hotspot.location.offset_y,
left: element.outerWidth() * hotspot.location.offset_x,
};
const client_rect = element.get(0).getBoundingClientRect();
const placement = {
top: client_rect.top + offset.top,
left: client_rect.left + offset.left,
};
icon.css("display", "block");
icon.css(placement);
return true;
}
function place_popover(hotspot) {
if (!hotspot.location.element) {
return;
}
const popover_width = $("#hotspot_" + hotspot.name + "_overlay .hotspot-popover").outerWidth();
const popover_height = $(
"#hotspot_" + hotspot.name + "_overlay .hotspot-popover",
).outerHeight();
const el_width = $(hotspot.location.element).outerWidth();
const el_height = $(hotspot.location.element).outerHeight();
const arrow_offset = 20;
let popover_offset;
let arrow_placement;
const orientation =
hotspot.location.popover ||
popovers.compute_placement(
$(hotspot.location.element),
popover_height,
popover_width,
false,
);
switch (orientation) {
case TOP:
popover_offset = {
top: -(popover_height + arrow_offset),
left: el_width / 2 - popover_width / 2,
};
arrow_placement = "bottom";
break;
case LEFT:
popover_offset = {
top: el_height / 2 - popover_height / 2,
left: -(popover_width + arrow_offset),
};
arrow_placement = "right";
break;
case BOTTOM:
popover_offset = {
top: el_height + arrow_offset,
left: el_width / 2 - popover_width / 2,
};
arrow_placement = "top";
break;
case RIGHT:
popover_offset = {
top: el_height / 2 - popover_height / 2,
left: el_width + arrow_offset,
};
arrow_placement = "left";
break;
case LEFT_BOTTOM:
popover_offset = {
top: 0,
left: -(popover_width + arrow_offset / 2),
};
arrow_placement = "";
break;
case VIEWPORT_CENTER:
popover_offset = {
top: el_height / 2,
left: el_width / 2,
};
arrow_placement = "";
break;
default:
blueslip.error("Invalid popover placement value for hotspot '" + hotspot.name + "'");
break;
}
// position arrow
arrow_placement = "arrow-" + arrow_placement;
$("#hotspot_" + hotspot.name + "_overlay .hotspot-popover")
.removeClass("arrow-top arrow-left arrow-bottom arrow-right")
.addClass(arrow_placement);
// position popover
let popover_placement;
if (orientation === VIEWPORT_CENTER) {
popover_placement = {
top: "45%",
left: "50%",
transform: "translate(-50%, -50%)",
};
} else {
const client_rect = $(hotspot.location.element).get(0).getBoundingClientRect();
popover_placement = {
top: client_rect.top + popover_offset.top,
left: client_rect.left + popover_offset.left,
transform: "",
};
}
$("#hotspot_" + hotspot.name + "_overlay .hotspot-popover").css(popover_placement);
}
function insert_hotspot_into_DOM(hotspot) {
if (hotspot.name === "intro_reply") {
$("#bottom_whitespace").append(render_intro_reply_hotspot({}));
return;
}
const hotspot_overlay_HTML = render_hotspot_overlay({
name: hotspot.name,
title: hotspot.title,
description: hotspot.description,
img: WHALE,
});
const hotspot_icon_HTML =
'<div class="hotspot-icon" id="hotspot_' +
hotspot.name +
'_icon">' +
'<span class="dot"></span>' +
'<span class="pulse"></span>' +
'<div class="bounce"><span class="bounce-icon">?</span></div>' +
"</div>";
setTimeout(() => {
$("body").prepend(hotspot_icon_HTML);
$("body").prepend(hotspot_overlay_HTML);
if (place_icon(hotspot)) {
place_popover(hotspot);
}
// reposition on any event that might update the UI
["resize", "scroll", "onkeydown", "click"].forEach((event_name) => {
window.addEventListener(
event_name,
_.debounce(() => {
if (place_icon(hotspot)) {
place_popover(hotspot);
}
}, 10),
true,
);
});
}, hotspot.delay * 1000);
}
exports.is_open = function () {
return $(".hotspot.overlay").hasClass("show");
};
exports.close_hotspot_icon = function (elem) {
$(elem).animate(
{opacity: 0},
{
duration: 300,
done: function () {
$(elem).css({display: "none"});
}.bind(elem),
},
);
};
function close_read_hotspots(new_hotspots) {
const unwanted_hotspots = _.difference(
Array.from(HOTSPOT_LOCATIONS.keys()),
new_hotspots.map((hotspot) => hotspot.name),
);
for (const hotspot_name of unwanted_hotspots) {
exports.close_hotspot_icon($("#hotspot_" + hotspot_name + "_icon"));
}
}
exports.load_new = function (new_hotspots) {
close_read_hotspots(new_hotspots);
new_hotspots.forEach((hotspot) => {
hotspot.location = HOTSPOT_LOCATIONS.get(hotspot.name);
});
new_hotspots.forEach(insert_hotspot_into_DOM);
};
exports.initialize = function () {
exports.load_new(page_params.hotspots);
};
window.hotspots = exports;