⇤ ← Revision 1 as of 2008-09-28 07:07:21
Size: 9984
Comment:
|
Size: 9759
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
''' 含有章节索引的 *PUG 文章通用模板 ''' ::-- ["hf_linux"] [[[DateTime(2008-09-28T07:07:21Z)]]] [[TableOfContents]] |
''' 含有章节索引的 *PUG 文章通用模板 ''' ::-- [:hf linux:hf_linux] [[[DateTime(2008-09-28T07:07:21Z)]]] [[TableOfContents]] |
Line 11: | Line 8: |
Line 14: | Line 10: |
Line 15: | Line 12: |
Line 33: | Line 29: |
Line 38: | Line 34: |
def setXmlSrc(self,xmlSrc): | def setXmlSrc(self,xmlSrc): |
Line 41: | Line 37: |
return self.__xmlSrc | return self.__xmlSrc |
Line 43: | Line 39: |
Line 54: | Line 49: |
Line 68: | Line 61: |
Line 70: | Line 62: |
Line 75: | Line 67: |
treeRootName = self.rootName | treeRootName = self.rootName |
Line 93: | Line 84: |
Line 100: | Line 91: |
Line 106: | Line 96: |
Line 113: | Line 102: |
Line 123: | Line 112: |
Line 131: | Line 120: |
Line 139: | Line 128: |
buf += prefix | buf += prefix |
Line 142: | Line 131: |
return buf |
return buf |
Line 158: | Line 147: |
if self.action: | if self.action: |
Line 164: | Line 153: |
Line 174: | Line 163: |
Line 184: | Line 172: |
Line 187: | Line 175: |
Line 193: | Line 180: |
Line 198: | Line 185: |
Line 224: | Line 211: |
Line 236: | Line 223: |
Line 240: | Line 227: |
Line 245: | Line 232: |
Line 251: | Line 237: |
if self.action: |
if self.action: |
Line 258: | Line 244: |
Line 267: | Line 253: |
return buf |
return buf |
Line 275: | Line 261: |
Line 279: | Line 265: |
self.inputType = "radio" | self.inputType = "radio" |
Line 282: | Line 268: |
Line 292: | Line 278: |
Line 297: | Line 283: |
Line 303: | Line 289: |
Line 308: | Line 294: |
Line 313: | Line 299: |
Line 316: | Line 301: |
Line 319: | Line 302: |
Line 321: | Line 303: |
xxx | [javascript:void(0);/*1222606527995*/ 目标效果] |
Line 324: | Line 306: |
Line 330: | Line 311: |
Line 332: | Line 312: |
yyy | |
Line 335: | Line 314: |
[[PageComment2]] [:/PageCommentData:PageCommentData] | [[PageComment2]] [:hf linux/2008-09-28/PageCommentData:PageCommentData] |
含有章节索引的 *PUG 文章通用模板 ::-- [:hf linux:hf_linux] [DateTime(2008-09-28T07:07:21Z)] TableOfContents
1. 文章大标
用python生成xtree树形菜单
1.1. 模型
1.1.1. 模型代码
1 #coding=utf-8
2 class TreeObject(object):
3 def __init__(self,name="",action="",xmlSrc=""):
4 self.name = name
5 if action:
6 self.action = "\""+action+"\""
7 else:
8 self.action = "null"
9 self.__xmlSrc = xmlSrc #tree.xmlSrc="/getNOde ?...."
10 self.icon = "null"
11 self.openIcon = "null"
12 self.parent = ""
13 self.spanLevel = 1
14 self.subTreeItems = []
15
16 def addSubTreeItem(self,treeItem):
17 self.subTreeItems.append(treeItem)
18 treeItem.parent = self
19 #TODO :escape
20 def setXmlSrc(self,xmlSrc):
21 self.__xmlSrc = xmlSrc
22 def getXmlSrc(self):
23 return self.__xmlSrc
24 xmlSrc = property(getXmlSrc,setXmlSrc)
25 def toXml(self):
26 buf = ""
27 buf += "<tree>\n"
28 i=0
29 for item in self.subTreeItems:
30 tmp = item.itemToXml(i)
31 buf += tmp
32 i+=1
33 buf += "</tree>\n"
34 return buf
35
36
37 class JsTree(TreeObject):
38 #static variable
39 idxGenerator = ""
40 def __init__(self,name="",action="",xmlSrc=""):
41 if name == "":
42 name = "jsTree"
43 super(JsTree, self).__init__(name,action,xmlSrc)
44 self.rootName = ""
45 self.behavior = "classic"
46
47 def getJsTreeStr(self):
48
49 buf= "<script language=\"JavaScript\">" + "\n"
50 if not self.rootName:
51 self.rootName = "root"+ str(JsTree.getJsVariableIndex())
52 #TODO :escapeJavaScript
53 treeRootName = self.rootName
54 if not self.xmlSrc:
55 buf += "\tvar "+treeRootName+" = new WebFXTree('" + self.name +"',"+\
56 self.action+",null,"+self.icon +\
57 ","+self.openIcon+");"+"\n";
58 else:
59 buf += "\tvar "+treeRootName+" = new WebFXTree('" + self.name +"',"+\
60 self.xmlSrc + "\",\"" +\
61 self.action+",null,"+self.icon +\
62 ","+self.openIcon+");"+"\n";
63 buf += "\t"+treeRootName+".setBehavior('" + self.behavior + "');" + "\n";
64 for treeItem in self.subTreeItems:
65 tmp = treeItem.buildJsTreeStr(treeRootName,2)
66 buf += tmp
67 # buf += treeItem.buildJsTreeStr(treeRootName,2)
68 buf += "\tdocument.write(" + treeRootName + ");" + "\n"+"</script>\n"
69 return buf
70
71 @staticmethod
72 def indexGenerator():
73 i = 0
74 while True:
75 i = i+1
76 yield i
77 @staticmethod
78 def getJsVariableIndex():
79 if not JsTree.idxGenerator:
80 JsTree.idxGenerator = JsTree.indexGenerator()
81 return JsTree.idxGenerator.next()
82
83 class TreeItem(TreeObject):
84 def __init__(self,name="",action="",xmlSrc=""):
85 if name == "":
86 name = "TreeItem"
87 super(TreeItem, self).__init__(name,action,xmlSrc)
88
89 def buildJsTreeStr(self,parent,indent):
90 prefix = ""
91 buf = ""
92 try:
93 indent = int(indent)
94 except:
95 indent = 0
96 for i in range(indent):
97 prefix = prefix+"\t"
98
99 item = "tree" + str(JsTree.getJsVariableIndex())
100 buf += prefix + "var " + item
101 if not self.xmlSrc:
102 buf += " = new WebFXTreeItem('"
103 else:
104 buf += " = new WebFXLoadTreeItem('"
105 buf += self.name + "',"
106
107 if self.xmlSrc:
108 buf += "\"" + self.xmlSrc + "\","
109 buf += self.action
110 buf += ",null,"
111 buf += self.icon+","+self.openIcon+");\n"
112 for treeItem in self.subTreeItems:
113 buf += treeItem.buildJsTreeStr(item,indent+1)
114 buf += prefix
115 buf += parent
116 buf += ".add("+item+");\n"
117 return buf
118
119 def itemToXml(self,indent=0):
120 prefix = ""
121 buf=""
122 try:
123 indent = int(indent)
124 except:
125 indent = 0
126 for i in range(indent):
127 prefix = prefix+"\t"
128 buf += prefix+ "<tree text=\""+self.name+"\" "
129 buf += " spanLevel=\""+str(self.spanLevel)+"\" "
130 if self.xmlSrc:
131 # TODO:ESCAPE XML
132 buf += " src=\""+self.xmlSrc+"\" "
133 if self.action:
134 buf += " action=\""+ self.action+"\" "
135 if self.icon:
136 buf += " icon=\""+ self.icon+"\" "
137 if self.openIcon:
138 buf += " openIcon=\""+ self.openIcon+"\" "
139
140 if self.subTreeItems:
141 buf += "/>\n"
142 for treeItem in self.subTreeItems:
143 tmp = treeItem.itemToXml(indent+1)
144 buf += tmp
145 buf += prefix+"</tree>\n"
146 else:
147 buf += "/>\n"
148 return buf
149
150
151 class InputTreeItem(TreeItem):
152 def __init__(self,name="",action="",xmlSrc=""):
153 super(InputTreeItem, self).__init__(name,action,xmlSrc)
154 self.fieldName = ""
155 self.fieldValue = ""
156 self.checked = False
157 self.inputType = "checkbox"
158
159 self.js_cls = "WebFXCheckBoxTreeItem"
160 self.js_xload_cls = "WebFXCheckBoxLoadTreeItem"
161 def buildJsTreeStr(self,parent,indent):
162 if not self.fieldName:
163 raise ValueError("fieldName can't be null")
164 if not self.fieldValue:
165 raise ValueError("fieldValue can't be null")
166
167 try:
168 indent = int(indent)
169 except:
170 indent = 0
171
172 buf = ""
173 prefix = ""
174 for i in range(int(indent)):
175 prefix += "\t"
176 treeItem = "tree" + str(JsTree.getJsVariableIndex())
177 buf += prefix + "var " + treeItem
178 if self.xmlSrc:
179 buf += " = new " +self.js_xload_cls +"('"
180 else:
181 buf += " = new "+ self.js_cls+" ('"
182 buf += self.name + "',"
183 if self.xmlSrc:
184 buf += "\""+self.xmlSrc+"\","
185 buf += "' " + self.fieldName + "','"+self.fieldValue
186 if self.checked:
187 booleanStr = "true"
188 else:
189 booleanStr = "false"
190 buf += "' , "+self.action+","+booleanStr+",null,"
191 buf += self.icon + "," + self.openIcon+");\n"
192 for curItem in self.subTreeItems:
193 tmp = curItem.buildJsTreeStr(treeItem,indent+1)
194 buf += tmp
195 buf += prefix + parent+".add("+treeItem+");\n"
196 return buf
197
198 def itemToXml(self,indent):
199 prefix = ""
200 buf=""
201 try:
202 indent = int(indent)
203 except:
204 indent = 0
205 for i in range(indent):
206 prefix = prefix+"\t"
207 buf += prefix+ "<tree text=\""+self.name+"\" "
208 buf += " inputType=\""+self.inputType+"\" "
209
210 buf += " spanLevel=\""+str(self.spanLevel)+"\" "
211 if self.xmlSrc:
212 buf += " src=\""+self.xmlSrc+"\" "
213
214 if self.fieldName:
215 buf += " fieldName=\""+self.fieldName+"\" "
216 if self.fieldValue:
217 buf += " fieldValue=\""+self.fieldValue+"\" "
218 if self.checked:
219 booleanStr = "true"
220 else:
221 booleanStr = "false"
222 buf += " checked=\""+booleanStr+"\" "
223
224 if self.action:
225 buf += " action=\""+ self.action+"\" "
226 if self.icon:
227 buf += " icon=\""+ self.icon+"\" "
228 if self.openIcon:
229 buf += " openIcon=\""+ self.openIcon+"\" "
230
231 if self.subTreeItems:
232 buf += "/>\n"
233 for treeItem in self.subTreeItems:
234 tmp = treeItem.itemToXml(indent+1)
235 buf += tmp
236 buf += prefix+"</tree>\n"
237 else:
238 buf += "/>\n"
239 return buf
240
241 class CheckboxTreeItem(InputTreeItem):
242 def __init__(self,name="",action="",xmlSrc=""):
243 super(CheckboxTreeItem, self).__init__(name,action,xmlSrc)
244 self.inputType = "checkbox"
245 self.js_cls = "WebFXCheckBoxTreeItem"
246 self.js_xload_cls = "WebFXCheckBoxLoadTreeItem"
247
248 class RadioTreeItem(InputTreeItem):
249 def __init__(self,name="",action="",xmlSrc=""):
250 super(RadioTreeItem, self).__init__(name,action,xmlSrc)
251 self.inputType = "radio"
252 self.js_cls = "WebFXRadioTreeItem"
253 self.js_xload_cls = "WebFXRadioLoadTreeItem"
254
255
256 if __name__ == "__main__":
257 tree = JsTree()
258 tree1 = TreeItem('tree1')
259 tree2 = TreeItem('tree2')
260 tree3 = TreeItem('tree3')
261 tree2.addSubTreeItem(tree3)
262 tree1.addSubTreeItem(tree2)
263 tree.addSubTreeItem(tree1)
264
265
266 input1 = RadioTreeItem('input1')
267 input1.fieldName = "input1"
268 input1.fieldValue = "input1"
269
270
271 input2 = RadioTreeItem('input2')
272 input2.fieldName = "input2"
273 input2.fieldValue = "input2"
274 input2.xmlSrc = "dddd"
275
276
277 input3 = RadioTreeItem('input3')
278 input3.fieldName = "input3"
279 input3.fieldValue = "input3"
280
281 input2.addSubTreeItem(input3)
282 input1.addSubTreeItem(input2)
283 tree.addSubTreeItem(input1)
284 tree1.addSubTreeItem(input1)
285 print tree.getJsTreeStr()
286 print tree1.itemToXml()
1.1.1.1. 次节标题1
[javascript:void(0);/*1222606527995*/ 目标效果]
1.2. 章标题2
1.2.1. 小节标题2
其它 代码引用
1.2.1.1. 次节标题2
2. 反馈
PageComment2 [:hf linux/2008-09-28/PageCommentData:PageCommentData]