UNPKG

2.09 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _util = require('../util');
6
7/**
8 * https://github.com/alibaba-fusion/next/issues/3638
9 * 通过栈的形式:解决弹窗关闭的时候对 document.body 上面的 style 恢复顺序问题
10 * 问题复现步骤:
11 * 1. dialog1 进来,把 body.style=overflow:hidden,如果关闭应该恢复 body.style=""
12 * 2. dialog2 进来,把 body.style=overflow:hidden, 因为此时 body.style是verflow:hidden,所以如果关闭应该恢复 body.style="verflow:hidden"
13 * 3. dialog1 关闭,恢复 body.style=""。造成问题:dialog2 页面可滚动
14 * 4. dialog2 关闭,恢复 body.style="verflow:hidden"。造成问题:页面不可滚动
15 * 解决方案:
16 * 1. 每个 dialog 修改 body.style 的时候都 push 入栈
17 * 2. 如果dialog1 先退出,发现 dialog2 存在则不做任何操作,把自己的恢复值给到 dialog2
18 */
19
20var lockcache = [];
21
22function lock(container, style) {
23 var originStyle = container.getAttribute('style');
24 var uuid = (0, _util.guid)();
25 lockcache.push({
26 uuid: uuid,
27 container: container,
28 originStyle: originStyle
29 });
30 _util.dom.setStyle(container, style);
31 return uuid;
32}
33
34function unlock(container, uuid) {
35 var list = lockcache.filter(function (i) {
36 return i.container === container;
37 });
38 var item = list.find(function (i) {
39 return i.uuid === uuid;
40 });
41 if (item) {
42 var idx = list.indexOf(item);
43 // 解锁的时候,链表有新增。说明 container style 已经被其他 Dialog 修改过了
44 if (idx !== -1 && idx < list.length - 1) {
45 var originStyle = item.originStyle;
46 // 下一个 dialog 解锁的时候直接替换为
47 list[idx + 1].originStyle = originStyle;
48 lockcache.splice(lockcache.indexOf(item), 1);
49
50 return;
51 }
52
53 container.setAttribute('style', item.originStyle || '');
54 lockcache.pop();
55 }
56}
57
58exports.default = {
59 lock: lock,
60 unlock: unlock
61};
62module.exports = exports['default'];
\No newline at end of file