1/**
2 * @license Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
5
6/**
7 * @fileOverview Image plugin
8 */
9
10(function() {
11
12	CKEDITOR.plugins.add( 'image', {
13		requires: 'dialog',
14		lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en-au,en-ca,en-gb,en,eo,es,et,eu,fa,fi,fo,fr-ca,fr,gl,gu,he,hi,hr,hu,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt-br,pt,ro,ru,sk,sl,sr-latn,sr,sv,th,tr,ug,uk,vi,zh-cn,zh', // %REMOVE_LINE_CORE%
15		icons: 'image', // %REMOVE_LINE_CORE%
16		init: function( editor ) {
17			var pluginName = 'image';
18
19			// Register the dialog.
20			CKEDITOR.dialog.add( pluginName, this.path + 'dialogs/image.js' );
21
22			// Register the command.
23			editor.addCommand( pluginName, new CKEDITOR.dialogCommand( pluginName ) );
24
25			// Register the toolbar button.
26			editor.ui.addButton && editor.ui.addButton( 'Image', {
27				label: editor.lang.common.image,
28				command: pluginName,
29				toolbar: 'insert,10'
30			});
31
32			editor.on( 'doubleclick', function( evt ) {
33				var element = evt.data.element;
34
35				if ( element.is( 'img' ) && !element.data( 'cke-realelement' ) && !element.isReadOnly() )
36					evt.data.dialog = 'image';
37			});
38
39			// If the "menu" plugin is loaded, register the menu items.
40			if ( editor.addMenuItems ) {
41				editor.addMenuItems({
42					image: {
43						label: editor.lang.image.menu,
44						command: 'image',
45						group: 'image'
46					}
47				});
48			}
49
50			// If the "contextmenu" plugin is loaded, register the listeners.
51			if ( editor.contextMenu ) {
52				editor.contextMenu.addListener( function( element, selection ) {
53					if ( getSelectedImage( editor, element ) )
54						return { image: CKEDITOR.TRISTATE_OFF };
55				});
56			}
57		},
58		afterInit: function( editor ) {
59			// Customize the behavior of the alignment commands. (#7430)
60			setupAlignCommand( 'left' );
61			setupAlignCommand( 'right' );
62			setupAlignCommand( 'center' );
63			setupAlignCommand( 'block' );
64
65			function setupAlignCommand( value ) {
66				var command = editor.getCommand( 'justify' + value );
67				if ( command ) {
68					if ( value == 'left' || value == 'right' ) {
69						command.on( 'exec', function( evt ) {
70							var img = getSelectedImage( editor ),
71								align;
72							if ( img ) {
73								align = getImageAlignment( img );
74								if ( align == value ) {
75									img.removeStyle( 'float' );
76
77									// Remove "align" attribute when necessary.
78									if ( value == getImageAlignment( img ) )
79										img.removeAttribute( 'align' );
80								} else
81									img.setStyle( 'float', value );
82
83								evt.cancel();
84							}
85						});
86					}
87
88					command.on( 'refresh', function( evt ) {
89						var img = getSelectedImage( editor ),
90							align;
91						if ( img ) {
92							align = getImageAlignment( img );
93
94							this.setState(
95							( align == value ) ? CKEDITOR.TRISTATE_ON : ( value == 'right' || value == 'left' ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
96
97							evt.cancel();
98						}
99					});
100				}
101			}
102		}
103	});
104
105	function getSelectedImage( editor, element ) {
106		if ( !element ) {
107			var sel = editor.getSelection();
108			element = sel.getSelectedElement();
109		}
110
111		if ( element && element.is( 'img' ) && !element.data( 'cke-realelement' ) && !element.isReadOnly() )
112			return element;
113	}
114
115	function getImageAlignment( element ) {
116		var align = element.getStyle( 'float' );
117
118		if ( align == 'inherit' || align == 'none' )
119			align = 0;
120
121		if ( !align )
122			align = element.getAttribute( 'align' );
123
124		return align;
125	}
126
127})();
128
129/**
130 * Whether to remove links when emptying the link URL field in the image dialog.
131 *
132 *		config.image_removeLinkByEmptyURL = false;
133 *
134 * @cfg {Boolean} [image_removeLinkByEmptyURL=true]
135 * @member CKEDITOR.config
136 */
137CKEDITOR.config.image_removeLinkByEmptyURL = true;
138
139/**
140 * Padding text to set off the image in preview area.
141 *
142 *		config.image_previewText = CKEDITOR.tools.repeat( '___ ', 100 );
143 *
144 * @cfg {String} [image_previewText='Lorem ipsum dolor...' (placeholder text)]
145 * @member CKEDITOR.config
146 */
147