Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 1 | import { queryAll, extend, createStyleSheet, matches, closest } from '../utils/util.js' |
| 2 | import { FRAGMENT_STYLE_REGEX } from '../utils/constants.js' |
| 3 | |
| 4 | // Counter used to generate unique IDs for auto-animated elements |
| 5 | let autoAnimateCounter = 0; |
| 6 | |
| 7 | /** |
| 8 | * Automatically animates matching elements across |
| 9 | * slides with the [data-auto-animate] attribute. |
| 10 | */ |
| 11 | export default class AutoAnimate { |
| 12 | |
| 13 | constructor( Reveal ) { |
| 14 | |
| 15 | this.Reveal = Reveal; |
| 16 | |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Runs an auto-animation between the given slides. |
| 21 | * |
| 22 | * @param {HTMLElement} fromSlide |
| 23 | * @param {HTMLElement} toSlide |
| 24 | */ |
| 25 | run( fromSlide, toSlide ) { |
| 26 | |
| 27 | // Clean up after prior animations |
| 28 | this.reset(); |
| 29 | |
| 30 | let allSlides = this.Reveal.getSlides(); |
| 31 | let toSlideIndex = allSlides.indexOf( toSlide ); |
| 32 | let fromSlideIndex = allSlides.indexOf( fromSlide ); |
| 33 | |
| 34 | // Ensure that both slides are auto-animate targets with the same data-auto-animate-id value |
| 35 | // (including null if absent on both) and that data-auto-animate-restart isn't set on the |
| 36 | // physically latter slide (independent of slide direction) |
| 37 | if( fromSlide.hasAttribute( 'data-auto-animate' ) && toSlide.hasAttribute( 'data-auto-animate' ) |
| 38 | && fromSlide.getAttribute( 'data-auto-animate-id' ) === toSlide.getAttribute( 'data-auto-animate-id' ) |
| 39 | && !( toSlideIndex > fromSlideIndex ? toSlide : fromSlide ).hasAttribute( 'data-auto-animate-restart' ) ) { |
| 40 | |
| 41 | // Create a new auto-animate sheet |
| 42 | this.autoAnimateStyleSheet = this.autoAnimateStyleSheet || createStyleSheet(); |
| 43 | |
| 44 | let animationOptions = this.getAutoAnimateOptions( toSlide ); |
| 45 | |
| 46 | // Set our starting state |
| 47 | fromSlide.dataset.autoAnimate = 'pending'; |
| 48 | toSlide.dataset.autoAnimate = 'pending'; |
| 49 | |
| 50 | // Flag the navigation direction, needed for fragment buildup |
| 51 | animationOptions.slideDirection = toSlideIndex > fromSlideIndex ? 'forward' : 'backward'; |
| 52 | |
| 53 | // Inject our auto-animate styles for this transition |
| 54 | let css = this.getAutoAnimatableElements( fromSlide, toSlide ).map( elements => { |
| 55 | return this.autoAnimateElements( elements.from, elements.to, elements.options || {}, animationOptions, autoAnimateCounter++ ); |
| 56 | } ); |
| 57 | |
| 58 | // Animate unmatched elements, if enabled |
| 59 | if( toSlide.dataset.autoAnimateUnmatched !== 'false' && this.Reveal.getConfig().autoAnimateUnmatched === true ) { |
| 60 | |
| 61 | // Our default timings for unmatched elements |
| 62 | let defaultUnmatchedDuration = animationOptions.duration * 0.8, |
| 63 | defaultUnmatchedDelay = animationOptions.duration * 0.2; |
| 64 | |
| 65 | this.getUnmatchedAutoAnimateElements( toSlide ).forEach( unmatchedElement => { |
| 66 | |
| 67 | let unmatchedOptions = this.getAutoAnimateOptions( unmatchedElement, animationOptions ); |
| 68 | let id = 'unmatched'; |
| 69 | |
| 70 | // If there is a duration or delay set specifically for this |
| 71 | // element our unmatched elements should adhere to those |
| 72 | if( unmatchedOptions.duration !== animationOptions.duration || unmatchedOptions.delay !== animationOptions.delay ) { |
| 73 | id = 'unmatched-' + autoAnimateCounter++; |
| 74 | css.push( `[data-auto-animate="running"] [data-auto-animate-target="${id}"] { transition: opacity ${unmatchedOptions.duration}s ease ${unmatchedOptions.delay}s; }` ); |
| 75 | } |
| 76 | |
| 77 | unmatchedElement.dataset.autoAnimateTarget = id; |
| 78 | |
| 79 | }, this ); |
| 80 | |
| 81 | // Our default transition for unmatched elements |
| 82 | css.push( `[data-auto-animate="running"] [data-auto-animate-target="unmatched"] { transition: opacity ${defaultUnmatchedDuration}s ease ${defaultUnmatchedDelay}s; }` ); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | // Setting the whole chunk of CSS at once is the most |
| 87 | // efficient way to do this. Using sheet.insertRule |
| 88 | // is multiple factors slower. |
| 89 | this.autoAnimateStyleSheet.innerHTML = css.join( '' ); |
| 90 | |
| 91 | // Start the animation next cycle |
| 92 | requestAnimationFrame( () => { |
| 93 | if( this.autoAnimateStyleSheet ) { |
| 94 | // This forces our newly injected styles to be applied in Firefox |
| 95 | getComputedStyle( this.autoAnimateStyleSheet ).fontWeight; |
| 96 | |
| 97 | toSlide.dataset.autoAnimate = 'running'; |
| 98 | } |
| 99 | } ); |
| 100 | |
| 101 | this.Reveal.dispatchEvent({ |
| 102 | type: 'autoanimate', |
| 103 | data: { |
| 104 | fromSlide, |
| 105 | toSlide, |
| 106 | sheet: this.autoAnimateStyleSheet |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Rolls back all changes that we've made to the DOM so |
| 116 | * that as part of animating. |
| 117 | */ |
| 118 | reset() { |
| 119 | |
| 120 | // Reset slides |
| 121 | queryAll( this.Reveal.getRevealElement(), '[data-auto-animate]:not([data-auto-animate=""])' ).forEach( element => { |
| 122 | element.dataset.autoAnimate = ''; |
| 123 | } ); |
| 124 | |
| 125 | // Reset elements |
| 126 | queryAll( this.Reveal.getRevealElement(), '[data-auto-animate-target]' ).forEach( element => { |
| 127 | delete element.dataset.autoAnimateTarget; |
| 128 | } ); |
| 129 | |
| 130 | // Remove the animation sheet |
| 131 | if( this.autoAnimateStyleSheet && this.autoAnimateStyleSheet.parentNode ) { |
| 132 | this.autoAnimateStyleSheet.parentNode.removeChild( this.autoAnimateStyleSheet ); |
| 133 | this.autoAnimateStyleSheet = null; |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Creates a FLIP animation where the `to` element starts out |
| 140 | * in the `from` element position and animates to its original |
| 141 | * state. |
| 142 | * |
| 143 | * @param {HTMLElement} from |
| 144 | * @param {HTMLElement} to |
| 145 | * @param {Object} elementOptions Options for this element pair |
| 146 | * @param {Object} animationOptions Options set at the slide level |
| 147 | * @param {String} id Unique ID that we can use to identify this |
| 148 | * auto-animate element in the DOM |
| 149 | */ |
| 150 | autoAnimateElements( from, to, elementOptions, animationOptions, id ) { |
| 151 | |
| 152 | // 'from' elements are given a data-auto-animate-target with no value, |
| 153 | // 'to' elements are are given a data-auto-animate-target with an ID |
| 154 | from.dataset.autoAnimateTarget = ''; |
| 155 | to.dataset.autoAnimateTarget = id; |
| 156 | |
| 157 | // Each element may override any of the auto-animate options |
| 158 | // like transition easing, duration and delay via data-attributes |
| 159 | let options = this.getAutoAnimateOptions( to, animationOptions ); |
| 160 | |
| 161 | // If we're using a custom element matcher the element options |
| 162 | // may contain additional transition overrides |
| 163 | if( typeof elementOptions.delay !== 'undefined' ) options.delay = elementOptions.delay; |
| 164 | if( typeof elementOptions.duration !== 'undefined' ) options.duration = elementOptions.duration; |
| 165 | if( typeof elementOptions.easing !== 'undefined' ) options.easing = elementOptions.easing; |
| 166 | |
| 167 | let fromProps = this.getAutoAnimatableProperties( 'from', from, elementOptions ), |
| 168 | toProps = this.getAutoAnimatableProperties( 'to', to, elementOptions ); |
| 169 | |
| 170 | // Maintain fragment visibility for matching elements when |
| 171 | // we're navigating forwards, this way the viewer won't need |
| 172 | // to step through the same fragments twice |
| 173 | if( to.classList.contains( 'fragment' ) ) { |
| 174 | |
| 175 | // Don't auto-animate the opacity of fragments to avoid |
| 176 | // conflicts with fragment animations |
| 177 | delete toProps.styles['opacity']; |
| 178 | |
| 179 | if( from.classList.contains( 'fragment' ) ) { |
| 180 | |
| 181 | let fromFragmentStyle = ( from.className.match( FRAGMENT_STYLE_REGEX ) || [''] )[0]; |
| 182 | let toFragmentStyle = ( to.className.match( FRAGMENT_STYLE_REGEX ) || [''] )[0]; |
| 183 | |
| 184 | // Only skip the fragment if the fragment animation style |
| 185 | // remains unchanged |
| 186 | if( fromFragmentStyle === toFragmentStyle && animationOptions.slideDirection === 'forward' ) { |
| 187 | to.classList.add( 'visible', 'disabled' ); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | |
| 194 | // If translation and/or scaling are enabled, css transform |
| 195 | // the 'to' element so that it matches the position and size |
| 196 | // of the 'from' element |
| 197 | if( elementOptions.translate !== false || elementOptions.scale !== false ) { |
| 198 | |
| 199 | let presentationScale = this.Reveal.getScale(); |
| 200 | |
| 201 | let delta = { |
| 202 | x: ( fromProps.x - toProps.x ) / presentationScale, |
| 203 | y: ( fromProps.y - toProps.y ) / presentationScale, |
| 204 | scaleX: fromProps.width / toProps.width, |
| 205 | scaleY: fromProps.height / toProps.height |
| 206 | }; |
| 207 | |
| 208 | // Limit decimal points to avoid 0.0001px blur and stutter |
| 209 | delta.x = Math.round( delta.x * 1000 ) / 1000; |
| 210 | delta.y = Math.round( delta.y * 1000 ) / 1000; |
| 211 | delta.scaleX = Math.round( delta.scaleX * 1000 ) / 1000; |
| 212 | delta.scaleX = Math.round( delta.scaleX * 1000 ) / 1000; |
| 213 | |
| 214 | let translate = elementOptions.translate !== false && ( delta.x !== 0 || delta.y !== 0 ), |
| 215 | scale = elementOptions.scale !== false && ( delta.scaleX !== 0 || delta.scaleY !== 0 ); |
| 216 | |
| 217 | // No need to transform if nothing's changed |
| 218 | if( translate || scale ) { |
| 219 | |
| 220 | let transform = []; |
| 221 | |
| 222 | if( translate ) transform.push( `translate(${delta.x}px, ${delta.y}px)` ); |
| 223 | if( scale ) transform.push( `scale(${delta.scaleX}, ${delta.scaleY})` ); |
| 224 | |
| 225 | fromProps.styles['transform'] = transform.join( ' ' ); |
| 226 | fromProps.styles['transform-origin'] = 'top left'; |
| 227 | |
| 228 | toProps.styles['transform'] = 'none'; |
| 229 | |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | |
| 234 | // Delete all unchanged 'to' styles |
| 235 | for( let propertyName in toProps.styles ) { |
| 236 | const toValue = toProps.styles[propertyName]; |
| 237 | const fromValue = fromProps.styles[propertyName]; |
| 238 | |
| 239 | if( toValue === fromValue ) { |
| 240 | delete toProps.styles[propertyName]; |
| 241 | } |
| 242 | else { |
| 243 | // If these property values were set via a custom matcher providing |
| 244 | // an explicit 'from' and/or 'to' value, we always inject those values. |
| 245 | if( toValue.explicitValue === true ) { |
| 246 | toProps.styles[propertyName] = toValue.value; |
| 247 | } |
| 248 | |
| 249 | if( fromValue.explicitValue === true ) { |
| 250 | fromProps.styles[propertyName] = fromValue.value; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | let css = ''; |
| 256 | |
| 257 | let toStyleProperties = Object.keys( toProps.styles ); |
| 258 | |
| 259 | // Only create animate this element IF at least one style |
| 260 | // property has changed |
| 261 | if( toStyleProperties.length > 0 ) { |
| 262 | |
| 263 | // Instantly move to the 'from' state |
| 264 | fromProps.styles['transition'] = 'none'; |
| 265 | |
| 266 | // Animate towards the 'to' state |
| 267 | toProps.styles['transition'] = `all ${options.duration}s ${options.easing} ${options.delay}s`; |
| 268 | toProps.styles['transition-property'] = toStyleProperties.join( ', ' ); |
| 269 | toProps.styles['will-change'] = toStyleProperties.join( ', ' ); |
| 270 | |
| 271 | // Build up our custom CSS. We need to override inline styles |
| 272 | // so we need to make our styles vErY IMPORTANT!1!! |
| 273 | let fromCSS = Object.keys( fromProps.styles ).map( propertyName => { |
| 274 | return propertyName + ': ' + fromProps.styles[propertyName] + ' !important;'; |
| 275 | } ).join( '' ); |
| 276 | |
| 277 | let toCSS = Object.keys( toProps.styles ).map( propertyName => { |
| 278 | return propertyName + ': ' + toProps.styles[propertyName] + ' !important;'; |
| 279 | } ).join( '' ); |
| 280 | |
| 281 | css = '[data-auto-animate-target="'+ id +'"] {'+ fromCSS +'}' + |
| 282 | '[data-auto-animate="running"] [data-auto-animate-target="'+ id +'"] {'+ toCSS +'}'; |
| 283 | |
| 284 | } |
| 285 | |
| 286 | return css; |
| 287 | |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Returns the auto-animate options for the given element. |
| 292 | * |
| 293 | * @param {HTMLElement} element Element to pick up options |
| 294 | * from, either a slide or an animation target |
| 295 | * @param {Object} [inheritedOptions] Optional set of existing |
| 296 | * options |
| 297 | */ |
| 298 | getAutoAnimateOptions( element, inheritedOptions ) { |
| 299 | |
| 300 | let options = { |
| 301 | easing: this.Reveal.getConfig().autoAnimateEasing, |
| 302 | duration: this.Reveal.getConfig().autoAnimateDuration, |
| 303 | delay: 0 |
| 304 | }; |
| 305 | |
| 306 | options = extend( options, inheritedOptions ); |
| 307 | |
| 308 | // Inherit options from parent elements |
| 309 | if( element.parentNode ) { |
| 310 | let autoAnimatedParent = closest( element.parentNode, '[data-auto-animate-target]' ); |
| 311 | if( autoAnimatedParent ) { |
| 312 | options = this.getAutoAnimateOptions( autoAnimatedParent, options ); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if( element.dataset.autoAnimateEasing ) { |
| 317 | options.easing = element.dataset.autoAnimateEasing; |
| 318 | } |
| 319 | |
| 320 | if( element.dataset.autoAnimateDuration ) { |
| 321 | options.duration = parseFloat( element.dataset.autoAnimateDuration ); |
| 322 | } |
| 323 | |
| 324 | if( element.dataset.autoAnimateDelay ) { |
| 325 | options.delay = parseFloat( element.dataset.autoAnimateDelay ); |
| 326 | } |
| 327 | |
| 328 | return options; |
| 329 | |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Returns an object containing all of the properties |
| 334 | * that can be auto-animated for the given element and |
| 335 | * their current computed values. |
| 336 | * |
| 337 | * @param {String} direction 'from' or 'to' |
| 338 | */ |
| 339 | getAutoAnimatableProperties( direction, element, elementOptions ) { |
| 340 | |
| 341 | let config = this.Reveal.getConfig(); |
| 342 | |
| 343 | let properties = { styles: [] }; |
| 344 | |
| 345 | // Position and size |
| 346 | if( elementOptions.translate !== false || elementOptions.scale !== false ) { |
| 347 | let bounds; |
| 348 | |
| 349 | // Custom auto-animate may optionally return a custom tailored |
| 350 | // measurement function |
| 351 | if( typeof elementOptions.measure === 'function' ) { |
| 352 | bounds = elementOptions.measure( element ); |
| 353 | } |
| 354 | else { |
| 355 | if( config.center ) { |
| 356 | // More precise, but breaks when used in combination |
| 357 | // with zoom for scaling the deck ¯\_(ツ)_/¯ |
| 358 | bounds = element.getBoundingClientRect(); |
| 359 | } |
| 360 | else { |
| 361 | let scale = this.Reveal.getScale(); |
| 362 | bounds = { |
| 363 | x: element.offsetLeft * scale, |
| 364 | y: element.offsetTop * scale, |
| 365 | width: element.offsetWidth * scale, |
| 366 | height: element.offsetHeight * scale |
| 367 | }; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | properties.x = bounds.x; |
| 372 | properties.y = bounds.y; |
| 373 | properties.width = bounds.width; |
| 374 | properties.height = bounds.height; |
| 375 | } |
| 376 | |
| 377 | const computedStyles = getComputedStyle( element ); |
| 378 | |
| 379 | // CSS styles |
| 380 | ( elementOptions.styles || config.autoAnimateStyles ).forEach( style => { |
| 381 | let value; |
| 382 | |
| 383 | // `style` is either the property name directly, or an object |
| 384 | // definition of a style property |
| 385 | if( typeof style === 'string' ) style = { property: style }; |
| 386 | |
| 387 | if( typeof style.from !== 'undefined' && direction === 'from' ) { |
| 388 | value = { value: style.from, explicitValue: true }; |
| 389 | } |
| 390 | else if( typeof style.to !== 'undefined' && direction === 'to' ) { |
| 391 | value = { value: style.to, explicitValue: true }; |
| 392 | } |
| 393 | else { |
| 394 | value = computedStyles[style.property]; |
| 395 | } |
| 396 | |
| 397 | if( value !== '' ) { |
| 398 | properties.styles[style.property] = value; |
| 399 | } |
| 400 | } ); |
| 401 | |
| 402 | return properties; |
| 403 | |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Get a list of all element pairs that we can animate |
| 408 | * between the given slides. |
| 409 | * |
| 410 | * @param {HTMLElement} fromSlide |
| 411 | * @param {HTMLElement} toSlide |
| 412 | * |
| 413 | * @return {Array} Each value is an array where [0] is |
| 414 | * the element we're animating from and [1] is the |
| 415 | * element we're animating to |
| 416 | */ |
| 417 | getAutoAnimatableElements( fromSlide, toSlide ) { |
| 418 | |
| 419 | let matcher = typeof this.Reveal.getConfig().autoAnimateMatcher === 'function' ? this.Reveal.getConfig().autoAnimateMatcher : this.getAutoAnimatePairs; |
| 420 | |
| 421 | let pairs = matcher.call( this, fromSlide, toSlide ); |
| 422 | |
| 423 | let reserved = []; |
| 424 | |
| 425 | // Remove duplicate pairs |
| 426 | return pairs.filter( ( pair, index ) => { |
| 427 | if( reserved.indexOf( pair.to ) === -1 ) { |
| 428 | reserved.push( pair.to ); |
| 429 | return true; |
| 430 | } |
| 431 | } ); |
| 432 | |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Identifies matching elements between slides. |
| 437 | * |
| 438 | * You can specify a custom matcher function by using |
| 439 | * the `autoAnimateMatcher` config option. |
| 440 | */ |
| 441 | getAutoAnimatePairs( fromSlide, toSlide ) { |
| 442 | |
| 443 | let pairs = []; |
| 444 | |
| 445 | const codeNodes = 'pre'; |
| 446 | const textNodes = 'h1, h2, h3, h4, h5, h6, p, li'; |
| 447 | const mediaNodes = 'img, video, iframe'; |
| 448 | |
| 449 | // Eplicit matches via data-id |
| 450 | this.findAutoAnimateMatches( pairs, fromSlide, toSlide, '[data-id]', node => { |
| 451 | return node.nodeName + ':::' + node.getAttribute( 'data-id' ); |
| 452 | } ); |
| 453 | |
| 454 | // Text |
| 455 | this.findAutoAnimateMatches( pairs, fromSlide, toSlide, textNodes, node => { |
| 456 | return node.nodeName + ':::' + node.innerText; |
| 457 | } ); |
| 458 | |
| 459 | // Media |
| 460 | this.findAutoAnimateMatches( pairs, fromSlide, toSlide, mediaNodes, node => { |
| 461 | return node.nodeName + ':::' + ( node.getAttribute( 'src' ) || node.getAttribute( 'data-src' ) ); |
| 462 | } ); |
| 463 | |
| 464 | // Code |
| 465 | this.findAutoAnimateMatches( pairs, fromSlide, toSlide, codeNodes, node => { |
| 466 | return node.nodeName + ':::' + node.innerText; |
| 467 | } ); |
| 468 | |
| 469 | pairs.forEach( pair => { |
| 470 | |
| 471 | // Disable scale transformations on text nodes, we transition |
| 472 | // each individual text property instead |
| 473 | if( matches( pair.from, textNodes ) ) { |
| 474 | pair.options = { scale: false }; |
| 475 | } |
| 476 | // Animate individual lines of code |
| 477 | else if( matches( pair.from, codeNodes ) ) { |
| 478 | |
| 479 | // Transition the code block's width and height instead of scaling |
| 480 | // to prevent its content from being squished |
| 481 | pair.options = { scale: false, styles: [ 'width', 'height' ] }; |
| 482 | |
| 483 | // Lines of code |
| 484 | this.findAutoAnimateMatches( pairs, pair.from, pair.to, '.hljs .hljs-ln-code', node => { |
| 485 | return node.textContent; |
| 486 | }, { |
| 487 | scale: false, |
| 488 | styles: [], |
| 489 | measure: this.getLocalBoundingBox.bind( this ) |
| 490 | } ); |
| 491 | |
| 492 | // Line numbers |
| 493 | this.findAutoAnimateMatches( pairs, pair.from, pair.to, '.hljs .hljs-ln-line[data-line-number]', node => { |
| 494 | return node.getAttribute( 'data-line-number' ); |
| 495 | }, { |
| 496 | scale: false, |
| 497 | styles: [ 'width' ], |
| 498 | measure: this.getLocalBoundingBox.bind( this ) |
| 499 | } ); |
| 500 | |
| 501 | } |
| 502 | |
| 503 | }, this ); |
| 504 | |
| 505 | return pairs; |
| 506 | |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Helper method which returns a bounding box based on |
| 511 | * the given elements offset coordinates. |
| 512 | * |
| 513 | * @param {HTMLElement} element |
| 514 | * @return {Object} x, y, width, height |
| 515 | */ |
| 516 | getLocalBoundingBox( element ) { |
| 517 | |
| 518 | const presentationScale = this.Reveal.getScale(); |
| 519 | |
| 520 | return { |
| 521 | x: Math.round( ( element.offsetLeft * presentationScale ) * 100 ) / 100, |
| 522 | y: Math.round( ( element.offsetTop * presentationScale ) * 100 ) / 100, |
| 523 | width: Math.round( ( element.offsetWidth * presentationScale ) * 100 ) / 100, |
| 524 | height: Math.round( ( element.offsetHeight * presentationScale ) * 100 ) / 100 |
| 525 | }; |
| 526 | |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Finds matching elements between two slides. |
| 531 | * |
| 532 | * @param {Array} pairs List of pairs to push matches to |
| 533 | * @param {HTMLElement} fromScope Scope within the from element exists |
| 534 | * @param {HTMLElement} toScope Scope within the to element exists |
| 535 | * @param {String} selector CSS selector of the element to match |
| 536 | * @param {Function} serializer A function that accepts an element and returns |
| 537 | * a stringified ID based on its contents |
| 538 | * @param {Object} animationOptions Optional config options for this pair |
| 539 | */ |
| 540 | findAutoAnimateMatches( pairs, fromScope, toScope, selector, serializer, animationOptions ) { |
| 541 | |
| 542 | let fromMatches = {}; |
| 543 | let toMatches = {}; |
| 544 | |
| 545 | [].slice.call( fromScope.querySelectorAll( selector ) ).forEach( ( element, i ) => { |
| 546 | const key = serializer( element ); |
| 547 | if( typeof key === 'string' && key.length ) { |
| 548 | fromMatches[key] = fromMatches[key] || []; |
| 549 | fromMatches[key].push( element ); |
| 550 | } |
| 551 | } ); |
| 552 | |
| 553 | [].slice.call( toScope.querySelectorAll( selector ) ).forEach( ( element, i ) => { |
| 554 | const key = serializer( element ); |
| 555 | toMatches[key] = toMatches[key] || []; |
| 556 | toMatches[key].push( element ); |
| 557 | |
| 558 | let fromElement; |
| 559 | |
| 560 | // Retrieve the 'from' element |
| 561 | if( fromMatches[key] ) { |
| 562 | const pimaryIndex = toMatches[key].length - 1; |
| 563 | const secondaryIndex = fromMatches[key].length - 1; |
| 564 | |
| 565 | // If there are multiple identical from elements, retrieve |
| 566 | // the one at the same index as our to-element. |
| 567 | if( fromMatches[key][ pimaryIndex ] ) { |
| 568 | fromElement = fromMatches[key][ pimaryIndex ]; |
| 569 | fromMatches[key][ pimaryIndex ] = null; |
| 570 | } |
| 571 | // If there are no matching from-elements at the same index, |
| 572 | // use the last one. |
| 573 | else if( fromMatches[key][ secondaryIndex ] ) { |
| 574 | fromElement = fromMatches[key][ secondaryIndex ]; |
| 575 | fromMatches[key][ secondaryIndex ] = null; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // If we've got a matching pair, push it to the list of pairs |
| 580 | if( fromElement ) { |
| 581 | pairs.push({ |
| 582 | from: fromElement, |
| 583 | to: element, |
| 584 | options: animationOptions |
| 585 | }); |
| 586 | } |
| 587 | } ); |
| 588 | |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * Returns a all elements within the given scope that should |
| 593 | * be considered unmatched in an auto-animate transition. If |
| 594 | * fading of unmatched elements is turned on, these elements |
| 595 | * will fade when going between auto-animate slides. |
| 596 | * |
| 597 | * Note that parents of auto-animate targets are NOT considerd |
| 598 | * unmatched since fading them would break the auto-animation. |
| 599 | * |
| 600 | * @param {HTMLElement} rootElement |
| 601 | * @return {Array} |
| 602 | */ |
| 603 | getUnmatchedAutoAnimateElements( rootElement ) { |
| 604 | |
| 605 | return [].slice.call( rootElement.children ).reduce( ( result, element ) => { |
| 606 | |
| 607 | const containsAnimatedElements = element.querySelector( '[data-auto-animate-target]' ); |
| 608 | |
| 609 | // The element is unmatched if |
| 610 | // - It is not an auto-animate target |
| 611 | // - It does not contain any auto-animate targets |
| 612 | if( !element.hasAttribute( 'data-auto-animate-target' ) && !containsAnimatedElements ) { |
| 613 | result.push( element ); |
| 614 | } |
| 615 | |
| 616 | if( element.querySelector( '[data-auto-animate-target]' ) ) { |
| 617 | result = result.concat( this.getUnmatchedAutoAnimateElements( element ) ); |
| 618 | } |
| 619 | |
| 620 | return result; |
| 621 | |
| 622 | }, [] ); |
| 623 | |
| 624 | } |
| 625 | |
| 626 | } |