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