forked from RubyLouvre/avalon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavalon.draggable.js
398 lines (373 loc) · 14.6 KB
/
avalon.draggable.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
define(["avalon"], function(avalon) {
var defaults = {
ghosting: false, //是否影子拖动,动态生成一个元素,拖动此元素,当拖动结束时,让原元素到达此元素的位置上,
delay: 0,
axis: "xy",
started: true,
start: avalon.noop,
beforeStart: avalon.noop,
drag: avalon.noop,
beforeStop: avalon.noop,
stop: avalon.noop,
scrollPlugin: true,
scrollSensitivity: 20,
scrollSpeed: 20
}
var body
var ua = navigator.userAgent;
var isAndroid = /Android/i.test(ua);
var isBlackBerry = /BlackBerry/i.test(ua)
var isWindowPhone = /IEMobile/i.test(ua)
var isIOS = /iPhone|iPad|iPod/i.test(ua)
var isMobile = isAndroid || isBlackBerry || isWindowPhone || isIOS
if (!isMobile) {
var dragstart = "mousedown"
var drag = "mousemove"
var dragstop = "mouseup"
} else {
dragstart = "touchstart"
drag = "touchmove"
dragstop = "touchend"
}
function filterData(obj, prefix) {
var result = {}
for (var i in obj) {
if (i.indexOf(prefix) === 0) {
result[ i.replace(prefix, "").replace(/\w/, function(a) {
return a.toLowerCase()
}) ] = obj[i]
}
}
return result
}
var draggable = avalon.bindingHandlers.draggable = function(data, vmodels) {
var ID = data.value.trim(), model
if (ID) {
model = avalon.vmodels[ID]//如果指定了此VM的ID
if (!model) {
data.remove = false
return
}
}
if (!model) {
model = vmodels.length ? vmodels[0].$model : {}
}
var element = data.element
var $element = avalon(element)
var options = avalon.mix({}, defaults, model, filterData($element.data(),"drag"));
//修正drag,stop为函数
"drag,stop,start,beforeStart,beforeStop".replace(avalon.rword, function(name) {
var method = options[name]
if (typeof method === "string") {
if (typeof model[method] === "function") {
options[name] = model[method]
} else {
options[name] = avalon.noop
}
}
})
if (options.axis !== "" && !/^(x|y|xy)$/.test(options.axis)) {
options.axis = "xy"
}
body = document.body //因为到这里时,肯定已经domReady
$element.bind(dragstart, function(e) {
var data = avalon.mix({}, options, {
element: element,
$element: $element,
pageX: getPosition(e, "X"), //相对于页面的坐标, 会改动
pageY: getPosition(e, "Y"), //相对于页面的坐标,会改动
marginLeft: parseFloat($element.css("marginLeft")) || 0,
marginTop: parseFloat($element.css("marginTop")) || 0
})
data.startPageX = data.pageX//一次拖放只赋值一次
data.startPageY = data.pageY//一次拖放只赋值一次
options.axis.replace(/./g, function(a) {
data["drag" + a.toUpperCase() ] = true
})
if (!data.dragX && !data.dragY) {
data.started = false
}
//在处理手柄拖动前做些事情
if (typeof options.beforeStart === "function") {
options.beforeStart.call(data.element, e, data)
}
if (data.handle && model) {// 实现手柄拖动
var handle = model[data.handle]
if (typeof handle === "function") {
var checked = handle.call(element, e, data)//要求返回一节点
if (checked && checked.nodeType === 1) {
if (!element.contains(checked)) {
return // 不能返回 false,这会阻止文本被选择
}
} else {
return
}
}
}
fixUserSelect()
var position = $element.css("position")
//如果原元素没有被定位
if (!/^(?:r|a|f)/.test(position)) {
element.style.position = "relative";
element.style.top = "0px"
element.style.left = "0px"
}
if (options.delay && isFinite(options.delay)) {
data.started = false;
setTimeout(function() {
data.started = true
}, options.delay)
}
var startOffset = $element.offset()
if (options.ghosting) {
var clone = element.cloneNode(true)
avalon(clone).css("opacity", .7).width(element.offsetWidth).height(element.offsetHeight)
data.clone = clone
if (position !== "fixed") {
clone.style.position = "absolute"
clone.style.top = startOffset.top - data.marginTop + "px"
clone.style.left = startOffset.left - data.marginLeft + "px"
}
body.appendChild(clone)
}
var target = avalon(data.clone || data.element)
//拖动前相对于offsetParent的坐标
data.startLeft = parseFloat(target.css("left"))
data.startTop = parseFloat(target.css("top"))
//拖动后相对于offsetParent的坐标
//如果是影子拖动,代理元素是绝对定位时,它与原元素的top, left是不一致的,因此当结束拖放时,不能直接将改变量赋给原元素
data.endLeft = parseFloat($element.css("left")) - data.startLeft
data.endTop = parseFloat($element.css("top")) - data.startTop
data.clickX = data.pageX - startOffset.left //鼠标点击的位置与目标元素左上角的距离
data.clickY = data.pageY - startOffset.top //鼠标点击的位置与目标元素左上角的距离
setContainment(options, data)//修正containment
draggable.dragData = data//决定有东西在拖动
"start,drag,beforeStop,stop".replace(avalon.rword, function(name) {
draggable[name] = [options[name]]
})
draggable.plugin.call("start", e, data)
})
}
var xy2prop = {
"X": "Left",
"Y": "Top"
}
//插件系统
draggable.dragData = {}
draggable.start = []
draggable.drag = []
draggable.stop = []
draggable.beforeStop = []
draggable.plugin = {
add: function(name, set) {
for (var i in set) {
var fn = set[i]
if (typeof fn === "function" && Array.isArray(draggable[i])) {
fn.isPlugin = true
fn.pluginName = name + "Plugin"
draggable[i].push(fn)
}
}
},
call: function(name, e, data) {
var array = draggable[name]
if (Array.isArray(array)) {
array.forEach(function(fn) {
//用户回调总会执行,插件要看情况
if (typeof fn.pluginName === "undefined" ? true : data[fn.pluginName]) {
fn.call(data.element, e, data)
}
})
}
if (name === "stop") {
for (var i in draggable) {
array = draggable[i]
if (Array.isArray(array)) {
array.forEach(function(fn) {
if (!fn.isPlugin) {// 用户回调都是一次性的,插件的方法永远放在列队中
avalon.Array.remove(array, fn)
}
})
}
}
}
}
}
function getPosition(e, pos) {
var page = "page" + pos
return isMobile ? e.changedTouches[0][page] : e[page]
}
function setPosition(e, element, data, pos, end) {
var page = getPosition(e, pos)
if (data.containment) {
var min = pos === "X" ? data.containment[0] : data.containment[1]
var max = pos === "X" ? data.containment[2] : data.containment[3]
var check = page - (pos === "X" ? data.clickX : data.clickY)
if (check < min) {
page += Math.abs(min - check)
} else if (check > max) {
page -= Math.abs(max - check)
}
}
data["page" + pos] = page//重设pageX, pageY
var Prop = xy2prop[pos]
var prop = Prop.toLowerCase()
var number = data["start" + Prop] + page - data["startPage" + pos] + (end ? data["end" + Prop] : 0)
data[prop] = number
if (data["drag" + pos]) {//保存top, left
element.style[ prop ] = number + "px"
}
}
var styleEl = document.createElement("style")
var cssText = "*{ -webkit-touch-callout: none!important;-webkit-user-select: none!important;-khtml-user-select: none!important;" +
"-moz-user-select: none!important;-ms-user-select: none!important;user-select: none!important;}"
var fixUserSelect = function() {
body.appendChild(styleEl)
//如果不插入DOM树,styleEl.styleSheet为null
if (typeof styleEl.styleSheet === "object") {
styleEl.styleSheet.cssText = cssText
} else {
styleEl.appendChild(document.createTextNode(cssText))
}
}
var restoreUserSelect = function() {
try {
styleEl.innerHTML = ""
} catch (e) {
styleEl.styleSheet.cssText = ""
}
body.removeChild(styleEl)
}
if (window.VBArray && !("msUserSelect" in document.documentElement.style)) {
var _ieSelectBack;//fix IE6789
function returnFalse() {
return false;
}
fixUserSelect = function() {
_ieSelectBack = body.onselectstart;
body.onselectstart = returnFalse;
}
restoreUserSelect = function() {
body.onselectstart = _ieSelectBack;
}
}
//统一处理拖动的事件
avalon(document).bind(drag, function(e) {
var data = draggable.dragData
if (data.started === true) {
//fix touchmove bug;
//IE 在 img 上拖动时默认不能拖动(不触发 mousemove,mouseup 事件,mouseup 后接着触发 mousemove ...)
//防止 html5 draggable 元素的拖放默认行为 (选中文字拖放)
e.preventDefault();
//使用document.selection.empty()来清除选择,会导致捕获失败
var element = data.clone || data.element
setPosition(e, element, data, "X")
setPosition(e, element, data, "Y")
draggable.plugin.call("drag", e, data)
}
})
//统一处理拖动结束的事件
avalon(document).bind(dragstop, function(e) {
var data = draggable.dragData
if (data.started === true) {
restoreUserSelect()
var element = data.element
draggable.plugin.call("beforeStop", e, data)
if (data.dragX) {
setPosition(e, element, data, "X", true)
}
if (data.dragY) {
setPosition(e, element, data, "Y", true)
}
if (data.clone) {
body.removeChild(data.clone)
}
draggable.plugin.call("stop", e, data)
draggable.dragData = {}
}
})
function getWindow(node) {
return node.window && node.document ? node : node.nodeType === 9 ? node.defaultView || node.parentWindow : false;
}
function setContainment(o, data) {
if (!o.containment) {
if (Array.isArray(data.containment)) {
return
}
data.containment = null;
return;
}
if (o.containment === "window") {
var $window = avalon(window)
//左, 上, 右, 下
data.containment = [
$window.scrollLeft(),
$window.scrollTop(),
$window.scrollLeft() + $window.width() - data.marginLeft,
$window.scrollTop() + $window.height() - data.marginTop
];
return;
}
if (o.containment === "document") {
data.containment = [
0,
0,
avalon(document).width() - data.marginLeft,
avalon(document).height() - data.marginTop
];
return;
}
if (Array.isArray(o.containment)) {
data.containment = o.containment;
return;
}
if (o.containment === "parent" || o.containment.charAt(0) === "#") {
var elem
if (o.containment === "parent") {
elem = data.element.parentNode;
} else {
elem = document.getElementById(o.containment.slice(1))
}
if (elem) {
var $offset = avalon(elem).offset()
data.containment = [
$offset.left,
$offset.top,
Math.floor($offset.left + elem.offsetWidth - data.marginLeft - data.$element.width()),
Math.floor($offset.top + elem.offsetHeight - data.marginTop - data.$element.height())
]
}
}
}
return avalon
})
/*
ms-draggable="VMID?" , VMID为一个VM的ID,可选
下面这些全部可用data-drag-*进行配置
drag 为VM中一个方法名
stop 为VM中一个方法名
start 为VM中一个方法名
handle 要求为VM中的一个函数,它会重置data.handle为一个元素节点,如果事件源位于data.handle的里面或等于它则继续进行操作
ghosting: false, //是否影子拖动,动态生成一个元素,拖动此元素,当拖动结束时,让原元素到达此元素的位置上,
delay: 0, 延迟时间
axis: "xy" "x", "y" 决定只能垂直拖动,还是水平拖动,还是任意拖动
containment: 拖动范围,#id值, "window", "document", "parent", "[0, 0, 400, 300]"
<body ms-controller="xxx">
<ul ms-each-el="array">
<li ms-draggable="xxx" >item {{$index}}</li>
</ul>
</body>
avalon.require("avalon.draggable", function() {
avalon.define("xxx", function(vm) {
vm.array = avalon.range(0, 10)
vm.drag = function(e, data) {
console.log(e.pageX + " : " + e.pageY)
}
vm.stop = function(e, data) {
console.log("done")
}
})
avalon.scan()
})
*
*/