開發(fā)基于Web的CSS設(shè)計器.代碼參考
2024-07-21 02:21:42
供稿:網(wǎng)友
這里對前面文章講的css設(shè)計器系統(tǒng)關(guān)鍵代碼作一些小結(jié),如果沒有看過前面文章的請先參看"開發(fā)基于web的css設(shè)計器"
解析css樣式文件
這段代碼主要作用是把css文件分解為多個樣式類,并按名稱/文本屬性生成classitem對象,并保存在一個arraylist(csslist)中(c#代碼)
//讀取文件
fileinfo thesource= new fileinfo (@m_filepath);
streamreader reader = thesource.opentext();
//將文件流轉(zhuǎn)化為文本
m_csstext = reader.readtoend();
reader.close();
//定義css文本分割符
char[] delimiters = new char[] { '{','}'};
int icheck = 1;
string classname = null;
//將文本轉(zhuǎn)化為arraylist
foreach ( string substring in m_csstext.split(delimiters))
{
if (icheck%2==0)
//當(dāng)icheck為偶數(shù)時,字符串為樣式屬性內(nèi)容
//將解析的樣式名和屬性作為classitem對象存入csslist
csslist.add( new classitem ( classname, substring.trim() ) );
else
//當(dāng)icheck為奇數(shù)時,字符串為樣式名,暫存
classname = substring.trim();
icheck++;
}
交互界面構(gòu)建
交互界面由javascript通過xmldocument讀取xml文件動態(tài)生成。
首先要讀取xml文件,然后遍歷整個xml文件,先遍歷樣式分類,再對每個分類遍歷其下的所有樣式屬性。比較關(guān)鍵的代碼是對xml的遍歷,下面是對樣式分類的遍歷代碼。
//loadxml是xml文件讀取函數(shù)
var dom = loadxml("css.xml");
//通過xpath和selectnodes方法返回一個xmldomnodelist對象
var onode = dom.selectnodes("//category/name");
//獲取該對象長度,即xml文檔中該路徑節(jié)點的數(shù)量
var intcategory = onodes.length;
for (i=0; i<intcategory; i++)
{
//獲取集合中的節(jié)點
onode = onodes.nextnode;
if (onode != null)
{
//樣式分類界面構(gòu)建代碼-略
……
}
}
樣式輸入控件構(gòu)建函數(shù),該函數(shù)作用是根據(jù)xpath路徑查詢xml定義,生成交互控件
function buildinput ( path )
{
var str="";
var anode=null;
var attvalue=null;
//通過selectsinglenode返回符合條件的第一個節(jié)點
var actnode = dom.selectsinglenode(path+"actiontype");
var namenode = dom.selectsinglenode(path+"cssname");
//如果屬性為選擇輸入,則讀取selectitems,并構(gòu)建select控件
if (actnode.text=="select")
{
str += "<select id='"+namenode.text+"' name='"+namenode.text+"' class='eselect'>/n";
//查詢該項的所有選擇列表項
var itemsnodes = dom.selectnodes (path+"selectitems/item");
str += "<option value='-1'>未設(shè)置</option>/n";
for (ii=0;ii<itemsnodes.length;ii++)
{
anode = dom.selectsinglenode (path+"selectitems/item["+ii+"]");
//如果該項含有name屬性則在列表中顯示name屬性值
attvalue = anode.getattribute("name")
var txtnode = dom.selectsinglenode (path+"selectitems/item["+ii+"]");
if (attvalue==null)
str += "<option value='"+txtnode.text+"'>"+txtnode.text+"</option>/n";
else
str += "<option value='"+txtnode.text+"'>"+attvalue+"</option>/n";
}
str += "</select>";
}
else
//如果屬性為其他模式,則構(gòu)建input輸入,設(shè)置class屬性為actiontype
{
str = "<input name='"+namenode.text+"' id='"+namenode.text+"' class='"+actnode.text+"'>/n";
}
return(str);
}
設(shè)計器初始化
js腳本讀取解析樣式元素的style屬性值,然后為設(shè)計器中構(gòu)建的控件賦值
//設(shè)計器初始化
function init()
{
//獲得由服務(wù)器端賦值的樣式屬性值
var txt=document.all("demoshow").style.csstext;
if (txt.length>0)
{
var strclassname;
//解析字符串
var aryclass = txt.split(/[:;]/);
for( i in aryclass)
{
var str = aryclass[i].replace(/(^/s*)|(/s*$)/g, "");
if(!(i%2==1))
{
//當(dāng)i為奇數(shù)時,解析的字符串應(yīng)該為樣式屬性名稱
strclassname=str;
}
else
{
//當(dāng)i為偶數(shù)時,獲得屬性值
//屬性名稱即控件id
//判斷該屬性對應(yīng)的控件是輸入框還是選擇列表
if(document.all(strclassname).type=="select-one")
{
//如果是選擇列表通過setindexofvalue函數(shù)設(shè)定選擇項
setindexofvalue(strclassname,str);
}
else
{
document.all(strclassname).value=str;
}
}
i++;
}
}
}
界面交互
在xml中一共定義了select/input_colorselect/input_sizeselect/input_borderselect(后3種為顏色/大小/邊框輸入模式)共4種輸入模式,除select為直接選擇外,其他在對應(yīng)控件初始化的時候作為class屬性賦值到控件中,類似class代碼如下
/* 顏色輸入模式input框的樣式類 */
.input_colorselect{
width:100px;
font-family:tahoma;
behavior:url(htc/effcolorselect.htc);
}
通過behavior屬性,把該輸入控件和相應(yīng)的組件相關(guān)聯(lián),該組件effcolorselect.htc代碼如下
<public:attach event="onfocus" onevent="getshow()"/>
<public:method name="getchange"/>
<script language="jscript">
function getshow()
{
element.blur();
//記錄當(dāng)前交互控件的id
effelement=element.id;
//在頁面中加載輸入控件
showcontrol ("selectcolor");
}
function getchange()
{
//當(dāng)值發(fā)生變化時,對可視化樣式元素賦值
setattribute(element.id,element.value);
}
</script>
其他
設(shè)計器中的值輸入模式框只是頁面中的幾個層,通過上面的htc組件觸發(fā)顯示出來,輸入后再把值傳入到樣式屬性控件中,同時也會設(shè)置可視化樣式元素。
另外還需要注意的是,xml文檔是可以自行擴(kuò)展或縮減的,但是在實際應(yīng)用中,不能完全依據(jù)css標(biāo)準(zhǔn)來定義,因為可視化元素的style屬性會自動格式化。例如如果你在xml中定義border-bottom-width屬性,在將值取出時會自動格式化為border-bottom,這樣會造成設(shè)計器中控件不能匹配。我在msdn沒有查到相關(guān)文檔,所以只有經(jīng)過實際測試來驗證。
ok,比較關(guān)鍵的代碼已經(jīng)差不多了……希望能對大家有所幫助。
參考
另外再列出部分技術(shù)參考,如果大家對其中的技術(shù)細(xì)節(jié)如htc和xmldom等有所疑問,可以再詳細(xì)研究一下,也歡迎大家來和我交流 [email protected] 。
msdn關(guān)于js操作xmldom的文檔
這是英文文檔,網(wǎng)上沒有看到比較詳細(xì)的中文文檔,好在不復(fù)雜,大家將就一下吧 :)
(最近msdn不知道什么毛病,經(jīng)常訪問有問題,如果無法訪問,請先登錄msdn,再輸入地址瀏覽)
藍(lán)色理想的htc教程
網(wǎng)上也沒看見比較全面的講述,這個簡單易學(xué),基本概念清楚了。