indexHandlers.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* global __dirname */
  2. "use strict";
  3. var _ = require('lodash');
  4. var Mustache = require('mustache');
  5. var path = require('path');
  6. var fs = require('fs');
  7. var chokidar = require('chokidar');
  8. var indexTmplPath = path.normalize(__dirname + '/../html/index.mustache.html');
  9. var indexTpml;
  10. var {
  11. readFilesAsync
  12. } = require('../lib/fsio');
  13. var translator = require('../lib/translator');
  14. var passLog = require('../lib/passLog');
  15. // One-liner for current directory, ignores .dotfiles
  16. chokidar.watch('./html', {ignored: /(^|[\/\\])\../}).on('all', (event, path) => {
  17. // console.log(event, path);
  18. if(path === 'html/index.mustache.html') {
  19. console.log('reload index html');
  20. indexTpml = fs.readFileSync(indexTmplPath).toString();
  21. }
  22. });
  23. module.exports = function(exStore, exDir, testMode) {
  24. function readExampleFiles(repoSlug, exampleSlug, config) {
  25. const exampleDir = exDir + '/' + repoSlug + '/' + exampleSlug;
  26. const libsCssDir = path.normalize(__dirname + '/../css/vendor');
  27. const libsJsDir = path.normalize(__dirname + '/../js/vendor');
  28. const { html, js, css } = config; // libsCss, libsJs
  29. const files = [].concat(html, js, css);
  30. return readFilesAsync(exampleDir, files);
  31. }
  32. function renderIndex(req, withRepo, withExample) {
  33. // Extract repoSlug and exampleSlug from req.params
  34. const { locale, params: { repoSlug, exampleSlug } } = req;
  35. let repo;
  36. let menuExample;
  37. let statusCode;
  38. // Initialize view data
  39. let data = {
  40. menuRepo: exStore.getRepoMenu(),
  41. testMode,
  42. appPath: req.path,
  43. _: translator.getAll(locale)
  44. };
  45. // Fetch example repository if needed
  46. if(withRepo) {
  47. data.repo = exStore.getRepo(repoSlug);
  48. if(! data.repo) {
  49. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  50. data.errorMessage = translator.getOne(locale, "repoNotFound", [repoSlug]); //'Repo ' + params.repoSlug + ' not found';
  51. statusCode = 404;
  52. }
  53. else {
  54. data.menuExample = exStore.getExampleMenu(data.repo.path);
  55. data.showControls = true;
  56. }
  57. }
  58. // Fetch example if needed
  59. if(withExample && data.repo) {
  60. data.example = _.find(data.repo.examples, { slug: exampleSlug });
  61. if(! data.example) {
  62. // return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
  63. data.errorMessage = translator.getOne(locale, "exampleNotFound", [repoSlug, exampleSlug]);
  64. statusCode = 404;
  65. }
  66. else {
  67. data.showEditor = true;
  68. }
  69. }
  70. // Mustache.render(indexTpml, data);
  71. return (
  72. exampleSlug && data.example ?
  73. readExampleFiles(repoSlug, exampleSlug, data.example) : Promise.resolve([])
  74. ).then(files =>
  75. ({ files, filesJSON: JSON.stringify(files) })
  76. )
  77. .then(({ files, filesJSON }) => Object.assign(data, { files, filesJSON }))
  78. .then(passLog('data before rendering, path: ' + req.path))
  79. .then(data => ({
  80. html: Mustache.render(indexTpml, data),
  81. code: statusCode ? statusCode : 200
  82. }));
  83. }
  84. return {
  85. /**
  86. * Extract language header from req
  87. */
  88. getAcceptLanguage: function (req, res, next) {
  89. const acceptLanguageHdr = req.headers["accept-language"];
  90. const re = /[a-z]{2}\-[A-Z]{2}/;
  91. const matches = re.exec(acceptLanguageHdr);
  92. if(matches) {
  93. req.locale = matches[0];
  94. }
  95. next();
  96. },
  97. /**
  98. * Get bare index without repo or examples
  99. */
  100. getIndexBare: function(req, res) {
  101. renderIndex(req)
  102. .then(({ html, code }) => res.send(html));
  103. },
  104. /**
  105. * Get bare index without repo or examples,
  106. * for running tests with QUnit
  107. */
  108. getIndexTest: function(req, res) {
  109. renderIndex(req, false, false)
  110. .then(({ html, code }) => res.send(html));
  111. },
  112. /**
  113. * Get index with repo selected only
  114. */
  115. getIndexRepo: function(req, res) {
  116. renderIndex(req, true)
  117. .then(({ html, code }) => res.status(code).send(html));
  118. // const repo = exStore.getRepo(req.params.repoSlug);
  119. // if(! repo) {
  120. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  121. // }
  122. // const menuRepo = exStore.getRepoMenu();
  123. // const menuExample = exStore.getExampleMenu(repo.path);
  124. // // const title = 'Home';
  125. // // console.log(menuExample);
  126. // res.send(Mustache.render(indexTpml, {
  127. // // title,
  128. // menuRepo,
  129. // menuExample,
  130. // filesJSON: '[]'
  131. // }));
  132. },
  133. /**
  134. * Get index with selected repo&example
  135. */
  136. getIndexExample: function(req, res) {
  137. renderIndex(req, true, true)
  138. .then(({ html, code }) => res.status(code).send(html));
  139. // console.log('getIndexExample', req.params);
  140. // const repo = exStore.getRepo(req.params.repoSlug);
  141. // if(! repo) {
  142. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  143. // }
  144. // const menuRepo = exStore.getRepoMenu();
  145. // const menuExample = exStore.getExampleMenu(repo.path);
  146. // const example = _.find(repo.examples, { slug: req.params.exampleSlug });
  147. // if(! example) {
  148. // return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
  149. // }
  150. // // console.log('#1');
  151. // // const title = 'Home';
  152. // // console.log(menuExample);
  153. // const { repoSlug, exampleSlug } = req.params;
  154. // readExampleFiles(repoSlug, exampleSlug, example)
  155. // .then(files => {
  156. // console.log('example files', files);
  157. // res.send(Mustache.render(indexTpml, {
  158. // // title,
  159. // menuRepo,
  160. // menuExample,
  161. // files,
  162. // filesJSON: JSON.stringify(files)
  163. // }));
  164. // });
  165. }
  166. };
  167. };