popup.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. export class Popup {
  2. public static CreatePopup(title: string, windowVariableName: string, width = 300, height = 800) {
  3. const windowCreationOptionsList = {
  4. width: width,
  5. height: height,
  6. top: (window.innerHeight - width) / 2 + window.screenY,
  7. left: (window.innerWidth - height) / 2 + window.screenX
  8. };
  9. var windowCreationOptions = Object.keys(windowCreationOptionsList)
  10. .map(
  11. (key) => key + '=' + (windowCreationOptionsList as any)[key]
  12. )
  13. .join(',');
  14. const popupWindow = window.open("", title, windowCreationOptions);
  15. if (!popupWindow) {
  16. return null;
  17. }
  18. const parentDocument = popupWindow.document;
  19. // Font
  20. const newLinkEl = parentDocument.createElement('link');
  21. newLinkEl.rel = 'stylesheet';
  22. newLinkEl.href = "https://use.typekit.net/cta4xsb.css";
  23. parentDocument.head!.appendChild(newLinkEl);
  24. parentDocument.title = title;
  25. parentDocument.body.style.width = "100%";
  26. parentDocument.body.style.height = "100%";
  27. parentDocument.body.style.margin = "0";
  28. parentDocument.body.style.padding = "0";
  29. let parentControl = parentDocument.createElement("div");
  30. parentControl.style.width = "100%";
  31. parentControl.style.height = "100%";
  32. parentControl.style.margin = "0";
  33. parentControl.style.padding = "0";
  34. popupWindow.document.body.appendChild(parentControl);
  35. this._CopyStyles(window.document, parentDocument);
  36. setTimeout(() => { // need this for late bindings
  37. this._CopyStyles(window.document, parentDocument);
  38. }, 0);
  39. (this as any)[windowVariableName] = popupWindow;
  40. return parentControl;
  41. }
  42. private static _CopyStyles(sourceDoc: HTMLDocument, targetDoc: HTMLDocument) {
  43. for (var index = 0; index < sourceDoc.styleSheets.length; index++) {
  44. var styleSheet: any = sourceDoc.styleSheets[index];
  45. try {
  46. if (styleSheet.cssRules) { // for <style> elements
  47. const newStyleEl = sourceDoc.createElement('style');
  48. for (var cssRule of styleSheet.cssRules) {
  49. // write the text of each rule into the body of the style element
  50. newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
  51. }
  52. targetDoc.head!.appendChild(newStyleEl);
  53. } else if (styleSheet.href) { // for <link> elements loading CSS from a URL
  54. const newLinkEl = sourceDoc.createElement('link');
  55. newLinkEl.rel = 'stylesheet';
  56. newLinkEl.href = styleSheet.href;
  57. targetDoc.head!.appendChild(newLinkEl);
  58. }
  59. } catch (e) {
  60. console.log(e)
  61. }
  62. }
  63. }
  64. }