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