YUI Library Home

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.

Show Dialog

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:

1(function() { 
2 
3    //hide and disable the non-dialog submit button: 
4    document.getElementById("submitButton").disabled = true
5    document.getElementById("submitButton").style.display = "none"
6     
7    //create the RTE: 
8    var editor = new YAHOO.widget.Editor('description', { 
9        width: '702px'
10        height: '200px' 
11    }); 
12 
13    //After the Editor renders it, we will hide it 
14    //so the iframe doesn't bleed through 
15    editor.on('afterRender', editor.hide); 
16 
17    //render the editor explicitly into a container 
18    //within the Dialog's DOM: 
19    editor.render(); 
20     
21    //create Dialog: 
22    var dlg = new YAHOO.widget.Dialog("dialogContainer", { 
23        width:"725px"
24        fixedcenter:true
25        modal:true
26        visible:false 
27    }); 
28 
29    //event handlers for our Dialog buttons: 
30     
31    //if the user clicks "save", then we save the HTML 
32    //content of the RTE and submit the dialog: 
33    function handleSave() { 
34        editor.saveHTML(); 
35        this.submit(); 
36    } 
37     
38    //if the user clicks cancel, we call Dialog's 
39    //cancel method: 
40    function handleCancel() { 
41        this.cancel(); 
42    } 
43     
44    //set up buttons for the Dialog and wire them 
45    //up to our handlers: 
46    var myButtons = [ { text:"Save",  
47                        handler:handleSave }, 
48                      { text:"Cancel",  
49                        handler:handleCancel, 
50                        isDefault:true } ]; 
51    dlg.cfg.queueProperty("buttons", myButtons); 
52 
53    //Dialog by default will use Connection Manager to POST 
54    //form contents to the URI specified in the action 
55    //attribute of the form; we can wire up success and 
56    //failure handlers for the XHR call and act on them 
57    //just as we would with any Connection Manager 
58    //transaction: 
59    var onSuccess = function(o) { 
60        //we're going to get JSON back from post.php; we 
61        //can parse it using JSON.parse: 
62        var data = YAHOO.lang.JSON.parse(o.responseText); 
63         
64        //in this case, we'll just output the contents to  
65        //a div to see what they contain: 
66        document.getElementById("container").innerHTML = 'Status: ' +  
67            data.Results.status +  
68            '<br>' + (new Date().toString()); 
69    } 
70    var onFailure = function(o) { 
71        //in the event of a failure, we can log the problem: 
72        YAHOO.log("Dialog reported a communication failure; connection object: " + YAHOO.lang.dump(o, 5)); 
73    } 
74    dlg.callback.success = onSuccess; 
75    dlg.callback.failure = onFailure; 
76     
77    //Now that our Dialog is fully configured, we can 
78    //render it: 
79    dlg.render(); 
80     
81    //RTE needs a little love to work in in a Dialog that can be  
82    //shown and hidden; we let it know that it's being 
83    //shown/hidden so that it can recover from these actions: 
84    dlg.showEvent.subscribe(editor.show, editor, true); 
85    dlg.hideEvent.subscribe(editor.hide, editor, true); 
86     
87    //instantiate button to show Dialog: 
88    var btn = new YAHOO.widget.Button("showDlg", {type:"link"}); 
89    btn.on("click", dlg.show, dlg, true); 
90     
91})(); 
view plain | print | ?

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.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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