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