| 1 |
import React, { Component } from 'react'; |
| 2 |
import Modal from 'react-modal'; |
| 3 |
|
| 4 |
// Only if modal is on page. |
| 5 |
if ( document.getElementById( 'wpupg-admin-modal' ) ) { |
| 6 |
Modal.setAppElement( '#wpupg-admin-modal' ); |
| 7 |
} |
| 8 |
|
| 9 |
import '../../css/admin/modal/app.scss'; |
| 10 |
import '../../css/admin/modal/general/fields.scss'; |
| 11 |
|
| 12 |
import ErrorBoundary from 'Shared/ErrorBoundary'; |
| 13 |
import { __wpupg } from 'Shared/Translations'; |
| 14 |
const { hooks } = WPUltimatePostGrid.shared; |
| 15 |
|
| 16 |
import Grid from './grid'; |
| 17 |
import Menu from './menu'; |
| 18 |
import Select from './select'; |
| 19 |
|
| 20 |
const contentBlocks = { |
| 21 |
edit: Grid, |
| 22 |
create: Grid, |
| 23 |
menu: Menu, |
| 24 |
select: Select, |
| 25 |
}; |
| 26 |
|
| 27 |
export default class App extends Component { |
| 28 |
constructor() { |
| 29 |
super(); |
| 30 |
|
| 31 |
this.state = { |
| 32 |
modalIsOpen: false, |
| 33 |
mode: '', |
| 34 |
args: {}, |
| 35 |
}; |
| 36 |
|
| 37 |
this.content = React.createRef(); |
| 38 |
|
| 39 |
this.close = this.close.bind(this); |
| 40 |
this.closeIfAllowed = this.closeIfAllowed.bind(this); |
| 41 |
} |
| 42 |
|
| 43 |
open( mode, args = {}, forceOpen = false ) { |
| 44 |
if ( forceOpen || ! this.state.modalIsOpen ) { |
| 45 |
this.setState({ |
| 46 |
modalIsOpen: true, |
| 47 |
mode, |
| 48 |
args, |
| 49 |
}, () => { |
| 50 |
window.onbeforeunload = () => __wpupg( 'Are you sure you want to leave this page?' ); |
| 51 |
}); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
close(callback = false) { |
| 56 |
this.setState({ |
| 57 |
modalIsOpen: false, |
| 58 |
}, () => { |
| 59 |
window.onbeforeunload = null; |
| 60 |
if ( 'function' === typeof callback ) { |
| 61 |
callback(); |
| 62 |
} |
| 63 |
}); |
| 64 |
} |
| 65 |
|
| 66 |
closeIfAllowed(callback = false) { |
| 67 |
const checkFunction = this.content.current && this.content.current.hasOwnProperty( 'allowCloseModal' ) ? this.content.current.allowCloseModal : false; |
| 68 |
|
| 69 |
if ( ! checkFunction || checkFunction() ) { |
| 70 |
this.close(callback); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
addTextToEditor( text, editorId ) { |
| 75 |
if (typeof tinyMCE == 'undefined' || !tinyMCE.get(editorId) || tinyMCE.get(editorId).isHidden()) { |
| 76 |
var current = jQuery('textarea#' + editorId).val(); |
| 77 |
jQuery('textarea#' + editorId).val(current + text); |
| 78 |
} else { |
| 79 |
tinyMCE.get(editorId).focus(true); |
| 80 |
tinyMCE.activeEditor.selection.collapse(false); |
| 81 |
tinyMCE.activeEditor.execCommand('mceInsertContent', false, text); |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
refreshEditor( editorId ) { |
| 86 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.get(editorId) && !tinyMCE.get(editorId).isHidden() ) { |
| 87 |
tinyMCE.get(editorId).focus(true); |
| 88 |
tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent()); |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
render() { |
| 93 |
let allContentBlocks = hooks.applyFilters( 'modal', contentBlocks ); |
| 94 |
const Content = allContentBlocks.hasOwnProperty(this.state.mode) ? allContentBlocks[this.state.mode] : false; |
| 95 |
|
| 96 |
if ( ! Content ) { |
| 97 |
return null; |
| 98 |
} |
| 99 |
|
| 100 |
return ( |
| 101 |
<Modal |
| 102 |
isOpen={ this.state.modalIsOpen } |
| 103 |
onRequestClose={ this.closeIfAllowed } |
| 104 |
overlayClassName="wpupg-admin-modal-overlay" |
| 105 |
className={`wpupg-admin-modal wpupg-admin-modal-${this.state.mode}`} |
| 106 |
> |
| 107 |
<ErrorBoundary module="Modal"> |
| 108 |
<Content |
| 109 |
ref={ this.content } |
| 110 |
mode={ this.state.mode } |
| 111 |
args={ this.state.args } |
| 112 |
maybeCloseModal={ this.closeIfAllowed } |
| 113 |
/> |
| 114 |
</ErrorBoundary> |
| 115 |
</Modal> |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|