| 1 | /* |
| 2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. |
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
| 4 | */ |
| 5 | |
| 6 | CKEDITOR.plugins.add( 'readonly', |
| 7 | { |
| 8 | requires : [ 'editingblock' ], |
| 9 | init : function( editor ) |
| 10 | { |
| 11 | editor.addCommand( 'readonly', |
| 12 | { |
| 13 | modes : { wysiwyg : 1, source : 1 }, |
| 14 | editorFocus : false, |
| 15 | canUndo : false, |
| 16 | exec : function ( editor ) |
| 17 | { |
| 18 | editor.readOnly( this.state == CKEDITOR.TRISTATE_OFF ); |
| 19 | this.toggleState(); |
| 20 | } |
| 21 | }); |
| 22 | |
| 23 | editor.ui.addButton( 'ReadOnly', |
| 24 | { |
| 25 | label : editor.lang.readOnly, |
| 26 | command : 'readonly' |
| 27 | }); |
| 28 | } |
| 29 | }); |
| 30 | |
| 31 | ( function() |
| 32 | { |
| 33 | var cancelEvent = function( evt ) |
| 34 | { |
| 35 | evt.cancel(); |
| 36 | }; |
| 37 | |
| 38 | function cancelKeyListener( evt ) |
| 39 | { |
| 40 | var keystroke = evt.data.keyCode; |
| 41 | if ( keystroke != 9 && keystroke != CKEDITOR.SHIFT + 9 ) |
| 42 | cancelEvent( evt ); |
| 43 | } |
| 44 | |
| 45 | // List of commands that are read-only. |
| 46 | CKEDITOR.plugins.readyOnly = |
| 47 | { |
| 48 | readyOnlyCommands : { readonly:1,maximize:1,showblocks:1,showborders:1,preview:1,save:1,copy:1,print:1,selectAll:1,about:1 } |
| 49 | }; |
| 50 | |
| 51 | CKEDITOR.editor.prototype.readOnly = function( isReadOnly ) |
| 52 | { |
| 53 | // Disable all commands in wysiwyg mode. |
| 54 | var command, |
| 55 | commands = this._.commands, |
| 56 | mode = this.mode; |
| 57 | |
| 58 | for ( var name in commands ) |
| 59 | { |
| 60 | if ( name in CKEDITOR.plugins.readyOnly.readyOnlyCommands ) |
| 61 | continue; |
| 62 | |
| 63 | command = commands[ name ]; |
| 64 | isReadOnly ? command.disable() : command[ command.modes[ mode ] ? 'enable' : 'disable' ](); |
| 65 | this[ isReadOnly ? 'on' : 'removeListener' ]( 'state', cancelEvent, null, null, 0 ); |
| 66 | } |
| 67 | |
| 68 | if ( this.mode == 'wysiwyg' ) |
| 69 | { |
| 70 | // Turn off contentEditable. |
| 71 | this.document.$.body.contentEditable = !isReadOnly; |
| 72 | |
| 73 | // Prevent key handling. |
| 74 | this[ isReadOnly ? 'on' : 'removeListener' ]( 'key', cancelKeyListener, null, null, 0 ); |
| 75 | this[ isReadOnly ? 'on' : 'removeListener' ]( 'selectionChange', cancelEvent, null, null, 0 ); |
| 76 | |
| 77 | // Deal with some toolbar items that don't get associated with commands. |
| 78 | var i, j, k; |
| 79 | var toolbars = this.toolbox.toolbars; |
| 80 | for ( i = 0; i < toolbars.length; i++ ) |
| 81 | { |
| 82 | var toolbarItems = toolbars[ i ].items; |
| 83 | for ( j = 0; j < toolbarItems.length; j++ ) |
| 84 | { |
| 85 | // Combos and panel buttons. |
| 86 | var combo = toolbarItems[ j ].combo; |
| 87 | if ( combo ) |
| 88 | combo.setState( isReadOnly ? CKEDITOR.TRISTATE_DISABLED : CKEDITOR.TRISTATE_OFF ); |
| 89 | |
| 90 | var button = toolbarItems[ j ].button; |
| 91 | if ( button instanceof CKEDITOR.ui.panelButton ) |
| 92 | button.setState( isReadOnly ? CKEDITOR.TRISTATE_DISABLED : CKEDITOR.TRISTATE_OFF ); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | else if ( this.mode == 'source' ) |
| 97 | { |
| 98 | if ( isReadOnly ) |
| 99 | this.textarea.setAttribute( 'disabled', true ); |
| 100 | else |
| 101 | this.textarea.removeAttribute( 'disabled' ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } )(); |