bgneal@442: /**
bgneal@442:  * editor_plugin_src.js
bgneal@442:  *
bgneal@442:  * Copyright 2011, Moxiecode Systems AB
bgneal@442:  * Released under LGPL License.
bgneal@442:  *
bgneal@442:  * License: http://tinymce.moxiecode.com/license
bgneal@442:  * Contributing: http://tinymce.moxiecode.com/contributing
bgneal@442:  */
bgneal@442: 
bgneal@442: (function() {
bgneal@442: 	tinymce.create('tinymce.plugins.AutolinkPlugin', {
bgneal@442: 	/**
bgneal@442: 	* Initializes the plugin, this will be executed after the plugin has been created.
bgneal@442: 	* This call is done before the editor instance has finished it's initialization so use the onInit event
bgneal@442: 	* of the editor instance to intercept that event.
bgneal@442: 	*
bgneal@442: 	* @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
bgneal@442: 	* @param {string} url Absolute URL to where the plugin is located.
bgneal@442: 	*/
bgneal@442: 
bgneal@442: 	init : function(ed, url) {
bgneal@442: 		var t = this;
bgneal@442: 
bgneal@442: 		// Internet Explorer has built-in automatic linking
bgneal@442: 		if (tinyMCE.isIE)
bgneal@442: 			return;
bgneal@442: 
bgneal@442: 		// Add a key down handler
bgneal@442: 		ed.onKeyDown.add(function(ed, e) {
bgneal@442: 			if (e.keyCode == 13)
bgneal@442: 				return t.handleEnter(ed);
bgneal@442: 			if (e.shiftKey && e.keyCode == 48)
bgneal@442: 				return t.handleEclipse(ed);
bgneal@442: 			});
bgneal@442: 
bgneal@442: 		// Add a key up handler
bgneal@442: 		ed.onKeyUp.add(function(ed, e) {
bgneal@442: 			if (e.keyCode == 32)
bgneal@442: 				return t.handleSpacebar(ed);
bgneal@442: 			});
bgneal@442: 	       },
bgneal@442: 
bgneal@442: 		handleEclipse : function(ed) {
bgneal@442: 			this.parseCurrentLine(ed, -1, '(', true);
bgneal@442: 		},
bgneal@442: 
bgneal@442: 		handleSpacebar : function(ed) {
bgneal@442: 			 this.parseCurrentLine(ed, 0, '', true);
bgneal@442: 		 },
bgneal@442: 
bgneal@442: 		handleEnter : function(ed) {
bgneal@442: 			this.parseCurrentLine(ed, -1, '', false);
bgneal@442: 		},
bgneal@442: 
bgneal@442: 		parseCurrentLine : function(ed, end_offset, delimiter, goback) {
bgneal@442: 			var r, end, start, endContainer, bookmark, text, matches, prev, len;
bgneal@442: 
bgneal@442: 			// We need at least five characters to form a URL,
bgneal@442: 			// hence, at minimum, five characters from the beginning of the line.
bgneal@442: 			r = ed.selection.getRng().cloneRange();
bgneal@442: 			if (r.startOffset < 5) {
bgneal@442: 				// During testing, the caret is placed inbetween two text nodes. 
bgneal@442: 				// The previous text node contains the URL.
bgneal@442: 				prev = r.endContainer.previousSibling;
bgneal@442: 				if (prev == null) {
bgneal@442: 					if (r.endContainer.firstChild == null || r.endContainer.firstChild.nextSibling == null)
bgneal@442: 						return;
bgneal@442: 
bgneal@442: 					prev = r.endContainer.firstChild.nextSibling;
bgneal@442: 				}
bgneal@442: 				len = prev.length;
bgneal@442: 				r.setStart(prev, len);
bgneal@442: 				r.setEnd(prev, len);
bgneal@442: 
bgneal@442: 				if (r.endOffset < 5)
bgneal@442: 					return;
bgneal@442: 
bgneal@442: 				end = r.endOffset;
bgneal@442: 				endContainer = prev;
bgneal@442: 			} else {
bgneal@442: 				endContainer = r.endContainer;
bgneal@442: 
bgneal@442: 				// Get a text node
bgneal@442: 				if (endContainer.nodeType != 3 && endContainer.firstChild) {
bgneal@442: 					while (endContainer.nodeType != 3 && endContainer.firstChild)
bgneal@442: 						endContainer = endContainer.firstChild;
bgneal@442: 
bgneal@442: 					r.setStart(endContainer, 0);
bgneal@442: 					r.setEnd(endContainer, endContainer.nodeValue.length);
bgneal@442: 				}
bgneal@442: 
bgneal@442: 				if (r.endOffset == 1)
bgneal@442: 					end = 2;
bgneal@442: 				else
bgneal@442: 					end = r.endOffset - 1 - end_offset;
bgneal@442: 			}
bgneal@442: 
bgneal@442: 			start = end;
bgneal@442: 
bgneal@442: 			do
bgneal@442: 			{
bgneal@442: 				// Move the selection one character backwards.
bgneal@442: 				r.setStart(endContainer, end - 2);
bgneal@442: 				r.setEnd(endContainer, end - 1);
bgneal@442: 				end -= 1;
bgneal@442: 
bgneal@442: 				// Loop until one of the following is found: a blank space, &nbsp;, delimeter, (end-2) >= 0
bgneal@442: 			} while (r.toString() != ' ' && r.toString() != '' && r.toString().charCodeAt(0) != 160 && (end -2) >= 0 && r.toString() != delimiter);
bgneal@442: 
bgneal@442: 			if (r.toString() == delimiter || r.toString().charCodeAt(0) == 160) {
bgneal@442: 				r.setStart(endContainer, end);
bgneal@442: 				r.setEnd(endContainer, start);
bgneal@442: 				end += 1;
bgneal@442: 			} else if (r.startOffset == 0) {
bgneal@442: 				r.setStart(endContainer, 0);
bgneal@442: 				r.setEnd(endContainer, start);
bgneal@442: 			}
bgneal@442: 			else {
bgneal@442: 				r.setStart(endContainer, end);
bgneal@442: 				r.setEnd(endContainer, start);
bgneal@442: 			}
bgneal@442: 
bgneal@442: 			text = r.toString();
bgneal@442: 			matches = text.match(/^(https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)(.+)$/i);
bgneal@442: 
bgneal@442: 			if (matches) {
bgneal@442: 				if (matches[1] == 'www.') {
bgneal@442: 					matches[1] = 'http://www.';
bgneal@442: 				}
bgneal@442: 
bgneal@442: 				bookmark = ed.selection.getBookmark();
bgneal@442: 
bgneal@442: 				ed.selection.setRng(r);
bgneal@442: 				tinyMCE.execCommand('mceInsertLink',false, matches[1] + matches[2]);
bgneal@442: 				ed.selection.moveToBookmark(bookmark);
bgneal@442: 
bgneal@442: 				// TODO: Determine if this is still needed.
bgneal@442: 				if (tinyMCE.isWebKit) {
bgneal@442: 					// move the caret to its original position
bgneal@442: 					ed.selection.collapse(false);
bgneal@442: 					var max = Math.min(endContainer.length, start + 1);
bgneal@442: 					r.setStart(endContainer, max);
bgneal@442: 					r.setEnd(endContainer, max);
bgneal@442: 					ed.selection.setRng(r);
bgneal@442: 				}
bgneal@442: 			}
bgneal@442: 		},
bgneal@442: 
bgneal@442: 		/**
bgneal@442: 		* Returns information about the plugin as a name/value array.
bgneal@442: 		* The current keys are longname, author, authorurl, infourl and version.
bgneal@442: 		*
bgneal@442: 		* @return {Object} Name/value array containing information about the plugin.
bgneal@442: 		*/
bgneal@442: 		getInfo : function() {
bgneal@442: 			return {
bgneal@442: 				longname : 'Autolink',
bgneal@442: 				author : 'Moxiecode Systems AB',
bgneal@442: 				authorurl : 'http://tinymce.moxiecode.com',
bgneal@442: 				infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autolink',
bgneal@442: 				version : tinymce.majorVersion + "." + tinymce.minorVersion
bgneal@442: 			};
bgneal@442: 		}
bgneal@442: 	});
bgneal@442: 
bgneal@442: 	// Register plugin
bgneal@442: 	tinymce.PluginManager.add('autolink', tinymce.plugins.AutolinkPlugin);
bgneal@442: })();