Setting up the Editor's HTML
Setting up the Editor's HTML is done by creating a textarea
control on the page.
Setting up the Editor's Javascript
Once the textarea
is on the page, then initialize the Editor like this:
Creating the Toolbar Button and Menu
Now we can create a button and add it to the Editor's Toolbar. First we subscribe to the Editor's toolbarLoaded
Custom Event.
From inside that function we will set up a new button config object literal with the following properties:
- type: (push, menu, split, spin, select, color)
- label: The text string displayed on the button
- value: The value is also called the Command for the button.
- menu: A
YAHOO.widget.Overlay
instance to be used as a menu.
Now add it to the Toolbar group called "insertitem" like this: myEditor.toolbar.addButtonToGroup(dateConfig, 'insertitem');
Handling the Button's State on Node Change
After we have created the new button and added it to the Toolbar, we need to listen for events to trigger our new button.
We can do that by listening to the afterNodeChange
CustomEvent.
The before/afterNodeChange
events are fired when something interesting happens inside the Editor. Clicks, Key Presses, etc.
From inside the afterNodeChange
Event we can get access the the last element (or current element) that was affected. We get access to this element via: obj._getSelectedElement()
Now that we have a reference we can use standard DOM manipulation to change the element and the Toolbar.
In this case, we are grabbing the current element and checking to see if it or its parent has a className of date.
If it does, then we are populating the var selectedDate
with the innerHTML
of the element.
We are also using this opportunity to select the "insertdate" button on the toolbar, so that it becomes active. We are doing this with the following command:this.toolbar.selectButton(this.toolbar.getButtonByValue('insertdate'))
this.toolbar.getButtonByValue('insertdate')
this method will return a Button reference from the toolbar, that we can pass to this.toolbar.selectButton()
and cause the button to be selected.
On the next nodeChange
Event, our button will be disabled automatically and this handler will re-select it if it is needed.
Rendering the Calendar and Handle Date Selection
Now we add this code at the bottom to be activated when the Element cal1Container
becomes available in the DOM.
Once that Element is active on the page, we can now build and render our Calendar control.
Notice, that we are subscribing to the Calendar's selectEvent
to actually execute the insertdateClick event for the Editor.
From there, we get a button reference and call the menu's hide method to hide the Calendar.
Styling the Button
There are 2 important states to style a button in the toolbar.
First is the default state, that can be accessed via this CSS rule: .yui-skin-sam .yui-toolbar-container .yui-toolbar-insertdate span.yui-toolbar-icon
Second is the selected state, that can be accessed via this CSS rule: .yui-skin-sam .yui-toolbar-container .yui-button-insertdate-selected span.yui-toolbar-icon
.yui-toolbar-container
is the class applied to the top-most container of the toolbar.
.yui-toolbar-icon
is an extra SPAN
injected into the button for spriting an image.
.yui-toolbar-VALUE
is a dynamic class added to the button based on the value
passed into the buttons config. It is used for specific styling of a button that may appear in several places on the page.
The Style Rules to Create the Calendar Button in this Example
Full Example Javascript Source
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.