From 6ddaa7cd4de0ee41e3a4f1bccc5e6a7b9f2b2ada Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 4 Jun 2014 16:57:30 +1000 Subject: [PATCH] Allows configurable segments to be advertised as variants --- .../settings/segmentdashboard.controller.js | 33 ++++++++++++------- .../dashboard/settings/segmentdashboard.html | 8 +++-- .../Editors/SegmentDashboardController.cs | 3 +- .../Models/Segments/SegmentProviderMatch.cs | 6 ++++ .../Segments/ConfigurableSegmentProvider.cs | 17 ++++++++-- .../Segments/ContentSegmentProvider.cs | 2 +- .../ContentSegmentProviderResolver.cs | 6 ++-- 7 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/views/dashboard/settings/segmentdashboard.controller.js b/src/Umbraco.Web.UI.Client/src/views/dashboard/settings/segmentdashboard.controller.js index 464dc77804..80f9968a3f 100644 --- a/src/Umbraco.Web.UI.Client/src/views/dashboard/settings/segmentdashboard.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/dashboard/settings/segmentdashboard.controller.js @@ -1,5 +1,14 @@ function segmentDashboardController($scope, umbRequestHelper, $log, $http, formHelper) { + function refreshData() { + return umbRequestHelper.resourcePromise( + $http.get(umbRequestHelper.getApiUrl("segmentDashboardApiBaseUrl", "GetProviders")), + "Failed to retrieve provider data") + .then(function(data) { + $scope.providers = data; + }); + } + $scope.providers = []; $scope.configuringSegments = false; $scope.providerConfig = { @@ -74,8 +83,11 @@ function segmentDashboardController($scope, umbRequestHelper, $log, $http, formH var provider = _.find($scope.providers, function (i) { return i.typeName === $scope.providerConfig.providerTypeName; }); provider.segmentConfig = $scope.segmentConfig; - //exit editing config - $scope.cancel(); + refreshData().then(function() { + //exit editing config + $scope.cancel(); + }); + }); } } @@ -94,17 +106,14 @@ function segmentDashboardController($scope, umbRequestHelper, $log, $http, formH var provider = _.find($scope.providers, function (i) { return i.typeName === $scope.providerConfig.providerTypeName; }); provider.variantConfig = $scope.variantConfig; - //exit editing config - $scope.cancel(); + refreshData().then(function () { + //exit editing config + $scope.cancel(); + }); }); } - - umbRequestHelper.resourcePromise( - $http.get(umbRequestHelper.getApiUrl("segmentDashboardApiBaseUrl", "GetProviders")), - "Failed to retrieve provider data") - .then(function(data) { - $scope.providers = data; - }); - + + //initial data load + refreshData(); } angular.module("umbraco").controller("Umbraco.Dashboard.SegmentDashboard", segmentDashboardController); \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/views/dashboard/settings/segmentdashboard.html b/src/Umbraco.Web.UI.Client/src/views/dashboard/settings/segmentdashboard.html index 84c4715f80..b4975806d5 100644 --- a/src/Umbraco.Web.UI.Client/src/views/dashboard/settings/segmentdashboard.html +++ b/src/Umbraco.Web.UI.Client/src/views/dashboard/settings/segmentdashboard.html @@ -81,8 +81,12 @@ Required - - + + + + + + diff --git a/src/Umbraco.Web/Editors/SegmentDashboardController.cs b/src/Umbraco.Web/Editors/SegmentDashboardController.cs index 38b2954ead..739cf6d35e 100644 --- a/src/Umbraco.Web/Editors/SegmentDashboardController.cs +++ b/src/Umbraco.Web/Editors/SegmentDashboardController.cs @@ -50,7 +50,8 @@ namespace Umbraco.Web.Editors { name = vari.VariantName, key = vari.SegmentMatchKey, - enabled = x.variantConfig.ContainsKey(vari.SegmentMatchKey) && x.variantConfig[vari.SegmentMatchKey] + enabled = x.variantConfig.ContainsKey(vari.SegmentMatchKey) && x.variantConfig[vari.SegmentMatchKey], + asVariant = x.variantConfig.ContainsKey(vari.SegmentMatchKey) && x.variantConfig[vari.SegmentMatchKey] }) }); diff --git a/src/Umbraco.Web/Models/Segments/SegmentProviderMatch.cs b/src/Umbraco.Web/Models/Segments/SegmentProviderMatch.cs index ba3778d25e..4e89313482 100644 --- a/src/Umbraco.Web/Models/Segments/SegmentProviderMatch.cs +++ b/src/Umbraco.Web/Models/Segments/SegmentProviderMatch.cs @@ -35,5 +35,11 @@ namespace Umbraco.Web.Models.Segments /// [DataMember(Name = "persist")] public bool Persist { get; set; } + + /// + /// A flag to determine if this segment is allowed to be a variant (advertised) + /// + [DataMember(Name = "asVariant")] + public bool AllowedAsVariant { get; set; } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Routing/Segments/ConfigurableSegmentProvider.cs b/src/Umbraco.Web/Routing/Segments/ConfigurableSegmentProvider.cs index 7f6acdcf56..c869f013a6 100644 --- a/src/Umbraco.Web/Routing/Segments/ConfigurableSegmentProvider.cs +++ b/src/Umbraco.Web/Routing/Segments/ConfigurableSegmentProvider.cs @@ -23,13 +23,26 @@ namespace Umbraco.Web.Routing.Segments /// which would normally be a regex statement, if it matches the returned value then we will apply the configured key/value as a segment /// in the request. /// - /// TODO: We need to decide if configurable segment provider can advertise segments to be used in variations - but I don't think so since they - /// can be added/removed by users /// public abstract class ConfigurableSegmentProvider : ContentSegmentProvider { private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); + /// + /// Override to return the statically assigned variants for this provider as well as any segments + /// that are configured to be variants. + /// + public override IEnumerable AssignableContentVariants + { + get + { + return base.AssignableContentVariants.Union( + ReadSegmentConfiguration() + .Where(x => x.AllowedAsVariant) + .Select(x => new ContentVariantAttribute(x.Key, x.Key, x.Value))); + } + } + /// /// Returns the current provider's value (i.e. if the provider was a referal provider, this would return the current referrer) /// diff --git a/src/Umbraco.Web/Routing/Segments/ContentSegmentProvider.cs b/src/Umbraco.Web/Routing/Segments/ContentSegmentProvider.cs index 690a1750a2..344e2f9ad1 100644 --- a/src/Umbraco.Web/Routing/Segments/ContentSegmentProvider.cs +++ b/src/Umbraco.Web/Routing/Segments/ContentSegmentProvider.cs @@ -41,7 +41,7 @@ namespace Umbraco.Web.Routing.Segments /// /// Returns the Content Variants that this provider supports /// - public IEnumerable AssignableContentVariants + public virtual IEnumerable AssignableContentVariants { get { return GetType().GetCustomAttributes(false).ToArray(); } } diff --git a/src/Umbraco.Web/Routing/Segments/ContentSegmentProviderResolver.cs b/src/Umbraco.Web/Routing/Segments/ContentSegmentProviderResolver.cs index 8ee4b6d445..d8e48e2cef 100644 --- a/src/Umbraco.Web/Routing/Segments/ContentSegmentProviderResolver.cs +++ b/src/Umbraco.Web/Routing/Segments/ContentSegmentProviderResolver.cs @@ -40,7 +40,8 @@ namespace Umbraco.Web.Routing.Segments public IEnumerable GetAssignableVariants(IDictionary segmentProviderStatus) { - //These are the assignable variants based on the installed providers (statically advertised variants) + //These are the assignable variants based on the installed providers (statically advertised variants) or + // configured segments that have been flagged as variants // that are enabled via the back office. If they are not enabled, they will not show up. var assignableSegments = this.Providers @@ -55,7 +56,8 @@ namespace Umbraco.Web.Routing.Segments .Select(vari => vari.Key).ToArray() // get the key }) //only allow the onces that are enabled - .SelectMany(x => x.instance.AssignableContentVariants.Where(vari => x.enabledVariants.Contains(vari.SegmentMatchKey))); + .SelectMany(x => + x.instance.AssignableContentVariants.Where(vari => x.enabledVariants.Contains(vari.SegmentMatchKey))); return assignableSegments; }