Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Commit

Permalink
Tested and got the extension working with Rowan's textbox field.
Browse files Browse the repository at this point in the history
Added the Preview link back to textareas without a formatter (just
returns the raw HTML anyway).
  • Loading branch information
tachyondecay committed Aug 16, 2011
1 parent c6abbcb commit ad45c94
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
52 changes: 21 additions & 31 deletions assets/preview_textarea.publish.js
@@ -1,27 +1,14 @@
/*
* Preview Textarea
*
* Grab value of a textarea and run it through that textarea's formatter,
* then display a live preview of the result.
*/

jQuery(document).ready(function($) {
/*
* Add a "Preview" link next to each textarea, as well as divs we'll be using to store stuff
*/
var ownerDocument = top.document;
$('body')
.append(
$(document.createElement('div')).attr('id','preview-textarea')
.append(
$(document.createElement('div')).attr('id','preview-textarea-title')
).append(
$(document.createElement('div')).attr('id','preview-textarea-content')
)
).append(
$(document.createElement('div')).attr('id','preview-textarea-overlay')
);

$('textarea[class!=""]').before(
$(document.createElement('a')).text('Preview').addClass('preview-textarea-button')
);
// Add a "Preview" link next to each textarea, as well as divs we'll be using to store stuff
$('body').append('<div id="preview-textarea"><div id="preview-textarea-title"></div><div id="preview-textarea-content"></div></div><div id="preview-textarea-overlay"></div>');
$('textarea').before('<a class="preview-textarea-button">Preview</a>');

$('a.preview-textarea-button').click(function(event) {
event.preventDefault();
Expand All @@ -30,6 +17,7 @@ jQuery(document).ready(function($) {
$(this).after('&#160;<strong>This textarea is empty!</strong>').next().delay(5000).fadeOut('slow');
}
else {
// Let's drop some AJAX.
$.ajax({
type: 'POST',
dataType: 'html',
Expand All @@ -43,23 +31,25 @@ jQuery(document).ready(function($) {
var windowWidth = $(window.top).width();

var fieldLabel = thePackage.parent().clone().children().remove().end().text();

$('#preview-textarea-title', ownerDocument).html('Preview of ' + fieldLabel + ' using the ' + $(xml).find("formatter").text() + ' formatter <a title="Close preview">Close</a>');
var formatter_name = ($(xml).find("formatter").text() == 'None') ? 'no formatter' : 'the <em>' + $(xml).find("formatter").text() + '</em> formatter';
$('#preview-textarea-title', top.document).html('Preview of <em>' + fieldLabel + '</em> using ' + formatter_name + ' <a title="Close preview">Close</a>');

$('#preview-textarea-content', ownerDocument).html(($(xml).find("preview").html()));
var previewTop = windowHeight/2 - $('#preview-textarea', ownerDocument).height()/2;
// Magic to determine the width and height of the preview box and the overlay
$('#preview-textarea-content', top.document).html(($(xml).find("preview").html()));
var previewTop = windowHeight/2 - $('#preview-textarea', top.document).height()/2;
if(previewTop < 0) { previewTop = 100; }
$('#preview-textarea', ownerDocument).css({
$('#preview-textarea', top.document).css({
'top': previewTop,
'left': windowWidth/2 - $('#preview-textarea', ownerDocument).width()/2
'left': windowWidth/2 - $('#preview-textarea', top.document).width()/2
});

var overlayHeight = ($(ownerDocument).height() > $('#preview-textarea', ownerDocument).height()) ? $(ownerDocument).height() : $('#preview-textarea', ownerDocument).height() + 200;
var overlayHeight = ($(top.document).height() > $('#preview-textarea', top.document).height()) ? $(top.document).height() : $('#preview-textarea', top.document).height() + 200;

$('#preview-textarea-overlay', ownerDocument).width(windowWidth).height(overlayHeight).fadeTo('fast',0.75);
$('#preview-textarea', ownerDocument).fadeIn('fast');
$('#preview-textarea-title a, #preview-textarea-overlay', ownerDocument).click(function() {
$('#preview-textarea, #preview-textarea-overlay', ownerDocument).fadeOut();
// Close the preview if the overlay or the "Close" anchor are clicked
$('#preview-textarea-overlay', top.document).width(windowWidth).height(overlayHeight).fadeTo('fast',0.75);
$('#preview-textarea', top.document).fadeIn('fast');
$('#preview-textarea-title a, #preview-textarea-overlay', top.document).click(function() {
$('#preview-textarea, #preview-textarea-overlay', top.document).fadeOut();
});
},
cache: false
Expand Down
27 changes: 19 additions & 8 deletions content/content.preview.php
Expand Up @@ -6,18 +6,29 @@
class contentExtensionPreview_textareaPreview extends AjaxPage {

public function view() {


// Determine which formatter to use
// The textarea keeps this info in its class attribute, but sometimes there might be additional classes.
// We'll grab one that matches the installed formatters.
$fM = new TextformatterManager($this);
$formatter = $fM->create($_POST['formatter']);
$formatter_about = $formatter->about();
$formatter_handle = array_pop(array_intersect(array_keys($fM->listAll()), explode(' ', $_POST['formatter'])));

// We pass the full formatter name back for use in the preview display
$format_name = new XMLElement('formatter');
$format_name->setValue($formatter_about['name']);
$this->_Result->appendChild($format_name);

$preview = new XMLElement('preview');
$preview->setValue($formatter->run($_POST['formatText']));

if(empty($formatter_handle)) {
$format_name->setValue('None');
$preview->setValue($_POST['formatText']);
}
else {
$formatter = $fM->create($formatter_handle);
$formatter_about = $formatter->about();

$format_name->setValue($formatter_about['name']);
$preview->setValue($formatter->run($_POST['formatText']));
}

$this->_Result->appendChild($format_name);
$this->_Result->appendChild($preview);
}

Expand Down
4 changes: 2 additions & 2 deletions extension.driver.php
Expand Up @@ -5,8 +5,8 @@
public function about() {
return array(
'name' => 'Preview Textarea',
'version' => '0.1',
'release-date' => '',
'version' => '1.0',
'release-date' => '2011-08-16',
'author' => array(
'name' => 'Ben Babcock',
'website' => 'http://tachyondecay.net/',
Expand Down

0 comments on commit ad45c94

Please sign in to comment.