Changeset 457
- Timestamp:
- Mar 26, 2010, 2:22:04 AM (15 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/AssetTreeService.groovy
r453 r457 7 7 def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib() 8 8 9 /** General */ 10 def nameDescriptionSeperator = ' -- ' 11 9 12 /** 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 'pane_close' 22 } 23 def tableDivHtmlClass() { 24 'tree' 25 } 26 def tableHtmlId() { 27 'assetTreeTable' 28 } 29 def tableLoadingImgId() { 30 'assetTreeLoadingImg' 13 def buttonHtmlClass = 'tree_button' 14 def paneHtmlClass = 'overlayPane' 15 def paneHtmlId = 'assetTreePane' 16 def paneCloseHtmlClass = 'pane_close' 17 def tableDivHtmlClass = 'tree' 18 def tableHtmlId = 'assetTreeTable' 19 def tableLoadingImgId = 'assetTreeLoadingImg' 20 21 /** Imgs */ 22 def treeRootImg = '' 23 def addImg = '' 24 def copyImg = '' 25 def bulletTreePlusImg = '' 26 def bulletTreeMinusImg = '' 27 def dashImg = '' 28 def closeImg = '' 29 30 /** Urls */ 31 def assetTreeActionUrl = '' 32 def saveAssetTreeStatusActionUrl = '' 33 34 /** Links */ 35 def siteCreateBaseLink = '' 36 def siteShowBaseLink = '' 37 def siteEditBaseLink = '' 38 def sectionCreateBaseLink = '' 39 def sectionShowBaseLink = '' 40 def sectionEditBaseLink = '' 41 def assetCreateBaseLink = '' 42 def assetShowBaseLink = '' 43 def assetEditBaseLink = '' 44 def assetCopyBaseLink = '' 45 def assetSubItemCreateBaseLink = '' 46 def assetSubItemCreateWithParentBaseLink = '' 47 def assetSubItemShowBaseLink = '' 48 def assetSubItemEditBaseLink = '' 49 50 /** 51 * Initialise some class wide variables. 52 * This has been done to optimise since g.link and g.resource calls are expensive. 53 * This can't be done by class construction since some of the metaclass stuff is not available yet, e.g. 'out' etc. 54 * Initialise can't be called from BootStrap. 55 */ 56 def initialise() { 57 log.debug "Initialise asset tree variables." 58 59 // Imgs. 60 treeRootImg = g.resource(dir:'images/skin',file:'chart_organisation.png') 61 addImg = g.resource(dir:'images/skin',file:'database_add.png') 62 copyImg = g.resource(dir:'images/skin',file:'page_copy.png') 63 bulletTreePlusImg = g.resource(dir:'images/skin',file:'bullet_tree_plus.png') 64 bulletTreeMinusImg = g.resource(dir:'images/skin',file:'bullet_tree_minus.png') 65 dashImg = g.resource(dir:'images/skin',file:'hline_short.png') 66 closeImg = g.resource(dir:'images/skin',file:'cross.png') 67 68 // Urls. 69 assetTreeActionUrl = g.createLink(controller: 'assetDetailed', action: 'assetTree') 70 saveAssetTreeStatusActionUrl = g.createLink(controller: 'assetDetailed', action: 'saveAssetTreeStatus') 71 72 // Links 73 siteCreateBaseLink = g.createLink(controller: 'siteDetailed', action: 'create').toString() 74 siteShowBaseLink = g.createLink(controller: 'siteDetailed', action: 'show').toString() 75 siteEditBaseLink = g.createLink(controller: 'siteDetailed', action: 'edit').toString() 76 sectionCreateBaseLink = g.createLink(controller: 'sectionDetailed', action: 'create').toString() 77 sectionShowBaseLink = g.createLink(controller: 'sectionDetailed', action: 'show').toString() 78 sectionEditBaseLink = g.createLink(controller: 'sectionDetailed', action: 'edit').toString() 79 assetCreateBaseLink = g.createLink(controller: 'assetDetailed', action: 'create').toString() 80 assetShowBaseLink = g.createLink(controller: 'assetDetailed', action: 'show').toString() 81 assetEditBaseLink = g.createLink(controller: 'assetDetailed', action: 'edit').toString() 82 assetCopyBaseLink = g.createLink(controller: 'assetDetailed', action: 'copy').toString() 83 assetSubItemCreateBaseLink = g.createLink(controller: 'assetSubItemDetailed', action: 'create').toString() 84 assetSubItemCreateWithParentBaseLink = g.createLink(controller: 'assetSubItemDetailed', action: 'create').toString() 85 assetSubItemShowBaseLink = g.createLink(controller: 'assetSubItemDetailed', action: 'show').toString() 86 assetSubItemEditBaseLink = g.createLink(controller: 'assetSubItemDetailed', action: 'edit').toString() 87 // Success. 88 return true 31 89 } 32 90 … … 39 97 def description(obj) { 40 98 def s = obj.description.encodeAsHTML() 41 if(s) s = ' -- ' + s 42 else '' 43 } 99 s = s? (nameDescriptionSeperator + s) : '' 100 } 101 102 /** 103 * Build and return the asset tree button. 104 * Built here instead of directly in the taglib since we may need to initialise. 105 */ 106 def buildAssetTreeButton(attrs) { 107 108 // Self initialisation ;-) 109 if(!treeRootImg) 110 initialise() 111 112 def sw = new StringWriter() 113 def mkp = new groovy.xml.MarkupBuilder(sw) 114 115 mkp.div(class: buttonHtmlClass) { 116 a( href: hrefShowPane() ) { 117 img(src: treeRootImg) 118 } 119 } // mkp 120 121 return sw.toString() 122 } 123 124 /** 125 * Build and return the empty asset tree pane, ready for populating by ajax call to buildAssetTree. 126 * Built here instead of directly in the taglib since we may need to initialise. 127 */ 128 def buildAssetTreePane(attrs) { 129 130 // Self initialisation ;-) 131 if(!treeRootImg) 132 initialise() 133 134 def sw = new StringWriter() 135 def mkp = new groovy.xml.MarkupBuilder(sw) 136 137 mkp.div(class: paneHtmlClass, id: paneHtmlId, style: 'display:none;') { 138 div(class: paneCloseHtmlClass) { 139 a( href: js.toggle(paneHtmlId) ) { 140 img(src: closeImg) 141 } 142 } 143 144 div(class: tableDivHtmlClass) { 145 table(id: tableHtmlId) { 146 tr() { 147 td(valign: 'top', class: 'value') { 148 ul() { 149 img(src: treeRootImg, id: tableLoadingImgId, alt: 'TreeRoot') 150 li() { 151 } // li 152 } // ul 153 } // td 154 } // tr 155 } // table 156 } // div 157 } // mkp 158 159 return sw.toString() 160 161 } // buildAssetPane 44 162 45 163 /** 46 164 * Build and return the asset tree table. 47 * To be used in conjunction with AssetTreeTagLib which inserts the wrapper div .165 * To be used in conjunction with AssetTreeTagLib which inserts the wrapper div built by buildAssetTreePane(). 48 166 * This table replaces the contents of the wrapper div. 49 167 * @returns The asset tree table as a String … … 51 169 def buildAssetTree(params, session) { 52 170 53 def sites = Site.list() 171 def startedAt = System.currentTimeMillis() 172 173 // Self initialisation ;-) 174 if(!treeRootImg) 175 initialise() 176 177 def sites = Site.withCriteria { 178 eq("isActive", true) 179 } 54 180 55 181 def visibleBranches = session.assetTreeVisibleBranches ? session.assetTreeVisibleBranches.tokenize(',') : [] … … 64 190 def branchImg = { branchId -> 65 191 if(visibleBranches.contains(branchId)) 66 bulletTreeMinusImg ()192 bulletTreeMinusImg 67 193 else 68 bulletTreePlusImg ()194 bulletTreePlusImg 69 195 } 70 196 … … 77 203 78 204 def sw = new StringWriter() 79 def mkp = new groovy.xml.MarkupBuilder(sw) //this line will be unnecessary in versions of Grails after version 1.2205 def mkp = new groovy.xml.MarkupBuilder(sw) 80 206 81 207 // Offer a site create link if no sites are found. 82 208 if(!sites) { 83 mkp.div(class: tableDivHtmlClass ()) {84 85 div(class: paneCloseHtmlClass ()) {86 a( href: js.toggle(paneHtmlId ()) ) {87 img(src: closeImg ())209 mkp.div(class: tableDivHtmlClass) { 210 211 div(class: paneCloseHtmlClass) { 212 a( href: js.toggle(paneHtmlId) ) { 213 img(src: closeImg) 88 214 } 89 215 } 90 216 91 table(id: tableHtmlId ()) {217 table(id: tableHtmlId) { 92 218 tr() { 93 219 td( valign: 'top', class: 'value') { 94 220 ul() { 95 img(src: treeRootImg (), alt: 'TreeRoot')221 img(src: treeRootImg, alt: 'TreeRoot') 96 222 li() { 97 a(href: siteCreate Link()) {98 img(src: addImg (), alt: 'Add', title: 'Add Site')223 a(href: siteCreateBaseLink()) { 224 img(src: addImg, alt: 'Add', title: 'Add Site') 99 225 } 100 226 } // li … … 106 232 div( class: 'buttons') { 107 233 span(class: 'button') { 108 input( type: 'button', value: g.message(code: 'default.close.text'), onclick: js.toggle(paneHtmlId (), "onclick") )234 input( type: 'button', value: g.message(code: 'default.close.text'), onclick: js.toggle(paneHtmlId, "onclick") ) 109 235 } 110 236 } // button div … … 116 242 // The main populated table. 117 243 /// @todo: use a loop for the subItem levels. 118 mkp.div(class: tableDivHtmlClass ()) {119 120 div(class: paneCloseHtmlClass ()) {244 mkp.div(class: tableDivHtmlClass) { 245 246 div(class: paneCloseHtmlClass) { 121 247 a( href: hrefHideAndSavePane() ) { 122 img(src: closeImg ())248 img(src: closeImg) 123 249 } 124 250 } 125 251 126 table(id: tableHtmlId ()) {252 table(id: tableHtmlId) { 127 253 tr() { 128 254 td(valign: 'top', class: 'value') { 129 255 ul() { 130 img(src: treeRootImg (), alt: 'TreeRoot')256 img(src: treeRootImg, alt: 'TreeRoot') 131 257 for(site in sites.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) { 132 258 li() { … … 137 263 } 138 264 else 139 img(src: dashImg ())265 img(src: dashImg) 140 266 a( href: siteShowLink(site.id), onclick: onclickHideAndSavePane() ) { 141 267 yieldUnescaped( name(site) ) … … 143 269 yieldUnescaped( description(site) ) 144 270 a(href: sectionCreateLink(site.id), onclick: onclickHideAndSavePane()) { 145 img(src: addImg (), alt: 'Add', title: 'Add Section')271 img(src: addImg, alt: 'Add', title: 'Add Section') 146 272 } 147 273 } … … 157 283 } 158 284 else 159 img(src: dashImg ())285 img(src: dashImg) 160 286 a( href: sectionShowLink(section.id), onclick: onclickHideAndSavePane() ) { 161 287 yieldUnescaped( name(section) ) … … 163 289 yieldUnescaped( description(section) ) 164 290 a(href: assetCreateLink(section.id), onclick: onclickHideAndSavePane()) { 165 img(src: addImg (), alt: 'Add', title: 'Add Asset')291 img(src: addImg, alt: 'Add', title: 'Add Asset') 166 292 } 167 293 } … … 178 304 } 179 305 else 180 img(src: dashImg ())306 img(src: dashImg) 181 307 a( href: assetShowLink(asset.id), onclick: onclickHideAndSavePane() ) { 182 308 yieldUnescaped( name(asset) ) … … 184 310 yieldUnescaped( description(asset) ) 185 311 a(href: assetSubItemCreateLink(asset.id), onclick: onclickHideAndSavePane()) { 186 img(src: addImg (), alt: 'Add', title: 'Add Sub Item')312 img(src: addImg, alt: 'Add', title: 'Add Sub Item') 187 313 } 188 314 a(href: assetCopyLink(asset.id), onclick: onclickHideAndSavePane()) { 189 img(src: copyImg (), alt: 'Add', title: 'Copy Asset')315 img(src: copyImg, alt: 'Add', title: 'Copy Asset') 190 316 } 191 317 } // li … … 202 328 } 203 329 else 204 img(src: dashImg ())330 img(src: dashImg) 205 331 a( href: assetSubItemShowLink(assetSubItemL1.id), onclick: onclickHideAndSavePane() ) { 206 332 yieldUnescaped( name(assetSubItemL1) ) … … 208 334 yieldUnescaped( description(assetSubItemL1) ) 209 335 a(href: assetSubItemCreateWithParentLink(assetSubItemL1.id), onclick: onclickHideAndSavePane()) { 210 img(src: addImg (), alt: 'Add', title: 'Add Sub Item')336 img(src: addImg, alt: 'Add', title: 'Add Sub Item') 211 337 } 212 338 } // li … … 223 349 } 224 350 else 225 img(src: dashImg ())351 img(src: dashImg) 226 352 a( href: assetSubItemShowLink(assetSubItemL2.id), onclick: onclickHideAndSavePane() ) { 227 353 yieldUnescaped( name(assetSubItemL2) ) … … 229 355 yieldUnescaped( description(assetSubItemL2) ) 230 356 a(href: assetSubItemCreateWithParentLink(assetSubItemL2.id), onclick: onclickHideAndSavePane()) { 231 img(src: addImg (), alt: 'Add', title: 'Add Sub Item')357 img(src: addImg, alt: 'Add', title: 'Add Sub Item') 232 358 } 233 359 } // li … … 244 370 } 245 371 else 246 img(src: dashImg ())372 img(src: dashImg) 247 373 a( href: assetSubItemShowLink(assetSubItemL3.id), onclick: onclickHideAndSavePane() ) { 248 374 yieldUnescaped( name(assetSubItemL3) ) … … 250 376 yieldUnescaped( description(assetSubItemL3) ) 251 377 a(href: assetSubItemCreateWithParentLink(assetSubItemL3.id), onclick: onclickHideAndSavePane()) { 252 img(src: addImg (), alt: 'Add', title: 'Add Sub Item')378 img(src: addImg, alt: 'Add', title: 'Add Sub Item') 253 379 } 254 380 } // li … … 265 391 // } 266 392 // else 267 img(src: dashImg ())393 img(src: dashImg) 268 394 a( href: assetSubItemShowLink(assetSubItemL4.id), onclick: onclickHideAndSavePane() ) { 269 395 yieldUnescaped( name(assetSubItemL4) ) … … 271 397 yieldUnescaped( description(assetSubItemL4) ) 272 398 // a(href: assetSubItemCreateWithParentLink(assetSubItemL4.id), onclick: onclickHideAndSavePane()) { 273 // img(src: addImg (), alt: 'Add', title: 'Add Sub Item')399 // img(src: addImg, alt: 'Add', title: 'Add Sub Item') 274 400 // } 275 401 } // li … … 319 445 } // mkp 320 446 447 def totalTime = (System.currentTimeMillis() - startedAt)/1000 448 log.debug "Total time to build asset tree: " + totalTime + "sec." 321 449 return sw.toString() 322 450 323 451 } // buildAssetTree 324 452 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 453 /** js calls */ 351 454 352 455 def hrefShowPane() { 353 def url = g.createLink(controller: 'assetDetailed', action: 'assetTree') 354 'javascript: showAssetTreePane(\"assetTreePane\", \"assetTreeLoadingImg' +'\", \"' + url + '\");' 456 'javascript: showAssetTreePane(\"assetTreePane\", \"assetTreeLoadingImg' +'\", \"' + assetTreeActionUrl + '\");' 355 457 } 356 458 357 459 def onclickHideAndSavePane() { 358 def saveUrl = g.createLink(controller: 'assetDetailed', action: 'saveAssetTreeStatus') 359 'return hideAssetTreePane(\"assetTreePane\", \"assetTreeTable' + '\", \"' + saveUrl + '\");' 460 'return hideAssetTreePane(\"assetTreePane\", \"assetTreeTable' + '\", \"' + saveAssetTreeStatusActionUrl + '\");' 360 461 } 361 462 362 463 def hrefHideAndSavePane() { 363 def saveUrl = g.createLink(controller: 'assetDetailed', action: 'saveAssetTreeStatus') 364 'javascript: hideAssetTreePane(\"assetTreePane\", \"assetTreeTable' + '\", \"' + saveUrl + '\");' 464 'javascript: hideAssetTreePane(\"assetTreePane\", \"assetTreeTable' + '\", \"' + saveAssetTreeStatusActionUrl + '\");' 365 465 } 366 466 367 467 def toggleBranch(divId) { 368 js.toggleWithImg(divId, divId + 'img', bulletTreeMinusImg (), bulletTreePlusImg())468 js.toggleWithImg(divId, divId + 'img', bulletTreeMinusImg, bulletTreePlusImg) 369 469 } 370 470 371 471 /** Links */ 372 472 373 def siteCreateLink() {374 g.createLink(controller: 'siteDetailed', action: 'create').toString()375 }376 473 def siteShowLink(id) { 377 g.createLink(controller: 'siteDetailed', action: 'show', params: ['id': id] ).toString()474 siteShowBaseLink + '/' + id 378 475 } 379 476 380 477 def siteEditLink(id) { 381 g.createLink(controller: 'siteDetailed', action: 'edit', params: ['id': id] ).toString()478 siteEditBaseLink + '/' + id 382 479 } 383 480 384 481 def sectionCreateLink(siteId) { 385 g.createLink(controller: 'sectionDetailed', action: 'create', params: ['site.id': siteId] ).toString()482 sectionCreateBaseLink + '?site.id=' + siteId 386 483 } 387 484 388 485 def sectionShowLink(id) { 389 g.createLink(controller: 'sectionDetailed', action: 'show', params: ['id': id] ).toString()486 sectionShowBaseLink + '/' + id 390 487 } 391 488 392 489 def sectionEditLink(id) { 393 g.createLink(controller: 'sectionDetailed', action: 'edit', params: ['id': id] ).toString()490 sectionEditBaseLink + '/' + id 394 491 } 395 492 396 493 def assetCreateLink(sectionId) { 397 g.createLink(controller: 'assetDetailed', action: 'create', params: ['section.id': sectionId] ).toString()494 assetCreateBaseLink + '?section.id=' + sectionId 398 495 } 399 496 400 497 def assetShowLink(id) { 401 g.createLink(controller: 'assetDetailed', action: 'show', id: id ).toString()498 assetShowBaseLink + '/' + id 402 499 } 403 500 404 501 def assetEditLink(id) { 405 g.createLink(controller: 'assetDetailed', action: 'edit', id: id ).toString()502 assetEditBaseLink + '/' + id 406 503 } 407 504 408 505 def assetCopyLink(id) { 409 g.createLink(controller: 'assetDetailed', action: 'copy', params: ['assetToCopy.id': id] ).toString()506 assetCopyBaseLink + '?assetToCopy.id=' + id 410 507 } 411 508 412 509 def assetSubItemCreateLink(assetId) { 413 g.createLink(controller: 'assetSubItemDetailed', action: 'create', params: ['asset.id': assetId] ).toString()510 assetSubItemCreateBaseLink + '?asset.id=' + assetId 414 511 } 415 512 416 513 def assetSubItemCreateWithParentLink(parentItemId) { 417 g.createLink(controller: 'assetSubItemDetailed', action: 'create', params: ['parentItem.id': parentItemId] ).toString()514 assetSubItemCreateWithParentBaseLink + '?parentItem.id=' + parentItemId 418 515 } 419 516 420 517 def assetSubItemShowLink(id) { 421 g.createLink(controller: 'assetSubItemDetailed', action: 'show', params: ['id': id] ).toString()518 assetSubItemShowBaseLink + '/' + id 422 519 } 423 520 424 521 def assetSubItemEditLink(id) { 425 g.createLink(controller: 'assetSubItemDetailed', action: 'edit', params: ['id': id] ).toString()522 assetSubItemEditBaseLink + '/' + id 426 523 } 427 524 -
trunk/grails-app/taglib/AssetTreeTagLib.groovy
r322 r457 7 7 8 8 def js = new JsUtilService() 9 def ts = new AssetTreeService()9 def assetTreeService 10 10 11 11 /** … … 20 20 */ 21 21 def assetTreeButton = { attrs -> 22 def mkp = new groovy.xml.MarkupBuilder(out) //this line will be unnecessary in versions of Grails after version 1.2 23 24 mkp.div( class: ts.buttonHtmlClass() ) { 25 a( href: ts.hrefShowPane() ) { 26 img(src: ts.treeRootImg()) 27 } 28 } // mkp 22 out << assetTreeService.buildAssetTreeButton(attrs) 29 23 } // assetTreeButton 30 24 … … 33 27 */ 34 28 def assetTreePane = { attrs -> 35 def mkp = new groovy.xml.MarkupBuilder(out) //this line will be unnecessary in versions of Grails after version 1.2 36 37 mkp.div(class: ts.paneHtmlClass(), id: ts.paneHtmlId(), style: 'display:none;') { 38 div(class: ts.paneCloseHtmlClass()) { 39 a( href: js.toggle(ts.paneHtmlId()) ) { 40 img(src: ts.closeImg()) 41 } 42 } 43 44 div(class: ts.tableDivHtmlClass()) { 45 table(id: ts.tableHtmlId()) { 46 tr() { 47 td(valign: 'top', class: 'value') { 48 ul() { 49 img(src: ts.treeRootImg(), id: ts.tableLoadingImgId(), alt: 'TreeRoot') 50 li() { 51 } // li 52 } // ul 53 } // td 54 } // tr 55 } // table 56 } // div 57 } // mkp 58 } // assetTreePane 29 out << assetTreeService.buildAssetTreePane(attrs) 30 } 59 31 60 32 } // end class
Note: See TracChangeset
for help on using the changeset viewer.