TemplatesController.coffee 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. define [
  2. "base"
  3. "ide/permissions/PermissionsManager"
  4. ], (App, PermissionsManager) ->
  5. App.controller "TemplatesController", ($scope, $modal, ide) ->
  6. $scope.openPublishTemplateModal = () ->
  7. resetState = ->
  8. $scope.problemTalkingToTemplateApi = false
  9. resetState()
  10. modal = $modal.open {
  11. templateUrl: "publishProjectAsTemplateModalTemplate"
  12. controller: "PublishProjectAsTemplateModalController"
  13. scope:$scope
  14. }
  15. modal.result.then(resetState, resetState)
  16. App.controller "PublishProjectAsTemplateModalController", ($scope, $modalInstance, ide, $http) ->
  17. user_id = ide.$scope.user.id
  18. $scope.templateDetails = {exists:false}
  19. $scope.state =
  20. publishInflight: false
  21. unpublishInflight: false
  22. problemTalkingToTemplateApi = ->
  23. $scope.problemTalkingToTemplateApi = true
  24. refreshPublishedStatus = ->
  25. $http.get("/project/#{ide.project_id}/template")
  26. .success (data) ->
  27. $scope.templateDetails = data
  28. $scope.templateDetails.publishedDate = moment(data.publishedDate).format("Do MMM YYYY, h:mm a")
  29. $scope.templateDetails.description = data.description
  30. .error () ->
  31. problemTalkingToTemplateApi()
  32. refreshPublishedStatus()
  33. $scope.$watch $scope.problemTalkingToTemplateApi, (value) ->
  34. if value?
  35. refreshPublishedStatus()
  36. updateProjectDescription = ->
  37. $http.post("/project/#{ide.project_id}/template/description", {
  38. description: $scope.templateDetails.description
  39. _csrf: window.csrfToken
  40. })
  41. # Save the description on modal close
  42. $modalInstance.result.finally () -> updateProjectDescription()
  43. $scope.publishTemplate = ->
  44. $scope.state.publishInflight = true
  45. updateProjectDescription()
  46. .error () ->
  47. problemTalkingToTemplateApi()
  48. .success () ->
  49. $http
  50. .post("/project/#{ide.project_id}/template/publish", {
  51. _csrf: window.csrfToken
  52. })
  53. .error () ->
  54. problemTalkingToTemplateApi()
  55. .success () ->
  56. refreshPublishedStatus()
  57. $scope.state.publishInflight = false
  58. $scope.unpublishTemplate = ->
  59. $scope.state.unpublishInflight = true
  60. $http
  61. .post("/project/#{ide.project_id}/template/unpublish", {
  62. _csrf: window.csrfToken
  63. })
  64. .success () ->
  65. refreshPublishedStatus()
  66. $scope.state.unpublishInflight = false
  67. .error () ->
  68. problemTalkingToTemplateApi()
  69. $scope.cancel = () ->
  70. $modalInstance.dismiss()