2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-10-10 02:18:49 -04:00
|
|
|
import { getSelector, inserted } from './feature_highlight_helper';
|
|
|
|
import { togglePopover, mouseenter, debouncedMouseleave } from '../shared/popover';
|
2018-01-10 23:16:27 -05:00
|
|
|
|
|
|
|
export function setupFeatureHighlightPopover(id, debounceTimeout = 300) {
|
|
|
|
const $selector = $(getSelector(id));
|
|
|
|
const $parent = $selector.parent();
|
|
|
|
const $popoverContent = $parent.siblings('.feature-highlight-popover-content');
|
|
|
|
const hideOnScroll = togglePopover.bind($selector, false);
|
|
|
|
|
|
|
|
$selector
|
2018-09-17 05:17:33 -04:00
|
|
|
// Set up popover
|
2018-01-10 23:16:27 -05:00
|
|
|
.data('content', $popoverContent.prop('outerHTML'))
|
|
|
|
.popover({
|
|
|
|
html: true,
|
|
|
|
// Override the existing template to add custom CSS classes
|
|
|
|
template: `
|
|
|
|
<div class="popover feature-highlight-popover" role="tooltip">
|
|
|
|
<div class="arrow"></div>
|
2018-05-21 12:48:06 -04:00
|
|
|
<div class="popover-body"></div>
|
2018-01-10 23:16:27 -05:00
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
.on('mouseenter', mouseenter)
|
2018-03-26 13:46:42 -04:00
|
|
|
.on('mouseleave', debouncedMouseleave(debounceTimeout))
|
2018-01-10 23:16:27 -05:00
|
|
|
.on('inserted.bs.popover', inserted)
|
2018-04-13 13:01:05 -04:00
|
|
|
.on('show.bs.popover', () => {
|
|
|
|
window.addEventListener('scroll', hideOnScroll, { once: true });
|
2018-01-10 23:16:27 -05:00
|
|
|
})
|
|
|
|
// Display feature highlight
|
|
|
|
.removeAttr('disabled');
|
|
|
|
}
|
|
|
|
|
2019-01-22 10:34:12 -05:00
|
|
|
const getPriority = e => parseInt(e.dataset.highlightPriority, 10) || 0;
|
|
|
|
|
2018-01-10 23:16:27 -05:00
|
|
|
export function findHighestPriorityFeature() {
|
|
|
|
let priorityFeature;
|
|
|
|
|
2018-10-10 02:18:49 -04:00
|
|
|
const sortedFeatureEls = [].slice
|
|
|
|
.call(document.querySelectorAll('.js-feature-highlight'))
|
2019-01-22 10:34:12 -05:00
|
|
|
.sort((a, b) => getPriority(b) - getPriority(a));
|
2018-01-10 23:16:27 -05:00
|
|
|
|
|
|
|
const [priorityFeatureEl] = sortedFeatureEls;
|
|
|
|
if (priorityFeatureEl) {
|
|
|
|
priorityFeature = priorityFeatureEl.dataset.highlight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return priorityFeature;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function highlightFeatures() {
|
|
|
|
const priorityFeature = findHighestPriorityFeature();
|
|
|
|
|
|
|
|
if (priorityFeature) {
|
|
|
|
setupFeatureHighlightPopover(priorityFeature);
|
|
|
|
}
|
|
|
|
|
|
|
|
return priorityFeature;
|
|
|
|
}
|