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