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/registry',
 29 	'util/class',
 30 	'aloha/console'
 31 ], function (
 32 	Registry,
 33 	Class,
 34 	console
 35 ) {
 36 	'use strict';
 37  38 
	/**
 39 	 * Create an contentHandler from the given definition.  Acts as a factory
 40 	 * method for contentHandler.
 41 	 *
 42 	 * @param {ContentHandlerManager} definition
 43 	 */
 44 	var ContentHandlerManager = Registry.extend({
 45 
 46 		createHandler: function (definition) {
 47 			if (typeof definition.handleContent !== 'function') {
 48 				throw 'ContentHandler has no function handleContent().';
 49 			}
 50 			var AbstractContentHandler = Class.extend({
 51 				handleContent: function (content) {
 52 					// Implement in subclass!
 53 				}
 54 			}, definition);
 55 			return new AbstractContentHandler();
 56 		},
 57 
 58 		/**
 59 		 * Manipulates the given contents of an editable by invoking content
 60 		 * handlers over the contents.
 61 		 *
 62 		 * @param {string} content The contents of an editable which will be
 63 		 *                         handled.
 64 		 * @param {object} options Used to filter limit which content handlers
 65 		 *                         should be used.
 66 		 * @param {Aloha.Editable} The editable whose content is being handled.
 67 		 * @return {string} The handled content.
 68 		 */
 69 		handleContent: function (content, options, editable) {
 70 			var manager = this;
 71 
 72 			// Because if no options is specified to indicate which content
 73 			// handler to use, then all that are available are used.
 74 			var handlers = options ? options.contenthandler : manager.getIds();
 75 
 76 			if (!handlers) {
 77 				return content;
 78 			}
 79 
 80 			var i;
 81 			var handler;
 82 			for (i = 0; i < handlers.length; i++) {
 83 				handler = manager.get(handlers[i]);
 84 				if (handler) {
 85 					content = handler.handleContent(content, options,
 86 							editable || Aloha.activeEditable);
 87 				}
 88 
 89 				// FIME: Is it ever valid for content to be null?  This breaks
 90 				//       the handleContent(string):string contract.
 91 				if (null === content) {
 92 					break;
 93 				}
 94 			}
 95 
 96 			return content;
 97 		}
 98 	});
 99 
100 	return new ContentHandlerManager();
101 });
102