Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4292fd

Browse files
committed
a part of state parttern
1 parent e54ccd5 commit f4292fd

File tree

1 file changed

+115
-7
lines changed

1 file changed

+115
-7
lines changed

State/state.html

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
};
5656
OffLightState.prototype.buttonWasPressed = function(){
5757
console.log("弱光"); // offLightState 对应的行为
58+
console.log(this.light);
5859
this.light.setState(this.light.weakLightState); // 切换状态到weakLightState
5960
};
6061

@@ -94,13 +95,12 @@
9495
}
9596
};
9697

97-
Light.prototype.setState = function(newState){
98-
this.currState = newState;
99-
}
100-
10198
var light = new Light();
10299
light.init();
103100

101+
Light.prototype.setState = function( newState ){
102+
this.currState = newState;
103+
};
104104
/********这时如果要加一个“超强光”状态,则不必修改函数内部,只需增加一个对象即可*********/
105105

106106
//如果SuperStrongLightState有一个父类State,且State的buttonWasPressed方法为空,则子类必须复写此方法
@@ -139,15 +139,123 @@
139139
self = this;
140140
this.button = document.body.appendChild( button );
141141
this.button.innerHTML = '开关';
142-
this.currState = this.offLightState; // 设置默认初始状态
143-
this.button.onclick = function(){ // 定义用户的请求动作
142+
this.currState = this.offLightState; // 设置当前状态
143+
this.button.onclick = function(){
144144
self.currState.buttonWasPressed();
145145
}
146146
};
147147

148148
var light = new Light();
149-
console.log('加入超强光状态/n');
149+
console.log('加入超强光状态\n');
150150
light.init();
151+
152+
153+
/***********上传文件的时候有扫描状态、上传状态、暂停状态、上传完成状态**********/
154+
window.external.upload = function(state){
155+
console.log(state); // 可能为sign、uploading、done、error
156+
};
157+
158+
var plugin = (function(){
159+
var plugin = document.createElement('embed');
160+
plugin.style.display = 'none';
161+
plugin.type = 'application/txftn-webkit';
162+
plugin.sign = function(){
163+
console.log('开始扫描文件');
164+
}
165+
plugin.pause = function(){
166+
console.log('暂停文件上传');
167+
}
168+
plugin.uploading = function(){
169+
console.log('开始上传文件');
170+
}
171+
plugin.del = function(){
172+
console.log('删除文件上传');
173+
}
174+
plugin.done = function(){
175+
console.log('文件上传完成');
176+
}
177+
document.body.appendChild(plugin);
178+
return plugin;
179+
})();
180+
181+
var Upload = function(fileName){
182+
this.plugin = plugin;
183+
this.fileName = fileName;
184+
this.button1 = null;
185+
this.button2 = null;
186+
this.state = 'sign'; // 设置初始状态为waiting
187+
}
188+
189+
Upload.prototype.init = function(){
190+
var that = this;
191+
this.dom = document.createElement('div');
192+
this.dom.innerHTML = '<span>文件名称:'+ this.fileName +'</span>\<button data-action="button1">扫描中</button>\<button data-action="button2">删除</button>';
193+
document.body.appendChild(this.dom);
194+
this.button1 = this.dom.querySelector('[data-action="button1"]');
195+
this.button2 = this.dom.querySelector('[data-action="button2"]');
196+
this.bindEvent();
197+
}
198+
199+
Upload.prototype.bindEvent = function(){
200+
var self = this;
201+
this.button1.onclick = function(){
202+
if(self.state === 'sign'){ // 扫描状态下,任何操作无效
203+
console.log( '扫描中,点击无效...' );
204+
}else if(self.state === 'uploading'){ // 上传中,点击切换到暂停
205+
self.changeState('pause');
206+
}else if(self.state === 'pause'){ // 暂停中,点击切换到上传中
207+
self.changeState( 'uploading' );
208+
}else if(self.state === 'done'){
209+
console.log('文件已上传完成,点击无效');
210+
}else if(self.state === 'error'){
211+
console.log('文件上传失败,单击无效');
212+
}
213+
};
214+
215+
this.button2.onclick = function(){
216+
if(self.state === 'done' || self.state === 'error' || self.state === 'pause'){
217+
// 上传完成、上传失败和暂停状态下可以删除
218+
self.changeState('del');
219+
}else if(self.state === 'sign'){
220+
console.log('文件正在扫描中,不能删除');
221+
}else if(self.state === 'uploading'){
222+
console.log('文件正在扫描中,不能删除');
223+
}
224+
}
225+
}
226+
227+
Upload.prototype.changeState = function(state){
228+
switch(state){
229+
case 'sign':
230+
this.plugin.sign();
231+
this.button1.innerHTML = '扫描中,任何操作无效';
232+
break;
233+
case 'uploading':
234+
this.plugin.uploading();
235+
this.button1.innerHTML = '正在上传,点击暂停';
236+
break;
237+
case 'pause':
238+
this.plugin.pause();
239+
this.button1.innerHTML = '已暂停,点击继续上传';
240+
break;
241+
case 'done':
242+
this.plugin.done();
243+
this.button1.innerHTML = '上传完成';
244+
break;
245+
case 'error':
246+
this.button1.innerHTML = '上传失败';
247+
break;
248+
case 'del':
249+
this.plugin.del();
250+
this.dom.parentNode.removeChild(this.dom);
251+
console.log('删除完成');
252+
break;
253+
}
254+
this.state = state;
255+
}
256+
257+
var uploadObj = new Upload('JavaScript 设计模式与开发实践');
258+
uploadObj.init();
151259
</script>
152260
</body>
153261
</html>

0 commit comments

Comments
 (0)