Nation Notes module contributed by Z&H Healthcare.
[openemr.git] / library / custom_template / ckeditor / _source / plugins / clipboard / plugin.js
blob27121bed6c73c3f60428a6da70ac0f56c7111849
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 /**
7  * @file Clipboard support
8  */
10 (function()
12         // Tries to execute any of the paste, cut or copy commands in IE. Returns a
13         // boolean indicating that the operation succeeded.
14         var execIECommand = function( editor, command )
15         {
16                 var doc = editor.document,
17                         body = doc.getBody();
19                 var enabled = 0;
20                 var onExec = function()
21                 {
22                         enabled = 1;
23                 };
25                 // The following seems to be the only reliable way to detect that
26                 // clipboard commands are enabled in IE. It will fire the
27                 // onpaste/oncut/oncopy events only if the security settings allowed
28                 // the command to execute.
29                 body.on( command, onExec );
31                 // IE6/7: document.execCommand has problem to paste into positioned element.
32                 ( CKEDITOR.env.version > 7 ? doc.$ : doc.$.selection.createRange() ) [ 'execCommand' ]( command );
34                 body.removeListener( command, onExec );
36                 return enabled;
37         };
39         // Attempts to execute the Cut and Copy operations.
40         var tryToCutCopy =
41                 CKEDITOR.env.ie ?
42                         function( editor, type )
43                         {
44                                 return execIECommand( editor, type );
45                         }
46                 :               // !IE.
47                         function( editor, type )
48                         {
49                                 try
50                                 {
51                                         // Other browsers throw an error if the command is disabled.
52                                         return editor.document.$.execCommand( type, false, null );
53                                 }
54                                 catch( e )
55                                 {
56                                         return false;
57                                 }
58                         };
60         // A class that represents one of the cut or copy commands.
61         var cutCopyCmd = function( type )
62         {
63                 this.type = type;
64                 this.canUndo = this.type == 'cut';              // We can't undo copy to clipboard.
65                 this.startDisabled = true;
66         };
68         cutCopyCmd.prototype =
69         {
70                 exec : function( editor, data )
71                 {
72                         this.type == 'cut' && fixCut( editor );
74                         var success = tryToCutCopy( editor, this.type );
76                         if ( !success )
77                                 alert( editor.lang.clipboard[ this.type + 'Error' ] );          // Show cutError or copyError.
79                         return success;
80                 }
81         };
83         // Paste command.
84         var pasteCmd =
85         {
86                 canUndo : false,
88                 exec :
89                         CKEDITOR.env.ie ?
90                                 function( editor )
91                                 {
92                                         // Prevent IE from pasting at the begining of the document.
93                                         editor.focus();
95                                         if ( !editor.document.getBody().fire( 'beforepaste' )
96                                                  && !execIECommand( editor, 'paste' ) )
97                                         {
98                                                 editor.fire( 'pasteDialog' );
99                                                 return false;
100                                         }
101                                 }
102                         :
103                                 function( editor )
104                                 {
105                                         try
106                                         {
107                                                 if ( !editor.document.getBody().fire( 'beforepaste' )
108                                                          && !editor.document.$.execCommand( 'Paste', false, null ) )
109                                                 {
110                                                         throw 0;
111                                                 }
112                                         }
113                                         catch ( e )
114                                         {
115                                                 setTimeout( function()
116                                                         {
117                                                                 editor.fire( 'pasteDialog' );
118                                                         }, 0 );
119                                                 return false;
120                                         }
121                                 }
122         };
124         // Listens for some clipboard related keystrokes, so they get customized.
125         var onKey = function( event )
126         {
127                 if ( this.mode != 'wysiwyg' )
128                         return;
130                 switch ( event.data.keyCode )
131                 {
132                         // Paste
133                         case CKEDITOR.CTRL + 86 :               // CTRL+V
134                         case CKEDITOR.SHIFT + 45 :              // SHIFT+INS
136                                 var body = this.document.getBody();
138                                 // Simulate 'beforepaste' event for all none-IEs.
139                                 if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )
140                                         event.cancel();
141                                 // Simulate 'paste' event for Opera/Firefox2.
142                                 else if ( CKEDITOR.env.opera
143                                                  || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )
144                                         body.fire( 'paste' );
145                                 return;
147                         // Cut
148                         case CKEDITOR.CTRL + 88 :               // CTRL+X
149                         case CKEDITOR.SHIFT + 46 :              // SHIFT+DEL
151                                 // Save Undo snapshot.
152                                 var editor = this;
153                                 this.fire( 'saveSnapshot' );            // Save before paste
154                                 setTimeout( function()
155                                         {
156                                                 editor.fire( 'saveSnapshot' );          // Save after paste
157                                         }, 0 );
158                 }
159         };
161         // Allow to peek clipboard content by redirecting the
162         // pasting content into a temporary bin and grab the content of it.
163         function getClipboardData( evt, mode, callback )
164         {
165                 var doc = this.document;
167                 // Avoid recursions on 'paste' event or consequent paste too fast. (#5730)
168                 if ( doc.getById( 'cke_pastebin' ) )
169                         return;
171                 // If the browser supports it, get the data directly
172                 if ( mode == 'text' && evt.data && evt.data.$.clipboardData )
173                 {
174                         // evt.data.$.clipboardData.types contains all the flavours in Mac's Safari, but not on windows.
175                         var plain = evt.data.$.clipboardData.getData( 'text/plain' );
176                         if ( plain )
177                         {
178                                 evt.data.preventDefault();
179                                 callback( plain );
180                                 return;
181                         }
182                 }
184                 var sel = this.getSelection(),
185                         range = new CKEDITOR.dom.range( doc );
187                 // Create container to paste into
188                 var pastebin = new CKEDITOR.dom.element( mode == 'text' ? 'textarea' : CKEDITOR.env.webkit ? 'body' : 'div', doc );
189                 pastebin.setAttribute( 'id', 'cke_pastebin' );
190                 // Safari requires a filler node inside the div to have the content pasted into it. (#4882)
191                 CKEDITOR.env.webkit && pastebin.append( doc.createText( '\xa0' ) );
192                 doc.getBody().append( pastebin );
194                 pastebin.setStyles(
195                         {
196                                 position : 'absolute',
197                                 // Position the bin exactly at the position of the selected element
198                                 // to avoid any subsequent document scroll.
199                                 top : sel.getStartElement().getDocumentPosition().y + 'px',
200                                 width : '1px',
201                                 height : '1px',
202                                 overflow : 'hidden'
203                         });
205                 // It's definitely a better user experience if we make the paste-bin pretty unnoticed
206                 // by pulling it off the screen.
207                 pastebin.setStyle( this.config.contentsLangDirection == 'ltr' ? 'left' : 'right', '-1000px' );
209                 var bms = sel.createBookmarks();
211                 // Turn off design mode temporarily before give focus to the paste bin.
212                 if ( mode == 'text' )
213                 {
214                         if ( CKEDITOR.env.ie )
215                         {
216                                 var ieRange = doc.getBody().$.createTextRange();
217                                 ieRange.moveToElementText( pastebin.$ );
218                                 ieRange.execCommand( 'Paste' );
219                                 evt.data.preventDefault();
220                         }
221                         else
222                                 pastebin.$.focus();
223                 }
224                 else
225                 {
226                         range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );
227                         range.setEndAt( pastebin, CKEDITOR.POSITION_BEFORE_END );
228                         range.select( true );
229                 }
231                 var editor  = this;
232                 // Wait a while and grab the pasted contents
233                 window.setTimeout( function()
234                 {
235                         mode == 'text' && CKEDITOR.env.gecko && editor.focusGrabber.focus();
236                         pastebin.remove();
238                         // Grab the HTML contents.
239                         // We need to look for a apple style wrapper on webkit it also adds
240                         // a div wrapper if you copy/paste the body of the editor.
241                         // Remove hidden div and restore selection.
242                         var bogusSpan;
243                         pastebin = ( CKEDITOR.env.webkit
244                                                  && ( bogusSpan = pastebin.getFirst() )
245                                                  && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?
246                                                         bogusSpan : pastebin );
248                         sel.selectBookmarks( bms );
249                         callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );
250                 }, 0 );
251         }
253         // Cutting off control type element in IE standards breaks the selection entirely. (#4881)
254         function fixCut( editor )
255         {
256                 if ( !CKEDITOR.env.ie || CKEDITOR.env.quirks )
257                         return;
259                 var sel = editor.getSelection();
260                 var control;
261                 if( ( sel.getType() == CKEDITOR.SELECTION_ELEMENT ) && ( control = sel.getSelectedElement() ) )
262                 {
263                         var range = sel.getRanges()[ 0 ];
264                         var dummy = editor.document.createText( '' );
265                         dummy.insertBefore( control );
266                         range.setStartBefore( dummy );
267                         range.setEndAfter( control );
268                         sel.selectRanges( [ range ] );
270                         // Clear up the fix if the paste wasn't succeeded.
271                         setTimeout( function()
272                         {
273                                 // Element still online?
274                                 if ( control.getParent() )
275                                 {
276                                         dummy.remove();
277                                         sel.selectElement( control );
278                                 }
279                         }, 0 );
280                 }
281         }
283         var depressBeforeEvent;
284         function stateFromNamedCommand( command, editor )
285         {
286                 // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',
287                 // guard to distinguish from the ordinary sources( either
288                 // keyboard paste or execCommand ) (#4874).
289                 CKEDITOR.env.ie && ( depressBeforeEvent = 1 );
291                 var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
292                 depressBeforeEvent = 0;
293                 return retval;
294         }
296         var inReadOnly;
297         function setToolbarStates()
298         {
299                 if ( this.mode != 'wysiwyg' )
300                         return;
302                 this.getCommand( 'cut' ).setState( inReadOnly ? CKEDITOR.TRISTATE_DISABLED : stateFromNamedCommand( 'Cut', this ) );
303                 this.getCommand( 'copy' ).setState( stateFromNamedCommand( 'Copy', this ) );
304                 var pasteState = inReadOnly ? CKEDITOR.TRISTATE_DISABLED :
305                                                 CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', this );
306                 this.fire( 'pasteState', pasteState );
307         }
309         // Register the plugin.
310         CKEDITOR.plugins.add( 'clipboard',
311                 {
312                         requires : [ 'dialog', 'htmldataprocessor' ],
313                         init : function( editor )
314                         {
315                                 // Inserts processed data into the editor at the end of the
316                                 // events chain.
317                                 editor.on( 'paste', function( evt )
318                                         {
319                                                 var data = evt.data;
320                                                 if ( data[ 'html' ] )
321                                                         editor.insertHtml( data[ 'html' ] );
322                                                 else if ( data[ 'text' ] )
323                                                         editor.insertText( data[ 'text' ] );
325                                         }, null, null, 1000 );
327                                 editor.on( 'pasteDialog', function( evt )
328                                         {
329                                                 setTimeout( function()
330                                                 {
331                                                         // Open default paste dialog.
332                                                         editor.openDialog( 'paste' );
333                                                 }, 0 );
334                                         });
336                                 editor.on( 'pasteState', function( evt )
337                                         {
338                                                 editor.getCommand( 'paste' ).setState( evt.data );
339                                         });
341                                 function addButtonCommand( buttonName, commandName, command, ctxMenuOrder )
342                                 {
343                                         var lang = editor.lang[ commandName ];
345                                         editor.addCommand( commandName, command );
346                                         editor.ui.addButton( buttonName,
347                                                 {
348                                                         label : lang,
349                                                         command : commandName
350                                                 });
352                                         // If the "menu" plugin is loaded, register the menu item.
353                                         if ( editor.addMenuItems )
354                                         {
355                                                 editor.addMenuItem( commandName,
356                                                         {
357                                                                 label : lang,
358                                                                 command : commandName,
359                                                                 group : 'clipboard',
360                                                                 order : ctxMenuOrder
361                                                         });
362                                         }
363                                 }
365                                 addButtonCommand( 'Cut', 'cut', new cutCopyCmd( 'cut' ), 1 );
366                                 addButtonCommand( 'Copy', 'copy', new cutCopyCmd( 'copy' ), 4 );
367                                 addButtonCommand( 'Paste', 'paste', pasteCmd, 8 );
369                                 CKEDITOR.dialog.add( 'paste', CKEDITOR.getUrl( this.path + 'dialogs/paste.js' ) );
371                                 editor.on( 'key', onKey, editor );
373                                 var mode = editor.config.forcePasteAsPlainText ? 'text' : 'html';
375                                 // We'll be catching all pasted content in one line, regardless of whether the
376                                 // it's introduced by a document command execution (e.g. toolbar buttons) or
377                                 // user paste behaviors. (e.g. Ctrl-V)
378                                 editor.on( 'contentDom', function()
379                                 {
380                                         var body = editor.document.getBody();
381                                         body.on( ( ( mode == 'text' && CKEDITOR.env.ie ) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',
382                                                 function( evt )
383                                                 {
384                                                         if ( depressBeforeEvent )
385                                                                 return;
387                                                         getClipboardData.call( editor, evt, mode, function ( data )
388                                                         {
389                                                                 // The very last guard to make sure the
390                                                                 // paste has successfully happened.
391                                                                 if ( !data )
392                                                                         return;
394                                                                 var dataTransfer = {};
395                                                                 dataTransfer[ mode ] = data;
396                                                                 editor.fire( 'paste', dataTransfer );
397                                                         } );
398                                                 });
400                                         body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );
402                                         body.on( 'mouseup', function(){ setTimeout( function(){ setToolbarStates.call( editor ); }, 0 ); }, editor );
403                                         body.on( 'keyup', setToolbarStates, editor );
404                                 });
406                                 // For improved performance, we're checking the readOnly state on selectionChange instead of hooking a key event for that.
407                                 editor.on( 'selectionChange', function( evt )
408                                 {
409                                         inReadOnly = evt.data.selection.getRanges()[ 0 ].checkReadOnly();
410                                         setToolbarStates.call( editor );
411                                 });
413                                 // If the "contextmenu" plugin is loaded, register the listeners.
414                                 if ( editor.contextMenu )
415                                 {
416                                         editor.contextMenu.addListener( function( element, selection )
417                                                 {
418                                                         var readOnly = selection.getRanges()[ 0 ].checkReadOnly();
419                                                         return {
420                                                                 cut : !readOnly && stateFromNamedCommand( 'Cut', editor ),
421                                                                 copy : stateFromNamedCommand( 'Copy', editor ),
422                                                                 paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', editor ) )
423                                                         };
424                                                 });
425                                 }
426                         }
427                 });
428 })();
431  * Fired when a clipboard operation is about to be taken into the editor.
432  * Listeners can manipulate the data to be pasted before having it effectively
433  * inserted into the document.
434  * @name CKEDITOR.editor#paste
435  * @since 3.1
436  * @event
437  * @param {String} [data.html] The HTML data to be pasted. If not available, e.data.text will be defined.
438  * @param {String} [data.text] The plain text data to be pasted, available when plain text operations are to used. If not available, e.data.html will be defined.
439  */
442  * Internal event to open the Paste dialog
443  * @name CKEDITOR.editor#pasteDialog
444  * @event
445  */