| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { escapeHTML } from '@wordpress/escape-html'; |
| 3 |
import { formatFileSize } from '../../utils'; |
| 4 |
import { UPGRADE_LINK } from '../../constants'; |
| 5 |
|
| 6 |
const notOptimizedTemplate = () => { |
| 7 |
return ` |
| 8 |
<button type="button" |
| 9 |
class="button button-primary image-optimization-control__button image-optimization-control__button--optimize"> |
| 10 |
${ __( 'Optimize now', 'image-optimization' ) } |
| 11 |
</button> |
| 12 |
`; |
| 13 |
}; |
| 14 |
|
| 15 |
const loadingTemplate = ( action ) => { |
| 16 |
let buttonText; |
| 17 |
|
| 18 |
switch ( action ) { |
| 19 |
case 'restore': |
| 20 |
buttonText = __( 'Restoring…', 'image-optimization' ); |
| 21 |
break; |
| 22 |
|
| 23 |
case 'optimize': |
| 24 |
buttonText = __( 'Optimizing…', 'image-optimization' ); |
| 25 |
break; |
| 26 |
|
| 27 |
case 'reoptimize': |
| 28 |
buttonText = __( 'Reoptimizing…', 'image-optimization' ); |
| 29 |
break; |
| 30 |
|
| 31 |
default: |
| 32 |
buttonText = __( 'Loading…', 'image-optimization' ); |
| 33 |
} |
| 34 |
|
| 35 |
return ` |
| 36 |
<button class="button button-secondary image-optimization-control__button image-optimization-control__button--optimize" |
| 37 |
disabled=""> |
| 38 |
<span class="spinner is-active"></span> ${ buttonText } |
| 39 |
</button> |
| 40 |
`; |
| 41 |
}; |
| 42 |
|
| 43 |
const errorTemplate = ( message, imagesLeft ) => { |
| 44 |
return ` |
| 45 |
<span class="image-optimization-control__error-message">${ escapeHTML( message ) }</span> |
| 46 |
|
| 47 |
${ imagesLeft === 0 |
| 48 |
? `<a class="button button-secondary button-large image-optimization-control__button" |
| 49 |
href="${ UPGRADE_LINK }" |
| 50 |
target="_blank" rel="noopener noreferrer"> |
| 51 |
${ __( 'Upgrade', 'image-optimization' ) } |
| 52 |
</a> |
| 53 |
` : ` |
| 54 |
<button class="button button-secondary button-large button-link-delete image-optimization-control__button image-optimization-control__button--try-again" |
| 55 |
type="button"> |
| 56 |
${ __( 'Try again', 'image-optimization' ) } |
| 57 |
</button>` } |
| 58 |
`; |
| 59 |
}; |
| 60 |
|
| 61 |
const optimizedTemplate = ( data ) => { |
| 62 |
const absoluteValue = formatFileSize( data?.saved?.absolute, 1 ); |
| 63 |
|
| 64 |
return ` |
| 65 |
<p class="image-optimization-control__property"> |
| 66 |
${ __( 'Image sizes optimized', 'image-optimization' ) }: |
| 67 |
|
| 68 |
<span> |
| 69 |
${ data?.sizesOptimized }/${ data?.sizesTotal } |
| 70 |
|
| 71 |
<button type="button" |
| 72 |
class="image-optimization-control__details-button" |
| 73 |
aria-label="${ __( 'Open optimization details', 'image-optimization' ) }"> |
| 74 |
(+) |
| 75 |
</button> |
| 76 |
</span> |
| 77 |
</p> |
| 78 |
|
| 79 |
<p class="image-optimization-control__property"> |
| 80 |
${ data?.saved?.absolute !== 0 |
| 81 |
? `${ __( 'Overall saving', 'image-optimization' ) }: <span>${ data?.saved?.relative }% (${ absoluteValue })</span>` |
| 82 |
: `<span>${ __( 'Image is fully optimized', 'image-optimization' ) }</span>` } |
| 83 |
</p> |
| 84 |
|
| 85 |
<div class="image-optimization-control__buttons-wrapper"> |
| 86 |
${ data?.canBeRestored ? ` |
| 87 |
<button type="button" |
| 88 |
class="button button-secondary image-optimization-control__button image-optimization-control__button--restore-original"> |
| 89 |
${ __( 'Restore original', 'image-optimization' ) } |
| 90 |
</button> |
| 91 |
` : '' } |
| 92 |
|
| 93 |
<button type="button" |
| 94 |
class="button button-secondary image-optimization-control__button image-optimization-control__button--reoptimize"> |
| 95 |
${ __( 'Reoptimize', 'image-optimization' ) } |
| 96 |
</button> |
| 97 |
</div> |
| 98 |
`; |
| 99 |
}; |
| 100 |
|
| 101 |
const listViewTemplates = Object.freeze( { |
| 102 |
notOptimizedTemplate, |
| 103 |
loadingTemplate, |
| 104 |
errorTemplate, |
| 105 |
optimizedTemplate, |
| 106 |
} ); |
| 107 |
|
| 108 |
export default listViewTemplates; |
| 109 |
|