media-library-scanner.js
5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* global WP_Smush */
/**
* Abstract Media Library Scanner.
*
*/
import Fetcher from '../utils/fetcher';
import { scanProgressBar } from './progressbar';
import { GlobalStats } from './globalStats';
const { __ } = wp.i18n;
export default class MediaLibraryScanner {
constructor() {
this.autoSyncDuration = 1500;
this.progressTimeoutId = 0;
this.scanProgress = scanProgressBar( this.autoSyncDuration );
}
startScan( optimizeOnScanCompleted = false ) {
this.onStart();
return Fetcher.scanMediaLibrary.start( optimizeOnScanCompleted ).then( ( response ) => {
if ( ! response?.success ) {
this.onStartFailure( response );
return;
}
this.showProgressBar().autoSyncStatus();
} );
}
onStart() {
// Do nothing at the moment.
}
onStartFailure( response ) {
WP_Smush.helpers.showNotice( response, {
showdismiss: true,
autoclose: false,
} );
}
showProgressBar() {
this.onShowProgressBar();
this.scanProgress.reset().setOnCancelCallback( this.showStopScanningModal.bind( this ) ).open();
return this;
}
onShowProgressBar() {
// Do nothing at the moment.
}
showStopScanningModal() {
if ( ! window.SUI ) {
return;
}
this.onShowStopScanningModal();
window.SUI.openModal(
'smush-stop-scanning-dialog',
'wpbody-content',
undefined,
false
);
}
onShowStopScanningModal() {
this.registerCancelProcessEvent();
}
registerCancelProcessEvent() {
const stopScanButton = document.querySelector( '.smush-stop-scanning-dialog-button' );
if ( ! stopScanButton ) {
return;
}
stopScanButton.addEventListener( 'click', this.cancelProgress.bind( this ) );
}
closeStopScanningModal() {
if ( ! window.SUI ) {
return;
}
const stopScanningElement = document.querySelector( '#smush-stop-scanning-dialog' );
const isModalClosed = ( ! stopScanningElement ) || ! stopScanningElement.classList.contains( 'sui-content-fade-in' );
if ( isModalClosed ) {
return;
}
window.SUI.closeModal( 'smush-stop-scanning-dialog' );
}
closeProgressBar() {
this.onCloseProgressBar();
this.scanProgress.close();
}
onCloseProgressBar() {
// Do nothing at the moment.
}
updateProgress( stats ) {
const totalItems = this.getTotalItems( stats );
const processedItems = this.getProcessedItems( stats );
return this.scanProgress.update( processedItems, totalItems );
}
getProcessedItems( stats ) {
return stats?.processed_items || 0;
}
getTotalItems( stats ) {
return stats?.total_items || 0;
}
cancelProgress() {
this.scanProgress.setCancelButtonOnCancelling();
return Fetcher.scanMediaLibrary.cancel().then( ( response ) => {
if ( ! response?.success ) {
this.onCancelFailure( response );
return;
}
this.onCancelled( response.data );
} );
}
onCancelFailure( response ) {
WP_Smush.helpers.showNotice( response, {
showdismiss: true,
autoclose: false,
} );
this.scanProgress.resetCancelButtonOnFailure();
}
getErrorProgressMessage() {
return __( 'Unfortunately the scan hit an error due to limited resources on your site, we have adjusted the scan to use fewer resources the next time.', 'wp-smushit' );
}
onDead( stats ) {
this.clearProgressTimeout();
this.closeProgressBar();
this.closeStopScanningModal();
this.showRetryScanModal();
}
showRetryScanModal() {
const retryScanModalElement = document.getElementById( 'smush-retry-scan-notice' );
if ( ! window.SUI || ! retryScanModalElement ) {
return;
}
retryScanModalElement.querySelector('.smush-retry-scan-notice-button').onclick = (e) => {
window.SUI.closeModal( 'smush-retry-scan-notice' );
const recheckImagesBtn = document.querySelector( '.wp-smush-scan' );
if ( ! recheckImagesBtn ) {
return;
}
e.preventDefault();
recheckImagesBtn.click();
}
window.SUI.openModal(
'smush-retry-scan-notice',
'wpbody-content',
undefined,
false
);
}
onCompleted( stats ) {
this.onFinish( stats );
}
onCancelled( stats ) {
this.onFinish( stats );
}
onFinish( stats ) {
this.clearProgressTimeout();
const globalStats = stats?.global_stats;
this.updateGlobalStatsAndBulkContent( globalStats );
this.closeProgressBar();
this.closeStopScanningModal();
}
clearProgressTimeout() {
if ( this.progressTimeoutId ) {
clearTimeout( this.progressTimeoutId );
}
}
updateGlobalStatsAndBulkContent( globalStats ) {
if ( ! globalStats ) {
return;
}
GlobalStats.updateGlobalStatsFromSmushScriptData( globalStats );
GlobalStats.renderStats();
}
getStatus() {
return Fetcher.scanMediaLibrary.getScanStatus();
}
autoSyncStatus() {
const startTime = new Date().getTime();
this.getStatus().then( ( response ) => {
if ( ! response?.success ) {
return;
}
const stats = response.data;
if ( stats.is_dead ) {
this.onDead( response.data );
return;
}
this.beforeUpdateStatus( stats );
this.updateProgress( stats ).then( () => {
this.scanProgress.increaseDurationToHaveChangeOnProgress( new Date().getTime() - startTime );
const isCompleted = stats?.is_completed;
if ( isCompleted ) {
this.onCompleted( stats );
return;
}
const isCancelled = stats?.is_cancelled;
if ( isCancelled ) {
this.onCancelled( stats );
return;
}
this.progressTimeoutId = setTimeout( () => this.autoSyncStatus(), this.autoSyncDuration );
} );
} );
}
beforeUpdateStatus() {
// Do nothing at the moment.
}
setInnerText( element, newText ) {
if ( ! element ) {
return;
}
element.dataset.originalText = element.dataset.originalText || element.innerText.trim();
element.innerText = newText;
}
revertInnerText( element ) {
if ( ! element || ! element.dataset.originalText ) {
return;
}
element.innerText = element.dataset.originalText.trim();
}
hideAnElement( element ) {
if ( element ) {
element.classList.add( 'sui-hidden' );
}
}
showAnElement( element ) {
if ( element ) {
element.classList.remove( 'sui-hidden' );
}
}
}