Repository navigation
Expand file tree
/
Copy pathPageHeader.cs
More file actions
179 lines (161 loc) · 6 KB
/
Copy pathPageHeader.cs
File metadata and controls
179 lines (161 loc) · 6 KB
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
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace BlogEngine.Core.Web.Scripting
{
/// <summary>
/// Class to process links dynamically injected into page header
/// </summary>
public class PageHeader
{
List<LiteralControl> HeaderLinks = new List<LiteralControl>();
/// <summary>
/// Add link to collection
/// </summary>
/// <param name="type">Type</param>
/// <param name="rel">Relation</param>
/// <param name="title">Title</param>
/// <param name="href">Href</param>
public void AddLink(string type, string rel, string title, string href)
{
var link = new LiteralControl();
var ptrn = "\n\t<link rel=\"{0}\" title=\"{1}\" href=\"{2}\" />";
if (!string.IsNullOrEmpty(type))
{
ptrn = "\n\t<link type=\"{0}\" rel=\"{1}\" title=\"{2}\" href=\"{3}\" />";
link.Text = string.Format(ptrn, type, rel, title, href);
}
else
{
link.Text = string.Format(ptrn, rel, title, href);
}
if (!HeaderLinks.Contains(link))
{
HeaderLinks.Add(link);
}
}
/// <summary>
/// Render links by adding them to the page header
/// </summary>
/// <param name="page">Base page</param>
public void Render(System.Web.UI.Page page)
{
int idx = 0;
foreach (var link in HeaderLinks)
{
page.Header.Controls.AddAt(idx, link);
idx++;
}
foreach (var style in GetStyles())
{
page.Header.Controls.AddAt(idx, style);
idx++;
}
idx = GetIndex(page);
foreach (var script in GetScripts())
{
page.Header.Controls.AddAt(idx, script);
idx++;
}
}
#region Private Methods
List<LiteralControl> GetStyles()
{
var headerStyles = new List<LiteralControl>();
var tmpl = "\n\t<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />";
foreach (var f in GetFiles($"{Utils.ApplicationRelativeWebRoot}Content/Auto"))
{
var href = $"{Utils.ApplicationRelativeWebRoot}Content/Auto/{f}";
headerStyles.Add(new LiteralControl(string.Format(tmpl, href)));
}
return headerStyles;
}
List<LiteralControl> GetScripts()
{
var headerScripts = new List<LiteralControl>();
var tmpl = "\n\t<script type=\"text/javascript\" src=\"{0}\"></script>";
var lang = BlogSettings.Instance.Language;
// if specific culture set in blog settings, use it instead
if (BlogSettings.Instance.Culture.ToLower() != "auto")
lang = BlogSettings.Instance.Culture;
var rsrc = HttpHandlers.ResourceHandler.GetScriptPath(new CultureInfo(lang));
headerScripts.Add(new LiteralControl(string.Format(tmpl, rsrc)));
foreach (var f in GetFiles($"{Utils.ApplicationRelativeWebRoot}Scripts/Auto"))
{
var href = $"{Utils.ApplicationRelativeWebRoot}Scripts/Auto/{f}";
headerScripts.Add(new LiteralControl(string.Format(tmpl, href)));
}
return headerScripts;
}
static List<string> GetFiles(string url)
{
List<string> files = new List<string>();
string path = System.Web.HttpContext.Current.Server.MapPath(url);
var folder = new DirectoryInfo(path);
if (folder.Exists)
{
foreach (var file in folder.GetFiles())
{
files.Add(file.Name);
}
}
return files;
}
static int GetIndex(System.Web.UI.Page page)
{
// insert global scripts just before first script tag in the header
// or after last css style tag if no script tags in the header found
int cnt = 0;
int idx = 0;
string ctrlText = "";
foreach (Control ctrl in page.Header.Controls)
{
cnt++;
try
{
if (ctrl.GetType() == typeof(LiteralControl))
{
LiteralControl lc = (LiteralControl)ctrl;
ctrlText = lc.Text.ToLower();
}
if (ctrl.GetType() == typeof(Literal))
{
Literal lc = (Literal)ctrl;
ctrlText = lc.Text.ToLower();
}
if (ctrl.GetType() == typeof(DataBoundLiteralControl))
{
DataBoundLiteralControl lc = (DataBoundLiteralControl)ctrl;
ctrlText = lc.Text.ToLower();
}
if (ctrl.GetType() == typeof(HtmlLink))
{
HtmlLink hl = (HtmlLink)ctrl;
if (!string.IsNullOrEmpty(hl.Attributes["type"]))
{
ctrlText = hl.Attributes["type"].ToLower();
}
}
if (ctrlText.Contains("text/css"))
idx = cnt;
if (ctrlText.Contains("text/javascript"))
{
idx = cnt;
// offset by 1 as we need inject before
if (idx > 1) idx = idx - 1;
break;
}
}
catch
{
break;
}
}
return idx;
}
#endregion
}
}