2014-07-01 09:25:32 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
|
|
|
|
2014-07-18 06:35:55 -04:00
|
|
|
App.controller "openInSlController", ($scope) ->
|
2014-07-01 09:25:32 -04:00
|
|
|
|
|
|
|
$scope.openInSlText = "Open in ShareLaTeX"
|
|
|
|
$scope.isDisabled = false
|
|
|
|
|
|
|
|
$scope.open = ->
|
|
|
|
$scope.openInSlText = "Creating..."
|
|
|
|
$scope.isDisabled = true
|
|
|
|
ga('send', 'event', 'template-site', 'open-in-sl', $('.page-header h1').text())
|
|
|
|
|
|
|
|
$scope.downloadZip = ->
|
|
|
|
ga('send', 'event', 'template-site', 'download-zip', $('.page-header h1').text())
|
|
|
|
|
|
|
|
|
2014-07-18 06:35:55 -04:00
|
|
|
App.factory "algolia", ->
|
2014-07-01 11:45:20 -04:00
|
|
|
if window?.sharelatex?.algolia?.app_id?
|
|
|
|
client = new AlgoliaSearch(window.sharelatex.algolia?.app_id, window.sharelatex.algolia?.api_key)
|
|
|
|
index = client.initIndex(window.sharelatex.algolia?.indexes?.templates)
|
2014-07-01 09:25:32 -04:00
|
|
|
return index
|
|
|
|
|
2014-09-01 10:13:37 -04:00
|
|
|
|
|
|
|
|
2014-07-18 06:35:55 -04:00
|
|
|
App.controller "SearchController", ($scope, algolia, _) ->
|
2014-07-01 09:25:32 -04:00
|
|
|
$scope.hits = []
|
|
|
|
|
2014-07-22 09:47:22 -04:00
|
|
|
$scope.clearSearchText = ->
|
|
|
|
$scope.searchQueryText = ""
|
2014-07-22 12:43:17 -04:00
|
|
|
updateHits []
|
2014-07-22 09:47:22 -04:00
|
|
|
|
2014-07-01 09:25:32 -04:00
|
|
|
$scope.safeApply = (fn)->
|
|
|
|
phase = $scope.$root.$$phase
|
|
|
|
if(phase == '$apply' || phase == '$digest')
|
|
|
|
$scope.$eval(fn)
|
|
|
|
else
|
|
|
|
$scope.$apply(fn)
|
|
|
|
|
|
|
|
buildHitViewModel = (hit)->
|
|
|
|
result =
|
|
|
|
name : hit._highlightResult.name.value
|
|
|
|
description: hit._highlightResult.description.value
|
|
|
|
url :"/templates/#{hit._id}"
|
|
|
|
image_url: "#{window.sharelatex?.templates?.cdnDomain}/#{hit._id}/v/#{hit.version}/pdf-converted-cache/style-thumbnail"
|
|
|
|
|
|
|
|
updateHits = (hits)->
|
|
|
|
$scope.safeApply ->
|
2014-09-09 11:51:49 -04:00
|
|
|
console.log hits
|
2014-07-01 09:25:32 -04:00
|
|
|
$scope.hits = hits
|
|
|
|
|
|
|
|
$scope.search = ->
|
|
|
|
query = $scope.searchQueryText
|
|
|
|
if !query? or query.length == 0
|
|
|
|
updateHits []
|
|
|
|
return
|
|
|
|
|
|
|
|
query = "#{window.sharelatex?.templates?.user_id} #{query}"
|
|
|
|
algolia.search query, (err, response)->
|
|
|
|
if response.hits.length == 0
|
|
|
|
updateHits []
|
|
|
|
else
|
|
|
|
hits = _.map response.hits, buildHitViewModel
|
|
|
|
updateHits hits
|
2014-09-01 10:13:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
App.controller "MissingTemplateController", ($scope, $modal)->
|
|
|
|
$scope.showMissingTemplateModal = ->
|
|
|
|
$modal.open {
|
|
|
|
templateUrl: "missingTemplateModal"
|
|
|
|
controller:"MissingTemplateModalController"
|
|
|
|
}
|
|
|
|
|
|
|
|
App.controller "MissingTemplateModalController", ($scope, $modalInstance) ->
|
|
|
|
$scope.cancel = () ->
|
|
|
|
$modalInstance.dismiss()
|
|
|
|
|