forked from extnet/Ext.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleLoader.ashx.cs
More file actions
295 lines (252 loc) · 10.1 KB
/
ExampleLoader.ashx.cs
File metadata and controls
295 lines (252 loc) · 10.1 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using Ext.Net.Examples;
using Ext.Net;
using Ext.Net.Utilities;
namespace Ext.Net.Examples
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ExampleLoader : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string id = context.Request["id"];
string url = context.Request["url"];
string action = context.Request["action"];
if (string.IsNullOrEmpty(url))
{
return;
}
//url = url.ToLower();
if (!url.EndsWith("/"))
{
url = url + "/";
}
string examplesFolder = new Uri(HttpContext.Current.Request.Url, "Examples/").AbsolutePath.ToLower();
if (!url.StartsWith(examplesFolder,true, CultureInfo.InvariantCulture))
{
url = examplesFolder.TrimEnd(new []{'/'}) + url;
}
string wId = context.Request["wId"];
HttpRequest r = HttpContext.Current.Request;
Uri uri = new Uri(r.Url.Scheme + "://" + r.Url.Authority + url, UriKind.Absolute);
string path = context.Server.MapPath(uri.AbsolutePath);
DirectoryInfo dir = new DirectoryInfo(path);
ExampleConfig cfg = null;
if (File.Exists(dir.FullName + "config.xml"))
{
cfg = new ExampleConfig(dir.FullName + "config.xml", false);
}
if (action.IsNotEmpty() && false)
{
switch (action)
{
case "comments.count" :
int count = 0;
if (cfg != null)
{
count = cfg.Comments.Count;
}
context.Response.Write(count);
context.Response.End();
return;
case "comments.build" :
if (cfg != null)
{
context.Response.Write(JSON.Serialize(cfg.Comments));
}
context.Response.End();
return;
case "comments.add" :
if (cfg != null)
{
this.AddComment(context, cfg);
}
return;
case "tags.add":
if (cfg != null)
{
this.AddTag(context, cfg);
}
return;
case "tags.read":
if (cfg != null)
{
context.Response.Write(JSON.Serialize(cfg.Tags.ConvertAll(input => new { Tag = input })));
}
context.Response.End();
return;
}
}
if (id.StartsWith("extnet"))
{
id = "e" + Math.Abs(url.ToLower().GetHashCode());
}
string tabs = BuildSourceTabs(id, wId, cfg, dir);
//string script = string.Format("var w = Ext.getCmp({0});w.add({1});w.doLayout();", JSON.Serialize(wId), tabs);
Ext.Net.Utilities.CompressionUtils.GZipAndSend(tabs);
//context.Response.Write(tabs);
}
private string Shrink(string str, int limit)
{
if (str.IsEmpty())
{
return "";
}
return HttpContext.Current.Server.HtmlEncode(str.Substring(0, Math.Min(str.Length, limit)));
}
private static object lockObj = new object();
private void AddTag(HttpContext context, ExampleConfig config)
{
lock (lockObj)
{
try
{
config.AddTag(this.Shrink(context.Request["tag"], 128));
new DirectResponse().Return();
}
catch (Exception e)
{
new DirectResponse{Success = false, ErrorMessage = e.Message}.Return();
}
}
}
private void AddComment(HttpContext context, ExampleConfig config)
{
lock (lockObj)
{
try
{
string title = context.Request["CommentName"];
title = this.Shrink(title.IsEmpty() ? "[no title]" : title, 128);
string name = context.Request["UserName"];
name = this.Shrink( name.IsEmpty() ? "[anonymous]" : name, 128);
string message = this.Shrink(context.Request["CommentMessage"], 1024);
config.AddComment(new ExampleComment{Name = name, Title = title, Message = message});
context.Response.Write(JSON.Serialize(new {success = true}));
}
catch (Exception e)
{
context.Response.Write(JSON.Serialize(new { success = false, msg = e.Message }));
}
context.Response.End();
}
}
readonly string[] excludeFolders = new[] { ".svn", "_svn" };
readonly string[] excludeList = new[] { "config.xml" };
readonly string[] excludeExtensions = new[] { ".png", ".jpg", ".gif", ".bmp", ".psd" };
private string BuildSourceTabs(string id, string wId, ExampleConfig cfg, DirectoryInfo dir)
{
List<string> files = cfg != null ? cfg.OuterFiles : new List<string>();
FileInfo[] filesInfo = dir.GetFiles();
List<FileInfo> fileList = new List<FileInfo>(filesInfo);
int dIndex = 0;
for (int ind = 0; ind < fileList.Count; ind++)
{
if (fileList[ind].Name.ToLower() == "default.aspx")
{
dIndex = ind;
}
}
if (dIndex>0)
{
FileInfo fi = fileList[dIndex];
fileList.RemoveAt(dIndex);
fileList.Insert(0, fi);
}
foreach (string file in files)
{
fileList.Add(new FileInfo(file));
}
DirectoryInfo[] resources = dir.GetDirectories("resources",SearchOption.TopDirectoryOnly);
if (resources.Length > 0)
{
GetSubFiles(fileList, resources[0]);
}
TabPanel tabs = new TabPanel
{
ID = "tpw"+id,
Border = false,
ActiveTabIndex = 0
};
int i = 0;
foreach (FileInfo fileInfo in fileList)
{
if (excludeList.Contains(fileInfo.Name) || excludeExtensions.Contains(fileInfo.Extension.ToLower()))
{
continue;
}
Panel panel = new Panel();
panel.ID = "tptw" + id + i++;
panel.Title = fileInfo.Name;
panel.CustomConfig.Add(new ConfigItem("url", UIHelpers.PhysicalToVirtual(fileInfo.FullName), ParameterMode.Value));
switch (fileInfo.Extension)
{
case ".aspx":
case ".ascx":
panel.Icon = Icon.PageWhiteCode;
break;
case ".cs":
panel.Icon = Icon.PageWhiteCsharp;
break;
case ".xml":
case ".xsl":
panel.Icon = Icon.ScriptCodeRed;
break;
case ".js":
panel.Icon = Icon.Script;
break;
case ".css":
panel.Icon = Icon.Css;
break;
}
panel.Loader = new ComponentLoader();
panel.Loader.Url = UIHelpers.ApplicationRoot + "/GenerateSource.ashx";
panel.Loader.Mode = LoadMode.Frame;
panel.Loader.Params.Add(new Parameter("f", UIHelpers.PhysicalToVirtual(fileInfo.FullName), ParameterMode.Value));
panel.Loader.LoadMask.ShowMask = true;
tabs.Items.Add(panel);
}
return tabs.ToScript(RenderMode.AddTo, wId);
}
private void GetSubFiles(List<FileInfo> fileList, DirectoryInfo dir)
{
FileInfo[] filesInfo = dir.GetFiles();
foreach (FileInfo file in filesInfo)
{
if (excludeList.Contains(file.Name) || excludeExtensions.Contains(file.Extension.ToLower()))
{
continue;
}
fileList.Add(file);
}
DirectoryInfo[] folders = dir.GetDirectories();
foreach (DirectoryInfo folder in folders)
{
if (excludeFolders.Contains(folder.Name.ToLower()) || folder.Name.StartsWith("_"))
{
continue;
}
GetSubFiles(fileList, folder);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}