router.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2016 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react'
  19. import ReactDOM from 'react-dom'
  20. import page from 'page'
  21. import qs from 'qs'
  22. import filesEnv from 'compiled/react_files/modules/filesEnv'
  23. import FilesApp from 'jsx/files/FilesApp'
  24. import ShowFolder from 'jsx/files/ShowFolder'
  25. import SearchResults from 'jsx/files/SearchResults'
  26. /**
  27. * Route Handlers
  28. */
  29. function renderShowFolder (ctx) {
  30. ReactDOM.render(
  31. <FilesApp
  32. query={ctx.query}
  33. params={ctx.params}
  34. splat={ctx.splat}
  35. pathname={ctx.pathname}
  36. contextAssetString={window.ENV.context_asset_string}
  37. >
  38. <ShowFolder />
  39. </FilesApp>
  40. , document.getElementById('content'));
  41. }
  42. function renderSearchResults (ctx) {
  43. ReactDOM.render(
  44. <FilesApp
  45. query={ctx.query}
  46. params={ctx.params}
  47. splat={ctx.splat}
  48. pathname={ctx.pathname}
  49. contextAssetString={window.ENV.context_asset_string}
  50. >
  51. <SearchResults />
  52. </FilesApp>
  53. , document.getElementById('content'));
  54. }
  55. /**
  56. * Middlewares
  57. */
  58. function parseQueryString (ctx, next) {
  59. ctx.query = qs.parse(ctx.querystring);
  60. next();
  61. }
  62. function getFolderSplat (ctx, next) {
  63. /* This function only gets called when hitting the /folder/*
  64. * route so we make that assumption here with many of the
  65. * things being done.
  66. */
  67. const PATH_PREFIX = '/folder/';
  68. const index = ctx.pathname.indexOf(PATH_PREFIX) + PATH_PREFIX.length;
  69. const rawSplat = ctx.pathname.slice(index);
  70. ctx.splat = rawSplat.split('/').map((part) => window.encodeURIComponent(part)).join('/');
  71. next();
  72. }
  73. function getSplat (ctx, next) {
  74. ctx.splat = '';
  75. next();
  76. }
  77. /**
  78. * Route Configuration
  79. */
  80. page.base(filesEnv.baseUrl);
  81. page('*', getSplat); // Generally this will overridden by the folder route's middleware
  82. page('*', parseQueryString); // Middleware to parse querystring to object
  83. page('/', renderShowFolder);
  84. page('/search', renderSearchResults);
  85. page('/folder', '/');
  86. page('/folder/*', getFolderSplat, renderShowFolder);
  87. export default {
  88. start () {
  89. page.start();
  90. },
  91. getFolderSplat // Export getSplat for testing
  92. };