Ajax and Ajax Frameworks

I have been a developer for nearly 20 years and a web developer for about 10 years. Over the years, I have considered creating a blog to share the things I have learned (and am learning) about web design and development, but I never seemed to have the time. When I began learning about web development using Ajax and Ajax Frameworks, I decided to take the time to create this blog.

My intention for this blog is to focus primarily on Ajax Frameworks, but since web development requires knowledge of many technologies, I will occasionally write about things such as CSS, Javascript, PHP, MySQL, Flash, etc.

Lately, I have been using JQuery as my primary Ajax tool. Although I have used other Ajax Frameworks in the past (Dojo Toolkit, Yahoo! User Interface Library, Scriptaculous/Prototype), JQuery has thus far been the easiest to learn. In my first few posts I will discuss some of the JQuery Plugins that I have created. Some of these include: a plugin for Google Maps (jquery.imGoogleMaps), Form validation and submission  (jquery.imValidateForm), Page Populater (jquery.imPagePopulate), and a plugin to create lists (jquery.imList).

While I am still learning about some of the other Ajax Frameworks, I hope that what I have learned will be helpful to others. Just remember, “When you can pull the pebbles from my hand…”

Share

Dojo and TinyMCE – Creating a Widget

Friday, May 8th, 2009

I’m working on a project using Dojo which requires a wysiwyg editor that is more robust than Dojo’s Dijit.Editor. After doing a bit of research, I decided to use TinyMCE.  This wysiwyg editor is very robust, has great documentation and is the editor that WordPress uses. Integrating TinyMCE did not appear to be difficult, but I also wanted to create a TinyMCE Dojo widget, so that I could easily reuse TinyMCE in other Dojo projects without having to remember how to set it up.

The Dojo widget that I created is based upon an implementation that I found on Disgruntleddevs.wordpress.com, but I changed the widget to make it more flexible – and I also don’t use a template (templatePath , templateString) with the widget because I did understand why it was needed. Also, while researching how to create a Dojo widget, I found a great article by Peter Higgins entitled, “What is a _Widget“.

Widget Location

When working on a project, I generally place all dojo files in a js/ or lib/ folder. I also have a folder named “mywidgets” which is a sibling to the dojo/ folder.  All of the widgets that I create are placed inside this folder. In this case, I created a widget named RichEditor.js and placed it in the “mywidgets” folder.

Loading Dojo and TinyMCE

When loading dojo, I have to set modulePaths for the widget in the djConfig attribute. The path of widget is relative to the location of the dojo.js file.

<script type="text/javascript" src="js/dojo/dojo.js" djConfig="parseOnLoad: true, modulePaths:{'im':'../mywidgets'}" </script><!-- script -->
<script type="text/javascript" src="js/tiny_mce/tiny_mce.js" </script><!-- script -->

Now I load the widget:

<script type="text/javascript">
dojo.require("im.imRichEditor");
</script>

In the body of the html file, I created a textarea that is to be used for the tinyMCE editor:

<div id="tEditor">
<textarea id="tAreas" cols="80" rows="20" name="tAreas" /><!-- div -->
</div>

The Editor Widget

The imRichEditor widget is instantiated in the dojo.addOnLoad function. The widget is set up by default to display tinyMCE’s “Advanced” theme (all the bells and whistles), so to use it, I can simply:

?View Code JAVASCRIPT
dojo.addOnLoad(function() {
     var ta = new im.imRichEditor({editorNode: "tAreas"}, 'tEditor');
});

The “editorNode” is the id of the textarea and the ‘tEditor’ is the container. All of the options of the Advanced theme can be changed. For instance, the skin_variant option is set to “silver” by default. If I want to use the TinyMCE’s default “blue” skin:

?View Code JAVASCRIPT
var ta = new im.imRichEditor({editorNode: "tAreas", skin_variant:""}, 'tEditor');

Or Black:

?View Code JAVASCRIPT
var ta = new im.imRichEditor({editorNode: "tAreas", skin_variant:"black"}, 'tEditor');

If I want the editor to use a specific CSS file:

?View Code JAVASCRIPT
var ta = new im.imRichEditor({editorNode: "tAreas", skin_variant:"", content_css:"squeeze/styles.css"}, 'tEditor');

I can also change which buttons display in the editor and where. For instance, if I only want to display save, new document, bold, italic, underline, and strike through on the first row of buttons (advanced_buttons1):

?View Code JAVASCRIPT
var b1 = "save,newdocument,|,bold,italic,underline,strikethrough";
var ta = new im.imRichEditor({editorNode: "tAreas", advanced_buttons1: b1}, 'tEditor');

I can also change the toolbar location. The default value is “top”:

?View Code JAVASCRIPT
var ta = new im.imRichEditor({editorNode: "tAreas", toolbar_location: "bottom"}, 'tEditor');

If I want to use the TinyMCE “simple” theme:

?View Code JAVASCRIPT
var ta = new im.imRichEditor({editorNode: "tAreas", theme: "simple"}, 'tEditor');

The default toolbar location for the “simple” theme is on the bottom (set by TinyMCE, not sure why). To create the “simple” editor with the toolbar located on the top:

?View Code JAVASCRIPT
var ta = new im.RichEditor({editorNode: "tAreas", theme: "simple", toolbar_location:"top"}, 'tEditor');

I can also set an initial value for the editor:

?View Code JAVASCRIPT
var txt = '<h2 id="headerText">This is my Header Text</h2>';
var ta = new im.imRichEditor({editorNode: "tAreas", skin_variant:"", content_css:"squeeze/styles.css", value: txt}, 'tEditor');

To retrieve the content of the editor, I use the getValue() method:

?View Code JAVASCRIPT
console.log(ta.getValue());

As you can see, the imRichEditor widget is quite flexible and much more robust than Dojo’s text editor. You can download the widget from Google Code. Click here to view the demo.

Share

3 Responses to “Dojo and TinyMCE – Creating a Widget”

  • Biren Says:

    Hey, thanks for this. It got me up and running with TinyMCE pretty fast.

    Just a couple notes: in your textboxes, the & are being converted to the code version, which causes errors.

    Also, the reason you need dijit._Templated and a templatePath or templateString is so that the widget puts a node on the page. That way you don’t have to add the textArea with an id that needs to match the one you used inside your widget–one less thing to get it up and running.

    After those two changes, I had a widget-based tinyMCE editor running.

    Thanks!

  • admin Says:

    Actually, I did not need dijit._Templated for my implementation. I have used it in other widgets that I have created, but in this case, I decided not to use it. I finally created a demo of this widget. You can view it here. I made a minor change to the widget because it was not displaying the toolbar on top when in simple mode. I now store the widget on Google Code (and updated the post with a link to the demo and Google Code).

  • Ben Says:

    Hi cool widget. I have got this up and runnig but im unable to get any data that i type into the tinymce textarea that is created out by using dojo to query the id of the textarea… ? Any idea what could be wrong?

CommentLuv badge