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