-
Notifications
You must be signed in to change notification settings - Fork 34
/
PageModel.cs
190 lines (169 loc) · 6.8 KB
/
PageModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using Newtonsoft.Json;
using Sdl.Web.Common.Logging;
using System;
using System.Globalization;
using System.Linq;
using Sdl.Web.Common.Configuration;
namespace Sdl.Web.Common.Models
{
/// <summary>
/// Represents the View Model for a Page
/// </summary>
[Serializable]
public class PageModel : ViewModel, ISyndicationFeedItemProvider
{
private const string XpmPageSettingsMarkup = "<!-- Page Settings: {{\"PageID\":\"{0}\",\"PageModified\":\"{1}\",\"PageTemplateID\":\"{2}\",\"PageTemplateModified\":\"{3}\"}} -->";
private const string XpmPageScript = "<script type=\"text/javascript\" language=\"javascript\" defer=\"defer\" src=\"{0}/WebUI/Editors/SiteEdit/Views/Bootstrap/Bootstrap.aspx?mode=js\" id=\"tridion.siteedit\"></script>";
private const string XpmDateTimeFormat = "yyyy-MM-ddTHH:mm:ss";
/// <summary>
/// Gets the Page Regions.
/// </summary>
[SemanticProperty(IgnoreMapping = true)]
public RegionModelSet Regions { get; private set; } = new RegionModelSet();
/// <summary>
/// Specifies whether the Page Model can be cached or not.
/// </summary>
[JsonIgnore]
[SemanticProperty(IgnoreMapping = true)]
public bool NoCache { get; set; }
/// <summary>
/// Gets or sets the URL path of the Page.
/// </summary>
[SemanticProperty(IgnoreMapping = true)]
public string Url { get; set; }
/// <summary>
/// Gets or sets the Page metadata which is typically rendered as HTML meta tags (name/value pairs).
/// </summary>
[SemanticProperty(IgnoreMapping = true)]
public IDictionary<string, string> Meta { get; set; }
/// <summary>
/// Gets or sets the identifier for the Page.
/// </summary>
[SemanticProperty(IgnoreMapping = true)]
public string Id { get; private set; }
/// <summary>
/// Gets or sets the Title of the Page which is typically rendered as HTML title tag.
/// </summary>
[SemanticProperty(IgnoreMapping = true)]
public string Title { get; set; }
public PageModel()
{
// required for deserialization
}
/// <summary>
/// Initializes a new instance of PageModel.
/// </summary>
/// <param name="id">The identifier of the Page.</param>
public PageModel(string id)
{
if (string.IsNullOrEmpty(id))
{
throw new DxaException("Page Model must have a non-empty identifier.");
}
Id = id;
Meta = new Dictionary<string, string>();
}
#region Overrides
/// <summary>
/// Gets the rendered XPM markup
/// </summary>
/// <param name="localization">The context Localization.</param>
/// <returns>The XPM markup.</returns>
public override string GetXpmMarkup(Localization localization)
{
if (XpmMetadata == null)
{
return string.Empty;
}
string cmsUrl = (localization.GetConfigValue("core.cmsurl") ?? string.Empty).TrimEnd('/');
return string.Format(
XpmPageSettingsMarkup,
XpmMetadata["PageID"],
GetDateTimeStr(XpmMetadata["PageModified"]),
XpmMetadata["PageTemplateID"],
GetDateTimeStr(XpmMetadata["PageTemplateModified"])) +
string.Format(XpmPageScript, cmsUrl);
}
private static string GetDateTimeStr(object datetime)
{
// legacy will pass a string here but R2 uses DateTime and so must be converted to the right
// format
var s = datetime as string;
return s ?? ((DateTime) datetime).ToString(XpmDateTimeFormat, CultureInfo.InvariantCulture);
}
#endregion
#region ISyndicationFeedItemProvider members
/// <summary>
/// Extracts syndication feed items.
/// </summary>
/// <param name="localization">The context <see cref="ILocalization"/>.</param>
/// <returns>The extracted syndication feed items; a concatentation of syndication feed items provided by <see cref="Regions"/> (if any).</returns>
public virtual IEnumerable<SyndicationItem> ExtractSyndicationFeedItems(Localization localization)
{
return ConcatenateSyndicationFeedItems(Regions, localization);
}
#endregion
/// <summary>
/// Filters (i.e. removes) conditional Entities which don't meet the conditions.
/// </summary>
/// <param name="localization">The context Localization.</param>
public void FilterConditionalEntities(Localization localization)
{
using (new Tracer(localization, this))
{
foreach (RegionModel region in Regions)
{
region.FilterConditionalEntities(localization);
}
}
}
/// <summary>
/// Creates a deep copy of this View Model.
/// </summary>
/// <returns>The copied View Model.</returns>
public override ViewModel DeepCopy()
{
PageModel clone = (PageModel) base.DeepCopy();
clone.Regions = new RegionModelSet(Regions.Select(r => (RegionModel) r.DeepCopy()));
if (Meta != null)
{
clone.Meta = new Dictionary<string, string>(Meta);
}
return clone;
}
/// <summary>
/// Determines whether the specified object is equal to the current Page Model.
/// </summary>
/// <returns>
/// <c>true</c> if the specified object in an Page Model with the same <see cref="Id"/> as the current one.
/// </returns>
/// <param name="obj">The object to compare with the current object. </param>
public override bool Equals(object obj)
{
PageModel other = obj as PageModel;
if (other == null)
{
return false;
}
return other.Id == Id;
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current Page Model.
/// </returns>
public override int GetHashCode()
=> Id.GetHashCode();
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string containing the type, identifier and title of the Page.
/// </returns>
public override string ToString()
=> $"{GetType().Name}: {Id} ('{Title}')";
}
}