2017-01-24 01:48:35 +01:00
|
|
|
var hotspots = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2017-08-01 00:36:09 +02:00
|
|
|
// popover orientations
|
|
|
|
var TOP = 'top';
|
|
|
|
var LEFT = 'left';
|
|
|
|
var RIGHT = 'right';
|
|
|
|
var BOTTOM = 'bottom';
|
|
|
|
var VIEWPORT_CENTER = 'viewport_center';
|
|
|
|
|
|
|
|
// popover orientation can optionally be fixed here (property: popover),
|
|
|
|
// otherwise popovers.compute_placement is used to compute orientation
|
2017-07-14 03:21:30 +02:00
|
|
|
var HOTSPOT_LOCATIONS = {
|
2017-08-30 02:13:04 +02:00
|
|
|
intro_reply: {
|
2017-07-14 03:21:30 +02:00
|
|
|
element: '.selected_message .messagebox-content',
|
2017-08-30 02:13:04 +02:00
|
|
|
offset_x: 0.85,
|
|
|
|
offset_y: 0.7,
|
|
|
|
popover: BOTTOM,
|
2017-07-14 03:21:30 +02:00
|
|
|
},
|
2017-08-30 02:19:55 +02:00
|
|
|
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,
|
|
|
|
},
|
2017-08-30 02:15:32 +02:00
|
|
|
intro_compose: {
|
2017-07-14 03:21:30 +02:00
|
|
|
element: '#left_bar_compose_stream_button_big',
|
2017-08-29 23:07:19 +02:00
|
|
|
offset_x: 0,
|
|
|
|
offset_y: 0,
|
2017-07-14 03:21:30 +02:00
|
|
|
},
|
2017-01-24 01:48:35 +01:00
|
|
|
};
|
|
|
|
|
2017-08-02 02:25:52 +02:00
|
|
|
// popover illustration url(s)
|
|
|
|
var WHALE = '/static/images/hotspots/whale.svg';
|
|
|
|
|
2017-07-14 03:21:30 +02:00
|
|
|
exports.post_hotspot_as_read = function (hotspot_name) {
|
2017-01-24 01:48:35 +01:00
|
|
|
channel.post({
|
|
|
|
url: '/json/users/me/hotspots',
|
2017-07-14 03:21:30 +02:00
|
|
|
data: { hotspot: JSON.stringify(hotspot_name) },
|
|
|
|
error: function (err) {
|
|
|
|
blueslip.error(err.responseText);
|
|
|
|
},
|
2017-01-24 01:48:35 +01:00
|
|
|
});
|
2017-07-14 03:21:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function place_icon(hotspot) {
|
2017-08-31 17:58:48 +02:00
|
|
|
var element = $(hotspot.location.element);
|
|
|
|
var icon = $('#hotspot_' + hotspot.name + '_icon');
|
2017-08-31 18:00:51 +02:00
|
|
|
|
|
|
|
if (element.length === 0 || element.css('display') === 'none' ||
|
|
|
|
!element.is(':visible') || element.is(':hidden')) {
|
2017-08-31 17:58:48 +02:00
|
|
|
icon.css('display', 'none');
|
2017-08-31 04:35:32 +02:00
|
|
|
return false;
|
2017-07-14 03:21:30 +02:00
|
|
|
}
|
|
|
|
|
2017-08-29 23:07:19 +02:00
|
|
|
var offset = {
|
2017-08-31 17:58:48 +02:00
|
|
|
top: element.outerHeight() * hotspot.location.offset_y,
|
|
|
|
left: element.outerWidth() * hotspot.location.offset_x,
|
2017-08-29 23:07:19 +02:00
|
|
|
};
|
2017-08-31 17:58:48 +02:00
|
|
|
var client_rect = element.get(0).getBoundingClientRect();
|
2017-07-14 03:21:30 +02:00
|
|
|
var placement = {
|
|
|
|
top: client_rect.top + offset.top,
|
|
|
|
left: client_rect.left + offset.left,
|
|
|
|
};
|
2017-08-31 17:58:48 +02:00
|
|
|
icon.css('display', 'block');
|
|
|
|
icon.css(placement);
|
2017-08-31 04:35:32 +02:00
|
|
|
return true;
|
2017-01-24 01:48:35 +01:00
|
|
|
}
|
|
|
|
|
2017-07-14 03:21:30 +02:00
|
|
|
function place_popover(hotspot) {
|
|
|
|
if (!hotspot.location.element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var popover_width = $('#hotspot_' + hotspot.name + '_overlay .hotspot-popover').outerWidth();
|
|
|
|
var popover_height = $('#hotspot_' + hotspot.name + '_overlay .hotspot-popover').outerHeight();
|
|
|
|
var el_width = $(hotspot.location.element).outerWidth();
|
|
|
|
var el_height = $(hotspot.location.element).outerHeight();
|
|
|
|
var arrow_offset = 20;
|
|
|
|
|
|
|
|
var popover_offset;
|
|
|
|
var arrow_placement;
|
2017-08-01 00:36:09 +02:00
|
|
|
var orientation = hotspot.location.popover ||
|
2017-08-25 19:59:23 +02:00
|
|
|
popovers.compute_placement(
|
|
|
|
$(hotspot.location.element),
|
|
|
|
popover_height,
|
|
|
|
popover_width,
|
|
|
|
false
|
|
|
|
);
|
2017-08-01 00:36:09 +02:00
|
|
|
|
|
|
|
switch (orientation) {
|
|
|
|
case TOP:
|
2017-07-14 03:21:30 +02:00
|
|
|
popover_offset = {
|
|
|
|
top: -(popover_height + arrow_offset),
|
|
|
|
left: (el_width / 2) - (popover_width / 2),
|
|
|
|
};
|
|
|
|
arrow_placement = 'bottom';
|
|
|
|
break;
|
|
|
|
|
2017-08-01 00:36:09 +02:00
|
|
|
case LEFT:
|
2017-07-14 03:21:30 +02:00
|
|
|
popover_offset = {
|
|
|
|
top: (el_height / 2) - (popover_height / 2),
|
|
|
|
left: -(popover_width + arrow_offset),
|
|
|
|
};
|
|
|
|
arrow_placement = 'right';
|
|
|
|
break;
|
|
|
|
|
2017-08-01 00:36:09 +02:00
|
|
|
case BOTTOM:
|
2017-07-14 03:21:30 +02:00
|
|
|
popover_offset = {
|
|
|
|
top: el_height + arrow_offset,
|
|
|
|
left: (el_width / 2) - (popover_width / 2),
|
|
|
|
};
|
|
|
|
arrow_placement = 'top';
|
|
|
|
break;
|
|
|
|
|
2017-08-01 00:36:09 +02:00
|
|
|
case RIGHT:
|
2017-07-14 03:21:30 +02:00
|
|
|
popover_offset = {
|
|
|
|
top: (el_height / 2) - (popover_height / 2),
|
|
|
|
left: el_width + arrow_offset,
|
|
|
|
};
|
|
|
|
arrow_placement = 'left';
|
|
|
|
break;
|
|
|
|
|
2017-08-01 00:36:09 +02:00
|
|
|
case VIEWPORT_CENTER:
|
|
|
|
popover_offset = {
|
|
|
|
top: el_height / 2,
|
|
|
|
left: el_width / 2,
|
|
|
|
};
|
|
|
|
arrow_placement = '';
|
|
|
|
break;
|
|
|
|
|
2017-07-14 03:21:30 +02:00
|
|
|
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
|
2017-08-01 00:36:09 +02:00
|
|
|
var popover_placement;
|
|
|
|
if (orientation === VIEWPORT_CENTER) {
|
|
|
|
popover_placement = {
|
|
|
|
top: '45%',
|
|
|
|
left: '50%',
|
|
|
|
transform: 'translate(-50%, -50%)',
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
var 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: '',
|
|
|
|
};
|
|
|
|
}
|
2017-07-14 03:21:30 +02:00
|
|
|
|
|
|
|
$('#hotspot_' + hotspot.name + '_overlay .hotspot-popover')
|
|
|
|
.css(popover_placement);
|
|
|
|
}
|
|
|
|
|
|
|
|
function insert_hotspot_into_DOM(hotspot) {
|
|
|
|
var hotspot_overlay_HTML = templates.render('hotspot_overlay', {
|
|
|
|
name: hotspot.name,
|
|
|
|
title: hotspot.title,
|
|
|
|
description: hotspot.description,
|
2017-08-02 02:25:52 +02:00
|
|
|
img: WHALE,
|
2017-01-24 01:48:35 +01:00
|
|
|
});
|
2017-07-14 03:21:30 +02:00
|
|
|
|
|
|
|
var hotspot_icon_HTML =
|
|
|
|
'<div class="hotspot-icon" id="hotspot_' + hotspot.name + '_icon">' +
|
|
|
|
'<span class="dot"></span>' +
|
|
|
|
'<span class="pulse"></span>' +
|
2018-02-28 23:22:02 +01:00
|
|
|
'<div class="bounce"><span class="bounce-icon">?</span></div>' +
|
2017-07-14 03:21:30 +02:00
|
|
|
'</div>';
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
$('body').prepend(hotspot_icon_HTML);
|
|
|
|
$('body').prepend(hotspot_overlay_HTML);
|
2017-08-31 04:35:32 +02:00
|
|
|
if (place_icon(hotspot)) {
|
|
|
|
place_popover(hotspot);
|
|
|
|
}
|
2017-07-14 03:21:30 +02:00
|
|
|
|
|
|
|
// reposition on any event that might update the UI
|
|
|
|
['resize', 'scroll', 'onkeydown', 'click']
|
|
|
|
.forEach(function (event_name) {
|
2017-08-02 03:01:17 +02:00
|
|
|
window.addEventListener(event_name, _.debounce(function () {
|
2017-08-31 04:35:32 +02:00
|
|
|
if (place_icon(hotspot)) {
|
|
|
|
place_popover(hotspot);
|
|
|
|
}
|
2017-08-02 03:01:17 +02:00
|
|
|
}, 10), true);
|
2017-07-14 03:21:30 +02:00
|
|
|
});
|
2017-08-31 05:13:37 +02:00
|
|
|
}, (hotspot.delay * 1000));
|
2017-07-14 03:21:30 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 04:46:56 +02:00
|
|
|
exports.is_open = function () {
|
|
|
|
return $('.hotspot.overlay').hasClass('show');
|
|
|
|
};
|
|
|
|
|
2018-03-17 19:35:19 +01:00
|
|
|
exports.close_hotspot_icon = function (elem) {
|
|
|
|
$(elem).animate({ opacity: 0 }, {
|
|
|
|
duration: 300,
|
|
|
|
done: function () {
|
|
|
|
$(elem).css({ display: 'none' });
|
|
|
|
}.bind(elem),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-17 19:37:40 +01:00
|
|
|
function close_read_hotspots(new_hotspots) {
|
|
|
|
var unwanted_hotspots = _.difference(_.keys(HOTSPOT_LOCATIONS), _.pluck(new_hotspots, 'name'));
|
|
|
|
_.each(unwanted_hotspots, function (hotspot_name) {
|
|
|
|
exports.close_hotspot_icon($('#hotspot_' + hotspot_name + '_icon'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-14 03:21:30 +02:00
|
|
|
exports.load_new = function (new_hotspots) {
|
2018-03-17 19:37:40 +01:00
|
|
|
close_read_hotspots(new_hotspots);
|
2017-08-31 04:46:10 +02:00
|
|
|
new_hotspots.forEach(function (hotspot) {
|
|
|
|
hotspot.location = HOTSPOT_LOCATIONS[hotspot.name];
|
|
|
|
});
|
2017-07-14 03:21:30 +02:00
|
|
|
new_hotspots.forEach(insert_hotspot_into_DOM);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.initialize = function () {
|
|
|
|
exports.load_new(page_params.hotspots);
|
|
|
|
};
|
2017-01-24 01:48:35 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = hotspots;
|
|
|
|
}
|