YUI recommends YUI3.
YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.
This documentation is no longer maintained.
This example demonstrates how to have one Editor instance control different editable areas on a page.
First we place a textarea on the page and set it to visibility hidden, then create the editable areas and assign them a class of editable
By using the .yui-editor-container
class, we are setting the Editor to an absolute position of -9999px top and -9999px left to render it off of the screen.
1 | .yui-editor-container { |
2 | position: absolute; |
3 | top: -9999px; |
4 | left: -9999px; |
5 | z-index: 999; /* So Safari behaves */ |
6 | } |
7 | #editor { |
8 | visibility: hidden; |
9 | position: absolute; |
10 | } |
11 | .editable { |
12 | border: 1px solid black; |
13 | margin: .25em; |
14 | float: left; |
15 | width: 350px; |
16 | height: 100px; |
17 | } |
view plain | print | ? |
1 | <textarea id="editor"></textarea> |
2 | |
3 | <div id="editable_cont"> |
4 | <div class="editable">#1. Double click me to edit the contents</div> |
5 | <div class="editable">#2. Double click me to edit the contents</div> |
6 | <div class="editable">#3. Double click me to edit the contents</div> |
7 | <div class="editable">#4. Double click me to edit the contents</div> |
8 | <div class="editable">#5. Double click me to edit the contents</div> |
9 | <div class="editable">#6. Double click me to edit the contents</div> |
10 | </div> |
view plain | print | ? |
First we are going to setup the editor with a smaller toolbar.
1 | (function() { |
2 | var Dom = YAHOO.util.Dom, |
3 | Event = YAHOO.util.Event, |
4 | editing = null; |
5 | |
6 | YAHOO.log('Setup a stripped down config for the editor', 'info', 'example'); |
7 | var myConfig = { |
8 | height: '150px', |
9 | width: '380px', |
10 | animate: true, |
11 | limitCommands: true, |
12 | toolbar: { |
13 | titlebar: 'My Editor', |
14 | collapse: true, |
15 | buttons: [ |
16 | { group: 'textstyle', label: 'Font Style', |
17 | buttons: [ |
18 | { type: 'push', label: 'Bold', value: 'bold' }, |
19 | { type: 'push', label: 'Italic', value: 'italic' }, |
20 | { type: 'push', label: 'Underline', value: 'underline' }, |
21 | { type: 'separator' }, |
22 | { type: 'select', label: 'Arial', value: 'fontname', disabled: true, |
23 | menu: [ |
24 | { text: 'Arial', checked: true }, |
25 | { text: 'Arial Black' }, |
26 | { text: 'Comic Sans MS' }, |
27 | { text: 'Courier New' }, |
28 | { text: 'Lucida Console' }, |
29 | { text: 'Tahoma' }, |
30 | { text: 'Times New Roman' }, |
31 | { text: 'Trebuchet MS' }, |
32 | { text: 'Verdana' } |
33 | ] |
34 | }, |
35 | { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true }, |
36 | { type: 'separator' }, |
37 | { type: 'color', label: 'Font Color', value: 'forecolor', disabled: true }, |
38 | { type: 'color', label: 'Background Color', value: 'backcolor', disabled: true } |
39 | ] |
40 | } |
41 | ] |
42 | } |
43 | }; |
44 | |
45 | YAHOO.log('Create the Editor..', 'info', 'example'); |
46 | myEditor = new YAHOO.widget.Editor('editor', myConfig); |
47 | myEditor.render(); |
48 | })(); |
view plain | print | ? |
Now that we have the Editor rendered and positioned off of the screen, we need to be able to show it when you double click on an editable area.
We add a dblclick
listener to the container editable_cont
.
Inside of this listener, we grab the target of the event and check to see if it has the editable
class.
Note: This logic may need to be refined for your use case as it relies on the user clicking the DIV with the editable class, so clicking on an element inside the div will fail. Most of the time, you would probably want the user to click on some sort of Edit button to exec this action.
Once we have the DIV, we set the Editor's HTML with the innerHTML of the DIV.
Next we position the Editor with the XY coordinates of the DIV.
1 | Event.on('editable_cont', 'dblclick', function(ev) { |
2 | var tar = Event.getTarget(ev); |
3 | while(tar.id != 'editable_cont') { |
4 | if (Dom.hasClass(tar, 'editable')) { |
5 | YAHOO.log('An element with the classname of editable was double clicked on.', 'info', 'example'); |
6 | if (editing !== null) { |
7 | YAHOO.log('There is an editor open, save its data before continuing..', 'info', 'example'); |
8 | myEditor.saveHTML(); |
9 | editing.innerHTML = myEditor.get('element').value; |
10 | } |
11 | YAHOO.log('Get the XY position of the Element that was clicked', 'info', 'example'); |
12 | var xy = Dom.getXY(tar); |
13 | YAHOO.log('Set the Editors HTML with the elements innerHTML', 'info', 'example'); |
14 | myEditor.setEditorHTML(tar.innerHTML); |
15 | YAHOO.log('Reposition the editor with the elements xy', 'info', 'example'); |
16 | Dom.setXY(myEditor.get('element_cont').get('element'), xy); |
17 | editing = tar; |
18 | } |
19 | tar = tar.parentNode; |
20 | } |
21 | }); |
view plain | print | ? |
Now we need to setup a way to save the Editor's data, we are going to override the toolbar's collapse button and use it as a save button.
First we need to override the text on the collapse button, we do that by overriding the var STR_COLLAPSE
on the YAHOO.widget.Toolbar
's prototype.
Now we listen for the toolbarLoaded
event so we can attach our handler to the toolbarCollapsed
event.
Inside of the toolbarCollapsed
event, we will save the Editor's data back to the editable area. Then we will set the position of the editor back to -9999px left and -9999px top.
Note: You could also do this using a Save button in the toolbar.
1 | YAHOO.log('Override the prototype of the toolbar to use a different string for the collapse button', 'info', 'example'); |
2 | YAHOO.widget.Toolbar.prototype.STR_COLLAPSE = 'Click to close the editor.'; |
3 | YAHOO.log('Create the Editor..', 'info', 'example'); |
4 | myEditor = new YAHOO.widget.Editor('editor', myConfig); |
5 | myEditor.on('toolbarLoaded', function() { |
6 | YAHOO.log('Toolbar is loaded, add a listener for the toolbarCollapsed event', 'info', 'example'); |
7 | this.toolbar.on('toolbarCollapsed', function() { |
8 | YAHOO.log('Toolbar was collapsed, reposition and save the editors data', 'info', 'example'); |
9 | Dom.setXY(this.get('element_cont').get('element'), [-99999, -99999]); |
10 | Dom.removeClass(this.toolbar.get('cont').parentNode, 'yui-toolbar-container-collapsed'); |
11 | myEditor.saveHTML(); |
12 | editing.innerHTML = myEditor.get('element').value; |
13 | editing = null; |
14 | }, myEditor, true); |
15 | }, myEditor, true); |
16 | myEditor.render(); |
view plain | print | ? |
1 | (function() { |
2 | var Dom = YAHOO.util.Dom, |
3 | Event = YAHOO.util.Event, |
4 | editing = null; |
5 | |
6 | YAHOO.log('Setup a stripped down config for the editor', 'info', 'example'); |
7 | var myConfig = { |
8 | height: '150px', |
9 | width: '380px', |
10 | animate: true, |
11 | limitCommands: true, |
12 | toolbar: { |
13 | titlebar: 'My Editor', |
14 | collapse: true, |
15 | buttons: [ |
16 | { group: 'textstyle', label: 'Font Style', |
17 | buttons: [ |
18 | { type: 'push', label: 'Bold', value: 'bold' }, |
19 | { type: 'push', label: 'Italic', value: 'italic' }, |
20 | { type: 'push', label: 'Underline', value: 'underline' }, |
21 | { type: 'separator' }, |
22 | { type: 'select', label: 'Arial', value: 'fontname', disabled: true, |
23 | menu: [ |
24 | { text: 'Arial', checked: true }, |
25 | { text: 'Arial Black' }, |
26 | { text: 'Comic Sans MS' }, |
27 | { text: 'Courier New' }, |
28 | { text: 'Lucida Console' }, |
29 | { text: 'Tahoma' }, |
30 | { text: 'Times New Roman' }, |
31 | { text: 'Trebuchet MS' }, |
32 | { text: 'Verdana' } |
33 | ] |
34 | }, |
35 | { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true }, |
36 | { type: 'separator' }, |
37 | { type: 'color', label: 'Font Color', value: 'forecolor', disabled: true }, |
38 | { type: 'color', label: 'Background Color', value: 'backcolor', disabled: true } |
39 | ] |
40 | } |
41 | ] |
42 | } |
43 | }; |
44 | |
45 | YAHOO.log('Override the prototype of the toolbar to use a different string for the collapse button', 'info', 'example'); |
46 | YAHOO.widget.Toolbar.prototype.STR_COLLAPSE = 'Click to close the editor.'; |
47 | YAHOO.log('Create the Editor..', 'info', 'example'); |
48 | myEditor = new YAHOO.widget.Editor('editor', myConfig); |
49 | myEditor.on('toolbarLoaded', function() { |
50 | YAHOO.log('Toolbar is loaded, add a listener for the toolbarCollapsed event', 'info', 'example'); |
51 | this.toolbar.on('toolbarCollapsed', function() { |
52 | YAHOO.log('Toolbar was collapsed, reposition and save the editors data', 'info', 'example'); |
53 | Dom.setXY(this.get('element_cont').get('element'), [-99999, -99999]); |
54 | Dom.removeClass(this.toolbar.get('cont').parentNode, 'yui-toolbar-container-collapsed'); |
55 | myEditor.saveHTML(); |
56 | editing.innerHTML = myEditor.get('element').value; |
57 | editing = null; |
58 | }, myEditor, true); |
59 | }, myEditor, true); |
60 | myEditor.render(); |
61 | |
62 | Event.on('editable_cont', 'dblclick', function(ev) { |
63 | var tar = Event.getTarget(ev); |
64 | while(tar.id != 'editable_cont') { |
65 | if (Dom.hasClass(tar, 'editable')) { |
66 | YAHOO.log('An element with the classname of editable was double clicked on.', 'info', 'example'); |
67 | if (editing !== null) { |
68 | YAHOO.log('There is an editor open, save its data before continuing..', 'info', 'example'); |
69 | myEditor.saveHTML(); |
70 | editing.innerHTML = myEditor.get('element').value; |
71 | } |
72 | YAHOO.log('Get the XY position of the Element that was clicked', 'info', 'example'); |
73 | var xy = Dom.getXY(tar); |
74 | YAHOO.log('Set the Editors HTML with the elements innerHTML', 'info', 'example'); |
75 | myEditor.setEditorHTML(tar.innerHTML); |
76 | YAHOO.log('Reposition the editor with the elements xy', 'info', 'example'); |
77 | Dom.setXY(myEditor.get('element_cont').get('element'), xy); |
78 | editing = tar; |
79 | } |
80 | tar = tar.parentNode; |
81 | } |
82 | }); |
83 | })(); |
view plain | print | ? |
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.
Copyright © 2013 Yahoo! Inc. All rights reserved.