YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Rich Text Editor: Editor in a Dialog Control

# Rich Text Editor: Editor in a Dialog Control

In YUI-based applications, it's common for forms to appear as part of Dialogs. In this example, a form enhanced with the Rich Text Editor appears in a Dialog. Read the tutorial below to learn how to integrate the RTE seamlessly with your Dialog's form contents.

View example in new window.

Enter Title and Description:

Title:

Description:

Dialog's post response will appear here after you submit the Dialog.

## Using Rich Text Editor in a Dialog Control

When using a Rich Text Editor in a Dialog Control, there are a few considerations:

- The editor and the elements it creates (particularly the floating properties palettes) need to be rendered into the Dialog's DOM structure. If the Dialog is modal, it will try to maintain focus within its DOM confines, so ensuring that all editor elements are children of the Dialog's DOM is crucial.
- The most common way to submit a Dialog's form contents is asynchronously using Connection Manager. This means that editor cannot automatically attach itself to the form's `submit` event. Instead, we need to call the editor's `saveHTML` method prior to executing the Dialog's `submit` method.
- The Rich Text Editor needs special handling when it is hidden (ie, when a parent element is set to `display:none`). Dialogs, of course, are usually hidden by default and appear only in response to user action. As a result, we need to listen for the Dialog's `showEvent` and `hideEvent` and prepare the Rich Text Editor to recover gracefully when the Dialog is hidden from view.

These three considerations are each accounted for in the code below, which is fully commented and represents the full JavaScript source for this implementation:

(function() { //hide and disable the non-dialog submit button: document.getElementById("submitButton").disabled = true; document.getElementById("submitButton").style.display = "none"; //create the RTE: var editor = new YAHOO.widget.Editor('description', { width: '702px', height: '200px' }); //After the Editor renders it, we will hide it //so the iframe doesn't bleed through editor.on('afterRender', editor.hide); //render the editor explicitly into a container //within the Dialog's DOM: editor.render(); //create Dialog: var dlg = new YAHOO.widget.Dialog("dialogContainer", { width:"725px", fixedcenter:true, modal:true, visible:false }); //event handlers for our Dialog buttons: //if the user clicks "save", then we save the HTML //content of the RTE and submit the dialog: function handleSave() { editor.saveHTML(); this.submit(); } //if the user clicks cancel, we call Dialog's //cancel method: function handleCancel() { this.cancel(); } //set up buttons for the Dialog and wire them //up to our handlers: var myButtons = [ { text:"Save", handler:handleSave }, { text:"Cancel", handler:handleCancel, isDefault:true } ]; dlg.cfg.queueProperty("buttons", myButtons); //Dialog by default will use Connection Manager to POST //form contents to the URI specified in the action //attribute of the form; we can wire up success and //failure handlers for the XHR call and act on them //just as we would with any Connection Manager //transaction: var onSuccess = function(o) { //we're going to get JSON back from post.php; we //can parse it using JSON.parse: var data = YAHOO.lang.JSON.parse(o.responseText); //in this case, we'll just output the contents to //a div to see what they contain: document.getElementById("container").innerHTML = 'Status: ' + data.Results.status + '<br>' + (new Date().toString()); } var onFailure = function(o) { //in the event of a failure, we can log the problem: YAHOO.log("Dialog reported a communication failure; connection object: " + YAHOO.lang.dump(o, 5)); } dlg.callback.success = onSuccess; dlg.callback.failure = onFailure; //Now that our Dialog is fully configured, we can //render it: dlg.render(); //RTE needs a little love to work in in a Dialog that can be //shown and hidden; we let it know that it's being //shown/hidden so that it can recover from these actions: dlg.showEvent.subscribe(editor.show, editor, true); dlg.hideEvent.subscribe(editor.hide, editor, true); //instantiate button to show Dialog: var btn = new YAHOO.widget.Button("showDlg", {type:"link"}); btn.on("click", dlg.show, dlg, true); })();

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Rich Text Editor Examples:

- Flickr Image Search
- Calendar Plugin
- Icon Insertion
- Plain Text Switcher
- Editor Data Post with Connection Manager
- Skinning the Editor
- Default Toolbar Config
- Code Editor
- Editor inside a Tabview Control
- One Editor, Multiple Edit Areas
- Editor with Custom Image Browser
- Simple Editor — Basic Buttons
- Simple Editor — Advanced Buttons
- Editor — Basic Buttons
- Editor Auto Adjusting Height
- Editor in a Dialog Control
- Custom resizing for the Rich Text Editor (included with examples for Resize Utility)
- Simple Application (included with examples for Layout Manager)
- Complex Application (included with examples for Layout Manager)

### More Rich Text Editor Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings