learn.coffee 2.6 KB

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