| 1 |
import '../../css/admin/meta-box.scss'; |
| 2 |
|
| 3 |
import { __wpupg } from 'Shared/Translations'; |
| 4 |
|
| 5 |
const metaBox = { |
| 6 |
elems: {}, |
| 7 |
initPost( container ) { |
| 8 |
this.elems = { |
| 9 |
container, |
| 10 |
imageButton: container.querySelector( '#wpupg_add_custom_image' ), |
| 11 |
imageRemoveButton: false, |
| 12 |
imageUrl: container.querySelector( '#wpupg_custom_image' ), |
| 13 |
imageId: container.querySelector( '#wpupg_custom_image_id' ), |
| 14 |
imagePreview: false, |
| 15 |
}; |
| 16 |
|
| 17 |
this.addEventListeners(); |
| 18 |
}, |
| 19 |
initTerm( container ) { |
| 20 |
this.elems = { |
| 21 |
container, |
| 22 |
imageButton: container.querySelector( '#wpupg_add_custom_image' ), |
| 23 |
imageRemoveButton: container.querySelector( '#wpupg_remove_custom_image' ), |
| 24 |
imageUrl: container.querySelector( '#wpupg_custom_image_url' ), |
| 25 |
imageId: container.querySelector( '#wpupg_custom_image' ), |
| 26 |
imagePreview: container.querySelector( '#wpupg_custom_image_img' ), |
| 27 |
}; |
| 28 |
|
| 29 |
this.addEventListeners(); |
| 30 |
}, |
| 31 |
addEventListeners() { |
| 32 |
// Open media library when clicking on button. |
| 33 |
this.elems.imageButton.addEventListener( 'click', (e) => { |
| 34 |
e.preventDefault(); |
| 35 |
|
| 36 |
if ( typeof wp.media == 'function' ) { |
| 37 |
const custom_uploader = wp.media({ |
| 38 |
title: __wpupg( 'Insert Media' ), |
| 39 |
button: { |
| 40 |
text: __wpupg( 'Set Custom Image' ), |
| 41 |
}, |
| 42 |
multiple: false |
| 43 |
}); |
| 44 |
|
| 45 |
custom_uploader.on('select', () => { |
| 46 |
const attachment = custom_uploader.state().get('selection').first().toJSON(); |
| 47 |
this.elems.imageUrl.value = attachment.url; |
| 48 |
this.elems.imageId.value = attachment.id; |
| 49 |
|
| 50 |
if ( this.elems.imagePreview ) { |
| 51 |
this.elems.imagePreview.src = attachment.url; |
| 52 |
} |
| 53 |
if ( this.elems.imageRemoveButton ) { |
| 54 |
this.elems.imageButton.style.display = 'none'; |
| 55 |
this.elems.imageRemoveButton.style.display = 'block'; |
| 56 |
} |
| 57 |
}).open(); |
| 58 |
} |
| 59 |
}); |
| 60 |
|
| 61 |
if ( this.elems.imageRemoveButton ) { |
| 62 |
this.elems.imageRemoveButton.addEventListener( 'click', (e) => { |
| 63 |
this.elems.imageId.value = ''; |
| 64 |
this.elems.imageRemoveButton.style.display = 'none'; |
| 65 |
this.elems.imageButton.style.display = 'block'; |
| 66 |
|
| 67 |
if ( this.elems.imagePreview ) { |
| 68 |
this.elems.imagePreview.src = ''; |
| 69 |
} |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
// Clear image ID if URL gets changed. |
| 74 |
this.elems.imageUrl.addEventListener( 'keyup', () => { |
| 75 |
this.elems.imageId.value = ''; |
| 76 |
} ); |
| 77 |
this.elems.imageUrl.addEventListener( 'change', () => { |
| 78 |
this.elems.imageId.value = ''; |
| 79 |
} ); |
| 80 |
}, |
| 81 |
} |
| 82 |
|
| 83 |
// Init meta box we can find one when the page is ready. |
| 84 |
ready(() => { |
| 85 |
const postMetaBoxContainer = document.querySelector( '#wpupg_meta_box_post' ); |
| 86 |
|
| 87 |
if ( postMetaBoxContainer ) { |
| 88 |
metaBox.initPost( postMetaBoxContainer ); |
| 89 |
} |
| 90 |
|
| 91 |
const termMetaBoxContainer = document.querySelector( '#wpupg_meta_box_term' ); |
| 92 |
|
| 93 |
if ( termMetaBoxContainer ) { |
| 94 |
metaBox.initTerm( termMetaBoxContainer ); |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
// Source: http://youmightnotneedjquery.com/#ready |
| 99 |
function ready( fn ) { |
| 100 |
if (document.readyState != 'loading'){ |
| 101 |
fn(); |
| 102 |
} else { |
| 103 |
document.addEventListener('DOMContentLoaded', fn); |
| 104 |
} |
| 105 |
} |