1 /* 2 * CKEditor - The text editor for Internet - http://ckeditor.com 3 * Copyright (C) 2003-2008 Frederico Caldeira Knabben 4 * 5 * == BEGIN LICENSE == 6 * 7 * Licensed under the terms of any of the following licenses at your 8 * choice: 9 * 10 * - GNU General Public License Version 2 or later (the "GPL") 11 * http://www.gnu.org/licenses/gpl.html 12 * 13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") 14 * http://www.gnu.org/licenses/lgpl.html 15 * 16 * - Mozilla Public License Version 1.1 or later (the "MPL") 17 * http://www.mozilla.org/MPL/MPL-1.1.html 18 * 19 * == END LICENSE == 20 */ 21 22 CKEDITOR.event.implementOn( CKEDITOR ); 23 24 CKEDITOR.loadFullCore = function() 25 { 26 delete CKEDITOR.loadFullCore; 27 28 var script = document.createElement( 'script' ); 29 script.type = 'text/javascript'; 30 script.src = CKEDITOR.basePath + 'ckeditor.js'; 31 32 document.getElementsByTagName( 'head' )[0].appendChild( script ); 33 }; 34 35 /** 36 * The class name used to identify <textarea> elements to be replace 37 * by CKEditor instances. 38 * @type string 39 * @default 'ckeditor' 40 * @example 41 * CKEDITOR.replaceClass = 'rich_editor'; 42 */ 43 CKEDITOR.replaceClass = 'ckeditor'; 44 45 /** 46 * Indicates that CKEditor will attempty to replace all textareas with 47 * class name matching {@link CKEDITOR.replaceClass}. 48 * @type Boolean 49 * @default true 50 * @example 51 * // Disable the auto-replace feature. 52 * CKEDITOR.replaceByClassEnabled = false; 53 */ 54 CKEDITOR.replaceByClassEnabled = true; 55 56 /** 57 * Replaces a specific <textarea> with a CKEditor instance. 58 * @param {object|string} elementOrIdOrName The DOM element (textarea), its 59 * ID or name. 60 * @param {Object} [config] The specific configurations to apply to this 61 * editor instance. Configurations set here will override global CKEditor 62 * settings. 63 * @type undefined 64 * @example 65 * <textarea id="myfield" name="myfield"><:/textarea> 66 * ... 67 * <b>CKEDITOR.replace( 'myfield' )</b>; 68 * @example 69 * var textarea = document.body.appendChild( document.createElement( 'textarea' ) ); 70 * <b>CKEDITOR.replace( textarea )</b>; 71 */ 72 CKEDITOR.replace = function( elementOrIdOrName, config ) 73 { 74 if ( CKEDITOR.env.isCompatible ) 75 { 76 var textarea = elementOrIdOrName; 77 78 if ( typeof textarea != 'object' ) 79 { 80 // First look for the element id, then the name. 81 var i = 0; 82 var textareasByName = document.getElementsByName( elementOrIdOrName ); 83 textarea = document.getElementById( elementOrIdOrName ) || textareasByName[ i++ ]; 84 85 while ( textarea ) 86 { 87 if ( textarea.tagName.toLowerCase() == 'textarea' ) 88 break; 89 textarea = textareasByName[ i++ ]; 90 } 91 92 if ( !textarea ) 93 { 94 throw '[CKEDITOR.replace] The <textarea> with id or name "' + elementOrIdOrName + '" was not found.'; 95 return; 96 } 97 } 98 99 CKEDITOR.replace._replaceElement( textarea, config ); 100 } 101 }; 102 103 // This function will be overwritten by the full core code implementation. 104 CKEDITOR.replace._replaceElement = function( textarea, config ) 105 { 106 // Do not replace the textarea right now, just hide it. the 107 // effective replacement will be done by the full core code. 108 textarea.style.visibility = 'hidden'; 109 110 // Push the textarea in the array of pending replacements. 111 var pending = CKEDITOR.replace._pending || ( CKEDITOR.replace._pending = [] ); 112 pending.push( [ textarea, config ] ); 113 114 // Check if it is time to load the full core code. 115 if ( CKEDITOR.loadFullCore && CKEDITOR.status == 'basic_ready' ) 116 CKEDITOR.loadFullCore(); 117 }; 118 119 /** 120 * Replace all <textarea> elements available in the document with 121 * editor instances. 122 * @type undefined 123 * @example 124 * // Replace all <textarea> elements in the page. 125 * CKEDITOR.replaceAll(); 126 * @example 127 * // Replace all <textarea class="myClassName"> elements in the page. 128 * CKEDITOR.replaceAll( 'myClassName' ); 129 * @example 130 * // Selectively replace <textarea> elements, based on custom assertions. 131 * CKEDITOR.replaceAll( function( textarea, config ) 132 * { 133 * // Custom code to evaluate the replace, returning false 134 * // if it must not be done. 135 * // It also passes the "config" parameter, so the 136 * // developer can customize the instance. 137 * } ); 138 */ 139 CKEDITOR.replaceAll = function() 140 { 141 var textareas = document.getElementsByTagName( 'textarea' ); 142 143 for ( var i = 0 ; i < textareas.length ; i++ ) 144 { 145 var config = null; 146 var textarea = textareas[i]; 147 var name = textarea.name; 148 149 // The "name" and/or "id" attribute must exist. 150 if ( !textarea.name && !textarea.id ) 151 continue; 152 153 if ( typeof arguments[0] == 'string' ) 154 { 155 // The textarea class name could be passed as the function 156 // parameter. 157 158 var classRegex = new RegExp( '(?:^| )' + arguments[0] + '(?:$| )' ); 159 160 if ( !classRegex.test( textarea.className ) ) 161 continue; 162 } 163 else if ( typeof arguments[0] == 'function' ) 164 { 165 // An assertion function could be passed as the function parameter. 166 // It must explicitly return "false" to ignore a specific <textarea>. 167 config = {}; 168 if ( arguments[0]( textarea, config ) === false ) 169 continue; 170 } 171 172 this.replace( textarea, config ); 173 } 174 }; 175 176 CKEDITOR.create = function( instanceName, config ) 177 { 178 }; 179 180 CKEDITOR.appendTo = function( element, config ) 181 { 182 }; 183 184 if ( CKEDITOR.status == 'unloaded' ) 185 { 186 (function() 187 { 188 var onload = function() 189 { 190 // Replace all textareas with the default class name. 191 if ( CKEDITOR.replaceByClassEnabled ) 192 CKEDITOR.replaceAll( CKEDITOR.replaceClass ); 193 194 if ( CKEDITOR.replace._pending && CKEDITOR.status == 'basic_loaded' && CKEDITOR.loadFullCore ) 195 CKEDITOR.loadFullCore(); 196 197 CKEDITOR.status = 'basic_ready'; 198 }; 199 200 if ( window.addEventListener ) 201 window.addEventListener( 'load', onload, false ); 202 else if ( window.attachEvent ) 203 window.attachEvent( 'onload', onload ); 204 })(); 205 } 206 207 CKEDITOR.status = 'basic_loaded'; 208