55
55
} ;
56
56
OffLightState . prototype . buttonWasPressed = function ( ) {
57
57
console . log ( "弱光" ) ; // offLightState 对应的行为
58
+ console . log ( this . light ) ;
58
59
this . light . setState ( this . light . weakLightState ) ; // 切换状态到weakLightState
59
60
} ;
60
61
94
95
}
95
96
} ;
96
97
97
- Light . prototype . setState = function ( newState ) {
98
- this . currState = newState ;
99
- }
100
-
101
98
var light = new Light ( ) ;
102
99
light . init ( ) ;
103
100
101
+ Light . prototype . setState = function ( newState ) {
102
+ this . currState = newState ;
103
+ } ;
104
104
/********这时如果要加一个“超强光”状态,则不必修改函数内部,只需增加一个对象即可*********/
105
105
106
106
//如果SuperStrongLightState有一个父类State,且State的buttonWasPressed方法为空,则子类必须复写此方法
139
139
self = this ;
140
140
this . button = document . body . appendChild ( button ) ;
141
141
this . button . innerHTML = '开关' ;
142
- this . currState = this . offLightState ; // 设置默认初始状态
143
- this . button . onclick = function ( ) { // 定义用户的请求动作
142
+ this . currState = this . offLightState ; // 设置当前状态
143
+ this . button . onclick = function ( ) {
144
144
self . currState . buttonWasPressed ( ) ;
145
145
}
146
146
} ;
147
147
148
148
var light = new Light ( ) ;
149
- console . log ( '加入超强光状态/ n' ) ;
149
+ console . log ( '加入超强光状态\ n' ) ;
150
150
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 ( ) ;
151
259
</ script >
152
260
</ body >
153
261
</ html >
0 commit comments