draggable.js
6.81 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Simple draggable tool, just for demo or testing.
* Use jquery.
*/
(function (global) {
var BORDER_WIDTH = 4;
var $ = global.jQuery;
global.draggable = {
/**
* @param {HTMLElement} mainEl
* @param {module:echarts/echarts~EChart} chart
* @param {Object} [opt] {width: ..., height: ...}
* @param {number} [opt.width] If not specified, use mainEl current width.
* @param {number} [opt.height] If not specified, use mainEl current height.
* @param {boolean} [opt.lockX=false]
* @param {boolean} [opt.lockY=false]
* @param {number} [opt.throttle=false]
* @return {type} description
*/
init: function (mainEl, chart, opt) {
opt = opt || {};
var chartResize = chart ? $.proxy(chart.resize, chart) : function () {};
if (opt.throttle) {
chartResize = throttle(chartResize, opt.throttle, true, false);
}
var mainEl = $(mainEl);
$('.draggable-control').remove();
var controlEl = $(
'<div class="draggable-control">DRAG<span class="draggable-label"></span></div>'
);
controlEl.css({
'position': 'absolute',
'border-radius': '30px',
'width': '60px',
'height': '60px',
'line-height': '60px',
'text-align': 'center',
'background': '#333',
'color': '#fff',
'cursor': 'pointer',
'font-size': '18px',
'box-shadow': '0 0 5px #333',
'-webkit-user-select': 'none',
'user-select': 'none'
});
var label = controlEl.find('.draggable-label');
label.css({
'display': 'block',
'position': 'absolute',
'color': '#000',
'font-size': '12px',
'text-align': 'center',
'left': 0,
'top': '65px',
'width': '60px',
'line-height': 1
});
mainEl.css({
'position': 'absolute',
'left': mainEl[0].offsetLeft + 'px',
'top': mainEl[0].offsetTop + 'px',
'width': mainEl[0].offsetWidth + 'px',
'height': mainEl[0].offsetHeight + 'px',
'border-style': 'solid',
'border-color': '#ddd',
'border-width': BORDER_WIDTH + 'px',
'padding': 0,
'margin': 0
});
mainEl.parent().append(controlEl);
var controlSize = controlEl[0].offsetWidth;
var boxSizing = mainEl.css('box-sizing');
var borderBoxBroder = boxSizing === 'border-box' ? 2 * BORDER_WIDTH : 0;
var mainContentWidth = opt.width || (mainEl.width() + borderBoxBroder);
var mainContentHeight = opt.height || (mainEl.height() + borderBoxBroder);
var mainOffset = mainEl.offset();
resize(
mainOffset.left + mainContentWidth + BORDER_WIDTH,
mainOffset.top + mainContentHeight + BORDER_WIDTH,
true
);
var dragging = false;
controlEl.on('mousedown', function () {
dragging = true;
});
$(document).on('mousemove', function (e) {
if (dragging) {
resize(e.pageX, e.pageY);
}
});
$(document).on('mouseup', function () {
dragging = false;
});
function resize(x, y, isInit) {
var mainOffset = mainEl.offset();
var mainPosition = mainEl.position();
var mainContentWidth = x - mainOffset.left - BORDER_WIDTH;
var mainContentHeight = y - mainOffset.top - BORDER_WIDTH;
if (isInit || !opt.lockX) {
controlEl.css(
'left',
(mainPosition.left + mainContentWidth + BORDER_WIDTH - controlSize / 2) + 'px'
);
mainEl.css(
'width',
(mainContentWidth + borderBoxBroder) + 'px'
);
}
if (isInit || !opt.lockY) {
controlEl.css(
'top',
(mainPosition.top + mainContentHeight + BORDER_WIDTH - controlSize / 2) + 'px'
);
mainEl.css(
'height',
(mainContentHeight + borderBoxBroder) + 'px'
);
}
label.text(Math.round(mainContentWidth) + ' x ' + Math.round(mainContentHeight));
chartResize();
}
}
};
function throttle(fn, delay, trailing, debounce) {
var currCall = (new Date()).getTime();
var lastCall = 0;
var lastExec = 0;
var timer = null;
var diff;
var scope;
var args;
var isSingle = typeof fn === 'function';
delay = delay || 0;
if (isSingle) {
return createCallback();
}
else {
var ret = [];
for (var i = 0; i < fn.length; i++) {
ret[i] = createCallback(i);
}
return ret;
}
function createCallback(index) {
function exec() {
lastExec = (new Date()).getTime();
timer = null;
(isSingle ? fn : fn[index]).apply(scope, args || []);
}
var cb = function () {
currCall = (new Date()).getTime();
scope = this;
args = arguments;
diff = currCall - (debounce ? lastCall : lastExec) - delay;
clearTimeout(timer);
if (debounce) {
if (trailing) {
timer = setTimeout(exec, delay);
}
else if (diff >= 0) {
exec();
}
}
else {
if (diff >= 0) {
exec();
}
else if (trailing) {
timer = setTimeout(exec, -diff);
}
}
lastCall = currCall;
};
/**
* Clear throttle.
* @public
*/
cb.clear = function () {
if (timer) {
clearTimeout(timer);
timer = null;
}
};
return cb;
}
}
})(window);