| 1 |
document.addEventListener('DOMContentLoaded', () => { |
| 2 |
window.PhotonicWPNativeUI = function(fnSendDataToWizard, fnGetDataFromWizard) { |
| 3 |
let messageChannel, mediaLibrary; |
| 4 |
const wpEditor = document.getElementById('wp-content-wrap'), |
| 5 |
mceTab = document.getElementById('content-tmce'), |
| 6 |
htmlTab = document.getElementById('content-html'); |
| 7 |
let lastClickedTab = wpEditor ? (wpEditor.classList.contains('tmce-active') ? mceTab : htmlTab) : htmlTab; |
| 8 |
|
| 9 |
const initializeMediaLibrary = function(mediaOptions, photonicOptions) { |
| 10 |
mediaLibrary = top.wp.media(mediaOptions); |
| 11 |
|
| 12 |
mediaLibrary.on('select', () => { |
| 13 |
let selection = mediaLibrary.state().get('selection'); |
| 14 |
let selected_data = ''; |
| 15 |
selection.map(function (attachment) { |
| 16 |
attachment = attachment.toJSON(); |
| 17 |
selected_data += attachment.id + ','; |
| 18 |
}); |
| 19 |
selected_data = selected_data.replace(/^,+|,+$/g, ''); |
| 20 |
|
| 21 |
messageChannel.port1.postMessage({ |
| 22 |
type: 'photonicReceiveMediaLibrarySelections', |
| 23 |
selection: selected_data, |
| 24 |
options: photonicOptions |
| 25 |
}); |
| 26 |
}); |
| 27 |
|
| 28 |
mediaLibrary.on('open', () => { |
| 29 |
const selection = mediaLibrary.state().get('selection'); |
| 30 |
let ids = photonicOptions.selectedIds; |
| 31 |
const shortcodeTag = photonicOptions.shortcodeTag; |
| 32 |
const isBlock = photonicOptions.isBlock; |
| 33 |
|
| 34 |
let editor_selection = photonicOptions.currentShortcode; |
| 35 |
let shortcode, attrs, win = window.dialogArguments || opener || parent || top; |
| 36 |
if (!isBlock && editor_selection && editor_selection !== '') { |
| 37 |
shortcode = top.wp.shortcode.next(shortcodeTag, editor_selection); |
| 38 |
attrs = shortcode.shortcode.attrs.named; |
| 39 |
} |
| 40 |
else if (isBlock) { // Gutenberg |
| 41 |
shortcode = photonicOptions.currentShortcode; |
| 42 |
if (shortcode && shortcode !== '') { |
| 43 |
attrs = JSON.parse(shortcode); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if (ids === '' && attrs !== undefined) { |
| 48 |
if (attrs.ids !== undefined) { |
| 49 |
ids = attrs.ids; |
| 50 |
} |
| 51 |
else if (attrs.include !== undefined) { |
| 52 |
ids = attrs.include; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
ids = ids.split(','); |
| 58 |
ids.forEach(function (id) { |
| 59 |
const attachment = top.wp.media.attachment(id); |
| 60 |
attachment.fetch(); |
| 61 |
selection.add(attachment ? [attachment] : []); |
| 62 |
}); |
| 63 |
}); |
| 64 |
} |
| 65 |
|
| 66 |
const open = function() { |
| 67 |
if (mediaLibrary) { |
| 68 |
mediaLibrary.open(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
const closeTB = function() { |
| 73 |
if (typeof tb_close === 'function') { |
| 74 |
tb_close(); |
| 75 |
} |
| 76 |
else { |
| 77 |
if (document.getElementById('TB_window')) { |
| 78 |
document.getElementById('TB_window').remove(); |
| 79 |
} |
| 80 |
if (document.getElementById('TB_overlay')) { |
| 81 |
document.getElementById('TB_overlay').remove(); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
const waitForIFrame = function() { |
| 87 |
const observer = new MutationObserver(() => { |
| 88 |
let iframe; |
| 89 |
iframe = document.querySelector('#TB_iframeContent'); |
| 90 |
if (iframe) { |
| 91 |
iframe.onload = () => { |
| 92 |
messageChannel = new MessageChannel(); |
| 93 |
// nativeMediaLibrary = new PhotonicWPNativeUI(sourceMessageChannel); |
| 94 |
|
| 95 |
messageChannel.port1.onmessage = (event) => { |
| 96 |
if (event.data.type === 'photonicAddTBClass') { |
| 97 |
document.getElementById('TB_window').classList.add('photonic-tb'); |
| 98 |
} |
| 99 |
else if (event.data.type === 'photonicInitializeMediaLibrary') { |
| 100 |
initializeMediaLibrary(event.data.mediaOptions, event.data.photonicOptions); |
| 101 |
} |
| 102 |
else if (event.data.type === 'photonicOpenMediaLibrary') { |
| 103 |
open(); |
| 104 |
} |
| 105 |
else if (event.data.type === 'photonicUpdateGallery') { |
| 106 |
fnGetDataFromWizard(event.data); |
| 107 |
closeTB(); |
| 108 |
} |
| 109 |
return true; |
| 110 |
}; |
| 111 |
|
| 112 |
// First, send a placeholder message to the iFrame and transfer port2 to it |
| 113 |
iframe.contentWindow.postMessage('init', '/', [messageChannel.port2]); |
| 114 |
|
| 115 |
// Second, send the actual shortcode |
| 116 |
fnSendDataToWizard(messageChannel); |
| 117 |
}; |
| 118 |
} |
| 119 |
}); |
| 120 |
|
| 121 |
observer.observe(document.body, { |
| 122 |
childList: true, |
| 123 |
subtree: true |
| 124 |
}); |
| 125 |
}; |
| 126 |
|
| 127 |
return { |
| 128 |
waitForIFrame: waitForIFrame |
| 129 |
}; |
| 130 |
} |
| 131 |
}); |
| 132 |
|