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