2016-03-24 05:35:11 -04:00
|
|
|
define [
|
|
|
|
"base"
|
2016-07-11 07:31:52 -04:00
|
|
|
"services/algolia-search"
|
2016-03-24 05:35:11 -04:00
|
|
|
], (App) ->
|
|
|
|
|
2016-07-11 07:31:52 -04:00
|
|
|
App.controller "SearchWikiController", ($scope, algoliaSearch, _) ->
|
2016-03-24 05:35:11 -04:00
|
|
|
$scope.hits = []
|
|
|
|
|
|
|
|
$scope.clearSearchText = ->
|
|
|
|
$scope.searchQueryText = ""
|
|
|
|
updateHits []
|
|
|
|
|
|
|
|
$scope.safeApply = (fn)->
|
|
|
|
phase = $scope.$root.$$phase
|
|
|
|
if(phase == '$apply' || phase == '$digest')
|
|
|
|
$scope.$eval(fn)
|
|
|
|
else
|
|
|
|
$scope.$apply(fn)
|
|
|
|
|
|
|
|
buildHitViewModel = (hit)->
|
|
|
|
page_underscored = hit.pageName.replace(/\s/g,'_')
|
|
|
|
section_underscored = hit.sectionName.replace(/\s/g,'_')
|
2016-03-24 07:38:56 -04:00
|
|
|
content = hit._highlightResult.content.value
|
|
|
|
# Replace many new lines
|
|
|
|
content = content.replace(/\n\n+/g, "\n\n")
|
|
|
|
lines = content.split("\n")
|
|
|
|
# Only show the lines that have a highlighted match
|
|
|
|
matching_lines = []
|
|
|
|
for line in lines
|
|
|
|
if !line.match(/^\[edit\]/)
|
|
|
|
content += line + "\n"
|
|
|
|
if line.match(/<em>/)
|
|
|
|
matching_lines.push line
|
|
|
|
content = matching_lines.join("\n...\n")
|
2016-03-24 05:35:11 -04:00
|
|
|
result =
|
|
|
|
name : hit._highlightResult.pageName.value + " - " + hit._highlightResult.sectionName.value
|
|
|
|
url :"/learn/#{page_underscored}##{section_underscored}"
|
2016-03-24 07:38:56 -04:00
|
|
|
content: content
|
2016-03-24 05:35:11 -04:00
|
|
|
return result
|
|
|
|
|
|
|
|
updateHits = (hits)->
|
|
|
|
$scope.safeApply ->
|
|
|
|
$scope.hits = hits
|
|
|
|
|
|
|
|
$scope.search = ->
|
|
|
|
query = $scope.searchQueryText
|
|
|
|
if !query? or query.length == 0
|
|
|
|
updateHits []
|
|
|
|
return
|
|
|
|
|
2016-07-11 07:31:52 -04:00
|
|
|
algoliaSearch.searchWiki query, (err, response)->
|
2016-03-24 05:35:11 -04:00
|
|
|
if response.hits.length == 0
|
|
|
|
updateHits []
|
|
|
|
else
|
|
|
|
hits = _.map response.hits, buildHitViewModel
|
|
|
|
updateHits hits
|