| 1 |
/* global tour_plugin, XMLHttpRequest */ |
| 2 |
/* eslint camelcase: "off" */ |
| 3 |
|
| 4 |
let tourSelectorActive = false; |
| 5 |
let tourId; |
| 6 |
let dialogOpen = false; |
| 7 |
|
| 8 |
const setTourCookie = function ( id ) { |
| 9 |
document.cookie = 'tour=' + escape( id ) + ';path=/'; |
| 10 |
enableTourCreation(); |
| 11 |
}; |
| 12 |
|
| 13 |
const deleteTourCookie = function () { |
| 14 |
document.cookie = 'tour=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; |
| 15 |
enableTourCreation(); |
| 16 |
}; |
| 17 |
|
| 18 |
document.addEventListener( 'click', function ( event ) { |
| 19 |
if ( |
| 20 |
! event.target.dataset.addMoreStepsText || |
| 21 |
! event.target.dataset.tourId |
| 22 |
) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
event.preventDefault(); |
| 27 |
if ( |
| 28 |
event.target.textContent === event.target.dataset.finishTourCreationText |
| 29 |
) { |
| 30 |
event.target.textContent = event.target.dataset.addMoreStepsText; |
| 31 |
deleteTourCookie(); |
| 32 |
return; |
| 33 |
} |
| 34 |
setTourCookie( event.target.dataset.tourId ); |
| 35 |
event.target.textContent = event.target.dataset.finishTourCreationText; |
| 36 |
} ); |
| 37 |
|
| 38 |
function enableTourCreation() { |
| 39 |
tourId = |
| 40 |
document.cookie.indexOf( 'tour=' ) > -1 |
| 41 |
? unescape( |
| 42 |
document.cookie.split( 'tour=' )[ 1 ].split( ';' )[ 0 ] |
| 43 |
) |
| 44 |
: ''; |
| 45 |
if ( tourId && document.getElementById( 'tour-launcher' ) ) { |
| 46 |
if ( |
| 47 |
typeof tour_plugin !== 'undefined' && |
| 48 |
typeof tour_plugin.tours[ tourId ] !== 'undefined' |
| 49 |
) { |
| 50 |
document.getElementById( 'tour-launcher' ).style.display = 'block'; |
| 51 |
document.getElementById( 'tour-title' ).textContent = |
| 52 |
tour_plugin.tours[ tourId ][ 0 ].title; |
| 53 |
document.getElementById( 'tour-steps' ).textContent = |
| 54 |
tour_plugin.tours[ tourId ].length - |
| 55 |
1 + |
| 56 |
' step' + |
| 57 |
( tour_plugin.tours[ tourId ].length > 2 ? 's' : '' ); |
| 58 |
for ( let i = 1; i < tour_plugin.tours[ tourId ].length; i++ ) { |
| 59 |
const el = document.querySelector( |
| 60 |
tour_plugin.tours[ tourId ][ i ].selector |
| 61 |
); |
| 62 |
if ( el ) { |
| 63 |
el.style.outline = |
| 64 |
'1px dashed ' + tour_plugin.tours[ tourId ][ 0 ].color; |
| 65 |
} else { |
| 66 |
reportMissingSelector( |
| 67 |
tour_plugin.tours[ tourId ][ 0 ].title, |
| 68 |
i, |
| 69 |
tour_plugin.tours[ tourId ][ i ].selector |
| 70 |
); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} else if ( document.getElementById( 'tour-launcher' ) ) { |
| 75 |
document.getElementById( 'tour-launcher' ).style.display = 'none'; |
| 76 |
} |
| 77 |
} |
| 78 |
document.addEventListener( 'DOMContentLoaded', function () { |
| 79 |
document |
| 80 |
.getElementById( 'tour-launcher' ) |
| 81 |
.addEventListener( 'click', toggleTourSelector ); |
| 82 |
enableTourCreation(); |
| 83 |
} ); |
| 84 |
|
| 85 |
function reportMissingSelector( tourTitle, step, selector ) { |
| 86 |
const xhr = new XMLHttpRequest(); |
| 87 |
xhr.open( 'POST', tour_plugin.rest_url + 'tour/v1/report-missing' ); |
| 88 |
xhr.setRequestHeader( 'Content-Type', 'application/json' ); |
| 89 |
xhr.setRequestHeader( 'X-WP-Nonce', tour_plugin.nonce ); |
| 90 |
xhr.send( |
| 91 |
JSON.stringify( { |
| 92 |
tour: tourId, |
| 93 |
selector, |
| 94 |
step, |
| 95 |
url: window.location.href, |
| 96 |
} ) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
function toggleTourSelector( event ) { |
| 101 |
event.stopPropagation(); |
| 102 |
if ( event.target.tagName.toLowerCase() === 'a' ) { |
| 103 |
deleteTourCookie(); |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
tourSelectorActive = ! tourSelectorActive; |
| 108 |
|
| 109 |
document.getElementById( 'tour-launcher' ).style.color = tourSelectorActive |
| 110 |
? tour_plugin.tours[ tourId ][ 0 ].color |
| 111 |
: ''; |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
const clearHighlight = function ( event ) { |
| 116 |
if ( typeof tour_plugin.tours[ tourId ] !== 'undefined' ) { |
| 117 |
for ( let i = 1; i < tour_plugin.tours[ tourId ].length; i++ ) { |
| 118 |
if ( |
| 119 |
event.target.matches( |
| 120 |
tour_plugin.tours[ tourId ][ i ].selector |
| 121 |
) |
| 122 |
) { |
| 123 |
document.querySelector( |
| 124 |
tour_plugin.tours[ tourId ][ i ].selector |
| 125 |
).style.outline = |
| 126 |
'1px dashed ' + tour_plugin.tours[ tourId ][ 0 ].color; |
| 127 |
return; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
event.target.style.outline = ''; |
| 132 |
event.target.style.cursor = ''; |
| 133 |
}; |
| 134 |
|
| 135 |
const tourStepHighlighter = function ( event ) { |
| 136 |
const target = event.target; |
| 137 |
if ( ! tourSelectorActive || target.closest( '#tour-launcher' ) ) { |
| 138 |
clearHighlight( event ); |
| 139 |
return; |
| 140 |
} |
| 141 |
// Highlight the element on hover |
| 142 |
target.style.outline = |
| 143 |
'2px solid ' + tour_plugin.tours[ tourId ][ 0 ].color; |
| 144 |
target.style.cursor = 'pointer'; |
| 145 |
}; |
| 146 |
|
| 147 |
const filter_selectors = function ( c ) { |
| 148 |
return ( |
| 149 |
c.indexOf( 'wp-' ) > -1 || |
| 150 |
c.indexOf( 'page' ) > -1 || |
| 151 |
c.indexOf( 'post' ) > -1 || |
| 152 |
c.indexOf( 'column' ) > -1 |
| 153 |
); |
| 154 |
}; |
| 155 |
|
| 156 |
const tourStepSelector = function ( event ) { |
| 157 |
if ( ! tourSelectorActive ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
function getSelectors( elem ) { |
| 162 |
const selectors = []; |
| 163 |
|
| 164 |
while ( elem.parentElement ) { |
| 165 |
const currentElement = elem.parentElement; |
| 166 |
const tagName = elem.tagName.toLowerCase(); |
| 167 |
const classes = []; |
| 168 |
|
| 169 |
if ( elem.id ) { |
| 170 |
selectors.push( tagName + '#' + elem.id ); |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
elem.classList.forEach( function ( c ) { |
| 175 |
if ( ! filter_selectors( c ) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
classes.push( c ); |
| 179 |
} ); |
| 180 |
|
| 181 |
if ( classes.length ) { |
| 182 |
selectors.push( tagName + '.' + classes.join( '.' ) ); |
| 183 |
} else { |
| 184 |
const index = |
| 185 |
Array.prototype.indexOf.call( |
| 186 |
currentElement.children, |
| 187 |
elem |
| 188 |
) + 1; |
| 189 |
selectors.push( tagName + ':nth-child(' + index + ')' ); |
| 190 |
} |
| 191 |
|
| 192 |
elem = currentElement; |
| 193 |
} |
| 194 |
|
| 195 |
return selectors.reverse(); |
| 196 |
} |
| 197 |
|
| 198 |
event.preventDefault(); |
| 199 |
|
| 200 |
dialogOpen = true; |
| 201 |
|
| 202 |
const stepName = /* eslint-disable-line no-alert */ window.prompt( |
| 203 |
'Enter description for step ' + tour_plugin.tours[ tourId ].length |
| 204 |
); |
| 205 |
|
| 206 |
if ( ! stepName ) { |
| 207 |
event.target.style.outline = ''; |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
const selectors = getSelectors( event.target ); |
| 212 |
tour_plugin.tours[ tourId ].push( { |
| 213 |
element: selectors.join( ' ' ), |
| 214 |
popover: { |
| 215 |
title: tour_plugin.tours[ tourId ][ 0 ].title, |
| 216 |
description: stepName, |
| 217 |
}, |
| 218 |
} ); |
| 219 |
|
| 220 |
event.target.style.outline = |
| 221 |
'1px dashed ' + tour_plugin.tours[ tourId ][ 0 ].color; |
| 222 |
|
| 223 |
if ( tour_plugin.tours[ tourId ].length > 1 ) { |
| 224 |
const xhr = new XMLHttpRequest(); |
| 225 |
xhr.open( 'POST', tour_plugin.rest_url + 'tour/v1/save' ); |
| 226 |
xhr.setRequestHeader( 'Content-Type', 'application/json' ); |
| 227 |
xhr.setRequestHeader( 'X-WP-Nonce', tour_plugin.nonce ); |
| 228 |
xhr.send( |
| 229 |
JSON.stringify( { |
| 230 |
tour: tourId, |
| 231 |
steps: JSON.stringify( tour_plugin.tours[ tourId ] ), |
| 232 |
} ) |
| 233 |
); |
| 234 |
|
| 235 |
document.getElementById( 'tour-steps' ).textContent = |
| 236 |
tour_plugin.tours[ tourId ].length - |
| 237 |
1 + |
| 238 |
' step' + |
| 239 |
( tour_plugin.tours[ tourId ].length > 2 ? 's' : '' ); |
| 240 |
document.getElementById( 'tour-title' ).textContent = 'Saved!'; |
| 241 |
|
| 242 |
setTimeout( function () { |
| 243 |
document.getElementById( 'tour-title' ).textContent = |
| 244 |
tour_plugin.tours[ tourId ][ 0 ].title; |
| 245 |
}, 1000 ); |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
return false; |
| 250 |
}; |
| 251 |
|
| 252 |
document.addEventListener( 'keyup', function ( event ) { |
| 253 |
if ( event.keyCode === 27 ) { |
| 254 |
if ( dialogOpen ) { |
| 255 |
dialogOpen = false; |
| 256 |
return; |
| 257 |
} |
| 258 |
tourSelectorActive = false; |
| 259 |
document.getElementById( 'tour-launcher' ).style.color = ''; |
| 260 |
} |
| 261 |
} ); |
| 262 |
document.addEventListener( 'mouseover', tourStepHighlighter ); |
| 263 |
document.addEventListener( 'mouseout', clearHighlight ); |
| 264 |
document.addEventListener( 'click', tourStepSelector ); |
| 265 |
|