| 1 |
class Modal |
| 2 |
{ |
| 3 |
constructor() { |
| 4 |
this.triggers = document.querySelectorAll('[data-modal]'); |
| 5 |
this.clearButtons = document.querySelectorAll('.ag-media-clear'); |
| 6 |
this.listeners(); |
| 7 |
} |
| 8 |
|
| 9 |
listeners = () => { |
| 10 |
this.triggers.forEach(el => { |
| 11 |
el.addEventListener('click', this.openModal, false); |
| 12 |
}); |
| 13 |
|
| 14 |
this.clearButtons.forEach(el => { |
| 15 |
el.addEventListener('click', this.clear, false); |
| 16 |
}); |
| 17 |
} |
| 18 |
|
| 19 |
clear = (e) => { |
| 20 |
const root = e.currentTarget.parentNode; |
| 21 |
root.querySelector('input').value = ''; |
| 22 |
root.querySelector('.ag-preview').innerHTML = ''; |
| 23 |
|
| 24 |
// Dispatch it. |
| 25 |
root.querySelector('input').dispatchEvent(new Event('change')); |
| 26 |
} |
| 27 |
|
| 28 |
openModal = (e) => { |
| 29 |
let file_frame = void 0; |
| 30 |
|
| 31 |
const root = e.currentTarget.parentNode; |
| 32 |
const types = root.querySelector('input').name.match(/logo/) ? ['image'] : ['image', 'video']; |
| 33 |
const selected = root.querySelector('input').value; |
| 34 |
const preview = root.querySelector('.ag-preview'); |
| 35 |
|
| 36 |
if (typeof wp !== 'undefined' && wp.media && wp.media.editor) { |
| 37 |
if (file_frame) { |
| 38 |
file_frame.uploader.param('post_id', selected); |
| 39 |
file_frame.open(); |
| 40 |
return; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
file_frame = wp.media.frames.select_image = wp.media({ |
| 47 |
title: 'Select image', |
| 48 |
button: { |
| 49 |
text: 'Add image', |
| 50 |
}, |
| 51 |
library: { |
| 52 |
type: types, |
| 53 |
}, |
| 54 |
multiple: false, |
| 55 |
post_id: selected, |
| 56 |
}); |
| 57 |
|
| 58 |
wp.media.model.settings.post.id = parseInt(selected); |
| 59 |
|
| 60 |
console.log(wp.media.model.settings.post); |
| 61 |
|
| 62 |
file_frame.on('select', () => { |
| 63 |
const image = file_frame.state().get('selection').first().toJSON(); |
| 64 |
|
| 65 |
const { |
| 66 |
url, |
| 67 |
id, |
| 68 |
mime, |
| 69 |
} = image; |
| 70 |
|
| 71 |
preview.innerHTML = ''; |
| 72 |
root.querySelector('input').value = id; |
| 73 |
root.querySelector('input').dispatchEvent(new Event('change')); |
| 74 |
const img = document.createElement(mime.match(/video/) ? 'video' : 'img'); |
| 75 |
|
| 76 |
img.src = url; |
| 77 |
|
| 78 |
preview.appendChild(img); |
| 79 |
}); |
| 80 |
|
| 81 |
file_frame.open(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
export default Modal; |
| 86 |
|