This example demonstrates how to use Connection Manager to post data to the server, filter it and return it to the Editor.
Setting up the Editor's HTML is done by creating a textarea
control on the page.
1 | <form method="post" action="#" id="form1"> |
2 | <textarea id="editor" name="editor" rows="20" cols="75"> |
3 | This is some more test text.<br>This is some more test text.<br>This is some more test text.<br> |
4 | This is some more test text.<br>This is some more test text.<br>This is some more test text. |
5 | </textarea> |
6 | </form> |
view plain | print | ? |
Once the textarea
is on the page, then initialize the Editor like this:
1 | (function() { |
2 | //Setup some private variables |
3 | var Dom = YAHOO.util.Dom, |
4 | Event = YAHOO.util.Event, |
5 | status = null; |
6 | |
7 | //The Editor config |
8 | var myConfig = { |
9 | height: '300px', |
10 | width: '600px', |
11 | animate: true, |
12 | dompath: true, |
13 | focusAtStart: true |
14 | }; |
15 | |
16 | //Now let's load the Editor.. |
17 | var myEditor = new YAHOO.widget.Editor('editor', myConfig); |
18 | myEditor.render(); |
19 | })(); |
view plain | print | ? |
We need to setup the callback object for Connection Manager. In the handleSuccess
function, we will eval
the data returned from the server.
Then we will call myEditor.setEditorHTML
with the new HTML.
1 | var handleSuccess = function(o) { |
2 | YAHOO.log('Post success', 'info', 'example'); |
3 | var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); |
4 | var data = eval('(' + json + ')'); |
5 | status.innerHTML = 'Status: ' + data.Results.status + |
6 | '<br>Filter: ' + data.Results.filter + '<br>' + (new Date().toString()); |
7 | myEditor.setEditorHTML(data.Results.data); |
8 | } |
9 | var handleFailure = function(o) { |
10 | YAHOO.log('Post failed', 'info', 'example'); |
11 | var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); |
12 | var data = eval('(' + json + ')'); |
13 | status.innerHTML = 'Status: ' + data.Results.status + '<br>'; |
14 | } |
15 | |
16 | var callback = { |
17 | success: handleSuccess, |
18 | failure: handleFailure |
19 | }; |
view plain | print | ? |
Using a Button control, we create the new Button and subscribe to the click
event to begin the transaction.
Before making the request, we need to pull the HTML from the editor by calling the myEditor.saveHTML
method. This will place the filtered HTML from the Editor back into the textarea.
Once we have that, we contruct our query string, and fire off the request.
Note: The request is wrapped in a short setTimeout
to allow the browser to finish the cleanup calls that the Editor is making.
1 | _button.on('click', function(ev) { |
2 | YAHOO.log('Button clicked, initiate transaction', 'info', 'example'); |
3 | Event.stopEvent(ev); |
4 | myEditor.saveHTML(); |
5 | window.setTimeout(function() { |
6 | var sUrl = "assets/post.php"; |
7 | var data = 'filter=' + ((Dom.get('filter').checked) ? 'yes' : 'no') + '&editor_data=' + encodeURIComponent(myEditor.get('textarea').value); |
8 | var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, data); |
9 | }, 200); |
10 | }); |
view plain | print | ? |
On the server side, first we filter the HTML to remove harmful HTML elements. Then we are doing a simple text filter (in this case the Elmer Fudd filter) and adding a "tagline" to the bottom of the data.
Once our filtering is complete, we are returning the new data to the browser.
1 | header('Content-type: application/json'); |
2 | |
3 | // Use Services_JSON |
4 | require_once('JSON.php'); |
5 | $json = new Services_JSON(); |
6 | |
7 | //Aggressive filtering... |
8 | $allow_tags = array( |
9 | 'b', |
10 | 'strong', |
11 | 'i', |
12 | 'em', |
13 | 'u', |
14 | 'a', |
15 | 'p', |
16 | 'sup', |
17 | 'sub', |
18 | 'div', |
19 | 'img', |
20 | 'span', |
21 | 'font', |
22 | 'br', |
23 | 'ul', |
24 | 'ol', |
25 | 'li' |
26 | ); |
27 | |
28 | $filter = $_POST['filter']; |
29 | $r_data = getRawEditorData('editor_data'); //Function defined in an include |
30 | $e_data = strip_tags($r_data, '<'.implode('><', $allow_tags).'>'); |
31 | |
32 | if ($filter == 'yes') { |
33 | // Replace the words: |
34 | $EditorData = fudd($e_data); //See full example code in the downloaded files.. |
35 | $EditorData .= '<br><br>--<br>Footer added on server side after filter'; |
36 | } else { |
37 | //Do some filtering here.. |
38 | $EditorData = $e_data; |
39 | } |
40 | |
41 | //Create the payload JSON object to deliver back to the browser.. |
42 | $data = new stdclass(); |
43 | $data->Results = new stdclass(); |
44 | $data->Results->raw_data = $r_data; |
45 | $data->Results->filter = $filter; |
46 | $data->Results->status = 'OK'; |
47 | $data->Results->data = $EditorData; |
48 | |
49 | echo($json->encode($data)); |
view plain | print | ? |
1 | (function() { |
2 | var Dom = YAHOO.util.Dom, |
3 | Event = YAHOO.util.Event, |
4 | status = null; |
5 | |
6 | var handleSuccess = function(o) { |
7 | YAHOO.log('Post success', 'info', 'example'); |
8 | var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); |
9 | var data = eval('(' + json + ')'); |
10 | status.innerHTML = 'Status: ' + data.Results.status + '<br>Filter: ' + data.Results.filter + '<br>' + (new Date().toString()); |
11 | myEditor.setEditorHTML(data.Results.data); |
12 | } |
13 | var handleFailure = function(o) { |
14 | YAHOO.log('Post failed', 'info', 'example'); |
15 | var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); |
16 | var data = eval('(' + json + ')'); |
17 | status.innerHTML = 'Status: ' + data.Results.status + '<br>'; |
18 | } |
19 | |
20 | var callback = { |
21 | success: handleSuccess, |
22 | failure: handleFailure |
23 | }; |
24 | |
25 | |
26 | YAHOO.log('Create Button Control (#toggleEditor)', 'info', 'example'); |
27 | var _button = new YAHOO.widget.Button('submitEditor'); |
28 | |
29 | var myConfig = { |
30 | height: '300px', |
31 | width: '600px', |
32 | animate: true, |
33 | dompath: true |
34 | }; |
35 | |
36 | YAHOO.log('Create the Editor..', 'info', 'example'); |
37 | var myEditor = new YAHOO.widget.Editor('editor', myConfig); |
38 | myEditor.render(); |
39 | |
40 | _button.on('click', function(ev) { |
41 | YAHOO.log('Button clicked, initiate transaction', 'info', 'example'); |
42 | Event.stopEvent(ev); |
43 | myEditor.saveHTML(); |
44 | window.setTimeout(function() { |
45 | var sUrl = "assets/post.php"; |
46 | var data = 'filter=' + ((Dom.get('filter').checked) ? 'yes' : 'no') + '&editor_data=' + encodeURIComponent(myEditor.get('textarea').value); |
47 | var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, data); |
48 | }, 200); |
49 | }); |
50 | |
51 | Event.onDOMReady(function() { |
52 | status = Dom.get('status'); |
53 | }); |
54 | })(); |
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 708ms (+3) 5:05:06 PM:
example
Create the Editor..
INFO 705ms (+704) 5:05:06 PM:
example
Create Button Control (#toggleEditor)
INFO 1ms (+1) 5:05:05 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