layout.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { ParamDefinition as PD } from '../mol-util/param-definition';
  7. import { PluginComponent } from './component';
  8. import { PluginContext } from './context';
  9. import { PluginCommands } from './command';
  10. // TODO: support collapsed state control orientation
  11. export const PluginLayoutStateParams = {
  12. isExpanded: PD.Boolean(false),
  13. showControls: PD.Boolean(true)
  14. }
  15. export type PluginLayoutStateProps = PD.Values<typeof PluginLayoutStateParams>
  16. interface RootState {
  17. top: string | null,
  18. bottom: string | null,
  19. left: string | null,
  20. right: string | null,
  21. width: string | null,
  22. height: string | null,
  23. maxWidth: string | null,
  24. maxHeight: string | null,
  25. margin: string | null,
  26. marginLeft: string | null,
  27. marginRight: string | null,
  28. marginTop: string | null,
  29. marginBottom: string | null,
  30. scrollTop: number,
  31. scrollLeft: number,
  32. position: string | null,
  33. overflow: string | null,
  34. viewports: HTMLElement[],
  35. zindex: string | null
  36. }
  37. export class PluginLayout extends PluginComponent<PluginLayoutStateProps> {
  38. readonly events = {
  39. updated: this.ev()
  40. }
  41. private updateProps(state: Partial<PluginLayoutStateProps>) {
  42. let prevExpanded = !!this.state.isExpanded;
  43. this.updateState(state);
  44. if (this.root && typeof state.isExpanded === 'boolean' && state.isExpanded !== prevExpanded) this.handleExpand();
  45. this.events.updated.next();
  46. }
  47. private root: HTMLElement | undefined;
  48. private rootState: RootState | undefined = void 0;
  49. private expandedViewport: HTMLMetaElement;
  50. setProps(props: Partial<PluginLayoutStateProps>) {
  51. this.updateState(props);
  52. }
  53. setRoot(root: HTMLElement) {
  54. this.root = root;
  55. if (this.state.isExpanded) this.handleExpand();
  56. }
  57. private getScrollElement() {
  58. if ((document as any).scrollingElement) return (document as any).scrollingElement;
  59. if (document.documentElement) return document.documentElement;
  60. return document.body;
  61. }
  62. private handleExpand() {
  63. try {
  64. let body = document.getElementsByTagName('body')[0];
  65. let head = document.getElementsByTagName('head')[0];
  66. if (!body || !head || !this.root) return;
  67. if (this.state.isExpanded) {
  68. let children = head.children;
  69. let hasExp = false;
  70. let viewports: HTMLElement[] = [];
  71. for (let i = 0; i < children.length; i++) {
  72. if (children[i] === this.expandedViewport) {
  73. hasExp = true;
  74. } else if (((children[i] as any).name || '').toLowerCase() === 'viewport') {
  75. viewports.push(children[i] as any);
  76. }
  77. }
  78. for (let v of viewports) {
  79. head.removeChild(v);
  80. }
  81. if (!hasExp) head.appendChild(this.expandedViewport);
  82. let s = body.style;
  83. let doc = this.getScrollElement();
  84. let scrollLeft = doc.scrollLeft;
  85. let scrollTop = doc.scrollTop;
  86. this.rootState = {
  87. top: s.top, bottom: s.bottom, right: s.right, left: s.left, scrollTop, scrollLeft, position: s.position, overflow: s.overflow, viewports, zindex: this.root.style.zIndex,
  88. width: s.width, height: s.height,
  89. maxWidth: s.maxWidth, maxHeight: s.maxHeight,
  90. margin: s.margin, marginLeft: s.marginLeft, marginRight: s.marginRight, marginTop: s.marginTop, marginBottom: s.marginBottom
  91. };
  92. s.overflow = 'hidden';
  93. s.position = 'fixed';
  94. s.top = '0';
  95. s.bottom = '0';
  96. s.right = '0';
  97. s.left = '0';
  98. s.width = '100%';
  99. s.height = '100%';
  100. s.maxWidth = '100%';
  101. s.maxHeight = '100%';
  102. s.margin = '0';
  103. s.marginLeft = '0';
  104. s.marginRight = '0';
  105. s.marginTop = '0';
  106. s.marginBottom = '0';
  107. // TODO: setting this breaks viewport controls for some reason. Is there a fix?
  108. // this.root.style.zIndex = '100000';
  109. } else {
  110. let children = head.children;
  111. for (let i = 0; i < children.length; i++) {
  112. if (children[i] === this.expandedViewport) {
  113. head.removeChild(this.expandedViewport);
  114. break;
  115. }
  116. }
  117. if (this.rootState) {
  118. let s = body.style, t = this.rootState;
  119. for (let v of t.viewports) {
  120. head.appendChild(v);
  121. }
  122. s.top = t.top;
  123. s.bottom = t.bottom;
  124. s.left = t.left;
  125. s.right = t.right;
  126. s.width = t.width;
  127. s.height = t.height;
  128. s.maxWidth = t.maxWidth;
  129. s.maxHeight = t.maxHeight;
  130. s.margin = t.margin;
  131. s.marginLeft = t.marginLeft;
  132. s.marginRight = t.marginRight;
  133. s.marginTop = t.marginTop;
  134. s.marginBottom = t.marginBottom;
  135. s.position = t.position;
  136. s.overflow = t.overflow;
  137. let doc = this.getScrollElement();
  138. doc.scrollTop = t.scrollTop;
  139. doc.scrollLeft = t.scrollLeft;
  140. this.rootState = void 0;
  141. this.root.style.zIndex = t.zindex;
  142. }
  143. }
  144. } catch (e) {
  145. this.context.log.error('Layout change error, you might have to reload the page.');
  146. console.log('Layout change error, you might have to reload the page.', e);
  147. }
  148. }
  149. constructor(private context: PluginContext) {
  150. super({ ...PD.getDefaultValues(PluginLayoutStateParams), ...(context.spec.layout && context.spec.layout.initial) });
  151. PluginCommands.Layout.Update.subscribe(context, e => this.updateProps(e.state));
  152. // TODO how best make sure it runs on node.js as well as in the browser?
  153. if (typeof document !== 'undefined') {
  154. // <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' />
  155. this.expandedViewport = document.createElement('meta') as any;
  156. this.expandedViewport.name = 'viewport';
  157. this.expandedViewport.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0';
  158. }
  159. }
  160. }