@@ -9,13 +9,12 @@ import $ from "jquery";
9
9
import ko from "knockout" ;
10
10
import $t from "mage/translate" ;
11
11
import events from "Magento_PageBuilder/js/events" ;
12
+ import mageUtils from "mageUtils" ;
12
13
import _ from "underscore" ;
13
14
import "vimeoWrapper" ;
14
15
import { PreviewSortableSortUpdateEventParams } from "../../binding/sortable-children.types" ;
15
- import ContentTypeConfigInterface from "../../content-type-config.types" ;
16
16
import ConditionalRemoveOption from "../../content-type-menu/conditional-remove-option" ;
17
17
import { OptionsInterface } from "../../content-type-menu/option.types" ;
18
- import ContentTypeInterface from "../../content-type.types" ;
19
18
import { DataObject } from "../../data-store" ;
20
19
import Uploader from "../../uploader" ;
21
20
import delayUntil from "../../utils/delay-until" ;
@@ -31,7 +30,6 @@ import nestingLinkDialog from "../../utils/nesting-link-dialog";
31
30
import WysiwygFactory from "../../wysiwyg/factory" ;
32
31
import WysiwygInterface from "../../wysiwyg/wysiwyg-interface" ;
33
32
import { ContentTypeMountEventParamsInterface , ContentTypeReadyEventParamsInterface } from "../content-type-events.types" ;
34
- import ObservableUpdater from "../observable-updater" ;
35
33
import BasePreview from "../preview" ;
36
34
import SliderPreview from "../slider/preview" ;
37
35
@@ -81,6 +79,18 @@ export default class Preview extends BasePreview {
81
79
*/
82
80
private handledDoubleClick : boolean = false ;
83
81
82
+ /**
83
+ * Properties change that should cause rebuilding video background
84
+ */
85
+ private videoUpdateProperties = [
86
+ "background_type" ,
87
+ "video_fallback_image" ,
88
+ "video_lazy_load" ,
89
+ "video_loop" ,
90
+ "video_play_only_visible" ,
91
+ "video_source" ,
92
+ ] ;
93
+
84
94
/**
85
95
* Debounce and defer the init of Jarallax
86
96
*
@@ -112,7 +122,7 @@ export default class Preview extends BasePreview {
112
122
const viewportElement = $ ( "<div class=\"jarallax-viewport-element\"></div>" ) as JQuery ;
113
123
114
124
$ ( this . wrapper ) . append (
115
- $ ( ' .jarallax-viewport-element' , this . wrapper ) . length ? '' : viewportElement
125
+ $ ( " .jarallax-viewport-element" , this . wrapper ) . length ? "" : viewportElement ,
116
126
) ;
117
127
118
128
jarallax (
@@ -123,34 +133,14 @@ export default class Preview extends BasePreview {
123
133
videoLoop : ( this . contentType . dataStore . get ( "video_loop" ) as string ) === "true" ,
124
134
speed : 1 ,
125
135
videoPlayOnlyVisible : ( this . contentType . dataStore . get ( "video_play_only_visible" ) as string ) === "true" ,
126
- elementInViewport : $ ( ' .jarallax-viewport-element' , this . wrapper )
136
+ elementInViewport : $ ( " .jarallax-viewport-element" , this . wrapper ) ,
127
137
} ,
128
138
) ;
129
139
} ) ;
130
140
}
131
141
132
142
} , 50 ) ;
133
143
134
- /**
135
- * @param {ContentTypeInterface } contentType
136
- * @param {ContentTypeConfigInterface } config
137
- * @param {ObservableUpdater } observableUpdater
138
- */
139
- constructor (
140
- contentType : ContentTypeInterface ,
141
- config : ContentTypeConfigInterface ,
142
- observableUpdater : ObservableUpdater ,
143
- ) {
144
- super ( contentType , config , observableUpdater ) ;
145
-
146
- this . contentType . dataStore . subscribe ( this . buildJarallax ) ;
147
- events . on ( "slide:mountAfter" , ( args : ContentTypeReadyEventParamsInterface ) => {
148
- if ( args . id === this . contentType . id ) {
149
- this . buildJarallax ( ) ;
150
- }
151
- } ) ;
152
- }
153
-
154
144
/**
155
145
* @param {HTMLElement } element
156
146
*/
@@ -475,6 +465,12 @@ export default class Preview extends BasePreview {
475
465
protected bindEvents ( ) {
476
466
super . bindEvents ( ) ;
477
467
468
+ events . on ( "slide:mountAfter" , ( args : ContentTypeReadyEventParamsInterface ) => {
469
+ if ( args . id === this . contentType . id ) {
470
+ this . buildJarallax ( ) ;
471
+ }
472
+ } ) ;
473
+
478
474
events . on ( `${ this . config . name } :${ this . contentType . id } :updateAfter` , ( ) => {
479
475
const dataStore = this . contentType . dataStore . getState ( ) ;
480
476
const imageObject = ( dataStore [ this . config . additional_data . uploaderConfig . dataScope ] as object [ ] ) [ 0 ] || { } ;
@@ -508,23 +504,29 @@ export default class Preview extends BasePreview {
508
504
$ ( ( slider . preview as SliderPreview ) . element ) . on ( "beforeChange" , ( ) => {
509
505
this . slideChanged = false ;
510
506
} ) ;
511
- $ ( ( slider . preview as SliderPreview ) . element ) . on ( "afterChange" , ( event , slider ) => {
512
- $ ( slider . $slides ) . each ( ( index , slide ) => {
513
- const videoSlide = slide . querySelector ( ' .jarallax' ) ;
507
+ $ ( ( slider . preview as SliderPreview ) . element ) . on ( "afterChange" , ( event , slick ) => {
508
+ $ ( slick . $slides ) . each ( ( index , slide ) => {
509
+ const videoSlide = slide . querySelector ( " .jarallax" ) ;
514
510
if ( videoSlide ) {
515
- jarallax ( videoSlide , ' onScroll' ) ;
511
+ jarallax ( videoSlide , " onScroll" ) ;
516
512
}
517
513
} ) ;
518
514
this . slideChanged = true ;
519
515
} ) ;
520
516
}
521
517
} ) ;
522
518
523
- this . contentType . dataStore . subscribe (
524
- ( data : DataObject ) => {
525
- this . slideName ( data . slide_name ) ;
526
- } ,
527
- ) ;
519
+ this . contentType . dataStore . subscribe ( function ( data : DataObject ) {
520
+ this . slideName ( data . slide_name ) ;
521
+
522
+ if ( this . isVideoShouldBeUpdated ( data ) ) {
523
+ this . buildJarallax ( ) ;
524
+ }
525
+ } . bind ( this ) ) ;
526
+
527
+ events . on ( `image:${ this . contentType . id } :uploadAfter` , ( ) => {
528
+ this . contentType . dataStore . set ( "background_type" , "image" ) ;
529
+ } ) ;
528
530
}
529
531
530
532
/**
@@ -554,4 +556,25 @@ export default class Preview extends BasePreview {
554
556
555
557
$ ( this . textarea ) . height ( scrollHeight ) ;
556
558
}
559
+
560
+ /**
561
+ * Check if video background should be rebuilt
562
+ *
563
+ * @return boolean
564
+ */
565
+ private isVideoShouldBeUpdated ( state : DataObject ) : boolean {
566
+ const previousState = this . contentType . dataStore . previousState ;
567
+ const diff = mageUtils . compare ( previousState , state ) . changes ;
568
+
569
+ if ( diff . length > 0 ) {
570
+ return _ . some ( diff , ( element ) => {
571
+ if ( element . name === "video_fallback_image" ) {
572
+ return ( ! _ . isEmpty ( previousState . video_fallback_image ) && previousState . video_fallback_image ) !==
573
+ ( ! _ . isEmpty ( state . video_fallback_image ) && state . video_fallback_image ) ;
574
+ }
575
+
576
+ return this . videoUpdateProperties . indexOf ( element . name ) !== - 1 ;
577
+ } ) ;
578
+ }
579
+ }
557
580
}
0 commit comments