1 /* contenthandlermanager.js is part of Aloha Editor project http://aloha-editor.org
  2  *
  3  * Aloha Editor is a WYSIWYG HTML5 inline editing library and editor. 
  4  * Copyright (c) 2010-2012 Gentics Software GmbH, Vienna, Austria.
  5  * Contributors http://aloha-editor.org/contribution.php 
  6  * 
  7  * Aloha Editor is free software; you can redistribute it and/or
  8  * modify it under the terms of the GNU General Public License
  9  * as published by the Free Software Foundation; either version 2
 10  * of the License, or any later version.
 11  *
 12  * Aloha Editor is distributed in the hope that it will be useful,
 13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU General Public License
 18  * along with this program; if not, write to the Free Software
 19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 20  * 
 21  * As an additional permission to the GNU GPL version 2, you may distribute
 22  * non-source (e.g., minimized or compacted) forms of the Aloha-Editor
 23  * source code without the copy of the GNU GPL normally required,
 24  * provided you include this license notice and a URL through which
 25  * recipients can access the Corresponding Source.
 26  */
 27 define([
 28 	'aloha/core',
 29 	'aloha/registry',
 30 	'util/class',
 31 	'aloha/console'
 32 ], function (
 33 	Aloha,
 34 	Registry,
 35 	Class,
 36 	console
 37 ) {
 38 	'use strict';
 39 
 40 	/**
 41 	 * Create an contentHandler from the given definition.  Acts as a factory
 42 	 * method for contentHandler.
 43 	 *
 44 	 * @param {ContentHandlerManager} definition
 45 	 */
 46 	var ContentHandlerManager = Registry.extend({
 47 
 48 		createHandler: function (definition) {
 49 			if (typeof definition.handleContent !== 'function') {
 50 				throw 'ContentHandler has no function handleContent().';
 51 			}
 52 			var AbstractContentHandler = Class.extend({
 53 				handleContent: function (content) {
 54 					// Implement in subclass!
 55 				}
 56 			}, definition);
 57 			return new AbstractContentHandler();
 58 		},
 59 
 60 		/**
 61 		 * Manipulates the given contents of an editable by invoking content
 62 		 * handlers over the contents.
 63 		 *
 64 		 * @param {string} content The contents of an editable which will be
 65 		 *                         handled.
 66 		 * @param {object} options Used to filter limit which content handlers
 67 		 *                         should be used.
 68 		 * @param {Aloha.Editable} The editable whose content is being handled.
 69 		 * @return {string} The handled content.
 70 		 */
 71 		handleContent: function (content, options, editable) {
 72 			var manager = this;
 73 
 74 			// Because if no options is specified to indicate which content
 75 			// handler to use, then all that are available are used.
 76 			var handlers = options ? options.contenthandler : manager.getIds();
 77 
 78 			if (!handlers) {
 79 				return content;
 80 			}
 81 
 82 			var i;
 83 			var handler;
 84 			for (i = 0; i < handlers.length; i++) {
 85 				handler = manager.get(handlers[i]);
 86 				if (handler) {
 87 					content = handler.handleContent(content, options,
 88 							editable || Aloha.activeEditable);
 89 				}
 90 
 91 				// FIME: Is it ever valid for content to be null?  This breaks
 92 				//       the handleContent(string):string contract.
 93 				if (null === content) {
 94 					break;
 95 				}
 96 			}
 97 
 98 			return content;
 99 		}
100 	});
101 
102 	return new ContentHandlerManager();
103 });
104