@@ -23,13 +23,13 @@ export default class Preview extends BasePreview {
23
23
public displayingBlockPreview : KnockoutObservable < boolean > = ko . observable ( false ) ;
24
24
public loading : KnockoutObservable < boolean > = ko . observable ( false ) ;
25
25
public placeholderText : KnockoutObservable < string > ;
26
- private lastBlockId : number ;
27
- private lastTemplate : string ;
28
- private lastRenderedHtml : string ;
29
- private messages = {
26
+ protected messages = {
30
27
NOT_SELECTED : $t ( "Empty Block" ) ,
31
28
UNKNOWN_ERROR : $t ( "An unknown error occurred. Please try again." ) ,
32
29
} ;
30
+ private lastBlockId : number ;
31
+ private lastTemplate : string ;
32
+ private lastRenderedHtml : string ;
33
33
34
34
/**
35
35
* @inheritdoc
@@ -52,6 +52,16 @@ export default class Preview extends BasePreview {
52
52
} ) ;
53
53
}
54
54
55
+ /**
56
+ *
57
+ * @param {DataObject } data
58
+ */
59
+ public processBlockData ( data : DataObject ) : void {
60
+ // Only load if something changed
61
+ this . displayPreviewPlaceholder ( data , "banner_ids" ) ;
62
+ this . processRequest ( data , "banner_ids" , "name" ) ;
63
+ }
64
+
55
65
/**
56
66
* @inheritdoc
57
67
*/
@@ -76,7 +86,19 @@ export default class Preview extends BasePreview {
76
86
const data = this . parent . dataStore . get ( ) as DataObject ;
77
87
78
88
// Only load if something changed
79
- if ( this . lastBlockId === data . block_id && this . lastTemplate === data . template ) {
89
+ this . processBlockData ( data ) ;
90
+
91
+ }
92
+
93
+ /**
94
+ * Displsay preview placeholder
95
+ *
96
+ * @param {DataObject } data
97
+ * @param {string } identifierName
98
+ */
99
+ protected displayPreviewPlaceholder ( data : DataObject , identifierName : string ) : void {
100
+ // Only load if something changed
101
+ if ( this . lastBlockId === data [ identifierName ] && this . lastTemplate === data . template ) {
80
102
// The mass converter will have transformed the HTML property into a directive
81
103
if ( this . lastRenderedHtml ) {
82
104
this . data . main . html ( this . lastRenderedHtml ) ;
@@ -88,28 +110,35 @@ export default class Preview extends BasePreview {
88
110
this . placeholderText ( "" ) ;
89
111
}
90
112
91
- if ( ! data . block_id || data . template . length === 0 ) {
113
+ if ( ! data [ identifierName ] || data . template . length === 0 ) {
92
114
this . showBlockPreview ( false ) ;
93
115
this . placeholderText ( this . messages . NOT_SELECTED ) ;
94
116
return ;
95
117
}
118
+ }
96
119
97
- this . loading ( true ) ;
98
-
120
+ /**
121
+ *
122
+ * @param {DataObject } data
123
+ * @param {string } identifierName
124
+ * @param {string } labelKey
125
+ */
126
+ protected processRequest ( data : DataObject , identifierName : string , labelKey : string ) : void {
99
127
const url = Config . getConfig ( "preview_url" ) ;
128
+ const identifier = data [ identifierName ] ;
100
129
const requestConfig = {
101
130
// Prevent caching
102
131
method : "POST" ,
103
132
data : {
104
133
role : this . config . name ,
105
- block_id : data . block_id ,
134
+ block_id : identifier ,
106
135
directive : this . data . main . html ( ) ,
107
136
} ,
108
137
} ;
109
-
138
+ this . loading ( true ) ;
110
139
// Retrieve a state object representing the block from the preview controller and process it on the stage
111
140
$ . ajax ( url , requestConfig )
112
- // The state object will contain the block name and either html or a message why there isn't any.
141
+ // The state object will contain the block name and either html or a message why there isn't any.
113
142
. done ( ( response ) => {
114
143
// Empty content means something bad happened in the controller that didn't trigger a 5xx
115
144
if ( typeof response . data !== "object" ) {
@@ -120,7 +149,7 @@ export default class Preview extends BasePreview {
120
149
}
121
150
122
151
// Update the stage content type label with the real block title if provided
123
- this . displayLabel ( response . data . title ? response . data . title : this . config . label ) ;
152
+ this . displayLabel ( response . data [ labelKey ] ? response . data [ labelKey ] : this . config . label ) ;
124
153
125
154
if ( response . data . content ) {
126
155
this . showBlockPreview ( true ) ;
@@ -131,7 +160,7 @@ export default class Preview extends BasePreview {
131
160
this . placeholderText ( response . data . error ) ;
132
161
}
133
162
134
- this . lastBlockId = parseInt ( data . block_id . toString ( ) , 10 ) ;
163
+ this . lastBlockId = parseInt ( identifier . toString ( ) , 10 ) ;
135
164
this . lastTemplate = data . template . toString ( ) ;
136
165
this . lastRenderedHtml = response . data . content ;
137
166
} )
0 commit comments