فهرست منبع

improved the publish button process so it talks to the templates-api and lets the user know what is going on

Henry Oswald 12 سال پیش
والد
کامیت
303a0e985b

+ 4 - 0
services/web/app/coffee/Features/Templates/TemplatesController.coffee

@@ -37,6 +37,10 @@ module.exports =
 		logger.log user_id:user_id, project_id:project_id, "reciving request to unpublish project as template"
 		TemplatesPublisher.unpublish user_id, project_id, callback
 
+	getTemplateDetails: (user_id, project_id, callback)->
+		TemplatesPublisher.getTemplateDetails user_id, project_id, callback
+
+
 setCompiler = (project_id, compiler, callback)->
 	if compiler?
 		ProjectOptionsHandler.setCompiler project_id, compiler, callback

+ 10 - 0
services/web/app/coffee/Features/Templates/TemplatesPublisher.coffee

@@ -17,5 +17,15 @@ module.exports =
 			callback()
 
 
+	getTemplateDetails: (user_id, project_id, callback)->
+		url = buildUrl(user_id, project_id)+"/details"
+		request.get url, (err, res, body)->
+			try
+				json = JSON.parse body
+			catch err
+				return callback err
+			callback(err, json)
+
+
 buildUrl = (user_id, project_id)->
 	url = "#{settings.apis.templates_api.url}/templates-api/user/#{user_id}/project/#{project_id}"

+ 4 - 0
services/web/app/coffee/router.coffee

@@ -358,3 +358,7 @@ module.exports = class Router
 
 			client.on "getLastTimePollHappned", (callback)->
 				EditorController.getLastTimePollHappned(callback)
+
+			client.on "getPublishedDetails", (user_id, callback)->
+				AuthorizationManager.ensureClientCanViewProject client, (error, project_id) =>
+					TemplatesController.getTemplateDetails user_id, project_id, callback

+ 11 - 4
services/web/app/views/templates.jade

@@ -289,18 +289,25 @@
 					|   Share URL
 
 
-	script(type="text/template")#publishProject
+	script(type="text/template")#publishProjectTemplate
 		-if(session && session.user && session.user.isAdmin)
 			.box
 				.page-header
 					h2 Publish Project
 
+				#publishedAsTemplateArea(style="display:none;")
+					a#templateLink(href='{{canonicalUrl}}') View Template
+					.btn.btn-warning#unPublishProjectAsTemplate unpublish project as template
+				#problemWithPublishingArea(style="display:none;")
+					p There is a problem with our publishing service, please try again in a few minutes.
+				#publishWorkingArea(style="display:none;")
+					p Working.....
+				#unpublishedAsTemplateArea(style="display:none;")
+					.btn.btn-success#publishProjectAsTemplate Publish project as template
 				div
-					.btn#publishProjectAsTemplate Publish project as template
-					.btn#unPublishProjectAsTemplate unpublish project as template
-				.row
 					textarea.span6#projectDescription {{description}}
 
+
 	script(type="text/template")#settingsPanelTemplate
 		.fullEditorArea.projectSettings
 			include project/partials/manage

+ 52 - 7
services/web/public/coffee/project-members/ProjectMembersManager.coffee

@@ -167,7 +167,7 @@ define [
 			@options.manager.removeMember(@model)
 
 	PublishProjectView = Backbone.View.extend
-		template: $("#publishProject").html()
+		template: $("#publishProjectTemplate").html()
 
 		events:
 			"click #publishProjectAsTemplate": "publishProjectAsTemplate"
@@ -181,18 +181,63 @@ define [
 			this.model.bind('change', this.render)
 
 		render: ->
-			viewModel = description:@model.get("description")
+			viewModel = 
+				description: @model.get("description")
+				canonicalUrl: @model.get("template.canonicalUrl")
+				isPublished: @model.get("template.isPublished")
+
 			$(@el).html $(Mustache.to_html(@template, viewModel))
+			@publishedArea = $('#publishedAsTemplateArea')
+			@unpublishedArea = $('#unpublishedAsTemplateArea')
+			@refreshPublishStatus()
 
-		updateDescription: ->
-			newDescription = $('#projectDescription').val()
-			this.model.set("description", newDescription)
+
+		refreshPublishStatus: ->
+			@ide.socket.emit "getPublishedDetails", @ide.user.get("id"), (err, details)=>
+				if err?
+					return @showError()
+
+				@model.set("template.isPublished", details.exists)
+				if details.exists
+					@model.set("template.canonicalUrl", details.canonicalUrl)
+					@publishedArea.show()
+					@unpublishedArea.hide()
+				else
+					@publishedArea.hide()
+					@unpublishedArea.show()
+
+		showError: ->
+			$('#problemWithPublishingArea').show()
+
+		showWorking: ->
+			$('#publishWorkingArea').show()
+
+		hideWorking: ->
+			$('#publishWorkingArea').hide()
 
 		publishProjectAsTemplate: ->
-			@ide.socket.emit "publishProjectAsTemplate", @ide.user.get("id"), ->
+			@showWorking()
+			@unpublishedArea.hide()
+			@ide.socket.emit "publishProjectAsTemplate", @ide.user.get("id"), (err)=>
+				@hideWorking()
+				if err?
+					@showError()
+				else 
+					@refreshPublishStatus()
 
 		unPublishProjectAsTemplate: ->
-			@ide.socket.emit "unPublishProjectAsTemplate", @ide.user.get("id"), ->
+			@showWorking()
+			@publishedArea.hide()
+			@ide.socket.emit "unPublishProjectAsTemplate", @ide.user.get("id"), (err)=>
+				@hideWorking()
+				if err?
+					@showError()
+				else 
+					@refreshPublishStatus()
+
+		updateDescription: ->
+			newDescription = $('#projectDescription').val()
+			@model.set("description", newDescription)
 
 	SocialSharingView = Backbone.View.extend
 		template: $("#socialSharingTemplate").html()

+ 16 - 1
services/web/test/UnitTests/coffee/Templates/TemplatesControllerTests.coffee

@@ -24,6 +24,7 @@ describe 'Templates Controller', ->
 		@TemplatesPublisher = 
 			publish: sinon.stub()
 			unpublish:sinon.stub()
+			getTemplateDetails: sinon.stub()
 		@controller = SandboxedModule.require modulePath, requires:
 			'../Uploads/ProjectUploadManager':@ProjectUploadManager
 			'../Project/ProjectOptionsHandler':@ProjectOptionsHandler
@@ -132,4 +133,18 @@ describe 'Templates Controller', ->
 			@controller.createProjectFromZipTemplate @req, res
 
 
-	describe '', ->
+	describe 'getTemplateDetails', ->
+
+		it "should return an error the templatePublisher", (done)->
+			error = "error"
+			@TemplatesPublisher.getTemplateDetails.callsArgWith(2, error)
+			@controller.getTemplateDetails @user_id, @project_id, (passedError)=>
+				passedError.should.equal error
+				done()
+
+		it "should return the details", (done)->
+			details = {exists:true}
+			@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, details)
+			@controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=>
+				details.should.equal passedDetails
+				done()

+ 22 - 2
services/web/test/UnitTests/coffee/Templates/TemplatesPublisherTests.coffee

@@ -4,7 +4,7 @@ assert = require('assert')
 path = require('path')
 sinon = require('sinon')
 modulePath = path.join __dirname, '../../../../app/js/Features/Templates/TemplatesPublisher'
-
+expect = require("chai").expect
 
 describe 'Templates publish', ->
 
@@ -12,6 +12,7 @@ describe 'Templates publish', ->
 		@request = 
 			post: sinon.stub().callsArgWith(1)
 			del: sinon.stub().callsArgWith(1)
+			get: sinon.stub()
 		@settings = 
 			apis:
 				templates_api:
@@ -39,4 +40,23 @@ describe 'Templates publish', ->
 			@TemplatesPublisher.unpublish @user_id, @project_id, =>
 				uri = "#{@settings.apis.templates_api.url}/templates-api/user/#{@user_id}/project/#{@project_id}"
 				@request.del.calledWith(uri).should.equal true
-				done()
+				done()
+
+
+	describe "getTemplateDetails", ->	
+		it "should make a get request to templates api", (done)->
+			body =
+				exists:true
+			@request.get.callsArgWith(1, null, null, JSON.stringify(body))
+			@TemplatesPublisher.getTemplateDetails @user_id, @project_id, (err, details)=>
+				uri = "#{@settings.apis.templates_api.url}/templates-api/user/#{@user_id}/project/#{@project_id}/details"
+				@request.get.calledWith(uri).should.equal true
+				assert.deepEqual details, body
+				done()
+
+
+		it "should catch an error thrown from trying to parse bad json", (done)->	
+			@request.get.callsArgWith(1, null, null, "<not json>")
+			@TemplatesPublisher.getTemplateDetails @user_id, @project_id, (err, details)=>
+				expect(err).to.exist	
+				done()