overleaf/services/web/public/coffee/ide/templates/controllers/TemplatesController.coffee
2014-11-07 10:26:09 +00:00

84 lines
2.4 KiB
CoffeeScript

define [
"base"
"ide/permissions/PermissionsManager"
], (App, PermissionsManager) ->
App.controller "TemplatesController", ($scope, $modal, ide) ->
$scope.openPublishTemplateModal = () ->
resetState = ->
$scope.problemTalkingToTemplateApi = false
resetState()
modal = $modal.open {
templateUrl: "publishProjectAsTemplateModalTemplate"
controller: "PublishProjectAsTemplateModalController"
scope:$scope
}
modal.result.then(resetState, resetState)
App.controller "PublishProjectAsTemplateModalController", ($scope, $modalInstance, ide, $http) ->
user_id = ide.$scope.user.id
$scope.templateDetails = {exists:false}
$scope.state =
publishInflight: false
unpublishInflight: false
problemTalkingToTemplateApi = ->
$scope.problemTalkingToTemplateApi = true
refreshPublishedStatus = ->
$http.get("/project/#{ide.project_id}/template")
.success (data) ->
$scope.templateDetails = data
$scope.templateDetails.publishedDate = moment(data.publishedDate).format("Do MMM YYYY, h:mm a")
$scope.templateDetails.description = data.description
.error () ->
problemTalkingToTemplateApi()
refreshPublishedStatus()
$scope.$watch $scope.problemTalkingToTemplateApi, (value) ->
if value?
refreshPublishedStatus()
updateProjectDescription = ->
$http.post("/project/#{ide.project_id}/template/description", {
description: $scope.templateDetails.description
_csrf: window.csrfToken
})
# Save the description on modal close
$modalInstance.result.finally () -> updateProjectDescription()
$scope.publishTemplate = ->
$scope.state.publishInflight = true
updateProjectDescription()
.error () ->
problemTalkingToTemplateApi()
.success () ->
$http
.post("/project/#{ide.project_id}/template/publish", {
_csrf: window.csrfToken
})
.error () ->
problemTalkingToTemplateApi()
.success () ->
refreshPublishedStatus()
$scope.state.publishInflight = false
$scope.unpublishTemplate = ->
$scope.state.unpublishInflight = true
$http
.post("/project/#{ide.project_id}/template/unpublish", {
_csrf: window.csrfToken
})
.success () ->
refreshPublishedStatus()
$scope.state.unpublishInflight = false
.error () ->
problemTalkingToTemplateApi()
$scope.cancel = () ->
$modalInstance.dismiss()