Guest User

Untitled

a guest
Dec 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="nav-home-tab">
  2. <searchTourSimple [script]="'src/app/scripts/homeSearch.js'"></searchTourSimple>
  3. </div>
  4. <div class="shop-window">
  5. <div id="shopwindow-container">
  6. <shopWindow [script]="'src/app/scripts/shopwindow.js'"></shopWindow>
  7. </div>
  8. </div>
  9.  
  10. @Directive({
  11. selector: 'searchTourSimple'
  12. })
  13.  
  14. export class SearchModuleSimpleDirective implements OnInit {
  15. @Input('script') param: any
  16. script: any
  17.  
  18. constructor(private renderer:Renderer2) { }
  19.  
  20. ngOnInit() {
  21.  
  22. this.script = this.renderer.createElement('script')
  23. this.script.type = 'text/javascript'
  24. this.script.src = this.param
  25. this.script.async = true
  26. this.renderer.appendChild(document.head, this.script)
  27.  
  28. document.write = function(input: string) {
  29. document.getElementById('home').innerHTML += input
  30. }
  31. }
  32. }
  33.  
  34. @Directive({
  35. selector: 'shopWindow'
  36. })
  37.  
  38. export class ShopWindowDirective implements OnInit {
  39. @Input('script') param: any
  40. script: any
  41.  
  42. constructor(private renderer:Renderer2) { }
  43.  
  44. ngOnInit() {
  45.  
  46. this.script = this.renderer.createElement('script')
  47. this.script.type = 'text/javascript'
  48. this.script.src = this.param
  49. this.script.async = true
  50. this.renderer.appendChild(document.head, this.script)
  51.  
  52. document.write = function(input: string) {
  53. document.getElementById('shopwindow-container').innerHTML += input
  54. }
  55. }
  56. }
  57.  
  58. import { Injectable } from '@angular/core';
  59.  
  60. import { Observable, of } from 'rxjs';
  61. import { tap, switchMap } from 'rxjs/operators';
  62.  
  63. @Injectable({ providedIn: 'root' })
  64. export class ScriptService {
  65. private loadResource(src: string): Observable<void> {
  66. return new Observable((observer) => {
  67. const script = document.createElement('script');
  68.  
  69. script.async = true;
  70. script.src = src;
  71.  
  72. script.addEventListener('load', () => {
  73. observer.next();
  74. observer.complete();
  75. });
  76.  
  77. document.head.appendChild(script);
  78. });
  79. }
  80.  
  81. private patchDocumentWrite(id: string): void {
  82. document.write = (input: string): void => {
  83. document.getElementById(id).innerHTML += input;
  84. };
  85. }
  86.  
  87. public loadExternalResources(): Observable<void> {
  88. return of(null).pipe(
  89. tap(() => this.patchDocumentWrite('home')),
  90. switchMap(() => this.loadResource('src/app/scripts/homeSearch.js')),
  91. tap(() => this.patchDocumentWrite('shopwindow-container')),
  92. switchMap(() => this.loadResource('src/app/scripts/shopwindow.js'))
  93. );
  94. }
  95. }
  96.  
  97. export class AppComponent implements AfterViewInit {
  98. constructor(private scriptService: ScriptService) {}
  99.  
  100. public ngAfterViewInit(): void {
  101. this.scriptService.loadExternalResources().subscribe(() => {
  102. console.log('все скрипты загрузились');
  103. });
  104. }
  105. }
Add Comment
Please, Sign In to add comment