| 1 |
var Designer = function ($, Option, events) { |
| 2 |
// vars |
| 3 |
var boxId = document.getElementById('post_ID').value || 0 |
| 4 |
var $editor |
| 5 |
var $editorFrame |
| 6 |
var $innerEditor |
| 7 |
var options = {} |
| 8 |
var visualEditorInitialised = false |
| 9 |
|
| 10 |
var $appearanceControls = $('#boxzilla-box-appearance-controls') |
| 11 |
|
| 12 |
// functions |
| 13 |
function init() { |
| 14 |
// Only run if TinyMCE has actually inited |
| 15 |
if (typeof (window.tinyMCE) !== 'object' || window.tinyMCE.get('content') === null) { |
| 16 |
return |
| 17 |
} |
| 18 |
|
| 19 |
// create Option objects |
| 20 |
options.borderColor = new Option('border-color') |
| 21 |
options.borderWidth = new Option('border-width') |
| 22 |
options.borderStyle = new Option('border-style') |
| 23 |
options.backgroundColor = new Option('background-color') |
| 24 |
options.width = new Option('box-width') |
| 25 |
options.color = new Option('color') |
| 26 |
|
| 27 |
// add classes to TinyMCE <html> |
| 28 |
$editorFrame = $('#content_ifr') |
| 29 |
$editor = $editorFrame.contents().find('html') |
| 30 |
$editor.css({ |
| 31 |
background: 'white' |
| 32 |
}) |
| 33 |
|
| 34 |
// add content class and padding to TinyMCE <body> |
| 35 |
$innerEditor = $editor.find('#tinymce') |
| 36 |
$innerEditor.addClass('boxzilla boxzilla-' + boxId) |
| 37 |
$innerEditor.css({ |
| 38 |
margin: 0, |
| 39 |
background: 'white', |
| 40 |
display: 'inline-block', |
| 41 |
width: 'auto', |
| 42 |
'min-width': '240px', |
| 43 |
position: 'relative' |
| 44 |
}) |
| 45 |
$innerEditor.get(0).style.cssText += ';padding: 25px !important;' |
| 46 |
|
| 47 |
visualEditorInitialised = true |
| 48 |
|
| 49 |
/* @since 2.0.3 */ |
| 50 |
events.trigger('editor.init') |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Applies the styles from the options to the TinyMCE Editor |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
function applyStyles() { |
| 59 |
if (!visualEditorInitialised) { |
| 60 |
return false |
| 61 |
} |
| 62 |
|
| 63 |
// Apply styles from CSS editor. |
| 64 |
// Use short timeout to make sure color values are updated. |
| 65 |
window.setTimeout(() => { |
| 66 |
$innerEditor.css({ |
| 67 |
'border-color': options.borderColor.getColorValue(), // getColorValue( 'borderColor', '' ), |
| 68 |
'border-width': options.borderWidth.getPxValue(), // getPxValue( 'borderWidth', '' ), |
| 69 |
'border-style': options.borderStyle.getValue(), // getValue('borderStyle', '' ), |
| 70 |
'background-color': options.backgroundColor.getColorValue(), // getColorValue( 'backgroundColor', ''), |
| 71 |
width: options.width.getPxValue(), // getPxValue( 'width', 'auto' ), |
| 72 |
color: options.color.getColorValue() // getColorValue( 'color', '' ) |
| 73 |
}) |
| 74 |
|
| 75 |
/* @since 2.0.3 */ |
| 76 |
events.trigger('editor.styles.apply') |
| 77 |
}, 10) |
| 78 |
|
| 79 |
return true |
| 80 |
} |
| 81 |
|
| 82 |
function resetStyles() { |
| 83 |
for (var key in options) { |
| 84 |
if (key.substring(0, 5) === 'theme') { |
| 85 |
continue |
| 86 |
} |
| 87 |
|
| 88 |
options[key].clear() |
| 89 |
} |
| 90 |
applyStyles() |
| 91 |
|
| 92 |
/* @since 2.0.3 */ |
| 93 |
events.trigger('editor.styles.reset') |
| 94 |
} |
| 95 |
|
| 96 |
// event binders |
| 97 |
$appearanceControls.find('input.boxzilla-color-field').wpColorPicker({ change: applyStyles, clear: applyStyles }) |
| 98 |
$appearanceControls.find(':input').not('.boxzilla-color-field').change(applyStyles) |
| 99 |
events.on('editor.init', applyStyles) |
| 100 |
|
| 101 |
// public methods |
| 102 |
return { |
| 103 |
init: init, |
| 104 |
resetStyles: resetStyles, |
| 105 |
options: options |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
module.exports = Designer |
| 110 |
|