-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proxy.html
164 lines (153 loc) · 5.35 KB
/
Proxy.html
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
<html><head><title>Proxy-代理模式</title><meta charset="utf-8"></head>
<body>
<style type="text/css">
body{padding: :0;margin:0;overflow: hidden;font-family: "微软雅黑"}
#modal-dialog{border: 3px solid green;padding: 10px;}
#modal-close{position: absolute;top: -20px;right: -18px;cursor: pointer;border: 2px solid green;padding: 1px;border-radius:50% 50%;width: 15px;height: 15px;text-align: center;line-height: 12px;color: green;font-weight: bold;}
#modal-loading{text-align: center;}
</style>
<script type="text/javascript">
var MyNamespace = window.MyNamespace || {};
// 定义在component模块
MyNamespace.component = (function(){
function Modal(option){
}
Modal.prototype = {
_addListener: function(){
var that = this;
window.onresize = function(){
that._resizeMask();
that._repositionDialog();
}
this.closeBtn.onclick = function(){
that.mask.parentNode.removeChild(that.mask);
that.dialog.parentNode.removeChild(that.dialog);
}
},
_resizeMask: function(){
this.mask.style.width = '100%';
this.mask.style.height = document.body.clientHeight;
},
_repositionDialog: function(){
this.dialog.style.left = (document.body.clientWidth-this.dialog.offsetWidth)/2;
this.dialog.style.top = (document.body.clientHeight-this.dialog.offsetHeight)/3;
},
show: function(){
var body = document.getElementsByTagName('body')[0];
//mask
this.mask = document.createElement('div');
this.mask.id = 'modal-mask';
this.mask.style.position = 'absolute';
this.mask.style.left = 0;
this.mask.style.top = 0;
this.mask.style.background = '#000';
this.mask.style.opacity = '0.5';
this.mask.style.filter = 'alpha(opacity=50)';
this._resizeMask();
body.appendChild(this.mask);
//diaglog
this.dialog = document.createElement('div');
this.dialog.innerHTML = 'HELLO MODAL!';
this.dialog.id = 'modal-dialog';
this.dialog.style.position = 'absolute';
//close-button
this.closeBtn = document.createElement('div');
var closeText = document.createTextNode('x');
this.closeBtn.id = 'modal-close';
this.closeBtn.appendChild(closeText);
this.dialog.appendChild(this.closeBtn);
body.appendChild(this.dialog);
this._repositionDialog();
this._addListener();
}
}
function ModalProxy(){
this.interval = null;
this.modal = new Modal();
this._initialize();
}
ModalProxy.prototype = {
_removeLoading: function(){
this.mask.parentNode.removeChild(this.mask);
this.loading.parentNode.removeChild(this.loading);
},
_resizeMask: function(){
this.mask.style.width = '100%';
this.mask.style.height = document.body.clientHeight;
},
_repositionLoading: function(){
this.loading.style.left = (document.body.clientWidth-this.loading.offsetWidth)/2;
this.loading.style.top = (document.body.clientHeight-this.loading.offsetHeight)/3;
},
_initialize: function(){
var that = this;
var body = document.getElementsByTagName('body')[0];
//mask
this.mask = document.createElement('div');
this.mask.id = 'modal-loading-mask';
this.mask.style.position = 'absolute';
this.mask.style.left = 0;
this.mask.style.top = 0;
this.mask.style.background = '#000';
this.mask.style.opacity = '0.5';
this.mask.style.filter = 'alpha(opacity=50)';
this._resizeMask();
this.num = 0;
body.appendChild(this.mask);
//loading text
this.loading = document.createElement('div');
this.loading.innerHTML = 'loading...';
this.loading.id = 'modal-loading';
this.loading.style.position = 'absolute';
body.appendChild(this.loading);
this._repositionLoading();
window.onresize = function(){
that._resizeMask();
that._repositionLoading();
}
this.interval = setTimeout(function(){
that._checkInitailization();
}, 100);
},
_checkInitailization: function(callback){
var that = this;
this.num++;
//这里用num++ 来模拟等待数据处理的过程. this.num > 20为loading停止条件
if(this.num > 20){
clearTimeout(this.interval);
this._removeLoading();
callback();
}else{
document.title = this.num;
this.interval = setTimeout(function(){
that._checkInitailization(callback)
}, 100);
}
},
show: function(){
var that = this;
this._checkInitailization(function(){
that.modal.show();
});
}
}
return {
ModalProxy:ModalProxy
}
})();
window.onload = function(){
var mp = new MyNamespace.component.ModalProxy();
mp.show();
}
</script>
1. Singleton - 单例模式<br/>
2. Factory - 工厂模式<br/>
3. Bridge - 桥接模式<br/>
4. Composite - 组合模式<br/>
5. Facade - 门面模式<br/>
6. Adapter - 适配器模式<br/>
7. Decorator - 装饰者模式<br/>
8. Flyweight - 享元模式<br/>
9. Proxy - 代理模式<br/>
</body>
</html>