JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / resize / plugin.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 CKEDITOR.plugins.add( 'resize',\r
7 {\r
8         init : function( editor )\r
9         {\r
10                 var config = editor.config;\r
11 \r
12                 // Resize in the same direction of chrome,\r
13                 // which is identical to dir of editor element. (#6614)\r
14                 var resizeDir = editor.element.getDirection( 1 );\r
15 \r
16                 !config.resize_dir && ( config.resize_dir = 'both' );\r
17                 ( config.resize_maxWidth == undefined ) && ( config.resize_maxWidth = 3000 );\r
18                 ( config.resize_maxHeight == undefined ) && ( config.resize_maxHeight = 3000 );\r
19                 ( config.resize_minWidth == undefined ) && ( config.resize_minWidth = 750 );\r
20                 ( config.resize_minHeight == undefined ) && ( config.resize_minHeight = 250 );\r
21 \r
22                 if ( config.resize_enabled !== false )\r
23                 {\r
24                         var container = null,\r
25                                 origin,\r
26                                 startSize,\r
27                                 resizeHorizontal = ( config.resize_dir == 'both' || config.resize_dir == 'horizontal' ) &&\r
28                                         ( config.resize_minWidth != config.resize_maxWidth ),\r
29                                 resizeVertical = ( config.resize_dir == 'both' || config.resize_dir == 'vertical' ) &&\r
30                                         ( config.resize_minHeight != config.resize_maxHeight );\r
31 \r
32                         function dragHandler( evt )\r
33                         {\r
34                                 var dx = evt.data.$.screenX - origin.x,\r
35                                         dy = evt.data.$.screenY - origin.y,\r
36                                         width = startSize.width,\r
37                                         height = startSize.height,\r
38                                         internalWidth = width + dx * ( resizeDir == 'rtl' ? -1 : 1 ),\r
39                                         internalHeight = height + dy;\r
40 \r
41                                 if ( resizeHorizontal )\r
42                                         width =  Math.max( config.resize_minWidth, Math.min( internalWidth, config.resize_maxWidth ) );\r
43 \r
44                                 if ( resizeVertical )\r
45                                         height =  Math.max( config.resize_minHeight, Math.min( internalHeight, config.resize_maxHeight ) );\r
46 \r
47                                 editor.resize( width, height );\r
48                         }\r
49 \r
50                         function dragEndHandler ( evt )\r
51                         {\r
52                                 CKEDITOR.document.removeListener( 'mousemove', dragHandler );\r
53                                 CKEDITOR.document.removeListener( 'mouseup', dragEndHandler );\r
54 \r
55                                 if ( editor.document )\r
56                                 {\r
57                                         editor.document.removeListener( 'mousemove', dragHandler );\r
58                                         editor.document.removeListener( 'mouseup', dragEndHandler );\r
59                                 }\r
60                         }\r
61 \r
62                         var mouseDownFn = CKEDITOR.tools.addFunction( function( $event )\r
63                                 {\r
64                                         if ( !container )\r
65                                                 container = editor.getResizable();\r
66 \r
67                                         startSize = { width : container.$.offsetWidth || 0, height : container.$.offsetHeight || 0 };\r
68                                         origin = { x : $event.screenX, y : $event.screenY };\r
69 \r
70                                         config.resize_minWidth > startSize.width && ( config.resize_minWidth = startSize.width );\r
71                                         config.resize_minHeight > startSize.height && ( config.resize_minHeight = startSize.height );\r
72 \r
73                                         CKEDITOR.document.on( 'mousemove', dragHandler );\r
74                                         CKEDITOR.document.on( 'mouseup', dragEndHandler );\r
75 \r
76                                         if ( editor.document )\r
77                                         {\r
78                                                 editor.document.on( 'mousemove', dragHandler );\r
79                                                 editor.document.on( 'mouseup', dragEndHandler );\r
80                                         }\r
81                                 });\r
82 \r
83                         editor.on( 'destroy', function() { CKEDITOR.tools.removeFunction( mouseDownFn ); } );\r
84 \r
85                         editor.on( 'themeSpace', function( event )\r
86                                 {\r
87                                         if ( event.data.space == 'bottom' )\r
88                                         {\r
89                                                 var direction = '';\r
90                                                 if ( resizeHorizontal && !resizeVertical )\r
91                                                         direction = ' cke_resizer_horizontal';\r
92                                                 if ( !resizeHorizontal && resizeVertical )\r
93                                                         direction = ' cke_resizer_vertical';\r
94 \r
95                                                 var resizerHtml =\r
96                                                         '<div' +\r
97                                                         ' class="cke_resizer' + direction + ' cke_resizer_' + resizeDir + '"' +\r
98                                                         ' title="' + CKEDITOR.tools.htmlEncode( editor.lang.resize ) + '"' +\r
99                                                         ' onmousedown="CKEDITOR.tools.callFunction(' + mouseDownFn + ', event)"' +\r
100                                                         '></div>';\r
101 \r
102                                                 // Always sticks the corner of botttom space.\r
103                                                 resizeDir == 'ltr' && direction == 'ltr' ?\r
104                                                         event.data.html += resizerHtml :\r
105                                                         event.data.html = resizerHtml + event.data.html;\r
106                                         }\r
107                                 }, editor, null, 100 );\r
108                 }\r
109         }\r
110 } );\r
111 \r
112 /**\r
113  * The minimum editor width, in pixels, when resizing it with the resize handle.\r
114  * Note: It fallbacks to editor's actual width if that's smaller than the default value.\r
115  * @name CKEDITOR.config.resize_minWidth\r
116  * @type Number\r
117  * @default 750\r
118  * @example\r
119  * config.resize_minWidth = 500;\r
120  */\r
121 \r
122 /**\r
123  * The minimum editor height, in pixels, when resizing it with the resize handle.\r
124  * Note: It fallbacks to editor's actual height if that's smaller than the default value.\r
125  * @name CKEDITOR.config.resize_minHeight\r
126  * @type Number\r
127  * @default 250\r
128  * @example\r
129  * config.resize_minHeight = 600;\r
130  */\r
131 \r
132 /**\r
133  * The maximum editor width, in pixels, when resizing it with the resize handle.\r
134  * @name CKEDITOR.config.resize_maxWidth\r
135  * @type Number\r
136  * @default 3000\r
137  * @example\r
138  * config.resize_maxWidth = 750;\r
139  */\r
140 \r
141 /**\r
142  * The maximum editor height, in pixels, when resizing it with the resize handle.\r
143  * @name CKEDITOR.config.resize_maxHeight\r
144  * @type Number\r
145  * @default 3000\r
146  * @example\r
147  * config.resize_maxHeight = 600;\r
148  */\r
149 \r
150 /**\r
151  * Whether to enable the resizing feature. If disabled the resize handler will not be visible.\r
152  * @name CKEDITOR.config.resize_enabled\r
153  * @type Boolean\r
154  * @default true\r
155  * @example\r
156  * config.resize_enabled = false;\r
157  */\r
158 \r
159 /**\r
160  * The directions to which the editor resizing is enabled. Possible values\r
161  * are "both", "vertical" and "horizontal".\r
162  * @name CKEDITOR.config.resize_dir\r
163  * @type String\r
164  * @default 'both'\r
165  * @since 3.3\r
166  * @example\r
167  * config.resize_dir = 'vertical';\r
168  */\r