原創|其它|編輯:郝浩|2009-09-24 09:40:40.000|閱讀 1363 次
概述:Ext4JSLint是使用ExtAspNet來展示JSLint-Toolkit檢查結果的開源項目。 JSLint-Toolkit是一個使用Rhino和JSLint的開源項目,可以對一個文件夾中的所有JavaScript進行語法檢查,并顯示友好的檢查結果。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
引子
Ext4JSLint是使用來展示JSLint-Toolkit檢查結果的開源項目。
是一個使用Rhino和JSLint的開源項目,可以對一個文件夾中的所有JavaScript進行語法檢查,并顯示友好的檢查結果。
下面是JSLint-Toolkit為JavaScript生成的錯誤列表:
1.
[
2.
[3, 39, 20,
"Expected a number and instead saw '''."
,
"/Expected '{' and instead saw '.+'\\./,"
],
3.
[3, 39, 21,
"Expected '}' and instead saw ' '."
,
"/Expected '{' and instead saw '.+'\\./,"
],
4.
[3, 68, 18,
"Be careful when making functions within a loop. Consider putting the function in a closure."
,
"})();"
]
5.
]
這是一個類似二位數組的結構,每個數組有4列,分別表示錯誤級別,第幾行,第幾列,錯誤信息,錯誤代碼。
界面截圖
使用SyntaxHighlighter顯示JavaScript源代碼
首先看下右側IFrame中,如何使用SyntaxHighlighter來顯示JavaScript源代碼。
01.
<
html
>
02.
<
head
>
03.
<
title
>Untitled Page</
title
>
04.
<
link
href
=
"js/syntax/styles/shCore.css"
rel
=
"stylesheet"
type
=
"text/css"
/>
05.
<
link
href
=
"js/syntax/styles/shThemeDefault.css"
rel
=
"stylesheet"
type
=
"text/css"
/>
06.
<
style
type
=
"text/css"
">
07.
html, body
08.
{
09.
font-size: 12px;
10.
margin: 0px;
11.
padding: 0px;
12.
}
13.
.syntaxhighlighter
14.
{
15.
margin: 0 !important;
16.
}
17.
</
style
>
18.
<
script
src
=
"js/jquery-1.3.2.min.js"
type
=
"text/javascript"
></
script
>
19.
<
script
src
=
"js/syntax/scripts/shCore.js"
type
=
"text/javascript"
></
script
>
20.
<
script
src
=
"js/syntax/scripts/shBrushJScript.js"
type
=
"text/javascript"
></
script
>
21.
<
script
type
=
"text/javascript"
>
22.
// TODO
23.
</
script
>
24.
</
head
>
25.
<
body
>
26.
</
body
>
27.
</
html
>
我們引入了jQuery,來完成AJAX操作以及頁面的平滑滾動,來看下JavaScript代碼:
01.
SyntaxHighlighter.config.clipboardSwf =
'js/syntax/scripts/clipboard.swf'
;
02.
window.highlight =
function
(number) {
03.
$(
".line, highlighted"
).removeClass(
"highlighted"
);
04.
var
top = $(
".line:eq("
+ (number - 1) +
")"
).addClass(
"highlighted"
).offset().top - 150;
05.
$(
"html, body"
).animate({
06.
scrollTop: top
07.
}, 500);
08.
};
09.
window.loadJS =
function
(url, fn) {
10.
$.get(url,
function
(data) {
11.
data = data.replace(/</g,
"<"
).replace(/>/g,
">"
).replace(/\/r\/n/g,
"<br />"
);
12.
$(
"body"
).html(
""
);
13.
$(
"<pre class=\"brush: js\">"
+ data +
"</pre>"
).appendTo(
"body"
);
14.
SyntaxHighlighter.highlight();
15.
if
(fn) {
16.
fn();
17.
}
18.
},
"text"
);
19.
};
其中window.loadJS用來讀取一個url的內容(這里是JavaScript文件),并使用SyntaxHighlighter高亮顯示在頁面。
window.highlight用來高亮顯示某一行源代碼,同時這里使用jQuery的animate函數來完成頁面的平滑滾動。
由于這個頁面是嵌入在父頁面中的,所以接下來你會看到如何在父頁面中操作這兩個JavaScript函數。
父頁面中的JavaScript定義
01.
<script type=
"text/javascript"
>
02.
function
onReady() {
03.
04.
selectGridRow();
05.
}
06.
function
selectGridRow() {
07.
var
grid = Ext.getCmp(
"<%= Grid1.ClientID %>"
);
08.
grid.getSelectionModel().on(
"rowselect"
,
function
(sm, rowIndex, record) {
09.
getMainWindow().highlight(parseInt(record.json[0].replace(/<.*?>/g,
""
)));
10.
});
11.
}
12.
function
getMainWindow() {
13.
return
Ext.query(
"iframe[name=main]"
)[0].contentWindow;
14.
}
15.
16.
</script>
其中getMainWindow用來獲取IFrame的window實例。
而selectGridRow會在頁面加載完畢后立即執行,用來在選中Grid的某一行時高亮顯示右側IFrame中的對應行的源代碼(這里用到了上面提到的highlight函數)。
點擊樹節點的事件處理
還記得在上一章中,我們為Tree注冊了OnNodeCommand="Tree1_NodeCommand",下面來看下此函數:
01.
protected
void
Tree1_NodeCommand(
object
sender, ExtAspNet.TreeCommandEventArgs e)
02.
{
03.
string
fileName = e.CommandName.Replace(
"/"
,
"_"
);
04.
if
(ViewState[
"CurrentFileName"
] !=
null
&& ViewState[
"CurrentFileName"
].ToString() == fileName)
05.
{
06.
return
;
07.
}
08.
ViewState[
"CurrentFileName"
] = fileName;
09.
string
errorstr = GetFileContent(String.Format(
"~/data/errors/{0}.json"
, fileName));
10.
JSONArray ja =
new
JSONArray(errorstr);
11.
DataTable table =
new
DataTable();
12.
table.Columns.Add(
"level"
,
typeof
(Int32));
13.
table.Columns.Add(
"line"
,
typeof
(Int32));
14.
table.Columns.Add(
"character"
,
typeof
(Int32));
15.
table.Columns.Add(
"reason"
,
typeof
(String));
16.
table.Columns.Add(
"evidence"
,
typeof
(String));
17.
foreach
(JSONArray error
in
ja.getArrayList())
18.
{
19.
DataRow row = table.NewRow();
20.
table.Rows.Add(row);
21.
row[0] = Convert.ToInt32(error[0]);
22.
row[1] = Convert.ToInt32(error[1]);
23.
row[2] = Convert.ToInt32(error[2]);
24.
row[3] = error[3].ToString();
25.
row[4] = error[4].ToString();
26.
}
27.
Grid1.DataSource = table;
28.
Grid1.DataBind();
29.
string
showSourceScript =
"(function(){getMainWindow().loadJS('data/source/"
+ fileName +
"');}).defer(100);"
;
30.
ExtAspNet.PageContext.RegisterStartupScript(showSourceScript);
31.
}
這里面有兩個應用技巧:
注:ExtAspNet.PageContext.RegisterStartupScript是一個常用的函數,以后你會經常遇到。
在注冊的腳本中,我們使用defer(100)來讓這段腳本延遲一段執行,這是為了防止瀏覽器卡(因為同時綁定Grid和發起AJAX可能很耗資源)。
根據錯誤級別為Grid每一項添加文字顏色
對于非常嚴重的錯誤,需要標示文字顏色為紅色,而對于不要嚴重的錯誤只需文字顏色為淡黃色就行了。
為了實現這一效果,我們有兩種不同的方法都可以達到相同的目的。
1. 方法一,在Grid的RowDataBound事件做文章(這也是Asp.net的GridView控件常用的技巧)
01.
protected
void
Grid1_RowDataBound(
object
sender, ExtAspNet.GridRowEventArgs e)
02.
{
03.
// DataItem是行的數據源,
04.
// 如果數據源是DataTable/DataView/DataSet,則e.DataItem是DataRow
05.
// 如果數據源是List<MyClass>,則e.DataItem是MyClass
06.
// e.Values 是實際賦予此行每列的值,可以修改
07.
DataRow row = e.DataItem
as
DataRow;
08.
if
(row !=
null
)
09.
{
10.
int
level = Convert.ToInt32(row[
"level"
]);
11.
string
style = String.Empty;
12.
if
(level == 1)
13.
{
14.
style =
"color:#FF0000;"
;
15.
}
16.
else
17.
{
18.
style =
"color:#FF9900;"
;
19.
}
20.
for
(
int
i = 0; i < e.Values.Length; i++)
21.
{
22.
e.Values[i] = String.Format(
"<span style=\"{0}\">{1}</span>"
, style, e.Values[i]);
23.
}
24.
}
25.
}
2. 方法二,在Grid的PreRowDataBound事件做文章(這是ExtAspNet特有的事件)
需要特別注意的是,這里改變的是一列的屬性(請細細品味是列的屬性,不是某一行的屬性)。
01.
protected
void
Grid1_PreRowDataBound(
object
sender, ExtAspNet.GridPreRowEventArgs e)
02.
{
03.
DataRow row = e.DataItem
as
DataRow;
04.
if
(row !=
null
)
05.
{
06.
int
level = Convert.ToInt32(row[
"level"
]);
07.
string
style = String.Empty;
08.
if
(level == 1)
09.
{
10.
style =
"color:#FF0000;"
;
11.
}
12.
else
13.
{
14.
style =
"color:#FF9900;"
;
15.
}
16.
foreach
(ExtAspNet.GridColumn column
in
Grid1.Columns)
17.
{
18.
ExtAspNet.BoundField field = column
as
ExtAspNet.BoundField;
19.
field.DataFormatString =
"<span style=\""
+ style +
"\">{0}</span>"
;
20.
}
21.
}
22.
}
關于Ext4JSLint的內容就先介紹到這,下一章我們會接著介紹AppBox。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:互聯網