1// tslint:disable max-classes-per-file
2
3class DOMNode {
4  nodeName: string;
5
6  constructor(nodeName: string) {
7    this.nodeName = nodeName;
8  }
9
10  get ownerDocument() {
11    return window.document;
12  }
13
14  appendChild(element: any) {
15    // unimplemented
16  }
17}
18
19class DOMElement extends DOMNode {
20  style = {};
21  // emitter = new EventEmitter();
22  constructor(tagName: string) {
23    super(tagName.toUpperCase());
24  }
25
26  get tagName() {
27    return this.nodeName;
28  }
29
30  insertBefore = () => {};
31  getContext() {
32    // if (global.canvasContext) {
33    //   return global.canvasContext;
34    // }
35    return {
36      fillRect: () => {},
37      drawImage: () => {},
38      getImageData: () => {},
39      getContextAttributes: () => ({
40        stencil: true,
41      }),
42      getExtension: () => ({
43        loseContext: () => {},
44      }),
45    };
46  }
47
48  addEventListener(eventName: string, listener: () => void) {
49    // this.emitter.addListener(eventName, listener);
50  }
51
52  removeEventListener(eventName: string, listener: () => void) {
53    // this.emitter.removeListener(eventName, listener);
54  }
55
56  get clientWidth() {
57    return this.innerWidth;
58  }
59  get clientHeight() {
60    return this.innerHeight;
61  }
62
63  get innerWidth() {
64    return window.innerWidth;
65  }
66  get innerHeight() {
67    return window.innerHeight;
68  }
69}
70
71class DOMDocument extends DOMElement {
72  body = new DOMElement('BODY');
73
74  constructor() {
75    super('#document');
76  }
77
78  createElement(tagName: string) {
79    return new DOMElement(tagName);
80  }
81
82  createElementNS(tagName: string) {
83    const canvas = this.createElement(tagName);
84    canvas.getContext = () => ({
85      fillRect: () => ({}),
86      drawImage: () => ({}),
87      getImageData: () => ({}),
88      getContextAttributes: () => ({
89        stencil: true,
90      }),
91      getExtension: () => ({
92        loseContext: () => ({}),
93      }),
94    });
95    // @ts-ignore
96    canvas.toDataURL = () => ({});
97
98    return canvas;
99  }
100
101  getElementById(id: string) {
102    return new DOMElement('div');
103  }
104}
105
106// @ts-ignore
107process.browser = true;
108
109// @ts-ignore
110window.emitter = window.emitter || {}; // new EventEmitter();
111window.addEventListener =
112  window.addEventListener || ((eventName: string, listener: () => void) => {});
113
114window.removeEventListener =
115  window.removeEventListener || ((eventName: string, listener: () => void) => {});
116
117// @ts-ignore
118window.document = new DOMDocument();
119
120// @ts-ignore
121window.document.body = new DOMElement('body');
122
123// @ts-ignore
124window.location = 'data:'; // <- Not sure about this... or anything for that matter ¯\_(ツ)_/¯
125
126// This could be made better, but I'm not sure if it'll matter for PIXI
127// @ts-ignore
128global.userAgent = global.navigator.userAgent = 'iPhone';
129
130class HTMLImageElement2 {
131  align: string;
132  alt: null;
133  border: null;
134  complete: boolean;
135  crossOrigin: string;
136  localUri: string;
137  lowSrc: string;
138  currentSrc: string;
139  src: string;
140  width: number;
141  height: number;
142  isMap: boolean;
143
144  constructor(props: { localUri: string; width: number; height: number }) {
145    this.align = 'center';
146    this.alt = null;
147    this.border = null;
148    this.complete = true;
149    this.crossOrigin = '';
150    this.localUri = this.lowSrc = this.currentSrc = this.src = props.localUri;
151    this.width = props.width;
152    this.height = props.height;
153    this.isMap = true;
154  }
155}
156
157// @ts-ignore
158global.HTMLImageElement = global.Image = HTMLImageElement2;
159
160// @ts-ignore
161global.performance = null;
162