Project Docs Github
Log in
ittf site docs t _js wz htmlBuilder.js.ittf Edit
  • /ittf/site/docs/t/_js/wz/htmlBuilder.js.ittf

/ittf/site/docs/t/_js/wz/htmlBuilder.js.ittf (primary)

edit
                                            
1 $group
2 function wzNodeRemove
3 param item
4 if !item.parent
5 throw new Error('wzNodeRemove. Missing item.parent of:' + item.tag + ',' + item.id)
6 var nodes = item.parent.elements
7 set item.parent.elements = []
8 foreach child in nodes
9 if child !== item
10 _ item.parent.elements.push(child)
11 function wzNodeReplace
12 param item
13 param newNode
14 if !item.parent
15 throw new Error('wzNodeReplace. Missing item.parent of:' + item.tag + ',' + item.id)
16 var nodes = item.parent.elements
17 set item.parent.elements = []
18 foreach child in nodes
19 if child !== item
20 _ item.parent.elements.push(child)
21 else
22 _ item.parent.elements.push(newNode)
23 class wzHtmlNode
24 ctor
25 param tag
26 param attrs
27 param textContent
28 if typeof textContent !== 'undefined'
29 set this.textContent = textContent
30 else
31 set this.tag = tag
32 set this.id = tag
33 set this.parent = null
34 set this.attributes = {}
35 set this.elements = []
36 if attrs
37 for var k in attrs
38 if attrs.hasOwnProperty(k)
39 if k === 'id'
40 set this.id = attrs[k]
41 if k === '_text'
42 _ this.text(attrs[k])
43 else
44 set this.attributes[k] = attrs[k]
45 m add
46 param tag
47 param attrs
48 if tag.tag
49 set tag.parent = this
50 _ this.elements.push
51 @ tag
52 return tag
53 else
54 var node = new wzHtmlNode(tag, attrs)
55 set node.parent = this
56 _ this.elements.push
57 @ node
58 return node
59 m set
60 param name
61 param value
62 set this.attributes[name] = value
63 return this
64 m text
65 param content
66 var node = new wzHtmlNode(null, null, content)
67 _ this.elements.push
68 @ node
69 return this
70 m textReplace
71 param content
72 var done = false
73 foreach item in this.elements
74 if !done && typeof item.textContent !== 'undefined'
75 set item.textContent = content
76 set done = true
77 if !done
78 _ this.text(content)
79 return this
80 m get
81 param elementId
82 param attributeId
83 if this.id === elementId
84 if typeof attributeId !== 'undefined'
85 return this.attributes[attributeId]
86 else
87 return this
88 else
89 if this.elements
90 var found
91 foreach item in this.elements
92 set found = item.get(elementId, attributeId)
93 if found
94 return found
95 return null
96 m wrap
97 param tag
98 param attrs
99 var wrapper = new wzHtmlNode(tag, attrs)
100 _ wzNodeReplace(this, wrapper)
101 _ wrapper.add
102 @ this
103 return wrapper
104 m unwrap
105 # unwrap means : replace the parent with this
106 _ wzNodeReplace(this.parent, this)
107 m remove
108 # unwrap means : replace the parent with this
109 _ wzNodeRemove(this)
110 m toHtml
111 param indent
112 set indent = indent || 0
113 if typeof this.textContent !== 'undefined'
114 return this.textContent
115 else
116 var ret = [ new Array(indent).join(' ') ]
117 _ ret.push
118 @ '<' + this.tag
119 foreach name in Object.keys(this.attributes)
120 if i > 0
121 _ ret.push
122 @ '\n' + new Array(indent+2).join(' ')
123 var value = this.attributes[name]
124 if typeof value === 'undefined'
125 _ ret.push
126 @ ' ' + name
127 else
128 _ ret.push
129 @ ' ' + name + '="' + value + '"'
130 _ ret.push
131 @ '>'
132 foreach node in this.elements
133 _ ret.push
134 _ node.toHtml
135 @ indent + 1
136 _ ret.push
137 @ '</' + this.tag + '>'
138 _ ret.push('\n')
139 return ret.join('')
140 class wzHtmlBuilder
141 ctor
142 param options
143 set options = options || {}
144 set this.elementId = options.id
145 set this.elements = []
146 set this.current = null
147 m prettyResult
148 param elementId
149 set this.prettyResultId = elementId
150 m add
151 param tag
152 param attrs
153 var node
154 if this.current
155 set node = this.current.add(tag, attrs)
156 else
157 set node = new wzHtmlNode(tag, attrs)
158 set node.parent = this
159 _ this.elements.push
160 @ node
161 set this.current = node
162 _ this.update
163 return node
164 m addTop
165 param tag
166 param attrs
167 set this.current = null
168 return
169 _ this.add(tag, attrs)
170 m set
171 param attributeName
172 param attributeValue
173 _ this.current.set(attributeName, attributeValue)
174 _ this.update
175 return this
176 m text
177 param content
178 _ this.current.text
179 @ content
180 _ this.update
181 return this
182 m textReplace
183 param content
184 _ this.current.textReplace
185 @ content
186 _ this.update
187 return this
188 m toHtml
189 var ret = []
190 foreach item in this.elements
191 _ ret.push
192 _ item.toHtml
193 return ret.join('\n')
194 m select
195 param id
196 var found
197 foreach item in this.elements
198 set found
199 _ item.get
200 @ id
201 log 'select.item', id, item.id, found
202 if found
203 break
204 log 'select', id, found
205 if !found
206 return false
207 set this.current = found
208 return true
209 m wrap
210 param id
211 param wrapperTag
212 param wrapperId
213 var found = this.select(id)
214 if !found
215 throw new Error("wzHtmlBuilder can't wrap id=" + id + ". Not found.")
216 set this.current
217 _ this.current.wrap(wrapperTag, wrapperId)
218 _ this.update
219 m unwrap
220 param id
221 # unwrap means : replace the parent of id with id
222 var found = this.select(id)
223 if !found
224 throw new Error("wzHtmlBuilder can't unwrap id=" + id + ". Not found.")
225 set this.current
226 _ this.current.unwrap()
227 _ this.update
228 m remove
229 param id
230 var found = this.select(id)
231 if !found
232 return
233 _ this.current.remove
234 m update
235 if this.elementId
236 _ wz.html
237 _ wz.element('#' + this.elementId)
238 _ this.toHtml
239 if this.prettyResultId
240 _ wz.htmlEscaped
241 _ wz.element('#' + this.prettyResultId)
242 _ this.toHtml
Save
Save & Refresh
Cancel