This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
/
Resizer.js
199 lines (167 loc) · 8.49 KB
/
Resizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, window */
/**
* Resizer is a Module utility to inject resizing capabilities to any element
* inside Brackets.
*
* On initialization, Resizer discovers all nodes tagged as "ver-resizable"
* and "hor-resizable" to add the resizer handler. Additionally, "top-resizer",
* "bottom-resizer", "left-resizer" and "right-resizer" classes control the
* position of the resizer on the element.
*
* An element can be made resizable at any time using the `makeResizable` API
*
* The resizable elements trigger a panelResizeStart, panelResizeUpdate and panelResizeEnd
* event that can be used to create performance optimizations (such as hiding/showing elements
* while resizing), custom or internal resizes and save the final resized value into local
* storage for example.
*
* TODO Trigger panelResizeStart and panelResizeUpdate as required. They aren't needed
* currently.
*/
define(function (require, exports, module) {
"use strict";
var DIRECTION_VERTICAL = "ver";
var DIRECTION_HORIZONTAL = "hor";
var POSITION_TOP = "top";
var POSITION_BOTTOM = "bottom";
var POSITION_LEFT = "left";
var POSITION_RIGHT = "right";
// Minimum size (height or width) for autodiscovered resizable panels
var DEFAULT_MIN_SIZE = 100;
// Load dependent modules
var AppInit = require("utils/AppInit"),
EditorManager = require("editor/EditorManager");
var $mainView;
/**
* Adds resizing capabilities to a given html element.
*
* Resizing can be configured in two directions:
* - Vertical ("ver"): Resizes the height of the element
* - Horizontal ("hor"): Resizes the width of the element
*
* Resizer handlers can be positioned on the element at:
* - Top ("top") or bottom ("bottom") for vertical resizing
* - Left ("left") or right ("right") for horizontal resizing
*
* A resizable element triggers the following events while resizing:
* - panelResizeStart: When the resize starts
* - panelResizeUpdate: On resize updates (every time it is redrawn)
* - panelResizeEnds: When the resize ends
*
* @param {DOMNode} element Html element which should be made resizable.
* @param {string} direction The direction of the resize action. Must be "hor" or "ver".
* @param {string} position The position of the resizer on the element. Can be "top" or "bottom"
* for vertical resizing and "left" or "right" for horizontal resizing.
* @param {int} minSize Minimum size (width or height) of the element.
*/
function makeResizable(element, direction, position, minSize) {
var $resizer = $('<div class="' + direction + '-resizer"></div>'),
$element = $(element),
$resizableElement = $($element.find(".resizable-content:first")[0]),
$body = $(window.document.body),
animationRequest = null,
directionProperty = direction === DIRECTION_HORIZONTAL ? "clientX" : "clientY",
elementSizeFunction = direction === DIRECTION_HORIZONTAL ? $element.width : $element.height,
contentSizeFunction = null;
minSize = minSize || 0;
$element.prepend($resizer);
$resizer.on("mousedown", function (e) {
var startPosition = e[directionProperty],
startSize = elementSizeFunction.apply($element),
newSize = startSize,
baseSize = 0,
doResize = true,
isMouseDown = true;
if ($resizableElement !== undefined) {
$element.children().not(".hor-resizer, .ver-resizer, .resizable-content").each(function (index, child) {
if (direction === DIRECTION_HORIZONTAL) {
baseSize += $(child).outerWidth();
} else {
baseSize += $(child).outerHeight();
}
});
contentSizeFunction = direction === DIRECTION_HORIZONTAL ? $resizableElement.width : $resizableElement.height;
}
$body.toggleClass(direction + "-resizing");
animationRequest = window.webkitRequestAnimationFrame(function doRedraw() {
// only run this if the mouse is down so we don't constantly loop even
// after we're done resizing.
if (!isMouseDown) {
return;
}
if (doResize) {
// resize the main element to the new size
elementSizeFunction.apply($element, [newSize]);
// if there is a content element, its size is the new size
// minus the size of the non-resizable elements
if ($resizableElement !== undefined) {
contentSizeFunction.apply($resizableElement, [newSize - baseSize]);
}
EditorManager.resizeEditor();
}
animationRequest = window.webkitRequestAnimationFrame(doRedraw);
});
$mainView.on("mousemove", function (e) {
// calculate newSize adding to startSize the difference
// between starting and current position, capped at minSize
newSize = Math.max(startSize + (startPosition - e[directionProperty]), minSize);
e.preventDefault();
});
function endResize(e) {
if (isMouseDown) {
isMouseDown = false;
$mainView.off("mousemove");
$body.toggleClass(direction + "-resizing");
$element.trigger("panelResizeEnd", [elementSizeFunction.apply($element)]);
}
}
$mainView.one("mouseup", endResize);
$mainView.mouseleave(endResize);
e.preventDefault();
});
}
// Scan DOM for hor-resizable and ver-resizable classes and make them resizable
AppInit.htmlReady(function () {
$mainView = $(".main-view");
$(".ver-resizable").each(function (index, element) {
if ($(element).hasClass("top-resizer")) {
makeResizable(element, DIRECTION_VERTICAL, POSITION_TOP, DEFAULT_MIN_SIZE);
}
//if ($(element).hasClass("bottom-resizer")) {
// makeResizable(element, DIRECTION_VERTICAL, POSITION_BOTTOM, DEFAULT_MIN_SIZE);
//}
});
$(".hor-resizable").each(function (index, element) {
//if ($(element).hasClass("left-resizer")) {
// makeResizable(element, DIRECTION_HORIZONTAL, POSITION_LEFT, DEFAULT_MIN_SIZE);
//}
//if ($(element).hasClass("bottom-resizer")) {
// makeResizable(element, DIRECTION_HORIZONTAL, POSITION_RIGHT, DEFAULT_MIN_SIZE);
//}
});
});
exports.makeResizable = makeResizable;
});