1 | class AssetTreeService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def js = new JsUtilService() |
---|
6 | |
---|
7 | def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib() |
---|
8 | |
---|
9 | /** Html class and id settings */ |
---|
10 | |
---|
11 | def buttonHtmlClass() { |
---|
12 | 'tree_button' |
---|
13 | } |
---|
14 | def paneHtmlClass() { |
---|
15 | 'overlayPane' |
---|
16 | } |
---|
17 | def paneHtmlId() { |
---|
18 | 'assetTreePane' |
---|
19 | } |
---|
20 | def paneCloseHtmlClass() { |
---|
21 | 'tree_pane_close' |
---|
22 | } |
---|
23 | def tableDivHtmlClass() { |
---|
24 | 'tree' |
---|
25 | } |
---|
26 | def tableHtmlId() { |
---|
27 | 'assetTreeTable' |
---|
28 | } |
---|
29 | def tableLoadingImgId() { |
---|
30 | 'assetTreeLoadingImg' |
---|
31 | } |
---|
32 | |
---|
33 | /** Name and Description Formatting */ |
---|
34 | |
---|
35 | def name(obj) { |
---|
36 | obj.name.encodeAsHTML() |
---|
37 | } |
---|
38 | |
---|
39 | def description(obj) { |
---|
40 | def s = obj.description.encodeAsHTML() |
---|
41 | if(s) s = ' -- ' + s |
---|
42 | else '' |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Build and return the asset tree table. |
---|
47 | * To be used in conjunction with AssetTreeTagLib which inserts the wrapper div. |
---|
48 | * This table replaces the contents of the wrapper div. |
---|
49 | * @returns The asset tree table as a String |
---|
50 | */ |
---|
51 | def buildAssetTree(params, session) { |
---|
52 | |
---|
53 | def sites = Site.list() |
---|
54 | |
---|
55 | def visibleBranches = session.assetTreeVisibleBranches ? session.assetTreeVisibleBranches.tokenize(',') : [] |
---|
56 | |
---|
57 | def branchStyle = { branchId -> |
---|
58 | if(visibleBranches.contains(branchId)) |
---|
59 | '' |
---|
60 | else |
---|
61 | 'display:none;' |
---|
62 | } |
---|
63 | |
---|
64 | def branchImg = { branchId -> |
---|
65 | if(visibleBranches.contains(branchId)) |
---|
66 | bulletTreeMinusImg() |
---|
67 | else |
---|
68 | bulletTreePlusImg() |
---|
69 | } |
---|
70 | |
---|
71 | def divIdCount = 0 |
---|
72 | def divId = '' |
---|
73 | def nextDivId = { |
---|
74 | divIdCount++ |
---|
75 | divId = 'assetTreeBranch'+divIdCount |
---|
76 | } |
---|
77 | |
---|
78 | def sw = new StringWriter() |
---|
79 | def mkp = new groovy.xml.MarkupBuilder(sw) //this line will be unnecessary in versions of Grails after version 1.2 |
---|
80 | |
---|
81 | // Offer a site create link if no sites are found. |
---|
82 | if(!sites) { |
---|
83 | mkp.div(class: tableDivHtmlClass()) { |
---|
84 | |
---|
85 | div(class: paneCloseHtmlClass()) { |
---|
86 | a( href: js.toggle(paneHtmlId()) ) { |
---|
87 | img(src: closeImg()) |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | table(id: tableHtmlId()) { |
---|
92 | tr() { |
---|
93 | td( valign: 'top', class: 'value') { |
---|
94 | ul() { |
---|
95 | img(src: treeRootImg(), alt: 'TreeRoot') |
---|
96 | li() { |
---|
97 | a(href: siteCreateLink()) { |
---|
98 | img(src: addImg(), alt: 'Add', title: 'Add Site') |
---|
99 | } |
---|
100 | } // li |
---|
101 | } // ul |
---|
102 | } // td |
---|
103 | } // tr |
---|
104 | } // table |
---|
105 | |
---|
106 | div( class: 'buttons') { |
---|
107 | span(class: 'button') { |
---|
108 | input( type: 'button', value: g.message(code: 'default.close.text'), onclick: js.toggle(paneHtmlId(), "onclick") ) |
---|
109 | } |
---|
110 | } // button div |
---|
111 | } // mkp |
---|
112 | return sw.toString() |
---|
113 | } // if(!sites) |
---|
114 | |
---|
115 | |
---|
116 | // The main populated table. |
---|
117 | /// @todo: use a loop for the subItem levels. |
---|
118 | mkp.div(class: tableDivHtmlClass()) { |
---|
119 | |
---|
120 | div(class: paneCloseHtmlClass()) { |
---|
121 | a( href: hrefHideAndSavePane() ) { |
---|
122 | img(src: closeImg()) |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | table(id: tableHtmlId()) { |
---|
127 | tr() { |
---|
128 | td(valign: 'top', class: 'value') { |
---|
129 | ul() { |
---|
130 | img(src: treeRootImg(), alt: 'TreeRoot') |
---|
131 | for(site in sites.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
---|
132 | li() { |
---|
133 | if(site.sections) { |
---|
134 | a(href: toggleBranch(nextDivId()) ) { |
---|
135 | img( src: branchImg(divId), id: divId+'img' ) |
---|
136 | } |
---|
137 | } |
---|
138 | else |
---|
139 | img(src: dashImg()) |
---|
140 | a( href: siteShowLink(site.id), onclick: onclickHideAndSavePane() ) { |
---|
141 | yieldUnescaped( name(site) ) |
---|
142 | } |
---|
143 | yieldUnescaped( description(site) ) |
---|
144 | a(href: sectionCreateLink(site.id), onclick: onclickHideAndSavePane()) { |
---|
145 | img(src: addImg(), alt: 'Add', title: 'Add Section') |
---|
146 | } |
---|
147 | } |
---|
148 | if(site.sections) { |
---|
149 | div( id: divId, style: branchStyle(divId) ) { |
---|
150 | ul() { |
---|
151 | for(section in site.sections.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
---|
152 | li() { |
---|
153 | if(section.assets) { |
---|
154 | a( href: toggleBranch(nextDivId()) ) { |
---|
155 | img(src: branchImg(divId), id: divId+'img' ) |
---|
156 | } |
---|
157 | } |
---|
158 | else |
---|
159 | img(src: dashImg()) |
---|
160 | a( href: sectionShowLink(section.id), onclick: onclickHideAndSavePane() ) { |
---|
161 | yieldUnescaped( name(section) ) |
---|
162 | } |
---|
163 | yieldUnescaped( description(section) ) |
---|
164 | a(href: assetCreateLink(section.id), onclick: onclickHideAndSavePane()) { |
---|
165 | img(src: addImg(), alt: 'Add', title: 'Add Asset') |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | if(section.assets) { |
---|
170 | div( id: divId, style: branchStyle(divId) ) { |
---|
171 | ul() { |
---|
172 | for(asset in section.assets.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
---|
173 | li() { |
---|
174 | if(asset.assetSubItems) { |
---|
175 | a( href: toggleBranch(nextDivId()) ) { |
---|
176 | img(src: branchImg(divId), id: divId+'img' ) |
---|
177 | } |
---|
178 | } |
---|
179 | else |
---|
180 | img(src: dashImg()) |
---|
181 | a( href: assetShowLink(asset.id), onclick: onclickHideAndSavePane() ) { |
---|
182 | yieldUnescaped( name(asset) ) |
---|
183 | } |
---|
184 | yieldUnescaped( description(asset) ) |
---|
185 | a(href: assetSubItemCreateLink(asset.id), onclick: onclickHideAndSavePane()) { |
---|
186 | img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
---|
187 | } |
---|
188 | a(href: assetCopyLink(asset.id), onclick: onclickHideAndSavePane()) { |
---|
189 | img(src: copyImg(), alt: 'Add', title: 'Copy Asset') |
---|
190 | } |
---|
191 | } // li |
---|
192 | |
---|
193 | if(asset.assetSubItems) { |
---|
194 | div( id: divId, style: branchStyle(divId) ) { |
---|
195 | ul() { |
---|
196 | for(assetSubItemL1 in asset.assetSubItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
---|
197 | li() { |
---|
198 | if(assetSubItemL1.subItems) { |
---|
199 | a( href: toggleBranch(nextDivId()) ) { |
---|
200 | img(src: branchImg(divId), id: divId+'img' ) |
---|
201 | } |
---|
202 | } |
---|
203 | else |
---|
204 | img(src: dashImg()) |
---|
205 | a( href: assetSubItemShowLink(assetSubItemL1.id), onclick: onclickHideAndSavePane() ) { |
---|
206 | yieldUnescaped( name(assetSubItemL1) ) |
---|
207 | } |
---|
208 | yieldUnescaped( description(assetSubItemL1) ) |
---|
209 | a(href: assetSubItemCreateWithParentLink(assetSubItemL1.id), onclick: onclickHideAndSavePane()) { |
---|
210 | img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
---|
211 | } |
---|
212 | } // li |
---|
213 | |
---|
214 | if(assetSubItemL1.subItems) { |
---|
215 | div( id: divId, style: branchStyle(divId) ) { |
---|
216 | ul() { |
---|
217 | for(assetSubItemL2 in assetSubItemL1.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
---|
218 | li() { |
---|
219 | if(assetSubItemL2.subItems) { |
---|
220 | a( href: toggleBranch(nextDivId()) ) { |
---|
221 | img( src: branchImg(divId), id: divId+'img' ) |
---|
222 | } |
---|
223 | } |
---|
224 | else |
---|
225 | img(src: dashImg()) |
---|
226 | a( href: assetSubItemShowLink(assetSubItemL2.id), onclick: onclickHideAndSavePane() ) { |
---|
227 | yieldUnescaped( name(assetSubItemL2) ) |
---|
228 | } |
---|
229 | yieldUnescaped( description(assetSubItemL2) ) |
---|
230 | a(href: assetSubItemCreateWithParentLink(assetSubItemL2.id), onclick: onclickHideAndSavePane()) { |
---|
231 | img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
---|
232 | } |
---|
233 | } // li |
---|
234 | |
---|
235 | if(assetSubItemL2.subItems) { |
---|
236 | div( id: divId, style: branchStyle(divId) ) { |
---|
237 | ul() { |
---|
238 | for(assetSubItemL3 in assetSubItemL2.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
---|
239 | li() { |
---|
240 | if(assetSubItemL3.subItems) { |
---|
241 | a( href: toggleBranch(nextDivId()) ) { |
---|
242 | img( src: branchImg(divId), id: divId+'img' ) |
---|
243 | } |
---|
244 | } |
---|
245 | else |
---|
246 | img(src: dashImg()) |
---|
247 | a( href: assetSubItemShowLink(assetSubItemL3.id), onclick: onclickHideAndSavePane() ) { |
---|
248 | yieldUnescaped( name(assetSubItemL3) ) |
---|
249 | } |
---|
250 | yieldUnescaped( description(assetSubItemL3) ) |
---|
251 | a(href: assetSubItemCreateWithParentLink(assetSubItemL3.id), onclick: onclickHideAndSavePane()) { |
---|
252 | img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
---|
253 | } |
---|
254 | } // li |
---|
255 | |
---|
256 | if(assetSubItemL3.subItems) { |
---|
257 | div( id: divId, style: branchStyle(divId) ) { |
---|
258 | ul() { |
---|
259 | for(assetSubItemL4 in assetSubItemL3.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
---|
260 | li() { |
---|
261 | // if(assetSubItemL4.subItems) { |
---|
262 | // a( href: toggleBranch(nextDivId()) ) { |
---|
263 | // img( src: branchImg(divId), id: divId+'img' ) |
---|
264 | // } |
---|
265 | // } |
---|
266 | // else |
---|
267 | img(src: dashImg()) |
---|
268 | a( href: assetSubItemShowLink(assetSubItemL4.id), onclick: onclickHideAndSavePane() ) { |
---|
269 | yieldUnescaped( name(assetSubItemL4) ) |
---|
270 | } |
---|
271 | yieldUnescaped( description(assetSubItemL4) ) |
---|
272 | // a(href: assetSubItemCreateWithParentLink(assetSubItemL4.id), onclick: onclickHideAndSavePane()) { |
---|
273 | // img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
---|
274 | // } |
---|
275 | } // li |
---|
276 | |
---|
277 | } // assetSubItemL4 |
---|
278 | } // ul |
---|
279 | } // div |
---|
280 | } // if(assetSubItemL3.subItems) |
---|
281 | |
---|
282 | |
---|
283 | } // assetSubItemL3 |
---|
284 | } // ul |
---|
285 | } // div |
---|
286 | } // if(assetSubItemL2.subItems) |
---|
287 | |
---|
288 | } // assetSubItemL2 |
---|
289 | } // ul |
---|
290 | } // div |
---|
291 | } // if(assetSubItemL1.subItems) |
---|
292 | |
---|
293 | } // assetSubItemL1 |
---|
294 | } // ul |
---|
295 | } // div |
---|
296 | } // if(asset.assetSubItems) |
---|
297 | |
---|
298 | } // assets |
---|
299 | } // ul |
---|
300 | } // div |
---|
301 | } // if(section.assets) |
---|
302 | |
---|
303 | } //sections |
---|
304 | } // ul |
---|
305 | } // div |
---|
306 | } // if(site.sections) |
---|
307 | } // sites |
---|
308 | } // ul |
---|
309 | } // td |
---|
310 | } // tr |
---|
311 | } // table |
---|
312 | |
---|
313 | div( class: 'buttons') { |
---|
314 | span(class: 'button') { |
---|
315 | input( type: 'button', value: g.message(code: 'default.close.text'), onclick: onclickHideAndSavePane() ) |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | } // mkp |
---|
320 | |
---|
321 | return sw.toString() |
---|
322 | |
---|
323 | } // buildAssetTree |
---|
324 | |
---|
325 | |
---|
326 | /** Imgs */ |
---|
327 | |
---|
328 | def treeRootImg() { |
---|
329 | g.resource(dir:'images/skin',file:'chart_organisation.png') |
---|
330 | } |
---|
331 | def addImg() { |
---|
332 | g.resource(dir:'images/skin',file:'database_add.png') |
---|
333 | } |
---|
334 | def copyImg() { |
---|
335 | g.resource(dir:'images/skin',file:'page_copy.png') |
---|
336 | } |
---|
337 | def bulletTreePlusImg() { |
---|
338 | g.resource(dir:'images/skin',file:'bullet_tree_plus.png') |
---|
339 | } |
---|
340 | def bulletTreeMinusImg() { |
---|
341 | g.resource(dir:'images/skin',file:'bullet_tree_minus.png') |
---|
342 | } |
---|
343 | def dashImg() { |
---|
344 | g.resource(dir:'images/skin',file:'hline_short.png') |
---|
345 | } |
---|
346 | def closeImg() { |
---|
347 | g.resource(dir:'images/skin',file:'cross.png') |
---|
348 | } |
---|
349 | |
---|
350 | /** js calls */ |
---|
351 | |
---|
352 | def hrefShowPane() { |
---|
353 | def url = g.createLink(controller: 'assetDetailed', action: 'assetTree') |
---|
354 | 'javascript: showAssetTreePane(\"assetTreePane\", \"assetTreeLoadingImg' +'\", \"' + url + '\");' |
---|
355 | } |
---|
356 | |
---|
357 | def onclickHideAndSavePane() { |
---|
358 | def saveUrl = g.createLink(controller: 'assetDetailed', action: 'saveAssetTreeStatus') |
---|
359 | 'return hideAssetTreePane(\"assetTreePane\", \"assetTreeTable' + '\", \"' + saveUrl + '\");' |
---|
360 | } |
---|
361 | |
---|
362 | def hrefHideAndSavePane() { |
---|
363 | def saveUrl = g.createLink(controller: 'assetDetailed', action: 'saveAssetTreeStatus') |
---|
364 | 'javascript: hideAssetTreePane(\"assetTreePane\", \"assetTreeTable' + '\", \"' + saveUrl + '\");' |
---|
365 | } |
---|
366 | |
---|
367 | def toggleBranch(divId) { |
---|
368 | js.toggleWithImg(divId, divId + 'img', bulletTreeMinusImg(), bulletTreePlusImg()) |
---|
369 | } |
---|
370 | |
---|
371 | /** Links */ |
---|
372 | |
---|
373 | def siteCreateLink() { |
---|
374 | g.createLink(controller: 'siteDetailed', action: 'create').toString() |
---|
375 | } |
---|
376 | def siteShowLink(id) { |
---|
377 | g.createLink(controller: 'siteDetailed', action: 'show', params: ['id': id] ).toString() |
---|
378 | } |
---|
379 | |
---|
380 | def siteEditLink(id) { |
---|
381 | g.createLink(controller: 'siteDetailed', action: 'edit', params: ['id': id] ).toString() |
---|
382 | } |
---|
383 | |
---|
384 | def sectionCreateLink(siteId) { |
---|
385 | g.createLink(controller: 'sectionDetailed', action: 'create', params: ['site.id': siteId] ).toString() |
---|
386 | } |
---|
387 | |
---|
388 | def sectionShowLink(id) { |
---|
389 | g.createLink(controller: 'sectionDetailed', action: 'show', params: ['id': id] ).toString() |
---|
390 | } |
---|
391 | |
---|
392 | def sectionEditLink(id) { |
---|
393 | g.createLink(controller: 'sectionDetailed', action: 'edit', params: ['id': id] ).toString() |
---|
394 | } |
---|
395 | |
---|
396 | def assetCreateLink(sectionId) { |
---|
397 | g.createLink(controller: 'assetDetailed', action: 'create', params: ['section.id': sectionId] ).toString() |
---|
398 | } |
---|
399 | |
---|
400 | def assetShowLink(id) { |
---|
401 | g.createLink(controller: 'assetDetailed', action: 'show', id: id ).toString() |
---|
402 | } |
---|
403 | |
---|
404 | def assetEditLink(id) { |
---|
405 | g.createLink(controller: 'assetDetailed', action: 'edit', id: id ).toString() |
---|
406 | } |
---|
407 | |
---|
408 | def assetCopyLink(id) { |
---|
409 | g.createLink(controller: 'assetDetailed', action: 'copy', params: ['assetToCopy.id': id] ).toString() |
---|
410 | } |
---|
411 | |
---|
412 | def assetSubItemCreateLink(assetId) { |
---|
413 | g.createLink(controller: 'assetSubItemDetailed', action: 'create', params: ['asset.id': assetId] ).toString() |
---|
414 | } |
---|
415 | |
---|
416 | def assetSubItemCreateWithParentLink(parentItemId) { |
---|
417 | g.createLink(controller: 'assetSubItemDetailed', action: 'create', params: ['parentItem.id': parentItemId] ).toString() |
---|
418 | } |
---|
419 | |
---|
420 | def assetSubItemShowLink(id) { |
---|
421 | g.createLink(controller: 'assetSubItemDetailed', action: 'show', params: ['id': id] ).toString() |
---|
422 | } |
---|
423 | |
---|
424 | def assetSubItemEditLink(id) { |
---|
425 | g.createLink(controller: 'assetSubItemDetailed', action: 'edit', params: ['id': id] ).toString() |
---|
426 | } |
---|
427 | |
---|
428 | } // end class |
---|