1 /*!
  2  * Aloha Editor
  3  * Author & Copyright (c) 2010 Gentics Software GmbH
  4  * aloha-sales@gentics.com
  5  * Licensed unter the terms of http://www.aloha-editor.com/license.html
  6  */
  7 define(
  8 ['aloha/jquery', 'aloha/registry'],
  9 function( jQuery, Registry ) {
 10 	"use strict";
 11 
 12 	/**
 13 	 * Create an contentHandler from the given definition. Acts as a factory method
 14 	 * for contentHandler.
 15 	 *
 16 	 * @param {Object} definition
 17 	 */
 18 	return new ( Registry.extend({
 19 
 20 		createHandler: function( definition ) {
 21 			
 22 			if ( typeof definition.handleContent != 'function' ) {
 23 				throw 'ContentHandler has no function handleContent().';
 24 			}
 25 
 26 			var AbstractContentHandler = Class.extend({
 27 				handleContent: function( content ) {
 28 					// Implement in subclass!
 29 				}
 30 			}, definition);
 31 			
 32 			return new AbstractContentHandler();
 33 		},
 34 		
 35 		handleContent: function ( content, options ) {
 36 			var handler,
 37 				handlers = this.getEntries();
 38 
 39 			if ( typeof options.contenthandler === 'undefined') {
 40 				options.contenthandler = [];
 41 				for ( handler in handlers ) {
 42 					if ( handlers.hasOwnProperty(handler) ) {
 43 						options.contenthandler.push(handler);
 44 					}
 45 				}
 46 			}
 47 
 48 			for ( handler in handlers ) {
 49 				if ( handlers.hasOwnProperty(handler) ) {
 50 					if (jQuery.inArray( handler, options.contenthandler ) < 0 ) {
 51 						continue;
 52 					}
 53 					
 54 					if ( typeof handlers[handler].handleContent === 'function') {
 55 						content = handlers[handler].handleContent( content, options );
 56 					} else {
 57 						console.error( 'A valid content handler needs the method handleContent.' );
 58 					}
 59 				}
 60 			}
 61 
 62 			return content;
 63 		}
 64 	}))();
 65 });