From 1b59c1ba6ec54654429454362ff0b5822eb5e8f1 Mon Sep 17 00:00:00 2001 From: Shannon Date: Sun, 9 Jun 2013 11:40:52 -0200 Subject: [PATCH] Got content item creation working and rte property editor working/saving --- .../App_Plugins/MyPackage/Package.manifest | 7 ++++ src/Umbraco.Web.UI/Umbraco.Web.UI.csproj | 1 - .../Views/propertyeditors/rte/rte.html | 3 ++ .../umbraco/js/umbraco.controllers.js | 22 ++++++----- .../umbraco/js/umbraco.resources.js | 37 ++++++++++++------- .../umbraco/js/umbraco.services.js | 3 ++ .../Editors/ContentEditorApiController.cs | 20 +++++++++- .../Models/ContentEditing/ContentItemBasic.cs | 11 +++++- .../ContentEditing/ContentItemDisplay.cs | 6 +-- .../ContentEditing/ContentSaveAction.cs | 19 +++++++++- .../Models/Mapping/ContentModelMapper.cs | 3 +- .../WebApi/Binders/ContentItemBinder.cs | 23 ++++++++++-- 12 files changed, 119 insertions(+), 36 deletions(-) create mode 100644 src/Umbraco.Web.UI/umbraco/Views/propertyeditors/rte/rte.html diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest index b6472e3358..45ce06e8fc 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/Package.manifest @@ -1,5 +1,12 @@ { propertyEditors: [ + { + id: "30CA72BD-A349-4386-B935-FA532DF24B4B", + name: "Rich Text Editor", + editor: { + view: "~/umbraco/Views/propertyeditors/rte/rte.html" + } + }, { id: "0BA0F832-D759-4526-9B3E-94BBFC98F92E", name: "Regex", diff --git a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj index 3253c3fa0f..4b03a9439b 100644 --- a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj +++ b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj @@ -566,7 +566,6 @@ - diff --git a/src/Umbraco.Web.UI/umbraco/Views/propertyeditors/rte/rte.html b/src/Umbraco.Web.UI/umbraco/Views/propertyeditors/rte/rte.html new file mode 100644 index 0000000000..6234d73fe1 --- /dev/null +++ b/src/Umbraco.Web.UI/umbraco/Views/propertyeditors/rte/rte.html @@ -0,0 +1,3 @@ +
+ +
\ No newline at end of file diff --git a/src/Umbraco.Web.UI/umbraco/js/umbraco.controllers.js b/src/Umbraco.Web.UI/umbraco/js/umbraco.controllers.js index 9a80185905..e193014a20 100644 --- a/src/Umbraco.Web.UI/umbraco/js/umbraco.controllers.js +++ b/src/Umbraco.Web.UI/umbraco/js/umbraco.controllers.js @@ -167,8 +167,12 @@ angular.module('umbraco').controller("Umbraco.Editors.ContentCreateController", }); angular.module("umbraco").controller("Umbraco.Editors.ContentEditController", function ($scope, $routeParams, contentResource, notifications, $q) { - if($routeParams.create) - $scope.content = contentResource.getContentScaffold($routeParams.parentId, $routeParams.doctype); + if ($routeParams.create) { + $q.when(contentResource.getContentScaffold($routeParams.id, $routeParams.doctype)) + .then(function (data) { + $scope.content = data; + }); + } else { $q.when(contentResource.getContent($routeParams.id)) .then(function (data) { @@ -177,14 +181,12 @@ angular.module("umbraco").controller("Umbraco.Editors.ContentEditController", fu } $scope.saveAndPublish = function (cnt) { - - contentResource.publishContent(cnt) - .then(function (data) { - //now we need to re-set the content model since the server will have updated it - $scope.content = data; - notifications.success("Published", "Content has been saved and published"); - }); - + contentResource.publishContent(cnt, $routeParams.create) + .then(function (data) { + //now we need to re-set the content model since the server will have updated it + $scope.content = data; + notifications.success("Published", "Content has been saved and published"); + }); }; $scope.save = function (cnt) { diff --git a/src/Umbraco.Web.UI/umbraco/js/umbraco.resources.js b/src/Umbraco.Web.UI/umbraco/js/umbraco.resources.js index ecd180700f..bca4c9892c 100644 --- a/src/Umbraco.Web.UI/umbraco/js/umbraco.resources.js +++ b/src/Umbraco.Web.UI/umbraco/js/umbraco.resources.js @@ -137,6 +137,10 @@ define(['app', 'angular'], function (app, angular) { function getContentUrl(contentId) { return Umbraco.Sys.ServerVariables.contentEditorApiBaseUrl + "GetContent?id=" + contentId; } + /** internal method to get the api url */ + function getEmptyContentUrl(contentTypeAlias, parentId) { + return Umbraco.Sys.ServerVariables.contentEditorApiBaseUrl + "GetEmptyContent?contentTypeAlias=" + contentTypeAlias + "&parentId=" + parentId; + } /** internal method to get the api url for publishing */ function getSaveUrl() { return Umbraco.Sys.ServerVariables.contentEditorApiBaseUrl + "PostSaveContent"; @@ -253,25 +257,32 @@ define(['app', 'angular'], function (app, angular) { // return undefined; - return content; + //return content; }, /** returns an empty content object which can be persistent on the content service requires the parent id and the alias of the content type to base the scaffold on */ getContentScaffold: function (parentId, alias) { - //use temp storage for now... + var deferred = $q.defer(); - var c = this.getContent(parentId); - c.name = "empty name"; + //go and get the data + $http.get(getEmptyContentUrl(alias, parentId)). + success(function (data, status, headers, config) { + //set the first tab to active + _.each(data.tabs, function (item) { + item.active = false; + }); + if (data.tabs.length > 0) + data.tabs[0].active = true; - $.each(c.tabs, function (index, tab) { - $.each(tab.properties, function (index, property) { - property.value = ""; + deferred.resolve(data); + }). + error(function (data, status, headers, config) { + deferred.reject('Failed to retreive data for empty content item type ' + alias); }); - }); - return c; + return deferred.promise; }, getChildren: function (parentId, options) { @@ -313,13 +324,13 @@ define(['app', 'angular'], function (app, angular) { }, /** saves or updates a content object */ - saveContent: function (content) { - return saveContentItem(content, "save"); + saveContent: function (content, isNew) { + return saveContentItem(content, "save" + (isNew ? "New" : "")); }, /** saves and publishes a content object */ - publishContent: function (content) { - return saveContentItem(content, "publish"); + publishContent: function (content, isNew) { + return saveContentItem(content, "publish" + (isNew ? "New" : "")); } }; diff --git a/src/Umbraco.Web.UI/umbraco/js/umbraco.services.js b/src/Umbraco.Web.UI/umbraco/js/umbraco.services.js index 664a10467c..64e1bf4aeb 100644 --- a/src/Umbraco.Web.UI/umbraco/js/umbraco.services.js +++ b/src/Umbraco.Web.UI/umbraco/js/umbraco.services.js @@ -78,6 +78,9 @@ define(['app', 'angular', 'underscore'], function (app, angular, underscore) { // we don't want to post all of the data as it is unecessary. var saveModel = { id: displayModel.id, + name: displayModel.name, + parentId: displayModel.parentId, + contentTypeAlias: displayModel.contentTypeAlias, properties: [], //set the action on the save model action: action diff --git a/src/Umbraco.Web/Editors/ContentEditorApiController.cs b/src/Umbraco.Web/Editors/ContentEditorApiController.cs index ef64521559..257f2d70f6 100644 --- a/src/Umbraco.Web/Editors/ContentEditorApiController.cs +++ b/src/Umbraco.Web/Editors/ContentEditorApiController.cs @@ -4,6 +4,7 @@ using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.ModelBinding; +using Umbraco.Core.Models; using Umbraco.Core.Models.Editors; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Models.Mapping; @@ -71,6 +72,23 @@ namespace Umbraco.Web.Editors return _contentModelMapper.ToContentItemDisplay(foundContent); } + /// + /// Gets an empty content item for the + /// + /// + /// + /// + public ContentItemDisplay GetEmptyContent(string contentTypeAlias, int parentId) + { + var contentType = Services.ContentTypeService.GetContentType(contentTypeAlias); + if (contentType == null) + { + throw new HttpResponseException(HttpStatusCode.NotFound); + } + var emptyContent = new Content("Empty", parentId, contentType); + return _contentModelMapper.ToContentItemDisplay(emptyContent); + } + /// /// Saves content /// @@ -91,7 +109,7 @@ namespace Umbraco.Web.Editors //Save the property values foreach (var p in contentItem.ContentDto.Properties) - { + { //get the dbo property var dboProperty = contentItem.PersistedContent.Properties[p.Alias]; diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs b/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs index 0dbae728d3..2cf545da93 100644 --- a/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs +++ b/src/Umbraco.Web/Models/ContentEditing/ContentItemBasic.cs @@ -26,6 +26,10 @@ namespace Umbraco.Web.Models.ContentEditing [Required] public int Id { get; set; } + [DataMember(Name = "name", IsRequired = true)] + [Required(AllowEmptyStrings = false)] + public string Name { get; set; } + [DataMember(Name = "properties")] public virtual IEnumerable Properties { @@ -39,7 +43,8 @@ namespace Umbraco.Web.Models.ContentEditing [DataMember(Name = "createDate")] public DateTime CreateDate { get; set; } - [DataMember(Name = "parentId")] + [DataMember(Name = "parentId", IsRequired = true)] + [Required] public int ParentId { get; set; } [DataMember(Name = "owner")] @@ -48,6 +53,10 @@ namespace Umbraco.Web.Models.ContentEditing [DataMember(Name = "updator")] public UserBasic Updator { get; set; } + [DataMember(Name = "contentTypeAlias", IsRequired = true)] + [Required(AllowEmptyStrings = false)] + public string ContentTypeAlias { get; set; } + /// /// The real persisted content object /// diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentItemDisplay.cs b/src/Umbraco.Web/Models/ContentEditing/ContentItemDisplay.cs index 2458c327fd..b778ecd865 100644 --- a/src/Umbraco.Web/Models/ContentEditing/ContentItemDisplay.cs +++ b/src/Umbraco.Web/Models/ContentEditing/ContentItemDisplay.cs @@ -15,11 +15,7 @@ namespace Umbraco.Web.Models.ContentEditing public ContentItemDisplay() { Tabs = new List>(); - } - - [DataMember(Name = "name", IsRequired = true)] - [Required(AllowEmptyStrings = false)] - public string Name { get; set; } + } [DataMember(Name = "icon")] public string Icon { get; set; } diff --git a/src/Umbraco.Web/Models/ContentEditing/ContentSaveAction.cs b/src/Umbraco.Web/Models/ContentEditing/ContentSaveAction.cs index 6c5440b115..91ac6e4fe4 100644 --- a/src/Umbraco.Web/Models/ContentEditing/ContentSaveAction.cs +++ b/src/Umbraco.Web/Models/ContentEditing/ContentSaveAction.cs @@ -5,7 +5,24 @@ /// public enum ContentSaveAction { + /// + /// Saves the content item, no publish + /// Save, - Publish + + /// + /// Saves and publishes the content item + /// + Publish, + + /// + /// Saves a new content item + /// + SaveNew, + + /// + /// Saves an publishes a new content item + /// + PublishNew } } \ No newline at end of file diff --git a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs index 7a29c0a973..8dcff66e6c 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs @@ -130,7 +130,8 @@ namespace Umbraco.Web.Models.Mapping Updator = _profileMapper.ToBasicUser(content.GetWriterProfile()), ParentId = content.ParentId, UpdateDate = content.UpdateDate, - CreateDate = content.CreateDate + CreateDate = content.CreateDate, + ContentTypeAlias = content.ContentType.Alias }; if (createProperties) result.Properties = content.Properties.Select(p => CreateProperty(p, propertyCreatedCallback)).ToArray(); diff --git a/src/Umbraco.Web/WebApi/Binders/ContentItemBinder.cs b/src/Umbraco.Web/WebApi/Binders/ContentItemBinder.cs index 47fe62ce5b..2871104aeb 100644 --- a/src/Umbraco.Web/WebApi/Binders/ContentItemBinder.cs +++ b/src/Umbraco.Web/WebApi/Binders/ContentItemBinder.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; @@ -9,8 +10,10 @@ using System.Web.Http.Controllers; using System.Web.Http.ModelBinding; using Newtonsoft.Json; using Umbraco.Core; +using Umbraco.Core.Models; using Umbraco.Web.Models.ContentEditing; using Umbraco.Web.Models.Mapping; +using Task = System.Threading.Tasks.Task; namespace Umbraco.Web.WebApi.Binders { @@ -133,8 +136,22 @@ namespace Umbraco.Web.WebApi.Binders }); } - //finally, let's lookup the real content item and create the DTO item - model.PersistedContent = _applicationContext.Services.ContentService.GetById(model.Id); + if (model.Action == ContentSaveAction.Publish && model.Action == ContentSaveAction.Save) + { + //finally, let's lookup the real content item and create the DTO item + model.PersistedContent = _applicationContext.Services.ContentService.GetById(model.Id); + } + else + { + //we are creating new content + var contentType = _applicationContext.Services.ContentTypeService.GetContentType(model.ContentTypeAlias); + if (contentType == null) + { + throw new InvalidOperationException("No content type found wth alias " + model.ContentTypeAlias); + } + model.PersistedContent = new Content(model.Name, model.ParentId, contentType); + } + model.ContentDto = _contentModelMapper.ToContentItemDto(model.PersistedContent); //we will now assign all of the values in the 'save' model to the DTO object foreach (var p in model.Properties)