This example will show how to open an "Image Browser" for the YUI Rich Text Editor.
Note: The "Image Browser" window will probably be blocked by your popup blocker.
Now, click on the "Insert Image" icon (the one outlined in blue) to see the "Image Browser" window.
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. <font face="Times New Roman">This is some more test text. |
4 | This is some more <b>test <i>text</i></b></font>. |
5 | This is some more test text. This is some more test text. |
6 | This is some more test text. This is some more test text. This is some more test text. |
7 | This is some more test text. |
8 | </textarea> |
9 | </form> |
view plain | print | ? |
Once the textarea
is on the page, then initialize the Editor like this:
1 | //The Editor config |
2 | var myConfig = { |
3 | height: '300px', |
4 | width: '600px', |
5 | animate: true, |
6 | dompath: true |
7 | }; |
8 | |
9 | //Now let's load the Editor. |
10 | var myEditor = new YAHOO.widget.Editor('editor', myConfig); |
view plain | print | ? |
To do this, we need start after the toolbar is loaded using the toolbarLoaded
event.
Now we use the Editor's _getSelectedElement
method to see if we have an image selected or not.
If we have an image selected, we don't do anything. But if we don't have one selected, we need to pop open the new window.
Note: the return false. This will stop the event from continuing, the Editor will not attempt to add a blank image.
1 | myEditor.on('toolbarLoaded', function() { |
2 | //When the toolbar is loaded, add a listener to the insertimage button |
3 | this.toolbar.on('insertimageClick', function() { |
4 | //Get the selected element |
5 | var _sel = this._getSelectedElement(); |
6 | //If the selected element is an image, do the normal thing so they can manipulate the image |
7 | if (_sel && _sel.tagName && (_sel.tagName.toLowerCase() == 'img')) { |
8 | //Do the normal thing here.. |
9 | } else { |
10 | //They don't have a selected image, open the image browser window |
11 | win = window.open('assets/browser.php', 'IMAGE_BROWSER', |
12 | 'left=20,top=20,width=500,height=500,toolbar=0,resizable=0,status=0'); |
13 | if (!win) { |
14 | //Catch the popup blocker |
15 | alert('Please disable your popup blocker!!'); |
16 | } |
17 | //This is important.. Return false here to not fire the rest of the listeners |
18 | return false; |
19 | } |
20 | }, this, true); |
21 | }, myEditor, true); |
view plain | print | ? |
From the popup window, we gain access to the Editor using the static
method YAHOO.widget.EditorInfo.getEditorById()
.
Calling it from window.opener
will give us a usable reference to the Editor in the other browser window.
How you get the image to display and how you allow the end user to select an image is up to your implementation. Here we are simply applying a click listener to the images parent container and getting a reference to the image from the event.
Once we have a reference or a URL to the image we want to insert, we simply call the Editors execCommand
for insert image and close the window.
1 | (function() { |
2 | var Dom = YAHOO.util.Dom, |
3 | Event = YAHOO.util.Event, |
4 | myEditor = window.opener.YAHOO.widget.EditorInfo.getEditorById('msgpost'); |
5 | //Get a reference to the editor on the other page |
6 | |
7 | //Add a listener to the parent of the images |
8 | Event.on('images', 'click', function(ev) { |
9 | var tar = Event.getTarget(ev); |
10 | //Check to see if we clicked on an image |
11 | if (tar && tar.tagName && (tar.tagName.toLowerCase() == 'img')) { |
12 | //Focus the editor's window |
13 | myEditor._focusWindow(); |
14 | //Fire the execCommand for insertimage |
15 | myEditor.execCommand('insertimage', tar.getAttribute('src', 2)); |
16 | //Close this window |
17 | window.close(); |
18 | } |
19 | }); |
20 | //Internet Explorer will throw this window to the back, this brings it to the front on load |
21 | Event.on(window, 'load', function() { |
22 | window.focus(); |
23 | }); |
24 | })(); |
view plain | print | ? |
This little piece of code will set the image url field in the Image Property Editor to disabled.
This will prevent the user from changing the images url.
1 | myEditor.on('afterOpenWindow', function() { |
2 | //When the window opens, disable the url of the image so they can't change it |
3 | var url = Dom.get('insertimage_url'); |
4 | if (url) { |
5 | url.disabled = true; |
6 | } |
7 | }, myEditor, true); |
view plain | print | ? |
1 | (function() { |
2 | var Dom = YAHOO.util.Dom, |
3 | Event = YAHOO.util.Event, |
4 | win = null; |
5 | |
6 | var myEditor = new YAHOO.widget.Editor('msgpost', { |
7 | height: '300px', |
8 | width: '522px', |
9 | dompath: true, //Turns on the bar at the bottom |
10 | animate: true //Animates the opening, closing and moving of Editor windows |
11 | }); |
12 | myEditor.on('toolbarLoaded', function() { |
13 | //When the toolbar is loaded, add a listener to the insertimage button |
14 | this.toolbar.on('insertimageClick', function() { |
15 | //Get the selected element |
16 | var _sel = this._getSelectedElement(); |
17 | //If the selected element is an image, do the normal thing so they can manipulate the image |
18 | if (_sel && _sel.tagName && (_sel.tagName.toLowerCase() == 'img')) { |
19 | //Do the normal thing here.. |
20 | } else { |
21 | //They don't have a selected image, open the image browser window |
22 | win = window.open('assets/browser.php', 'IMAGE_BROWSER', |
23 | 'left=20,top=20,width=500,height=500,toolbar=0,resizable=0,status=0'); |
24 | if (!win) { |
25 | //Catch the popup blocker |
26 | alert('Please disable your popup blocker!!'); |
27 | } |
28 | //This is important.. Return false here to not fire the rest of the listeners |
29 | return false; |
30 | } |
31 | }, this, true); |
32 | }, myEditor, true); |
33 | myEditor.on('afterOpenWindow', function() { |
34 | //When the window opens, disable the url of the image so they can't change it |
35 | var url = Dom.get('insertimage_url'); |
36 | if (url) { |
37 | url.disabled = true; |
38 | } |
39 | }, myEditor, true); |
40 | myEditor.render(); |
41 | |
42 | })(); |
view plain | print | ? |
1 | (function() { |
2 | var Dom = YAHOO.util.Dom, |
3 | Event = YAHOO.util.Event, |
4 | myEditor = window.opener.YAHOO.widget.EditorInfo.getEditorById('msgpost'); |
5 | //Get a reference to the editor on the other page |
6 | |
7 | //Add a listener to the parent of the images |
8 | Event.on('images', 'click', function(ev) { |
9 | var tar = Event.getTarget(ev); |
10 | //Check to see if we clicked on an image |
11 | if (tar && tar.tagName && (tar.tagName.toLowerCase() == 'img')) { |
12 | //Focus the editor's window |
13 | myEditor._focusWindow(); |
14 | //Fire the execCommand for insertimage |
15 | myEditor.execCommand('insertimage', tar.getAttribute('src', 2)); |
16 | //Close this window |
17 | window.close(); |
18 | } |
19 | }); |
20 | })(); |
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:33:57 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