-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripta.cs
More file actions
340 lines (292 loc) · 9.99 KB
/
Scripta.cs
File metadata and controls
340 lines (292 loc) · 9.99 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*!
* Scripta
* https://github.com/richet/Scripta
*
* Copyright 2011, Rich Chetwynd
* Released under the MIT Licenses.
*
* Date: 2011-03-01
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
namespace Scripta
{
public static class ScriptaExtensions
{
/// <summary>
/// Load up a collection of Bundle files
/// </summary>
/// <param name="source"></param>
/// <param name="name">The name of the Bundle section in the Scripta.config</param>
/// <returns></returns>
public static ScriptaHelper Scripta(this HtmlHelper source, string name)
{
return new ScriptaHelper(source, name);
}
}
public class ScriptaHelper
{
private readonly HtmlHelper _htmlHelper;
private readonly FilesCollection _files;
private readonly string _version;
private readonly string _cdn1;
private readonly string _cdn2;
public ScriptaHelper(HtmlHelper helper, string name)
{
_htmlHelper = helper;
var debug = false;
#if DEBUG
debug = true;
#endif
// Load up our config
var settings = ConfigurationManager.GetSection("scripta/settings") as Scripta.SettingsConfig;
var scripts = ConfigurationManager.GetSection("scripta/scripts") as Scripta.ScriptsConfig;
if (settings != null)
{
if (!string.IsNullOrEmpty(settings.Version))
_version = string.Format("?v={0}", settings.Version);
// Check if CDNs are in use
_cdn1 = settings.Cdn1;
_cdn2 = settings.Cdn2;
}
// Search for the Bundle group to add
var Bundle = (from s in scripts.Bundles.OfType<BundleElement>()
where s.Name == name
select s).FirstOrDefault();
// Biff an error if we dont find anything
if (Bundle == null)
throw new ArgumentException("Scripta has no idea what you're trying to achieve.");
// Load up the files based on the current app debug setting
_files = debug ? Bundle.Debug : Bundle.Prod;
}
/// <summary>
/// Render all of the css files in this section as html links
/// </summary>
/// <returns></returns>
public string Css()
{
StringBuilder sb = new StringBuilder();
foreach (FileElement f in _files)
{
if (CheckBrowser(f.Browser))
if (f.Src.EndsWith(".css"))
sb.AppendFormat("<link href=\"{0}{1}\" rel=\"stylesheet\" type=\"text/css\">", CheckCdn(f.Src), _version);
}
return sb.ToString();
}
/// <summary>
/// Render all of js files in this section as html Bundle tags
/// </summary>
/// <returns></returns>
public string Js()
{
StringBuilder sb = new StringBuilder();
foreach (FileElement f in _files)
{
if (CheckBrowser(f.Browser))
if (f.Src.EndsWith(".js"))
sb.AppendFormat("<script type=\"text/javascript\" src=\"{0}{1}\"></script>", CheckCdn(f.Src), _version);
}
return sb.ToString();
}
/// <summary>
/// Insert the cdn location if a placeholder has been specified in the file src
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
private string CheckCdn(string src)
{
if (!string.IsNullOrEmpty(_cdn1) || !string.IsNullOrEmpty(_cdn1))
return src.Replace("[cdn1]", _cdn1).Replace("[cdn2]", _cdn2);
return src;
}
/// <summary>
/// Do a cheap ol check on browser type.. This is pretty awful & needs some work :)
/// </summary>
/// <param name="browser"></param>
/// <returns></returns>
private bool CheckBrowser(string browser)
{
if (string.IsNullOrEmpty(browser)) return true;
if (HttpContext.Current != null)
{
var rq = HttpContext.Current.Request;
switch (browser)
{
case "ie6":
return (rq.Browser.Browser == "IE" && rq.Browser.MajorVersion == 6);
default:
return false;
}
}
return true;
}
/// <summary>
/// Test the useragent for the existince of a string..
/// </summary>
/// <param name="request"></param>
/// <param name="userAgentToTest"></param>
/// <returns></returns>
private bool IsUserAgent(HttpRequest request, string userAgentToTest)
{
if (request != null)
if (!string.IsNullOrEmpty(request.UserAgent))
return (request.UserAgent.IndexOf(userAgentToTest, StringComparison.OrdinalIgnoreCase) > 0);
return false;
}
}
/// <summary>
/// A source file
/// </summary>
public class FileElement : ConfigurationElement
{
[ConfigurationProperty("src", IsRequired = true, IsKey = true)]
public string Src
{
get { return (string)this["src"]; }
set { this["src"] = value; }
}
[ConfigurationProperty("browser", IsRequired = false, IsKey = true)]
public string Browser
{
get { return (string)this["browser"]; }
set { this["browser"] = value; }
}
}
/// <summary>
/// A collection of source files
/// </summary>
public class FilesCollection : ConfigurationElementCollection
{
public FileElement this[int index]
{
get { return base.BaseGet(index) as FileElement; }
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
this.BaseAdd(index, value);
}
}
}
protected override ConfigurationElement CreateNewElement()
{
return new FileElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((FileElement)element).Src;
}
}
public class BundleElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get
{
return (string)this["name"];
}
set
{
this["name"] = value;
}
}
/// <summary>
/// Files to use in production
/// </summary>
[ConfigurationProperty("prod", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(FileElement), AddItemName = "file")]
public FilesCollection Prod
{
get { return (FilesCollection)base["prod"]; }
}
/// <summary>
/// Files to use in debug/dev
/// </summary>
[ConfigurationProperty("debug", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(FileElement), AddItemName = "file")]
public FilesCollection Debug
{
get { return (FilesCollection)base["debug"]; }
}
}
/// <summary>
/// A collection of Bundles that we use in our application
/// </summary>
public class BundlesCollection : ConfigurationElementCollection
{
public BundleElement this[int index]
{
get { return base.BaseGet(index) as BundleElement; }
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
this.BaseAdd(index, value);
}
}
}
protected override ConfigurationElement CreateNewElement()
{
return new BundleElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((BundleElement)element).Name;
}
}
public class SettingsConfig : ConfigurationSection
{
public static SettingsConfig GetConfiguration()
{
SettingsConfig configuration = ConfigurationManager.GetSection("scripta/settings") as SettingsConfig;
if (configuration != null)
return configuration;
return new SettingsConfig();
}
[ConfigurationProperty("version", IsRequired = false, IsKey = true)]
public string Version
{
get { return (string)this["version"]; }
set { this["version"] = value; }
}
[ConfigurationProperty("cdn1", IsRequired = false, IsKey = true)]
public string Cdn1
{
get { return (string)this["cdn1"]; }
set { this["cdn1"] = value; }
}
[ConfigurationProperty("cdn2", IsRequired = false, IsKey = true)]
public string Cdn2
{
get { return (string)this["cdn2"]; }
set { this["cdn2"] = value; }
}
}
public class ScriptsConfig : ConfigurationSection
{
public static ScriptsConfig GetConfiguration()
{
ScriptsConfig configuration = ConfigurationManager.GetSection("scripta/scripts") as ScriptsConfig;
if (configuration != null)
return configuration;
return new ScriptsConfig();
}
[ConfigurationProperty("bundles", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(BundleElement), AddItemName = "bundle")]
public BundlesCollection Bundles
{
get { return (BundlesCollection)base["bundles"]; }
}
}
}