Javascript, jQuery Rich text editor

Filed in C# | Html | jquery Leave a comment

I did a post earlier today (yes, I’m on a roll while jr. is sleeping!) on my Top 10 favorite jQuery functions and plugins. This post is dedicated to one of them: TinyMCE. In the past I’ve used editors like Telerik’s RAD Editor and obout’s Html Editor, both very powerful editors. I was turned on to TinyMCE not too long ago, and haven’t looked back since.

Integrating TinyMCE into any ASP.NET project (MVC included) is really easy, this post will cover the basics of setting up TinyMCE, and then some of the different variations available.

Setup TinyMCE

After you download TinyMCE, copy all the files into your project. Typically I’ll drop the TinyMCE folder into my “Scripts” directory. Once copied, you add a reference in either your page’s header:

<script type="text/javascript" src="<%=Url.Content("~/scripts/tiny_mce/tiny_mce.js") %>"></script>
1

<h2>Initialize TinyMCE</h2>
This is where the fun begins. You can show all the possible buttons, or just a subset of buttons if you need to keep it simple. Below is a "keep it simple" scenario, where only the most basic buttons are presented to the user:

1
tinyMCE.init({
mode: "textareas",
theme: "advanced",
plugins: "style,iespell,searchreplace,contextmenu,paste,html",

// Theme options
theme_advanced_buttons1: "bold,italic,underline,formatselect,cut,copy,paste,pastetext,pasteword",
theme_advanced_buttons2: "fontselect,fontsizeselect,|,link,unlink,code",
theme_advanced_buttons3: "justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist",
theme_advanced_buttons4: "",
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_resizing: true,
readonly: false
});

The theme_advanced_buttons1 – 4 represent the rows where the buttons will appear. If you have a really wide textarea, you could theoretically show all the buttons on a single row. Consult the TinyMCE documentation for a detailed list of every single button.

Next is the generic “advanced” configuration I’ll use when I have a more savvy user. It gives users a html button so they can modify the markup:

tinyMCE.init({
mode : "textareas",
theme : "advanced",

theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_resizing: true,
readonly: false
});

There are also times where I want to keep the custom toolbar, but also allow the user to show/hide the html. To accomplish that, you can add this hyperlink:

<a href="#" onclick="ToggleEditor('textareaID');">Show/Hide Html</a>

And this function:

function ToggleEditor(id)
{
if (!tinyMCE.get(id)) {
tinyMCE.execCommand('mceAddControl', false, id);
}
else {
tinyMCE.execCommand('mceRemoveControl', false, id);
}
}

, , ,

Top 10 jQuery Functions and Plugins (Part I)

Filed in Html | jquery 1 Comment

The following is a set of jQuery shortcuts I use at some point on a daily basis.

10. appendTo()

While there are many ways to use appendTo(), I find myself taking advantage of it when dealing with dual listboxes. With that one little command, I can move listitems from one listbox to another with no crazy loops. To illustrate the ease of use, here the entire function corresponding to the “add all items” button:

jQuery(“#availableItems option”).appendTo(“#selectedItems”);

Read more here: http://docs.jquery.com/Manipulation/appendTo

9. html()

The html attribute allows you to get or set the innerHTML of an element. Typically I’ll have a div to handle some kind of validation results, and populate it based upon some logic. For example, here is a div to hold the results:

<div id=”someresults” style=”display:none;”></div>

Then some function to populate it, in this case, display an error:

function someFunction()
{
$(“#someresults”).show();
$(“#someresults”).html(“<h2 style=’color:red;’>Something bad happened…</h2>”);
}

While that example is pretty vanilla, you can imagine how far you can stretch this.

Read more here: http://docs.jquery.com/Attributes/html

8. val()

If you plan on doing any kind of form manipulation, this one’s a must! The val attribute gets/sets a form field’s value. When called with a value like here:

$(“#firstName”).val(“William”);

The value of the firstName textbox is overwritten with William. To read the value, you simply call without an argument:

var theFirstName = $(“#firstName”).val();

One fun way to use the val attribute is creating the classic “billing address is the same as the mailing address” checkbox button. Here is how the code would look:

$(“#BillingAddress1″).val($(“#MailingAddress1″).val());
$(“#BillingAddress2″).val($(“#MailingAddress2″).val());
$(“#BillingCity”).val($(“#MailingCity”).val());
$(“#BillingState”).val($(“#MailingState”).val());
$(“#BillingZip”).val($(“#MailingZip”).val());
$(“#BillingCountry”).val($(“#MailingCountry”).val());

Read more here:  http://docs.jquery.com/Attributes/val

7. TinyMCE

Back when I used to exclusively program in ASP.NET 2.0, Telerik’s RAD Editor (http://demos.telerik.com/aspnet-ajax/editor/examples/overview/defaultcs.aspx) was the coolest thing ever. It is still really cool with some really advanced features, but for projects with a tight budget, found an awesome free tool in TinyMCE.

While there are unlimited numbers of configurations, I’ll typically use it in one of two modes:

1. I’ll convert all textareas into rich text editors, usually for an admin master page

2. Initialize individual textareas as needed

Initializing the textbox is the same, the difference is where you call it. Here is how you initialize the editor:

tinyMCE.init({
mode : “textareas”,
theme : “advanced”,

theme_advanced_toolbar_location: “top”,
theme_advanced_toolbar_align: “left”,
theme_advanced_resizing: true,
readonly: false
});

Read more here:  http://tinymce.moxiecode.com

6. tablesorter 

This might be one of my favorite plugins of all! This jQuery plugin converts a generic html table into a sorting machine, with the smarts to distinguish between dates, integers, markup, etc. baked right in. Like TinyMCE, you can initialize every table to be sortable, or on an individual basis. There is also an optional paging add-on which works decently, although I tend to go with my own personal paging control. So what do you need to do?

1. Add the reference to the plugin, then the following javascript:

jQuery(document).ready(function() {
jQuery(“table”)
.tablesorter({ widthFixed: true, widgets: ['zebra'] })
});

2. Set the table’s class to tablesorter, and include <thead> and <tbody> tags.

The link above takes you to the detailed documentation and how far you can bend and flex this plugin. But if you need to support sorting, this is one powerful plugin.

Read more here:  http://tablesorter.com/docs/

Stay tuned for Part II!

, , , ,

TOP