| 
									
										
										
										
											2015-08-27 00:02:07 -07:00
										 |  |  | $(document).ready(function() { | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |   const common = window.common; | 
					
						
							|  |  |  |   const { Observable } = window.Rx; | 
					
						
							| 
									
										
										
										
											2015-12-02 14:56:06 -08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     addLoopProtect, | 
					
						
							|  |  |  |     challengeName, | 
					
						
							|  |  |  |     challengeType, | 
					
						
							|  |  |  |     challengeTypes | 
					
						
							|  |  |  |   } = common; | 
					
						
							| 
									
										
										
										
											2015-09-09 18:59:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-27 15:58:59 -07:00
										 |  |  |   common.init.forEach(function(init) { | 
					
						
							|  |  |  |     init($); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-24 15:12:54 -08:00
										 |  |  |   // only run if editor present
 | 
					
						
							| 
									
										
										
										
											2015-11-24 14:27:32 -08:00
										 |  |  |   if (common.editor.getValue) { | 
					
						
							|  |  |  |     const code$ = common.editorKeyUp$ | 
					
						
							|  |  |  |       .debounce(750) | 
					
						
							|  |  |  |       .map(() => common.editor.getValue()) | 
					
						
							|  |  |  |       .distinctUntilChanged() | 
					
						
							|  |  |  |       .shareReplay(); | 
					
						
							| 
									
										
										
										
											2015-11-23 23:47:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-24 14:27:32 -08:00
										 |  |  |     // update storage
 | 
					
						
							|  |  |  |     code$.subscribe( | 
					
						
							|  |  |  |         code => { | 
					
						
							|  |  |  |           common.codeStorage.updateStorage(common.challengeName, code); | 
					
						
							|  |  |  |           common.codeUri.querify(code); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         err => console.error(err) | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-24 14:27:32 -08:00
										 |  |  |     code$ | 
					
						
							| 
									
										
										
										
											2015-11-24 15:12:54 -08:00
										 |  |  |       // only run for HTML
 | 
					
						
							|  |  |  |       .filter(() => common.challengeType === challengeTypes.HTML) | 
					
						
							| 
									
										
										
										
											2015-11-24 14:27:32 -08:00
										 |  |  |       .flatMap(code => { | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  |         return common.detectUnsafeCode$(code) | 
					
						
							| 
									
										
										
										
											2015-12-02 14:56:06 -08:00
										 |  |  |           .map(() => { | 
					
						
							| 
									
										
										
										
											2016-01-09 18:16:10 -08:00
										 |  |  |             const combinedCode = common.head + code + common.tail; | 
					
						
							| 
									
										
										
										
											2015-12-02 14:56:06 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return addLoopProtect(combinedCode); | 
					
						
							|  |  |  |           }) | 
					
						
							| 
									
										
										
										
											2015-11-24 16:03:11 -08:00
										 |  |  |           .flatMap(code => common.updatePreview$(code)) | 
					
						
							| 
									
										
										
										
											2015-12-02 14:56:06 -08:00
										 |  |  |           .flatMap(() => common.checkPreview$({ code })) | 
					
						
							| 
									
										
										
										
											2015-11-24 16:03:11 -08:00
										 |  |  |           .catch(err => Observable.just({ err })); | 
					
						
							| 
									
										
										
										
											2015-11-24 14:27:32 -08:00
										 |  |  |       }) | 
					
						
							|  |  |  |       .subscribe( | 
					
						
							|  |  |  |         ({ err }) => { | 
					
						
							|  |  |  |           if (err) { | 
					
						
							| 
									
										
										
										
											2015-11-24 16:03:11 -08:00
										 |  |  |             console.error(err); | 
					
						
							|  |  |  |             return common.updatePreview$(`
 | 
					
						
							|  |  |  |               <h1>${err}</h1> | 
					
						
							|  |  |  |             `).subscribe(() => {});
 | 
					
						
							| 
									
										
										
										
											2015-11-24 14:27:32 -08:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |           return null; | 
					
						
							| 
									
										
										
										
											2015-11-24 14:27:32 -08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         err => console.error(err) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-11-23 23:47:14 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |   common.resetBtn$ | 
					
						
							|  |  |  |     .doOnNext(() => { | 
					
						
							|  |  |  |       common.editor.setValue(common.replaceSafeTags(common.seed)); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .flatMap(() => { | 
					
						
							| 
									
										
										
										
											2015-11-24 16:03:11 -08:00
										 |  |  |       return common.executeChallenge$() | 
					
						
							|  |  |  |         .catch(err => Observable.just({ err })); | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |     }) | 
					
						
							|  |  |  |     .subscribe( | 
					
						
							| 
									
										
										
										
											2016-07-05 08:22:36 +05:30
										 |  |  |       ({ err, output, originalCode, tests }) => { | 
					
						
							| 
									
										
										
										
											2015-11-24 16:03:11 -08:00
										 |  |  |         if (err) { | 
					
						
							|  |  |  |           console.error(err); | 
					
						
							| 
									
										
										
										
											2016-01-13 07:29:27 +04:00
										 |  |  |           return common.updateOutputDisplay('' + err); | 
					
						
							| 
									
										
										
										
											2015-11-24 16:03:11 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  |         common.codeStorage.updateStorage(challengeName, originalCode); | 
					
						
							| 
									
										
										
										
											2016-01-02 02:05:38 +01:00
										 |  |  |         common.codeUri.querify(originalCode); | 
					
						
							| 
									
										
										
										
											2016-07-05 08:22:36 +05:30
										 |  |  |         common.displayTestResults(tests, true); | 
					
						
							| 
									
										
										
										
											2016-01-13 01:55:23 +04:00
										 |  |  |         common.updateOutputDisplay(output); | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2015-11-24 16:03:11 -08:00
										 |  |  |       (err) => { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							| 
									
										
										
										
											2015-11-21 20:56:05 -08:00
										 |  |  |           console.error(err); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-13 07:29:27 +04:00
										 |  |  |         common.updateOutputDisplay('' + err); | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-21 19:48:24 -08:00
										 |  |  |   Observable.merge( | 
					
						
							|  |  |  |     common.editorExecute$, | 
					
						
							|  |  |  |     common.submitBtn$ | 
					
						
							|  |  |  |   ) | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |     .flatMap(() => { | 
					
						
							| 
									
										
										
										
											2015-11-21 19:48:24 -08:00
										 |  |  |       common.appendToOutputDisplay('\n// testing challenge...'); | 
					
						
							| 
									
										
										
										
											2015-11-21 20:56:05 -08:00
										 |  |  |       return common.executeChallenge$() | 
					
						
							|  |  |  |         .map(({ tests, ...rest }) => { | 
					
						
							|  |  |  |           const solved = tests.every(test => !test.err); | 
					
						
							|  |  |  |           return { ...rest, tests, solved }; | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2015-11-24 16:03:11 -08:00
										 |  |  |         .catch(err => Observable.just({ err })); | 
					
						
							| 
									
										
										
										
											2015-11-21 19:48:24 -08:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |     .subscribe( | 
					
						
							| 
									
										
										
										
											2015-11-21 20:56:05 -08:00
										 |  |  |       ({ err, solved, output, tests }) => { | 
					
						
							|  |  |  |         if (err) { | 
					
						
							|  |  |  |           console.error(err); | 
					
						
							| 
									
										
										
										
											2015-11-23 17:50:09 -08:00
										 |  |  |           if (common.challengeType === common.challengeTypes.HTML) { | 
					
						
							|  |  |  |             return common.updatePreview$(`
 | 
					
						
							|  |  |  |               <h1>${err}</h1> | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  |             `).first().subscribe(() => {});
 | 
					
						
							| 
									
										
										
										
											2015-11-23 17:50:09 -08:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2016-01-13 07:29:27 +04:00
										 |  |  |           return common.updateOutputDisplay('' + err); | 
					
						
							| 
									
										
										
										
											2015-11-21 20:56:05 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-13 01:55:23 +04:00
										 |  |  |         common.updateOutputDisplay(output); | 
					
						
							| 
									
										
										
										
											2016-06-20 22:05:15 -04:00
										 |  |  |         common.displayTestResults(tests, true); | 
					
						
							| 
									
										
										
										
											2015-11-21 19:48:24 -08:00
										 |  |  |         if (solved) { | 
					
						
							|  |  |  |           common.showCompletion(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2015-11-21 20:56:05 -08:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2015-11-23 17:50:09 -08:00
										 |  |  |       ({ err }) => { | 
					
						
							| 
									
										
										
										
											2015-11-21 20:56:05 -08:00
										 |  |  |         console.error(err); | 
					
						
							| 
									
										
										
										
											2016-01-13 07:29:27 +04:00
										 |  |  |         common.updateOutputDisplay('' + err); | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-24 13:48:14 -08:00
										 |  |  |   // initial challenge run to populate tests
 | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |   if (challengeType === challengeTypes.HTML) { | 
					
						
							|  |  |  |     var $preview = $('#preview'); | 
					
						
							|  |  |  |     return Observable.fromCallback($preview.ready, $preview)() | 
					
						
							|  |  |  |       .delay(500) | 
					
						
							|  |  |  |       .flatMap(() => common.executeChallenge$()) | 
					
						
							| 
									
										
										
										
											2015-11-24 13:48:14 -08:00
										 |  |  |       .catch(err => Observable.just({ err })) | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |       .subscribe( | 
					
						
							| 
									
										
										
										
											2015-11-23 17:50:09 -08:00
										 |  |  |         ({ err, tests }) => { | 
					
						
							|  |  |  |           if (err) { | 
					
						
							|  |  |  |             console.error(err); | 
					
						
							|  |  |  |             if (common.challengeType === common.challengeTypes.HTML) { | 
					
						
							|  |  |  |               return common.updatePreview$(`
 | 
					
						
							|  |  |  |                 <h1>${err}</h1> | 
					
						
							|  |  |  |               `).subscribe(() => {});
 | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-01-13 07:29:27 +04:00
										 |  |  |             return common.updateOutputDisplay('' + err); | 
					
						
							| 
									
										
										
										
											2015-11-23 17:50:09 -08:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2016-06-20 22:05:15 -04:00
										 |  |  |           common.displayTestResults(tests, false); | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |           return null; | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         ({ err }) => { | 
					
						
							|  |  |  |           console.error(err); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ( | 
					
						
							| 
									
										
										
										
											2015-11-24 15:12:54 -08:00
										 |  |  |     challengeType === challengeTypes.BONFIRE || | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |     challengeType === challengeTypes.JS | 
					
						
							| 
									
										
										
										
											2015-11-08 20:04:43 -08:00
										 |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |     return Observable.just({}) | 
					
						
							| 
									
										
										
										
											2015-11-21 21:17:39 -08:00
										 |  |  |       .delay(500) | 
					
						
							|  |  |  |       .flatMap(() => common.executeChallenge$()) | 
					
						
							| 
									
										
										
										
											2015-11-24 15:12:54 -08:00
										 |  |  |       .catch(err => Observable.just({ err })) | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |       .subscribe( | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  |         ({ err, originalCode, tests }) => { | 
					
						
							| 
									
										
										
										
											2015-11-24 15:12:54 -08:00
										 |  |  |           if (err) { | 
					
						
							|  |  |  |             console.error(err); | 
					
						
							| 
									
										
										
										
											2016-01-13 07:29:27 +04:00
										 |  |  |             return common.updateOutputDisplay('' + err); | 
					
						
							| 
									
										
										
										
											2015-11-24 15:12:54 -08:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  |           common.codeStorage.updateStorage(challengeName, originalCode); | 
					
						
							| 
									
										
										
										
											2016-06-20 22:05:15 -04:00
										 |  |  |           common.displayTestResults(tests, false); | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |           return null; | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2015-11-24 15:12:54 -08:00
										 |  |  |         (err) => { | 
					
						
							|  |  |  |           console.error(err); | 
					
						
							| 
									
										
										
										
											2016-01-13 07:29:27 +04:00
										 |  |  |           common.updateOutputDisplay('' + err); | 
					
						
							| 
									
										
										
										
											2015-11-19 21:51:38 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2015-08-27 00:15:13 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-03-02 20:54:14 -08:00
										 |  |  |   return null; | 
					
						
							| 
									
										
										
										
											2015-08-23 21:59:29 +01:00
										 |  |  | }); |