| 
									
										
										
										
											2015-11-23 13:58:51 -08:00
										 |  |  | window.common = (function(global) { | 
					
						
							|  |  |  |   const { | 
					
						
							| 
									
										
										
										
											2015-12-01 08:01:51 -08:00
										 |  |  |     Rx: { BehaviorSubject, Observable }, | 
					
						
							| 
									
										
										
										
											2015-11-23 13:58:51 -08:00
										 |  |  |     common = { init: [] } | 
					
						
							|  |  |  |   } = global; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  |   // the first script tag here is to proxy jQuery
 | 
					
						
							|  |  |  |   // We use the same jQuery on the main window but we change the
 | 
					
						
							|  |  |  |   // context to that of the iframe.
 | 
					
						
							| 
									
										
										
										
											2015-11-17 21:25:16 -08:00
										 |  |  |   var libraryIncludes = `
 | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  | <script> | 
					
						
							| 
									
										
										
										
											2015-12-01 17:24:50 -08:00
										 |  |  |   window.loopProtect = parent.loopProtect; | 
					
						
							| 
									
										
										
										
											2015-12-01 17:38:48 -08:00
										 |  |  |   window.__err = null; | 
					
						
							| 
									
										
										
										
											2015-12-02 14:56:06 -08:00
										 |  |  |   window.loopProtect.hit = function(line) { | 
					
						
							|  |  |  |     window.__err = new Error( | 
					
						
							| 
									
										
										
										
											2015-12-19 22:30:56 +00:00
										 |  |  |       'Potential infinite loop at line ' + | 
					
						
							|  |  |  |       line + | 
					
						
							| 
									
										
										
										
											2016-01-03 05:39:47 +01:00
										 |  |  |       '. To disable loop protection, write:' + | 
					
						
							|  |  |  |       ' \\n\\/\\/ noprotect\\nas the first' + | 
					
						
							| 
									
										
										
										
											2015-12-19 22:30:56 +00:00
										 |  |  |       ' line. Beware that if you do have an infinite loop in your code' + | 
					
						
							|  |  |  |       ' this will crash your browser.' | 
					
						
							| 
									
										
										
										
											2015-12-02 14:56:06 -08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  | </script> | 
					
						
							| 
									
										
										
										
											2015-11-17 21:25:16 -08:00
										 |  |  | <link | 
					
						
							|  |  |  |   rel='stylesheet' | 
					
						
							|  |  |  |   href='//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.min.css' | 
					
						
							|  |  |  |   /> | 
					
						
							|  |  |  | <link | 
					
						
							|  |  |  |   rel='stylesheet' | 
					
						
							|  |  |  |   href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css' | 
					
						
							|  |  |  |   /> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <link | 
					
						
							|  |  |  |   rel='stylesheet' | 
					
						
							|  |  |  |   href='//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css' | 
					
						
							|  |  |  |   /> | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  |   body { padding: 0px 3px 0px 3px; } | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  |   `;
 | 
					
						
							| 
									
										
										
										
											2015-12-03 15:09:52 -08:00
										 |  |  |   const codeDisabledError = `
 | 
					
						
							|  |  |  |     <script> | 
					
						
							|  |  |  |       window.__err = new Error('code has been disabled'); | 
					
						
							|  |  |  |     </script> | 
					
						
							|  |  |  |   `;
 | 
					
						
							| 
									
										
										
										
											2015-11-17 21:25:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |   const iFrameScript$ = | 
					
						
							|  |  |  |     common.getScriptContent$('/js/iFrameScripts.js').shareReplay(); | 
					
						
							| 
									
										
										
										
											2015-12-03 12:13:25 -08:00
										 |  |  |   const jQueryScript$ = common.getScriptContent$( | 
					
						
							|  |  |  |     '/bower_components/jquery/dist/jquery.js' | 
					
						
							|  |  |  |   ).shareReplay(); | 
					
						
							| 
									
										
										
										
											2015-11-17 21:25:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-01 08:01:51 -08:00
										 |  |  |   // behavior subject allways remembers the last value
 | 
					
						
							|  |  |  |   // we use this to determine if runPreviewTest$ is defined
 | 
					
						
							|  |  |  |   // and prime it with false
 | 
					
						
							|  |  |  |   common.previewReady$ = new BehaviorSubject(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 14:56:06 -08:00
										 |  |  |   // These should be set up in the preview window
 | 
					
						
							|  |  |  |   // if this error is seen it is because the function tried to run
 | 
					
						
							|  |  |  |   // before the iframe has completely loaded
 | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |   common.runPreviewTests$ = | 
					
						
							| 
									
										
										
										
											2015-12-02 14:56:06 -08:00
										 |  |  |     common.checkPreview$ = | 
					
						
							|  |  |  |     () => Observable.throw(new Error('Preview not fully loaded')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-17 21:25:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |   common.updatePreview$ = function updatePreview$(code = '') { | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  |     const preview = common.getIframe('preview'); | 
					
						
							| 
									
										
										
										
											2015-11-17 21:25:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 12:13:25 -08:00
										 |  |  |     return Observable.combineLatest( | 
					
						
							|  |  |  |       iFrameScript$, | 
					
						
							|  |  |  |       jQueryScript$, | 
					
						
							|  |  |  |       (iframe, jQuery) => ({ | 
					
						
							|  |  |  |         iframeScript: `<script>${iframe}</script>`, | 
					
						
							|  |  |  |         jQuery: `<script>${jQuery}</script>` | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |       .first() | 
					
						
							|  |  |  |       .flatMap(({ iframeScript, jQuery }) => { | 
					
						
							| 
									
										
										
										
											2015-12-01 08:01:51 -08:00
										 |  |  |         // we make sure to override the last value in the
 | 
					
						
							|  |  |  |         // subject to false here.
 | 
					
						
							|  |  |  |         common.previewReady$.onNext(false); | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |         preview.open(); | 
					
						
							| 
									
										
										
										
											2015-12-03 12:13:25 -08:00
										 |  |  |         preview.write( | 
					
						
							|  |  |  |           libraryIncludes + | 
					
						
							|  |  |  |           jQuery + | 
					
						
							| 
									
										
										
										
											2015-12-03 15:09:52 -08:00
										 |  |  |           (common.shouldRun() ? code : codeDisabledError) + | 
					
						
							| 
									
										
										
										
											2015-12-03 12:13:25 -08:00
										 |  |  |           '<!-- -->' + | 
					
						
							|  |  |  |           iframeScript | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |         preview.close(); | 
					
						
							| 
									
										
										
										
											2015-12-01 08:01:51 -08:00
										 |  |  |         // now we filter false values and wait for the first true
 | 
					
						
							|  |  |  |         return common.previewReady$ | 
					
						
							|  |  |  |           .filter(ready => ready) | 
					
						
							| 
									
										
										
										
											2015-11-30 14:27:39 -08:00
										 |  |  |           .first() | 
					
						
							|  |  |  |           // the delay here is to give code within the iframe
 | 
					
						
							|  |  |  |           // control to run
 | 
					
						
							| 
									
										
										
										
											2015-12-02 11:19:15 -08:00
										 |  |  |           .delay(400); | 
					
						
							| 
									
										
										
										
											2015-11-22 19:42:53 -08:00
										 |  |  |       }) | 
					
						
							|  |  |  |       .map(() => code); | 
					
						
							| 
									
										
										
										
											2015-11-17 21:25:16 -08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return common; | 
					
						
							|  |  |  | }(window)); |