| 1 |
( function( $ ) { |
| 2 |
$( '.lp-metabox-field__email-content' ).each( function() { |
| 3 |
const $field = $( this ), |
| 4 |
$select = $field.find( 'select.lp-email-format' ), |
| 5 |
$templates = $field.find( '.learn-press-email-template' ), |
| 6 |
$variables = $field.find( '.learn-press-email-variables' ); |
| 7 |
|
| 8 |
function _insertVariableToEditor( edId, variable ) { |
| 9 |
let editorId = null; |
| 10 |
const activeEditor = tinyMCE.activeEditor; |
| 11 |
|
| 12 |
for ( editorId in tinyMCE.editors ) { |
| 13 |
if ( editorId == edId ) { |
| 14 |
break; |
| 15 |
} |
| 16 |
editorId = null; |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! editorId ) { |
| 20 |
_insertVariableToTextarea( edId, variable ); |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
if ( activeEditor && $( activeEditor.getElement() ).attr( 'id' ) == editorId ) { |
| 25 |
activeEditor.execCommand( 'insertHTML', false, variable ); |
| 26 |
|
| 27 |
if ( $( activeEditor.getElement() ).is( ':visible' ) ) { |
| 28 |
_insertVariableToTextarea( edId, variable ); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
function _insertVariableToTextarea( eId, varibale ) { |
| 34 |
const $el = $( '#' + eId ).get( 0 ); |
| 35 |
|
| 36 |
if ( document.selection ) { |
| 37 |
$el.focus(); |
| 38 |
sel = document.selection.createRange(); |
| 39 |
sel.text = varibale; |
| 40 |
$el.focus(); |
| 41 |
} else if ( $el.selectionStart || $el.selectionStart == '0' ) { |
| 42 |
const startPos = $el.selectionStart; |
| 43 |
const endPos = $el.selectionEnd; |
| 44 |
const scrollTop = $el.scrollTop; |
| 45 |
$el.value = $el.value.substring( 0, startPos ) + varibale + $el.value.substring( endPos, $el.value.length ); |
| 46 |
$el.focus(); |
| 47 |
$el.selectionStart = startPos + varibale.length; |
| 48 |
$el.selectionEnd = startPos + varibale.length; |
| 49 |
$el.scrollTop = scrollTop; |
| 50 |
} else { |
| 51 |
$el.value += varibale; |
| 52 |
$el.focus(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
$select.on( 'change', function() { |
| 57 |
if ( this.value ) { |
| 58 |
$templates.filter( '.' + this.value ).removeClass( 'hide-if-js' ).siblings().addClass( 'hide-if-js' ); |
| 59 |
} |
| 60 |
} ).trigger( 'change' ); |
| 61 |
|
| 62 |
$variables.each( function() { |
| 63 |
const $list = $( this ), |
| 64 |
hasEditor = $list.hasClass( 'has-editor' ); |
| 65 |
|
| 66 |
$list.on( 'click', 'li', function() { |
| 67 |
if ( hasEditor ) { |
| 68 |
_insertVariableToEditor( $list.attr( 'data-target' ), $( this ).data( 'variable' ) ); |
| 69 |
} else { |
| 70 |
_insertVariableToTextarea( $list.attr( 'data-target' ), $( this ).data( 'variable' ) ); |
| 71 |
} |
| 72 |
} ); |
| 73 |
} ); |
| 74 |
} ); |
| 75 |
}( jQuery ) ); |
| 76 |
|