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 /*global define:true */
 28 define(
 29 ['jquery', 'aloha/registry', 'util/class', 'aloha/console'],
 30 function (jQuery, Registry, Class, console) {
 31 	"use strict";
 32 
 33 	/**
 34 	 * Create an contentHandler from the given definition. Acts as a factory method
 35 	 * for contentHandler.
 36 	 *
 37 	 * @param {Object} definition
 38 	 */
 39 	return new (Registry.extend({
 40 
 41 		createHandler: function (definition) {
 42 			
 43 			if (typeof definition.handleContent !== 'function') {
 44 				throw 'ContentHandler has no function handleContent().';
 45 			}
 46 
 47 			var AbstractContentHandler = Class.extend({
 48 				handleContent: function (content) {
 49 					// Implement in subclass!
 50 				}
 51 			}, definition);
 52 			
 53 			return new AbstractContentHandler();
 54 		},
 55 		
 56 		handleContent: function (content, options) {
 57 			var handler, id,
 58 				ids = this.getIds();
 59 
 60 			if (typeof options.contenthandler === 'undefined') {
 61 				options.contenthandler = [];
 62 				for (id in ids) {
 63 					if (ids.hasOwnProperty(id)) {
 64 						options.contenthandler.push(ids[id]);
 65 					}
 66 				}
 67 			}
 68 
 69 			for (id in options.contenthandler) {
 70 				if (options.contenthandler.hasOwnProperty(id)) {
 71 					handler = this.get(options.contenthandler[id]);
 72 					if (handler) {
 73 						if (typeof handler.handleContent === 'function') {
 74 							content = handler.handleContent(content, options);
 75 						} else {
 76 							console.error('A valid content handler needs the method handleContent.');
 77 						}
 78 					}
 79 					if (null === content) {
 80 						break;
 81 					}
 82 				}
 83 			}
 84 
 85 			return content;
 86 		}
 87 	}))();
 88 });
 89