-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalign.js
More file actions
500 lines (431 loc) · 15.7 KB
/
align.js
File metadata and controls
500 lines (431 loc) · 15.7 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
Copyright 2013, KISSY v1.40dev
MIT Licensed
build time: Sep 17 22:57
*/
/*
Combined processedModules by KISSY Module Compiler:
component/extension/align
*/
/**
* @ignore
* Component.Extension.Align
* @author yiminghe@gmail.com, qiaohua@taobao.com
*/
KISSY.add(function (S, Node) {
var win = S.Env.host,
$ = Node.all,
UA = S.UA;
// http://yiminghe.iteye.com/blog/1124720
/**
* @ignore
* 得到会导致元素显示不全的祖先元素
*/
function getOffsetParent(element) {
// ie 这个也不是完全可行
/*
<div style="width: 50px;height: 100px;overflow: hidden">
<div style="width: 50px;height: 100px;position: relative;" id="d6">
元素 6 高 100px 宽 50px<br/>
</div>
</div>
*/
// element.offsetParent does the right thing in ie7 and below. Return parent with layout!
// In other browsers it only includes elements with position absolute, relative or
// fixed, not elements with overflow set to auto or scroll.
// if (UA.ie && ieMode < 8) {
// return element.offsetParent;
// }
// 统一的 offsetParent 方法
var doc = element.ownerDocument,
body = doc.body,
parent,
positionStyle = $(element).css('position'),
skipStatic = positionStyle == 'fixed' || positionStyle == 'absolute';
if (!skipStatic) {
return element.nodeName.toLowerCase() == 'html' ? null : element.parentNode;
}
for (parent = element.parentNode; parent && parent != body; parent = parent.parentNode) {
positionStyle = $(parent).css('position');
if (positionStyle != "static") {
return parent;
}
}
return null;
}
/**
* @ignore
* 获得元素的显示部分的区域
*/
function getVisibleRectForElement(element) {
var visibleRect = {
left: 0,
right: Infinity,
top: 0,
bottom: Infinity
},
el,
scrollX,
scrollY,
winSize,
doc = element.ownerDocument,
$win = $(doc).getWindow(),
body = doc.body,
documentElement = doc.documentElement;
// Determine the size of the visible rect by climbing the dom accounting for
// all scrollable containers.
for (el = element; el = getOffsetParent(el);) {
// clientWidth is zero for inline block elements in ie.
if ((!UA.ie || el.clientWidth != 0) &&
// body may have overflow set on it, yet we still get the entire
// viewport. In some browsers, el.offsetParent may be
// document.documentElement, so check for that too.
(el != body &&
el != documentElement &&
$(el).css('overflow') != 'visible')) {
var pos = $(el).offset();
// add border
pos.left += el.clientLeft;
pos.top += el.clientTop;
visibleRect.top = Math.max(visibleRect.top, pos.top);
visibleRect.right = Math.min(visibleRect.right,
// consider area without scrollBar
pos.left + el.clientWidth);
visibleRect.bottom = Math.min(visibleRect.bottom,
pos.top + el.clientHeight);
visibleRect.left = Math.max(visibleRect.left, pos.left);
}
}
// Clip by window's viewport.
scrollX = $win.scrollLeft();
scrollY = $win.scrollTop();
visibleRect.left = Math.max(visibleRect.left, scrollX);
visibleRect.top = Math.max(visibleRect.top, scrollY);
winSize = {
width: $win.width(),
height: $win.height()
};
visibleRect.right = Math.min(visibleRect.right, scrollX + winSize.width);
visibleRect.bottom = Math.min(visibleRect.bottom, scrollY + winSize.height);
return visibleRect.top >= 0 && visibleRect.left >= 0 &&
visibleRect.bottom > visibleRect.top &&
visibleRect.right > visibleRect.left ?
visibleRect : null;
}
function getElFuturePos(elRegion, refNodeRegion, points, offset) {
var xy,
diff,
p1,
p2;
xy = {
left: elRegion.left,
top: elRegion.top
};
p1 = getAlignOffset(refNodeRegion, points[0]);
p2 = getAlignOffset(elRegion, points[1]);
diff = [p2.left - p1.left, p2.top - p1.top];
return {
left: xy.left - diff[0] + (+offset[0]),
top: xy.top - diff[1] + (+offset[1])
};
}
function isFailX(elFuturePos, elRegion, visibleRect) {
return elFuturePos.left < visibleRect.left ||
elFuturePos.left + elRegion.width > visibleRect.right;
}
function isFailY(elFuturePos, elRegion, visibleRect) {
return elFuturePos.top < visibleRect.top ||
elFuturePos.top + elRegion.height > visibleRect.bottom;
}
function adjustForViewport(elFuturePos, elRegion, visibleRect, overflow) {
var pos = S.clone(elFuturePos),
size = {
width: elRegion.width,
height: elRegion.height
};
if (overflow.adjustX && pos.left < visibleRect.left) {
pos.left = visibleRect.left;
}
// Left edge inside and right edge outside viewport, try to resize it.
if (overflow['resizeWidth'] &&
pos.left >= visibleRect.left &&
pos.left + size.width > visibleRect.right) {
size.width -= (pos.left + size.width) - visibleRect.right;
}
// Right edge outside viewport, try to move it.
if (overflow.adjustX && pos.left + size.width > visibleRect.right) {
// 保证左边界和可视区域左边界对齐
pos.left = Math.max(visibleRect.right - size.width, visibleRect.left);
}
// Top edge outside viewport, try to move it.
if (overflow.adjustY && pos.top < visibleRect.top) {
pos.top = visibleRect.top;
}
// Top edge inside and bottom edge outside viewport, try to resize it.
if (overflow['resizeHeight'] &&
pos.top >= visibleRect.top &&
pos.top + size.height > visibleRect.bottom) {
size.height -= (pos.top + size.height) - visibleRect.bottom;
}
// Bottom edge outside viewport, try to move it.
if (overflow.adjustY && pos.top + size.height > visibleRect.bottom) {
// 保证上边界和可视区域上边界对齐
pos.top = Math.max(visibleRect.bottom - size.height, visibleRect.top);
}
return S.mix(pos, size);
}
function flip(points, reg, map) {
var ret = [];
S.each(points, function (p) {
ret.push(p.replace(reg, function (m) {
return map[m];
}));
});
return ret;
}
function flipOffset(offset, index) {
offset[index] = -offset[index];
return offset;
}
/**
* @class KISSY.Component.Extension.Align
* Align extension class.Align component with specified element.
*/
function Align() {
}
Align.__getOffsetParent = getOffsetParent;
Align.__getVisibleRectForElement = getVisibleRectForElement;
Align.ATTRS =
{
/**
* alignment config.
* @type {Object}
* @property align
*
* for example:
* @example
* {
* node: null, // 参考元素, falsy 或 window 为可视区域, 'trigger' 为触发元素, 其他为指定元素
* points: ['cc','cc'], // ['tr', 'tl'] 表示 overlay 的 tl 与参考节点的 tr 对齐
* offset: [0, 0] // 有效值为 [n, m]
* }
*/
/**
* alignment config.
* @cfg {Object} align
*
* for example:
* @example
* {
* node: null, // 参考元素, falsy 或 window 为可视区域, 'trigger' 为触发元素, 其他为指定元素
* points: ['cc','cc'], // ['tr', 'tl'] 表示 overlay 的 tl 与参考节点的 tr 对齐
* offset: [0, 0] // 有效值为 [n, m]
* }
*/
/**
* @ignore
*/
align: {
value: {}
}
};
function getRegion(node) {
var offset, w, h,
domNode = node[0];
if (!S.isWindow(domNode)) {
offset = node.offset();
w = node.outerWidth();
h = node.outerHeight();
} else {
var $win = $(domNode).getWindow();
offset = {
left: $win.scrollLeft(),
top: $win.scrollTop()
};
w = $win.width();
h = $win.height();
}
offset.width = w;
offset.height = h;
return offset;
}
/**
* 获取 node 上的 align 对齐点 相对于页面的坐标
* @param region
* @param align
* @ignore
*/
function getAlignOffset(region, align) {
var V = align.charAt(0),
H = align.charAt(1),
w = region.width,
h = region.height,
x, y;
x = region.left;
y = region.top;
if (V === 'c') {
y += h / 2;
} else if (V === 'b') {
y += h;
}
if (H === 'c') {
x += w / 2;
} else if (H === 'r') {
x += w;
}
return { left: x, top: y };
}
function beforeVisibleChange(e) {
if (e.target == this && e.newVal) {
realign.call(this);
}
}
function onResize() {
if (this.get('visible')) {
realign.call(this);
}
}
function realign() {
this._onSetAlign(this.get('align'));
}
Align.prototype = {
__bindUI: function () {
// auto align on window resize or before el show
var self = this;
self.on('beforeVisibleChange', beforeVisibleChange, self);
self.$el.getWindow().on('resize', onResize, self);
},
'_onSetAlign': function (v) {
if (v && v.points) {
this.align(v.node, v.points, v.offset, v.overflow);
}
},
/*
* 对齐 Overlay 到 node 的 points 点, 偏移 offset 处
* @ignore
* @param {Element} node 参照元素, 可取配置选项中的设置, 也可是一元素
* @param {String[]} points 对齐方式
* @param {Number[]} [offset] 偏移
* @chainable
*/
align: function (refNode, points, offset, overflow) {
refNode = Node.one(refNode || win);
offset = offset && [].concat(offset) || [0, 0];
overflow = overflow || {};
var self = this,
el = self.$el,
fail = 0;
// 当前节点可以被放置的显示区域
var visibleRect = getVisibleRectForElement(el[0]);
// 当前节点所占的区域, left/top/width/height
var elRegion = getRegion(el);
// 参照节点所占的区域, left/top/width/height
var refNodeRegion = getRegion(refNode);
// 当前节点将要被放置的位置
var elFuturePos = getElFuturePos(elRegion,
refNodeRegion, points, offset);
// 当前节点将要所处的区域
var newElRegion = S.merge(elRegion, elFuturePos);
// 如果可视区域不能完全放置当前节点时允许调整
if (visibleRect && (overflow.adjustX || overflow.adjustY)) {
// 如果横向不能放下
if (isFailX(elFuturePos, elRegion, visibleRect)) {
fail = 1;
// 对齐位置反下
points = flip(points, /[lr]/ig, {
l: "r",
r: "l"
});
// 偏移量也反下
offset = flipOffset(offset, 0);
}
// 如果纵向不能放下
if (isFailY(elFuturePos, elRegion, visibleRect)) {
fail = 1;
// 对齐位置反下
points = flip(points, /[tb]/ig, {
t: "b",
b: "t"
});
// 偏移量也反下
offset = flipOffset(offset, 1);
}
// 如果失败,重新计算当前节点将要被放置的位置
if (fail) {
elFuturePos = getElFuturePos(elRegion, refNodeRegion, points, offset);
S.mix(newElRegion, elFuturePos);
}
//var newOverflowCfg = {};
// 检查反下后的位置是否可以放下了
// 如果仍然放不下只有指定了可以调整当前方向才调整
// newOverflowCfg.adjustX = overflow.adjustX &&
// isFailX(elFuturePos, elRegion, visibleRect);
//
// newOverflowCfg.adjustY = overflow.adjustY &&
// isFailY(elFuturePos, elRegion, visibleRect);
//
// 确实要调整,甚至可能会调整高度宽度
// if (newOverflowCfg.adjustX || newOverflowCfg.adjustY) {
// newElRegion = adjustForViewport(elFuturePos, elRegion,
// visibleRect, newOverflowCfg);
// }
}
// https://github.com/kissyteam/kissy/issues/190
// http://localhost:8888/kissy/src/overlay/demo/other/relative_align/align.html
// 相对于屏幕位置没变,而 left/top 变了
// 例如 <div 'relative'><el absolute></div>
self.set({
"x": newElRegion.left,
"y": newElRegion.top
}, {
force: 1
});
// need judge to in case set fixed with in css on height auto element
if (newElRegion.width != elRegion.width) {
self.set('width', el.width() + newElRegion.width - elRegion.width);
}
if (newElRegion.height != elRegion.height) {
self.set('height', el.height() + newElRegion.height - elRegion.height);
}
self.realAlign = {
node: refNode,
points: points,
offset: [newElRegion.left, newElRegion.top]
};
return self;
},
/**
* Make current element center within node.
* @param {undefined|String|HTMLElement|KISSY.NodeList} node
* Same as node config of {@link KISSY.Component.Extension.Align#cfg-align}
* @chainable
*/
center: function (node) {
var self = this;
self.set('align', {
node: node,
points: ["cc", "cc"],
offset: [0, 0]
});
return self;
},
__destructor: function () {
if (this.$el) {
this.$el.getWindow().detach('resize', realign, this);
}
}
};
return Align;
}, {
requires: ["node"]
});
/**
* @ignore
*
* 2012-04-26 yiminghe@gmail.com
* - 优化智能对齐算法
* - 慎用 resizeXX
*
* 2011-07-13 yiminghe@gmail.com note:
* - 增加智能对齐,以及大小调整选项
**/