<commit_before>/**
 * Created by <NAME> on 15.02.2017.
 */

import {randomId, mixin} from '../';

export interface INodeVis {
  node: Element;
  destroy?();
}

export interface IPopupProxyOptions {
  args?: any[];
  name?: string;
}


export class PopupProxy<T extends INodeVis> {
  private current: T;
  private popup: Window;

  private options: IPopupProxyOptions = {
    args: [],
    name: `${self.document.title} PopUp ${randomId(3)}`
  };

  private handler: ProxyHandler<T> = {};

  constructor(private readonly parent: HTMLElement, private readonly factory: (parent: HTMLElement, ...args: any[]) => T, options: IPopupProxyOptions = {}) {
    this.options = mixin(this.options, options);
    this.build(this.parent);
  }

  private build(parent: HTMLElement) {
    this.current = this.factory(parent, ...this.options.args);
  }


  private buildPopup() {
    const serializer = new XMLSerializer();
    const links = Array.from(document.head.querySelectorAll('link')).map((link: HTMLLinkElement) => serializer.serializeToString(link));
    const template = `<!doctype html>
      <html>
      <head>
          <meta charset="utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>${this.options.name}</title>
          <meta name="description" content="">
          <meta name="viewport" content="width=device-width">
          <script src="//cdn.polyfill.io/v2/polyfill.min.js"></script>
          ${links.join('\n')}
      </head>
      <body class="popup-content">
      </body>
    </html>`;

    const blob = new Blob([template], {type: 'text/html'});
    return URL.createObjectURL(blob);
  }

  get proxy() {
    return new Proxy<T>(<T>{}, this.handler);
  }

  close() {
    if (this.current.destroy) {
      this.current.destroy();
    } else {
      this.current.node.remove();
    }
    this.current = null;
    this.parent.classList.remove('as-popup');
    this.build(this.parent);
  }

  open() {
    const rect = this.current.node.getBoundingClientRect();
    if (this.current.destroy) {
      this.current.destroy();
    } else {
      this.current.node.remove();
    }
    this.parent.classList.add('as-popup');
    this.current = null;
    this.popup = self.open(this.buildPopup(), this.options.name, `width=${rect.width},height=${rect.height},toolbar=0,menubar=0,location=0`);
    this.popup.onunload = () => this.close();
    const document = this.popup.document;
    if (document.readyState === 'ready') {
      this.build(document.body);
    } else {
      document.onload = () => this.build(document.body);
    }
  }
}
<commit_msg>fix popup to write content<commit_after>/**
 * Created by <NAME> on 15.02.2017.
 */

import {randomId, mixin} from '../';

export interface INodeVis {
  node: Element;
  destroy?();
}

export interface IPopupProxyOptions {
  args?: any[];
  name?: string;
}


export default class PopupProxy<T extends INodeVis> {
  private current: T;
  private popup: Window;

  private options: IPopupProxyOptions = {
    args: [],
    name: `${self.document.title} PopUp ${randomId(3)}`
  };

  private handler: ProxyHandler<T> = {};

  constructor(private readonly parent: HTMLElement, private readonly factory: (parent: HTMLElement, ...args: any[]) => T, options: IPopupProxyOptions = {}) {
    this.options = mixin(this.options, options);
    this.build(this.parent);
  }

  private build(parent: HTMLElement) {
    this.current = this.factory(parent, ...this.options.args);
  }


  private buildPopup(callbackFunction: string) {
    const serializer = new XMLSerializer();
    const links = Array.from(document.head.querySelectorAll('link')).map((link: HTMLLinkElement) => `<link href="${link.href}" rel="${link.rel}" type="${link.type}" />`);
    const template = `<!doctype html>
      <html>
      <head>
          <meta charset="utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>${this.options.name}</title>
          <meta name="description" content="">
          <meta name="viewport" content="width=device-width">
          <script src="//cdn.polyfill.io/v2/polyfill.min.js"></script>
          ${links.join('\n')}
      </head>
      <body class="popup-content">
          <script>
            if ('${callbackFunction}' in window.opener) {
              window.opener.${callbackFunction}(document.body);
            }
          </script>
      </body>
    </html>`;

    const blob = new Blob([template], {type: 'text/html'});
    return URL.createObjectURL(blob);
  }

  get proxy() {
    return new Proxy<T>(<T>{}, this.handler);
  }

  close() {
    if (this.current.destroy) {
      this.current.destroy();
    } else {
      this.current.node.remove();
    }
    this.current = null;
    this.parent.classList.remove('as-popup');
    this.build(this.parent);
  }

  open() {
    const rect = this.current.node.getBoundingClientRect();
    if (this.current.destroy) {
      this.current.destroy();
    } else {
      this.current.node.remove();
    }
    this.parent.classList.add('as-popup');
    this.current = null;
    // use a callback function similar to jsonp, don't know why the popup state if overridden
    const name = 'popupCallback' + randomId(8);
    (<any>window)[name] = (popupBody) => {
      this.build(popupBody);
      delete (<any>window)[name];
    };
    this.popup = self.open(this.buildPopup(name), this.options.name, `width=${rect.width}, height=${rect.height}, left=${rect.left}, top=${rect.top}`);
    this.popup.onbeforeunload = () => this.close();
  }
}

{'fraction_non_alphanumeric': 0.17243253033870676, 'fraction_numerical': 0.00470929179496468, 'mean_word_length': 3.0306569343065695, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6683701', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1763, 'n_words': 349}