indexHandlers.js 5.4 KB

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