templates.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. no-undef,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. define(['base', 'services/algolia-search'], App =>
  18. App.controller('SearchWikiController', function($scope, algoliaSearch, _) {
  19. $scope.hits = []
  20. $scope.clearSearchText = function() {
  21. $scope.searchQueryText = ''
  22. return updateHits([])
  23. }
  24. $scope.safeApply = function(fn) {
  25. const phase = $scope.$root.$$phase
  26. if (phase === '$apply' || phase === '$digest') {
  27. return $scope.$eval(fn)
  28. } else {
  29. return $scope.$apply(fn)
  30. }
  31. }
  32. const buildHitViewModel = function(hit) {
  33. const page_underscored = hit.pageName.replace(/\s/g, '_')
  34. const section_underscored = hit.sectionName.replace(/\s/g, '_')
  35. let content = hit._highlightResult.content.value
  36. // Replace many new lines
  37. content = content.replace(/\n\n+/g, '\n\n')
  38. const lines = content.split('\n')
  39. // Only show the lines that have a highlighted match
  40. const matching_lines = []
  41. for (let line of Array.from(lines)) {
  42. if (!/^\[edit\]/.test(line)) {
  43. content += line + '\n'
  44. if (/<em>/.test(line)) {
  45. matching_lines.push(line)
  46. }
  47. }
  48. }
  49. content = matching_lines.join('\n...\n')
  50. const result = {
  51. name:
  52. hit._highlightResult.pageName.value +
  53. ' - ' +
  54. hit._highlightResult.sectionName.value,
  55. url: `/learn/${page_underscored}#${section_underscored}`,
  56. content
  57. }
  58. return result
  59. }
  60. var updateHits = hits => $scope.safeApply(() => ($scope.hits = hits))
  61. return ($scope.search = function() {
  62. const query = $scope.searchQueryText
  63. if (query == null || query.length === 0) {
  64. updateHits([])
  65. return
  66. }
  67. return algoliaSearch.searchWiki(query, function(err, response) {
  68. if (response.hits.length === 0) {
  69. return updateHits([])
  70. } else {
  71. const hits = _.map(response.hits, buildHitViewModel)
  72. return updateHits(hits)
  73. }
  74. })
  75. })
  76. }))