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.
Dialog's post response will appear here after you submit the Dialog.
When using a Rich Text Editor in a Dialog Control, there are a few considerations:
submit
event. Instead, we need to call the editor's saveHTML
method prior to executing the Dialog's submit
method.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 | ? |
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.
INFO 0ms (+0) 5:14:53 PM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings