PageRenderTime 55ms CodeModel.GetById 20ms app.highlight 15ms RepoModel.GetById 15ms app.codeStats 0ms

/Settings/RedditButtonPartSettings.cs

https://bitbucket.org/kobowi/kobowi.redditbutton
C# | 76 lines | 56 code | 14 blank | 6 comment | 4 complexity | c1794e4ed8d134d4374e4904d97d8499 MD5 | raw file
 1using System.Collections.Generic;
 2using Orchard.ContentManagement;
 3using Orchard.ContentManagement.MetaData;
 4using Orchard.ContentManagement.MetaData.Builders;
 5using Orchard.ContentManagement.MetaData.Models;
 6using Orchard.ContentManagement.ViewModels;
 7using Kobowi.RedditButton.Services;
 8
 9namespace Kobowi.RedditButton.Settings
10{
11
12    /// <summary>
13    /// Per-Content Type settings for a RedditButton part.
14    /// </summary>
15    public class RedditButtonPartTypeSettings
16    {
17        private string templateName;
18        private bool? openLinksInNewWindow;
19        private bool? styled;
20
21        public RedditButtonPartTypeSettings()
22        {
23            templateName = ButtonService.DefaultTemplateName;
24        }
25
26        public string TemplateName 
27        {
28            get { return templateName; }
29            set { templateName = value; }
30        }
31
32        public bool OpenLinksInNewWindow 
33        {
34            get { return openLinksInNewWindow ?? true; }
35            set { openLinksInNewWindow = value; }
36        }
37
38        public bool Styled
39        {
40            get { return styled ?? false; }
41            set { styled = value; }
42        }
43    }
44
45    public class RedditButtonPartSettings : ContentDefinitionEditorEventsBase
46    {
47
48        // Part settings on content type editor
49        public override IEnumerable<TemplateViewModel> TypePartEditor(ContentTypePartDefinition definition)
50        {
51            if (definition.PartDefinition.Name != "RedditButtonPart")
52                yield break;
53
54            // Get the settings for this type definition
55            var model = definition.Settings.GetModel<RedditButtonPartTypeSettings>();
56
57            yield return DefinitionTemplate(model);
58        }
59
60        // The update invoked by changes made on the part settings in the content type editor
61        public override IEnumerable<TemplateViewModel> TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel)
62        {
63            if (builder.Name != "RedditButtonPart")
64                yield break;
65
66            var model = new RedditButtonPartTypeSettings();
67            updateModel.TryUpdateModel(model, "RedditButtonPartTypeSettings", null, null);  
68
69            builder.WithSetting("RedditButtonPartTypeSettings.TemplateName", model.TemplateName);
70            builder.WithSetting("RedditButtonPartTypeSettings.Styled", model.Styled.ToString());
71            builder.WithSetting("RedditButtonPartTypeSettings.OpenLinksInNewWindow", model.OpenLinksInNewWindow.ToString());
72
73            yield return DefinitionTemplate(model);
74        }
75    }
76}