2020-06-15 11:08:20 -04:00
import $ from 'jquery' ;
import { parseBoolean , getCookie , setCookie , removeCookie } from '~/lib/utils/common_utils' ;
import { _ _ } from '~/locale' ;
2020-06-16 08:09:00 -04:00
import Tracking from '~/tracking' ;
2020-06-15 11:08:20 -04:00
const COOKIE _NAME = 'onboarding_issues_settings' ;
const POPOVER _LOCATIONS = {
GROUPS _SHOW : 'groups#show' ,
PROJECTS _SHOW : 'projects#show' ,
ISSUES _INDEX : 'issues#index' ,
} ;
const removeLearnGitLabCookie = ( ) => {
removeCookie ( COOKIE _NAME ) ;
} ;
function disposePopover ( event ) {
event . preventDefault ( ) ;
this . popover ( 'dispose' ) ;
removeLearnGitLabCookie ( ) ;
2020-06-16 08:09:00 -04:00
Tracking . event ( 'Growth::Conversion::Experiment::OnboardingIssues' , 'dismiss_popover' ) ;
2020-06-15 11:08:20 -04:00
}
const showPopover = ( el , path , footer , options ) => {
// Cookie value looks like `{ 'groups#show': true, 'projects#show': true, 'issues#index': true }`. When it doesn't exist, don't show the popover.
const cookie = getCookie ( COOKIE _NAME ) ;
if ( ! cookie ) return ;
// When the popover action has already been taken, don't show the popover.
const settings = JSON . parse ( cookie ) ;
if ( ! parseBoolean ( settings [ path ] ) ) return ;
const defaultOptions = {
boundary : 'window' ,
html : true ,
placement : 'top' ,
template : ` <div class="popover blue learn-gitlab d-none d-xl-block" role="tooltip">
< div class = "arrow" > < / d i v >
< div class = "close cursor-pointer gl-font-base text-white gl-opacity-10 p-2" > & # 10005 < / d i v >
< div class = "popover-body gl-font-base gl-line-height-20 pb-0 px-3" > < / d i v >
< div class = "bold text-right text-white p-2" > $ { footer } < / d i v >
< / d i v > ` ,
} ;
// When one of the popovers is dismissed, remove the cookie.
const closeButton = ( ) => document . querySelector ( '.learn-gitlab.popover .close' ) ;
// We still have to use jQuery, since Bootstrap's Popover is based on jQuery.
const jQueryEl = $ ( el ) ;
const clickCloseButton = disposePopover . bind ( jQueryEl ) ;
jQueryEl
. popover ( { ... defaultOptions , ... options } )
. on ( 'inserted.bs.popover' , ( ) => closeButton ( ) . addEventListener ( 'click' , clickCloseButton ) )
. on ( 'hide.bs.dropdown' , ( ) => closeButton ( ) . removeEventListener ( 'click' , clickCloseButton ) )
. popover ( 'show' ) ;
// The previous popover actions have been taken, don't show those popovers anymore.
Object . keys ( settings ) . forEach ( pathSetting => {
if ( path !== pathSetting ) {
settings [ pathSetting ] = false ;
} else {
setCookie ( COOKIE _NAME , settings ) ;
}
} ) ;
// The final popover action will be taken on click, we then no longer need the cookie.
if ( path === POPOVER _LOCATIONS . ISSUES _INDEX ) {
el . addEventListener ( 'click' , removeLearnGitLabCookie ) ;
}
} ;
export const showLearnGitLabGroupItemPopover = id => {
const el = document . querySelector ( ` #group- ${ id } .group-text a ` ) ;
if ( ! el ) return ;
const options = {
content : _ _ (
'Here are all your projects in your group, including the one you just created. To start, let’ s take a look at your personalized learning project which will help you learn about GitLab at your own pace.' ,
) ,
} ;
showPopover ( el , POPOVER _LOCATIONS . GROUPS _SHOW , '1 / 2' , options ) ;
} ;
export const showLearnGitLabProjectPopover = ( ) => {
// Do not show a popover if we are not viewing the 'Learn GitLab' project.
if ( ! window . location . pathname . includes ( 'learn-gitlab' ) ) return ;
const el = document . querySelector ( 'a.shortcuts-issues' ) ;
if ( ! el ) return ;
const options = {
content : _ _ (
'Go to <strong>Issues</strong> > <strong>Boards</strong> to access your personalized learning issue board.' ,
) ,
} ;
showPopover ( el , POPOVER _LOCATIONS . PROJECTS _SHOW , '2 / 2' , options ) ;
} ;
export const showLearnGitLabIssuesPopover = ( ) => {
// Do not show a popover if we are not viewing the 'Learn GitLab' project.
if ( ! window . location . pathname . includes ( 'learn-gitlab' ) ) return ;
const el = document . querySelector ( 'a[data-qa-selector="issue_boards_link"]' ) ;
if ( ! el ) return ;
const options = {
content : _ _ (
'Go to <strong>Issues</strong> > <strong>Boards</strong> to access your personalized learning issue board.' ,
) ,
} ;
showPopover ( el , POPOVER _LOCATIONS . ISSUES _INDEX , '2 / 2' , options ) ;
} ;