2
0

client.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import 'babel-polyfill';
  15. import ReactDOM from 'react-dom';
  16. import FastClick from 'fastclick';
  17. import Router from './routes';
  18. import Location from './core/Location';
  19. import { addEventListener, removeEventListener } from './core/DOMUtils';
  20. let cssContainer = document.getElementById('css');
  21. const appContainer = document.getElementById('app');
  22. const context = {
  23. insertCss: styles => styles._insertCss(),
  24. onSetTitle: value => (document.title = value),
  25. onSetMeta: (name, content) => {
  26. // Remove and create a new <meta /> tag in order to make it work
  27. // with bookmarks in Safari
  28. const elements = document.getElementsByTagName('meta');
  29. Array.from(elements).forEach((element) => {
  30. if (element.getAttribute('name') === name) {
  31. element.parentNode.removeChild(element);
  32. }
  33. });
  34. const meta = document.createElement('meta');
  35. meta.setAttribute('name', name);
  36. meta.setAttribute('content', content);
  37. document
  38. .getElementsByTagName('head')[0]
  39. .appendChild(meta);
  40. },
  41. };
  42. // Google Analytics tracking. Don't send 'pageview' event after the first
  43. // rendering, as it was already sent by the Html component.
  44. let trackPageview = () => (trackPageview = () => window.ga('send', 'pageview'));
  45. function render(state) {
  46. Router.dispatch(state, (newState, component) => {
  47. ReactDOM.render(component, appContainer, () => {
  48. // Restore the scroll position if it was saved into the state
  49. if (state.scrollY !== undefined) {
  50. window.scrollTo(state.scrollX, state.scrollY);
  51. } else {
  52. window.scrollTo(0, 0);
  53. }
  54. trackPageview();
  55. // Remove the pre-rendered CSS because it's no longer used
  56. // after the React app is launched
  57. if (cssContainer) {
  58. cssContainer.parentNode.removeChild(cssContainer);
  59. cssContainer = null;
  60. }
  61. });
  62. });
  63. }
  64. function run() {
  65. let currentLocation = null;
  66. let currentState = null;
  67. // Make taps on links and buttons work fast on mobiles
  68. FastClick.attach(document.body);
  69. // Re-render the app when window.location changes
  70. const unlisten = Location.listen(location => {
  71. currentLocation = location;
  72. currentState = Object.assign({}, location.state, {
  73. path: location.pathname,
  74. query: location.query,
  75. state: location.state,
  76. context,
  77. });
  78. render(currentState);
  79. });
  80. // Save the page scroll position into the current location's state
  81. const supportPageOffset = window.pageXOffset !== undefined;
  82. const isCSS1Compat = ((document.compatMode || '') === 'CSS1Compat');
  83. const setPageOffset = () => {
  84. currentLocation.state = currentLocation.state || Object.create(null);
  85. if (supportPageOffset) {
  86. currentLocation.state.scrollX = window.pageXOffset;
  87. currentLocation.state.scrollY = window.pageYOffset;
  88. } else {
  89. currentLocation.state.scrollX = isCSS1Compat ?
  90. document.documentElement.scrollLeft : document.body.scrollLeft;
  91. currentLocation.state.scrollY = isCSS1Compat ?
  92. document.documentElement.scrollTop : document.body.scrollTop;
  93. }
  94. };
  95. addEventListener(window, 'scroll', setPageOffset);
  96. addEventListener(window, 'pagehide', () => {
  97. removeEventListener(window, 'scroll', setPageOffset);
  98. unlisten();
  99. });
  100. }
  101. // Run the application when both DOM is ready and page content is loaded
  102. if (['complete', 'loaded', 'interactive'].includes(document.readyState) && document.body) {
  103. run();
  104. } else {
  105. document.addEventListener('DOMContentLoaded', run, false);
  106. }