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-2013 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 ], function (
 32 	Aloha,
 33 	Registry,
 34 	Class
 35 ) {
 36 	'use strict';
 37 
 38 	var ContentHandlerManager = Registry.extend({
 39 
 40 		/**
 41 		 * Create a contentHandler from the given definition.  Acts as a factory
 42 		 * method for contentHandler.
 43 		 *
 44 		 * @param {object} definition
 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 it.
 61 		 *
 62 		 * @param {string} content The content 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 are 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(
 86 						content,
 87 						options,
 88 						editable || Aloha.activeEditable
 89 					);
 90 				}
 91 
 92 				// FIXME: Is it ever valid for content to be null?  This would
 93 				//        break the handleContent(string):string contract.
 94 				if (null === content) {
 95 					break;
 96 				}
 97 			}
 98 
 99 			return content;
100 		}
101 	});
102 
103 	return new ContentHandlerManager();
104 });
105