1 /*
  2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5 
  6 /**
  7  * @fileOverview The "div" plugin. It wraps the selected block level elements with a 'div' element with specified styles and attributes.
  8  *
  9  */
 10 
 11 (function()
 12 {
 13 	CKEDITOR.plugins.add( 'div',
 14 	{
 15 		requires : [ 'editingblock', 'dialog', 'domiterator', 'styles' ],
 16 
 17 		init : function( editor )
 18 		{
 19 			var lang = editor.lang.div;
 20 
 21 			editor.addCommand( 'creatediv', new CKEDITOR.dialogCommand( 'creatediv' ) );
 22 			editor.addCommand( 'editdiv', new CKEDITOR.dialogCommand( 'editdiv' ) );
 23 			editor.addCommand( 'removediv',
 24 				{
 25 					exec : function( editor )
 26 					{
 27 						var selection = editor.getSelection(),
 28 							ranges = selection && selection.getRanges(),
 29 							range,
 30 							bookmarks = selection.createBookmarks(),
 31 							walker,
 32 							toRemove = [];
 33 
 34 						function findDiv( node )
 35 						{
 36 							var path = new CKEDITOR.dom.elementPath( node ),
 37 								blockLimit = path.blockLimit,
 38 								div = blockLimit.is( 'div' ) && blockLimit;
 39 
 40 							if ( div && !div.data( 'cke-div-added' ) )
 41 							{
 42 								toRemove.push( div );
 43 								div.data( 'cke-div-added' );
 44 							}
 45 						}
 46 
 47 						for ( var i = 0 ; i < ranges.length ; i++ )
 48 						{
 49 							range = ranges[ i ];
 50 							if ( range.collapsed )
 51 								findDiv( selection.getStartElement() );
 52 							else
 53 							{
 54 								walker = new CKEDITOR.dom.walker( range );
 55 								walker.evaluator = findDiv;
 56 								walker.lastForward();
 57 							}
 58 						}
 59 
 60 						for ( i = 0 ; i < toRemove.length ; i++ )
 61 							toRemove[ i ].remove( true );
 62 
 63 						selection.selectBookmarks( bookmarks );
 64 					}
 65 				} );
 66 
 67 			editor.ui.addButton( 'CreateDiv',
 68 			{
 69 				label : lang.toolbar,
 70 				command :'creatediv'
 71 			} );
 72 
 73 			if ( editor.addMenuItems )
 74 			{
 75 				editor.addMenuItems(
 76 					{
 77 						editdiv :
 78 						{
 79 							label : lang.edit,
 80 							command : 'editdiv',
 81 							group : 'div',
 82 							order : 1
 83 						},
 84 
 85 						removediv:
 86 						{
 87 							label : lang.remove,
 88 							command : 'removediv',
 89 							group : 'div',
 90 							order : 5
 91 						}
 92 					} );
 93 
 94 				if ( editor.contextMenu )
 95 				{
 96 					editor.contextMenu.addListener( function( element, selection )
 97 						{
 98 							if ( !element || element.isReadOnly() )
 99 								return null;
100 
101 							var elementPath = new CKEDITOR.dom.elementPath( element ),
102 								blockLimit = elementPath.blockLimit;
103 
104 							if ( blockLimit && blockLimit.getAscendant( 'div', true ) )
105 							{
106 								return {
107 									editdiv : CKEDITOR.TRISTATE_OFF,
108 									removediv : CKEDITOR.TRISTATE_OFF
109 								};
110 							}
111 
112 							return null;
113 						} );
114 				}
115 			}
116 
117 			CKEDITOR.dialog.add( 'creatediv', this.path + 'dialogs/div.js' );
118 			CKEDITOR.dialog.add( 'editdiv', this.path + 'dialogs/div.js' );
119 		}
120 	} );
121 })();
122