learn.coffee 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. define [
  2. "base"
  3. "services/algolia-search"
  4. ], (App) ->
  5. App.controller "SearchWikiController", ($scope, algoliaSearch, _, $modal) ->
  6. $scope.hits = []
  7. $scope.clearSearchText = ->
  8. $scope.searchQueryText = ""
  9. updateHits []
  10. $scope.safeApply = (fn)->
  11. phase = $scope.$root.$$phase
  12. if(phase == '$apply' || phase == '$digest')
  13. $scope.$eval(fn)
  14. else
  15. $scope.$apply(fn)
  16. buildHitViewModel = (hit)->
  17. page_underscored = hit.pageName.replace(/\s/g,'_')
  18. section_underscored = hit.sectionName.replace(/\s/g,'_')
  19. content = hit._highlightResult.content.value
  20. # Replace many new lines
  21. content = content.replace(/\n\n+/g, "\n\n")
  22. lines = content.split("\n")
  23. # Only show the lines that have a highlighted match
  24. matching_lines = []
  25. for line in lines
  26. if !line.match(/^\[edit\]/)
  27. content += line + "\n"
  28. if line.match(/<em>/)
  29. matching_lines.push line
  30. content = matching_lines.join("\n...\n")
  31. result =
  32. name : hit._highlightResult.pageName.value + " - " + hit._highlightResult.sectionName.value
  33. url :"/learn/#{page_underscored}##{section_underscored}"
  34. content: content
  35. return result
  36. updateHits = (hits)->
  37. $scope.safeApply ->
  38. $scope.hits = hits
  39. $scope.search = ->
  40. query = $scope.searchQueryText
  41. if !query? or query.length == 0
  42. updateHits []
  43. return
  44. algoliaSearch.searchWiki query, (err, response)->
  45. if response.hits.length == 0
  46. updateHits []
  47. else
  48. hits = _.map response.hits, buildHitViewModel
  49. updateHits hits
  50. $scope.showMissingTemplateModal = () ->
  51. modalInstance = $modal.open(
  52. templateUrl: "missingWikiPageModal"
  53. controller: "MissingWikiPageController"
  54. )
  55. App.controller 'MissingWikiPageController', ($scope, $modalInstance) ->
  56. $scope.form = {}
  57. $scope.sent = false
  58. $scope.sending = false
  59. $scope.contactUs = ->
  60. if !$scope.form.message?
  61. console.log "message not set"
  62. return
  63. $scope.sending = true
  64. ticketNumber = Math.floor((1 + Math.random()) * 0x10000).toString(32)
  65. params =
  66. email: $scope.form.email or "support@sharelatex.com"
  67. message: $scope.form.message or ""
  68. subject: "new wiki page sujection - [#{ticketNumber}]"
  69. labels: "support wiki"
  70. Groove.createTicket params, (err, json)->
  71. $scope.sent = true
  72. $scope.$apply()
  73. $scope.close = () ->
  74. $modalInstance.close()