| 1 | /** |
|---|
| 2 | * Asset Tree tags. |
|---|
| 3 | * Specific to gnuMims hence the namespace. |
|---|
| 4 | */ |
|---|
| 5 | class AssetTreeTagLib { |
|---|
| 6 | static namespace = 'gnuMims' |
|---|
| 7 | |
|---|
| 8 | def resources = { attrs -> |
|---|
| 9 | ///@todo: should include our javascript and do setup here. |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| 12 | def assetTreeButton = { |
|---|
| 13 | def mkp = new groovy.xml.MarkupBuilder(out) //this line will be unnecessary in versions of Grails after version 1.2 |
|---|
| 14 | |
|---|
| 15 | mkp.div(class: "tree_button") { |
|---|
| 16 | a(href: showPane()) { |
|---|
| 17 | img(src: treeRootImg()) |
|---|
| 18 | } |
|---|
| 19 | } |
|---|
| 20 | } // mkp |
|---|
| 21 | |
|---|
| 22 | def assetTree = { attrs -> |
|---|
| 23 | |
|---|
| 24 | def sites = Site.list() |
|---|
| 25 | |
|---|
| 26 | def visibleBranches = session.assetTreeVisibleBranches ? session.assetTreeVisibleBranches.tokenize(',') : [] |
|---|
| 27 | |
|---|
| 28 | def branchStyle = { branchId -> |
|---|
| 29 | if(visibleBranches.contains(branchId)) |
|---|
| 30 | '' |
|---|
| 31 | else |
|---|
| 32 | 'display:none;' |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | def branchImg = { branchId -> |
|---|
| 36 | if(visibleBranches.contains(branchId)) |
|---|
| 37 | bulletTreeMinusImg() |
|---|
| 38 | else |
|---|
| 39 | bulletTreePlusImg() |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | def divIdCount = 0 |
|---|
| 43 | def divId = '' |
|---|
| 44 | def nextDivId = { |
|---|
| 45 | divIdCount++ |
|---|
| 46 | divId = 'assetTreeBranch'+divIdCount |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | def mkp = new groovy.xml.MarkupBuilder(out) //this line will be unnecessary in versions of Grails after version 1.2 |
|---|
| 50 | |
|---|
| 51 | // Offer a site create link if no sites are found. |
|---|
| 52 | if(!sites) { |
|---|
| 53 | mkp.div(class: 'overlayPane', id: 'assetTreePane', style: 'display:none;') { |
|---|
| 54 | div(class: 'tree', id: 'assetTreeTable') { |
|---|
| 55 | table() { |
|---|
| 56 | tr() { |
|---|
| 57 | td( valign: 'top', class: 'value') { |
|---|
| 58 | ul() { |
|---|
| 59 | img(src: treeRootImg(), alt: 'TreeRoot') |
|---|
| 60 | li() { |
|---|
| 61 | a(href: siteCreateLink()) { |
|---|
| 62 | img(src: addImg(), alt: 'Add', title: 'Add Site') |
|---|
| 63 | } |
|---|
| 64 | } // li |
|---|
| 65 | } // ul |
|---|
| 66 | } // td |
|---|
| 67 | } // tr |
|---|
| 68 | } // table |
|---|
| 69 | } // div |
|---|
| 70 | |
|---|
| 71 | div( class: 'buttons') { |
|---|
| 72 | span(class: 'button') { |
|---|
| 73 | input( type: 'button', value: 'Close', onclick: hideAssetTreePane() ) |
|---|
| 74 | } |
|---|
| 75 | } // button div |
|---|
| 76 | } // mkp |
|---|
| 77 | return |
|---|
| 78 | } // if(!sites) |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /// @todo: use a loop for the subItem levels. |
|---|
| 82 | mkp.div(class: 'overlayPane', id: 'assetTreePane', style: 'display:none;') { |
|---|
| 83 | div(class: 'tree', id: 'assetTreeTable') { |
|---|
| 84 | table() { |
|---|
| 85 | tr() { |
|---|
| 86 | td( valign: 'top', class: 'value') { |
|---|
| 87 | ul() { |
|---|
| 88 | img(src: treeRootImg(), alt: 'TreeRoot') |
|---|
| 89 | for(site in sites.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
|---|
| 90 | li() { |
|---|
| 91 | if(site.sections) { |
|---|
| 92 | a(href: toggleBranch(nextDivId()) ) { |
|---|
| 93 | img( src: branchImg(divId), id: divId+'img' ) |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | else |
|---|
| 97 | img(src: dashImg()) |
|---|
| 98 | a( href: siteShowLink(site.id), onclick: hideAssetTreePane() ) { |
|---|
| 99 | yieldUnescaped( site.encodeAsHTML() ) |
|---|
| 100 | } |
|---|
| 101 | a(href: sectionCreateLink(site.id), onclick: hideAssetTreePane()) { |
|---|
| 102 | img(src: addImg(), alt: 'Add', title: 'Add Section') |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | if(site.sections) { |
|---|
| 106 | div( id: divId, style: branchStyle(divId) ) { |
|---|
| 107 | ul() { |
|---|
| 108 | for(section in site.sections.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
|---|
| 109 | li() { |
|---|
| 110 | if(section.assets) { |
|---|
| 111 | a( href: toggleBranch(nextDivId()) ) { |
|---|
| 112 | img(src: branchImg(divId), id: divId+'img' ) |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | else |
|---|
| 116 | img(src: dashImg()) |
|---|
| 117 | a( href: sectionShowLink(section.id), onclick: hideAssetTreePane() ) { |
|---|
| 118 | yieldUnescaped( section.encodeAsHTML() ) |
|---|
| 119 | } |
|---|
| 120 | a(href: assetCreateLink(section.id), onclick: hideAssetTreePane()) { |
|---|
| 121 | img(src: addImg(), alt: 'Add', title: 'Add Asset') |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if(section.assets) { |
|---|
| 126 | div( id: divId, style: branchStyle(divId) ) { |
|---|
| 127 | ul() { |
|---|
| 128 | for(asset in section.assets.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
|---|
| 129 | li() { |
|---|
| 130 | if(asset.assetSubItems) { |
|---|
| 131 | a( href: toggleBranch(nextDivId()) ) { |
|---|
| 132 | img(src: branchImg(divId), id: divId+'img' ) |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | else |
|---|
| 136 | img(src: dashImg()) |
|---|
| 137 | a( href: assetShowLink(asset.id), onclick: hideAssetTreePane() ) { |
|---|
| 138 | yieldUnescaped( asset.encodeAsHTML() ) |
|---|
| 139 | } |
|---|
| 140 | a(href: assetSubItemCreateLink(asset.id), onclick: hideAssetTreePane()) { |
|---|
| 141 | img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
|---|
| 142 | } |
|---|
| 143 | a(href: assetCopyLink(asset.id), onclick: hideAssetTreePane()) { |
|---|
| 144 | img(src: copyImg(), alt: 'Add', title: 'Copy Asset') |
|---|
| 145 | } |
|---|
| 146 | } // li |
|---|
| 147 | |
|---|
| 148 | if(asset.assetSubItems) { |
|---|
| 149 | div( id: divId, style: branchStyle(divId) ) { |
|---|
| 150 | ul() { |
|---|
| 151 | for(assetSubItemL1 in asset.assetSubItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
|---|
| 152 | li() { |
|---|
| 153 | if(assetSubItemL1.subItems) { |
|---|
| 154 | a( href: toggleBranch(nextDivId()) ) { |
|---|
| 155 | img(src: branchImg(divId), id: divId+'img' ) |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | else |
|---|
| 159 | img(src: dashImg()) |
|---|
| 160 | a( href: assetSubItemShowLink(assetSubItemL1.id), onclick: hideAssetTreePane() ) { |
|---|
| 161 | yieldUnescaped( assetSubItemL1.encodeAsHTML() ) |
|---|
| 162 | } |
|---|
| 163 | a(href: assetSubItemCreateWithParentLink(assetSubItemL1.id), onclick: hideAssetTreePane()) { |
|---|
| 164 | img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
|---|
| 165 | } |
|---|
| 166 | } // li |
|---|
| 167 | |
|---|
| 168 | if(assetSubItemL1.subItems) { |
|---|
| 169 | div( id: divId, style: branchStyle(divId) ) { |
|---|
| 170 | ul() { |
|---|
| 171 | for(assetSubItemL2 in assetSubItemL1.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
|---|
| 172 | li() { |
|---|
| 173 | if(assetSubItemL2.subItems) { |
|---|
| 174 | a( href: toggleBranch(nextDivId()) ) { |
|---|
| 175 | img( src: branchImg(divId), id: divId+'img' ) |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | else |
|---|
| 179 | img(src: dashImg()) |
|---|
| 180 | a( href: assetSubItemShowLink(assetSubItemL2.id), onclick: hideAssetTreePane() ) { |
|---|
| 181 | yieldUnescaped( assetSubItemL2.encodeAsHTML() ) |
|---|
| 182 | } |
|---|
| 183 | a(href: assetSubItemCreateWithParentLink(assetSubItemL2.id), onclick: hideAssetTreePane()) { |
|---|
| 184 | img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
|---|
| 185 | } |
|---|
| 186 | } // li |
|---|
| 187 | |
|---|
| 188 | if(assetSubItemL2.subItems) { |
|---|
| 189 | div( id: divId, style: branchStyle(divId) ) { |
|---|
| 190 | ul() { |
|---|
| 191 | for(assetSubItemL3 in assetSubItemL2.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
|---|
| 192 | li() { |
|---|
| 193 | if(assetSubItemL3.subItems) { |
|---|
| 194 | a( href: toggleBranch(nextDivId()) ) { |
|---|
| 195 | img( src: branchImg(divId), id: divId+'img' ) |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | else |
|---|
| 199 | img(src: dashImg()) |
|---|
| 200 | a( href: assetSubItemShowLink(assetSubItemL3.id), onclick: hideAssetTreePane() ) { |
|---|
| 201 | yieldUnescaped( assetSubItemL3.encodeAsHTML() ) |
|---|
| 202 | } |
|---|
| 203 | a(href: assetSubItemCreateWithParentLink(assetSubItemL3.id), onclick: hideAssetTreePane()) { |
|---|
| 204 | img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
|---|
| 205 | } |
|---|
| 206 | } // li |
|---|
| 207 | |
|---|
| 208 | if(assetSubItemL3.subItems) { |
|---|
| 209 | div( id: divId, style: branchStyle(divId) ) { |
|---|
| 210 | ul() { |
|---|
| 211 | for(assetSubItemL4 in assetSubItemL3.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { |
|---|
| 212 | li() { |
|---|
| 213 | // if(assetSubItemL4.subItems) { |
|---|
| 214 | // a( href: toggleBranch(nextDivId()) ) { |
|---|
| 215 | // img( src: branchImg(divId), id: divId+'img' ) |
|---|
| 216 | // } |
|---|
| 217 | // } |
|---|
| 218 | // else |
|---|
| 219 | img(src: dashImg()) |
|---|
| 220 | a( href: assetSubItemShowLink(assetSubItemL4.id), onclick: hideAssetTreePane() ) { |
|---|
| 221 | yieldUnescaped( assetSubItemL4.encodeAsHTML() ) |
|---|
| 222 | } |
|---|
| 223 | // a(href: assetSubItemCreateWithParentLink(assetSubItemL4.id), onclick: hideAssetTreePane()) { |
|---|
| 224 | // img(src: addImg(), alt: 'Add', title: 'Add Sub Item') |
|---|
| 225 | // } |
|---|
| 226 | } // li |
|---|
| 227 | |
|---|
| 228 | } // assetSubItemL4 |
|---|
| 229 | } // ul |
|---|
| 230 | } // div |
|---|
| 231 | } // if(assetSubItemL3.subItems) |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | } // assetSubItemL3 |
|---|
| 235 | } // ul |
|---|
| 236 | } // div |
|---|
| 237 | } // if(assetSubItemL2.subItems) |
|---|
| 238 | |
|---|
| 239 | } // assetSubItemL2 |
|---|
| 240 | } // ul |
|---|
| 241 | } // div |
|---|
| 242 | } // if(assetSubItemL1.subItems) |
|---|
| 243 | |
|---|
| 244 | } // assetSubItemL1 |
|---|
| 245 | } // ul |
|---|
| 246 | } // div |
|---|
| 247 | } // if(asset.assetSubItems) |
|---|
| 248 | |
|---|
| 249 | } // assets |
|---|
| 250 | } // ul |
|---|
| 251 | } // div |
|---|
| 252 | } // if(section.assets) |
|---|
| 253 | |
|---|
| 254 | } //sections |
|---|
| 255 | } // ul |
|---|
| 256 | } // div |
|---|
| 257 | } // if(site.sections) |
|---|
| 258 | } // sites |
|---|
| 259 | } // ul |
|---|
| 260 | } // td |
|---|
| 261 | } // tr |
|---|
| 262 | } // table |
|---|
| 263 | } // div |
|---|
| 264 | |
|---|
| 265 | div( class: 'buttons') { |
|---|
| 266 | span(class: 'button') { |
|---|
| 267 | input( type: 'button', value: 'Close', onclick: hideAssetTreePane() ) |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | } // mkp |
|---|
| 272 | |
|---|
| 273 | } // assetTree |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | /** Imgs */ |
|---|
| 277 | |
|---|
| 278 | def treeRootImg() { |
|---|
| 279 | resource(dir:'images/skin',file:'chart_organisation.png').toString() |
|---|
| 280 | } |
|---|
| 281 | def addImg() { |
|---|
| 282 | resource(dir:'images/skin',file:'database_add.png').toString() |
|---|
| 283 | } |
|---|
| 284 | def copyImg() { |
|---|
| 285 | resource(dir:'images/skin',file:'page_copy.png').toString() |
|---|
| 286 | } |
|---|
| 287 | def bulletTreePlusImg() { |
|---|
| 288 | resource(dir:'images/skin',file:'bullet_tree_plus.png').toString() |
|---|
| 289 | } |
|---|
| 290 | // actually set in javascript function. |
|---|
| 291 | def bulletTreeMinusImg() { |
|---|
| 292 | resource(dir:'images/skin',file:'bullet_tree_minus.png').toString() |
|---|
| 293 | } |
|---|
| 294 | def dashImg() { |
|---|
| 295 | resource(dir:'images/skin',file:'hline_short.png').toString() |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /** js calls */ |
|---|
| 299 | |
|---|
| 300 | def showPane() { |
|---|
| 301 | 'javascript: showDiv(\"assetTreePane\");' |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | def hideAssetTreePane() { |
|---|
| 305 | def saveUrl = createLink(controller: 'appCore', action: 'saveAssetTreeStatus').toString() |
|---|
| 306 | 'return hideAssetTreePane(\"assetTreePane\", \"assetTreeTable' + '\", \"' + saveUrl + '\");' |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | def toggleBranch(divId) { |
|---|
| 310 | /// @todo: toggleBranch is in overlayPane.js and should be moved to tree.js |
|---|
| 311 | 'javascript: toggleBranch(\"' + divId + '\", \"' + divId + 'img' +'\", \"' + bulletTreeMinusImg() +'\", \"' + bulletTreePlusImg() + '\");' |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | /** Links */ |
|---|
| 315 | |
|---|
| 316 | def siteCreateLink() { |
|---|
| 317 | createLink(controller: 'siteDetailed', action: 'create').toString() |
|---|
| 318 | } |
|---|
| 319 | def siteShowLink(id) { |
|---|
| 320 | createLink(controller: 'siteDetailed', action: 'show', params: ['id': id] ).toString() |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | def siteEditLink(id) { |
|---|
| 324 | createLink(controller: 'siteDetailed', action: 'edit', params: ['id': id] ).toString() |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | def sectionCreateLink(siteId) { |
|---|
| 328 | createLink(controller: 'sectionDetailed', action: 'create', params: ['site.id': siteId] ).toString() |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | def sectionShowLink(id) { |
|---|
| 332 | createLink(controller: 'sectionDetailed', action: 'show', params: ['id': id] ).toString() |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | def sectionEditLink(id) { |
|---|
| 336 | createLink(controller: 'sectionDetailed', action: 'edit', params: ['id': id] ).toString() |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | def assetCreateLink(sectionId) { |
|---|
| 340 | createLink(controller: 'assetDetailed', action: 'create', params: ['section.id': sectionId] ).toString() |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | def assetShowLink(id) { |
|---|
| 344 | createLink(controller: 'assetDetailed', action: 'show', id: id ).toString() |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | def assetEditLink(id) { |
|---|
| 348 | createLink(controller: 'assetDetailed', action: 'edit', id: id ).toString() |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | def assetCopyLink(id) { |
|---|
| 352 | createLink(controller: 'assetDetailed', action: 'copy', params: ['assetToCopy.id': id] ).toString() |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | def assetSubItemCreateLink(assetId) { |
|---|
| 356 | createLink(controller: 'assetSubItemDetailed', action: 'create', params: ['asset.id': assetId] ).toString() |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | def assetSubItemCreateWithParentLink(parentItemId) { |
|---|
| 360 | createLink(controller: 'assetSubItemDetailed', action: 'create', params: ['parentItem.id': parentItemId] ).toString() |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | def assetSubItemShowLink(id) { |
|---|
| 364 | createLink(controller: 'assetSubItemDetailed', action: 'show', params: ['id': id] ).toString() |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | def assetSubItemEditLink(id) { |
|---|
| 368 | createLink(controller: 'assetSubItemDetailed', action: 'edit', params: ['id': id] ).toString() |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | } // end class |
|---|