Skip to content
This repository has been archived by the owner on Aug 20, 2019. It is now read-only.

Commit

Permalink
Switched to using content type aliases, rather than guids. Everything…
Browse files Browse the repository at this point in the history
… is guid tolerant though so it should all be self updating / fixing.

Added support for having multiple views for a doc type alias by trying the editor alias first
  • Loading branch information
mattbrailsford committed Nov 28, 2015
1 parent 76f80f5 commit cb284a7
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 73 deletions.
Expand Up @@ -265,9 +265,9 @@
<PropertyGroup>
<PostBuildEvent>IF %25ComputerName%25 == MBP13-PC-BC (
IF NOT "$(SolutionDir)" == "*Undefined*" (
xcopy /s /y "$(TargetPath)" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoCms.7.2.6\bin"
xcopy /s /y "$(TargetDir)$(ProjectName).pdb" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoCms.7.2.6\bin"
xcopy /s /y "$(ProjectDir)Web\UI\*.*" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoCms.7.2.6"
xcopy /s /y "$(TargetPath)" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoUHangoutDemo\bin"
xcopy /s /y "$(TargetDir)$(ProjectName).pdb" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoUHangoutDemo\bin"
xcopy /s /y "$(ProjectDir)Web\UI\*.*" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoUHangoutDemo"
)
)</PostBuildEvent>
</PropertyGroup>
Expand Down
Expand Up @@ -40,9 +40,12 @@ public IEnumerable<object> GetContentTypes([System.Web.Http.ModelBinding.ModelBi
}

[System.Web.Http.HttpGet]
public object GetContentTypeIcon([System.Web.Http.ModelBinding.ModelBinder] Guid guid)
public object GetContentTypeIcon([System.Web.Http.ModelBinding.ModelBinder] string contentTypeAlias)
{
var contentTypeAlias = Services.ContentTypeService.GetAliasByGuid(guid);
Guid docTypeGuid;
if (Guid.TryParse(contentTypeAlias, out docTypeGuid))
contentTypeAlias = Services.ContentTypeService.GetAliasByGuid(docTypeGuid);

var contentType = Services.ContentTypeService.GetContentType(contentTypeAlias);
return new
{
Expand Down
Expand Up @@ -2,15 +2,18 @@
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Our.Umbraco.DocTypeGridEditor.Extensions;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Web;
using Content = System.Web.UI.WebControls.Content;

namespace Our.Umbraco.DocTypeGridEditor.Web.Extensions
{
public static class HtmlHelperExtensions
{
public static HtmlString RenderDocTypeGridEditorItem(this HtmlHelper helper,
IPublishedContent content,
string editorAlias = "",
string viewPath = "",
string previewViewPath = "")
{
Expand All @@ -25,12 +28,20 @@ public static class HtmlHelperExtensions
if (!string.IsNullOrWhiteSpace(previewViewPath))
previewViewPath = previewViewPath.TrimEnd('/') + "/";

var actionName = content.DocumentTypeAlias;

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
if (umbracoHelper.SurfaceControllerExists(controllerName, actionName, true))
if (!editorAlias.IsNullOrWhiteSpace() && umbracoHelper.SurfaceControllerExists(controllerName, editorAlias, true))
{
return helper.Action(actionName, controllerName, new
return helper.Action(editorAlias, controllerName, new
{
dtgeModel = content,
dtgeViewPath = viewPath,
dtgePreviewViewPath = previewViewPath
});
}

if (umbracoHelper.SurfaceControllerExists(controllerName, content.DocumentTypeAlias, true))
{
return helper.Action(content.DocumentTypeAlias, controllerName, new
{
dtgeModel = content,
dtgeViewPath = viewPath,
Expand All @@ -42,7 +53,13 @@ public static class HtmlHelperExtensions
if (!string.IsNullOrWhiteSpace(previewViewPath)
&& helper.ViewContext.RequestContext.HttpContext.Request.QueryString["dtgePreview"] == "1")
{
var fullPreviewViewPath = previewViewPath + content.DocumentTypeAlias + ".cshtml";
var fullPreviewViewPath = previewViewPath + editorAlias + ".cshtml";
if (ViewEngines.Engines.ViewExists(helper.ViewContext, fullPreviewViewPath, true))
{
return helper.Partial(fullPreviewViewPath, content);
}

fullPreviewViewPath = previewViewPath + content.DocumentTypeAlias + ".cshtml";
if (ViewEngines.Engines.ViewExists(helper.ViewContext, fullPreviewViewPath, true))
{
return helper.Partial(fullPreviewViewPath, content);
Expand All @@ -52,14 +69,25 @@ public static class HtmlHelperExtensions
// Check for view path view
if (!string.IsNullOrWhiteSpace(viewPath))
{
var fullViewPath = viewPath + content.DocumentTypeAlias + ".cshtml";
var fullViewPath = viewPath + editorAlias + ".cshtml";
if (ViewEngines.Engines.ViewExists(helper.ViewContext, fullViewPath, true))
{
return helper.Partial(fullViewPath, content);
}

fullViewPath = viewPath + content.DocumentTypeAlias + ".cshtml";
if (ViewEngines.Engines.ViewExists(helper.ViewContext, fullViewPath, true))
{
return helper.Partial(fullViewPath, content);
}
}

// Resort to standard partial views
if (ViewEngines.Engines.ViewExists(helper.ViewContext, editorAlias, true))
{
return helper.Partial(editorAlias, content);
}

// Resort to standard partial view
return helper.Partial(content.DocumentTypeAlias, content);
}
}
Expand Down
Expand Up @@ -4,30 +4,33 @@
"$rootScope",
"$timeout",
"$routeParams",
"editorState",
'assetsService',
"Our.Umbraco.DocTypeGridEditor.Services.DocTypeDialogService",
"Our.Umbraco.DocTypeGridEditor.Resources.DocTypeGridEditorResources",

function ($scope, $rootScope, $timeout, $routeParams, assetsService, dtgeDialogService, dtgeResources) {
function ($scope, $rootScope, $timeout, $routeParams, editorState, assetsService, dtgeDialogService, dtgeResources) {

$scope.title = "Click to insert item";
$scope.icon = "icon-item-arrangement";
$scope.icon = "icon-item-arrangement";

$scope.setValue = function (data) {
$scope.setValue = function (data, callback) {
$scope.control.value = data;
if (!("id" in $scope.control.value) || $scope.control.value.id == "") {
$scope.control.value.id = guid();
}
if ("name" in $scope.control.value.value && $scope.control.value.value.name) {
$scope.title = $scope.control.value.value.name;
}
if ("docType" in $scope.control.value && $scope.control.value.docType) {
dtgeResources.getContentTypeIcon($scope.control.value.docType).then(function (data2) {
if ("dtgeContentTypeAlias" in $scope.control.value && $scope.control.value.dtgeContentTypeAlias) {
dtgeResources.getContentTypeIcon($scope.control.value.dtgeContentTypeAlias).then(function (data2) {
if (data2.icon) {
$scope.icon = data2.icon;
}
});
}
if (callback)
callback();
};

$scope.setDocType = function () {
Expand All @@ -36,12 +39,12 @@
allowedDocTypes: $scope.control.editor.config.allowedDocTypes || [],
nameTemplate: $scope.control.editor.config.nameTemplate,
dialogData: {
docType: $scope.control.value.docType,
docTypeAlias: $scope.control.value.dtgeContentTypeAlias,
value: $scope.control.value.value
},
callback: function (data) {
$scope.setValue({
docType: data.docType,
dtgeContentTypeAlias: data.docTypeAlias,
value: data.value
});
$scope.setPreview($scope.control.value);
Expand All @@ -52,7 +55,7 @@
$scope.setPreview = function (model) {
if ("enablePreview" in $scope.control.editor.config && $scope.control.editor.config.enablePreview) {
var nodeId = $routeParams.id;
dtgeResources.getEditorMarkupForDocTypePartial(nodeId, model.id, model.docType, model.value, $scope.control.editor.config.viewPath, $scope.control.editor.config.previewViewPath)
dtgeResources.getEditorMarkupForDocTypePartial(nodeId, model.id, $scope.control.editor.alias, model.dtgeContentTypeAlias, model.value, $scope.control.editor.config.viewPath, $scope.control.editor.config.previewViewPath)
.success(function(htmlResult) {
if (htmlResult.trim().length > 0) {
$scope.preview = htmlResult;
Expand All @@ -61,11 +64,38 @@
}
};

$scope.setValue($scope.control.value || {
id: guid(),
docType: "",
value: {}
});
function init() {
$timeout(function () {
if ($scope.control.$initializing) {
$scope.setDocType();
} else if ($scope.control.value) {
$scope.setPreview($scope.control.value);
}
}, 200);
}

if ($scope.control.value) {
if (!$scope.control.value.dtgeContentTypeAlias && $scope.control.value.docType) {
$scope.control.value.dtgeContentTypeAlias = $scope.control.value.docType;
}
if ($scope.control.value.docType) {
delete $scope.control.value.docType;
}
if (isGuid($scope.control.value.dtgeContentTypeAlias)) {
dtgeResources.getContentTypeAliasByGuid($scope.control.value.dtgeContentTypeAlias).then(function(data1) {
$scope.control.value.dtgeContentTypeAlias = data1.alias;
$scope.setValue($scope.control.value, init);
});
} else {
$scope.setValue($scope.control.value, init);
}
} else {
$scope.setValue({
id: guid(),
dtgeContentTypeAlias: "",
value: {}
}, init);
}

// Load preview css / js files
if ("enablePreview" in $scope.control.editor.config && $scope.control.editor.config.enablePreview)
Expand All @@ -79,14 +109,6 @@
}
}

$timeout(function () {
if ($scope.control.$initializing) {
$scope.setDocType();
} else if ($scope.control.value) {
$scope.setPreview($scope.control.value);
}
}, 200);

function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
Expand All @@ -97,6 +119,10 @@
s4() + '-' + s4() + s4() + s4();
}

function isGuid(input) {
return new RegExp("^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$", "i").test(input.toString());
}

}
]);

Expand All @@ -122,7 +148,7 @@ angular.module("umbraco").controller("Our.Umbraco.DocTypeGridEditor.Dialogs.DocT

$scope.selectDocType = function () {
$scope.dialogMode = "edit";
$scope.dialogData.docType = $scope.selectedDocType.guid;
$scope.dialogData.docTypeAlias = $scope.selectedDocType.alias;
loadNode();
};

Expand Down Expand Up @@ -164,33 +190,30 @@ angular.module("umbraco").controller("Our.Umbraco.DocTypeGridEditor.Dialogs.DocT
};

function loadNode() {
dtgeResources.getContentAliasByGuid($scope.dialogData.docType).then(function (data1) {
contentResource.getScaffold(-20, data1.alias).then(function (data) {
// Remove the last tab
data.tabs.pop();

// Merge current value
if ($scope.dialogData.value) {
for (var t = 0; t < data.tabs.length; t++) {
var tab = data.tabs[t];
for (var p = 0; p < tab.properties.length; p++) {
var prop = tab.properties[p];
if ($scope.dialogData.value[prop.alias]) {
prop.value = $scope.dialogData.value[prop.alias];
}
contentResource.getScaffold(-20, $scope.dialogData.docTypeAlias).then(function (data) {
// Remove the last tab
data.tabs.pop();

// Merge current value
if ($scope.dialogData.value) {
for (var t = 0; t < data.tabs.length; t++) {
var tab = data.tabs[t];
for (var p = 0; p < tab.properties.length; p++) {
var prop = tab.properties[p];
console.log(prop.alias);
if ($scope.dialogData.value[prop.alias]) {
prop.value = $scope.dialogData.value[prop.alias];
}
}
};

// Assign the model to scope
$scope.nodeContext = $scope.node = data;
}
};

//editorState.set($scope.node);
});
// Assign the model to scope
$scope.nodeContext = $scope.node = data;
});
};
}

if ($scope.dialogData.docType) {
if ($scope.dialogData.docTypeAlias) {
$scope.dialogMode = "edit";
loadNode();
} else {
Expand All @@ -199,7 +222,7 @@ angular.module("umbraco").controller("Our.Umbraco.DocTypeGridEditor.Dialogs.DocT
dtgeResources.getContentTypes($scope.dialogOptions.allowedDocTypes).then(function (docTypes) {
$scope.docTypes = docTypes;
if ($scope.docTypes.length == 1) {
$scope.dialogData.docType = $scope.docTypes[0].guid;
$scope.dialogData.docTypeAlias = $scope.docTypes[0].alias;
$scope.dialogMode = "edit";
loadNode();
}
Expand Down
@@ -1,7 +1,7 @@
angular.module('umbraco.resources').factory('Our.Umbraco.DocTypeGridEditor.Resources.DocTypeGridEditorResources',
function ($q, $http, umbRequestHelper) {
return {
getContentAliasByGuid: function (guid) {
getContentTypeAliasByGuid: function (guid) {
var url = "/umbraco/backoffice/DocTypeGridEditorApi/DocTypeGridEditorApi/GetContentTypeAliasByGuid?guid=" + guid;
return umbRequestHelper.resourcePromise(
$http.get(url),
Expand All @@ -20,8 +20,8 @@
'Failed to retrieve content types'
);
},
getContentTypeIcon: function (guid) {
var url = "/umbraco/backoffice/DocTypeGridEditorApi/DocTypeGridEditorApi/GetContentTypeIcon?guid=" + guid;
getContentTypeIcon: function (contentTypeAlias) {
var url = "/umbraco/backoffice/DocTypeGridEditorApi/DocTypeGridEditorApi/GetContentTypeIcon?contentTypeAlias=" + contentTypeAlias;
return umbRequestHelper.resourcePromise(
$http.get(url),
'Failed to retrieve content type icon'
Expand All @@ -34,14 +34,15 @@
'Failed to retrieve datatypes'
);
},
getEditorMarkupForDocTypePartial: function (nodeId, id, docType, value, viewPath, previewViewPath) {
getEditorMarkupForDocTypePartial: function (nodeId, id, editorAlias, contentTypeAlias, value, viewPath, previewViewPath) {
var url = "/" + nodeId +"?dtgePreview=1";
return $http({
method: 'POST',
url: url,
data: $.param({
id: id,
docType: docType,
editorAlias: editorAlias,
contentTypeAlias: contentTypeAlias,
value: JSON.stringify(value),
viewPath: viewPath,
previewViewPath: previewViewPath
Expand Down
@@ -1,20 +1,30 @@
@using System.Web.Mvc
@using System.Web.Mvc.Html
@using Microsoft.CSharp.RuntimeBinder
@using Our.Umbraco.DocTypeGridEditor.Helpers
@using Our.Umbraco.DocTypeGridEditor.Web.Extensions
@inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic>
@if (Model.value != null)
{
string id = Model.value.id.ToString();
string docType = Model.value.docType.ToString();
string editorAlias = Model.editor.alias.ToString();
string contentTypeAlias = "";
string value = Model.value.value.ToString();
string viewPath = Model.editor.config.viewPath.ToString();
string previewViewPath = Model.editor.config.previewViewPath.ToString();

if(docType != "")
{
var content = DocTypeGridEditorHelper.ConvertValueToContent(id, docType, value);

try
{
contentTypeAlias = Model.value.dtgeContentTypeAlias.ToString();
}
catch (RuntimeBinderException)
{
contentTypeAlias = Model.value.docType.ToString();
}

if (contentTypeAlias != "")
{
var content = DocTypeGridEditorHelper.ConvertValueToContent(id, contentTypeAlias, value);

@Html.RenderDocTypeGridEditorItem(content, viewPath)
@Html.RenderDocTypeGridEditorItem(content, editorAlias, viewPath)
}
}

0 comments on commit cb284a7

Please sign in to comment.