source: trunk/grails-app/taglib/AssetTreeTagLib.groovy @ 284

Last change on this file since 284 was 284, checked in by gav, 14 years ago

Move asset copy and create functions into AssetService.
Repair create links in AssetTreeTagLib.

File size: 18.4 KB
Line 
1
2class AssetTreeTagLib {
3    static namespace = 'gnumims'
4
5    def resources = { attrs ->
6        ///@todo: should include our javascript and do setup here.
7    }
8
9    def assetTree = { attrs ->
10
11        def sites = Site.list()
12
13        def assetInstance = Asset.get(1) /// @todo: remove this.
14
15        def divIdCount = 0
16        def divId = ''
17        def nextDivId = {
18            divIdCount++
19            divId = 'assetTreeBranch'+divIdCount
20        }
21
22        def mkp = new groovy.xml.MarkupBuilder(out) //this line will be unnecessary in versions of Grails after version 1.2
23
24
25        /// @todo: use a loop for the subItem levels.
26        mkp.div(class: 'tree') {
27            ul() {
28                img(src: treeRootImg(), alt: 'TreeRoot')
29                for(site in sites.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) {
30                    li() {
31                        if(site.sections) {
32                            a(href: toggleBranch(nextDivId()) ) {
33                                img( src: bulletTreePlusImg(), id: divId+'img' )
34                            }
35                        }
36                        else
37                            img(src: dashImg())
38                        a( href: siteShowLink(site.id) ) {
39                            yieldUnescaped( site.encodeAsHTML() )
40                        }
41                        a(href: sectionCreateLink(site.id)) {
42                            img(src: addImg(), alt: 'Add', title: 'Add Section')
43                        }
44                    }
45                    if(site.sections) {
46                        div(id: divId,  style: 'display:none;') {
47                            ul() {
48                                for(section in site.sections.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) {
49                                    li() {
50                                        if(section.assets) {
51                                            a(href: toggleBranch(nextDivId()) ) {
52                                                img(src: bulletTreePlusImg(), id: divId+'img' )
53                                            }
54                                        }
55                                        else
56                                            img(src: dashImg())
57                                        a( href: sectionShowLink(section.id) ) {
58                                            yieldUnescaped( section.encodeAsHTML() )
59                                        }
60                                        a(href: assetCreateLink(section.id)) {
61                                            img(src: addImg(), alt: 'Add', title: 'Add Asset')
62                                        }
63                                    }
64
65                                    if(section.assets) {
66                                        div(id: divId,  style: 'display:none;') {
67                                            ul() {
68                                                for(asset in section.assets.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) {
69                                                    li() {
70                                                        if(asset.assetSubItems) {
71                                                            a(href: toggleBranch(nextDivId()) ) {
72                                                                img(src: bulletTreePlusImg(), id: divId+'img' )
73                                                            }
74                                                        }
75                                                        else
76                                                            img(src: dashImg())
77                                                        a( href: assetShowLink(asset.id) ) {
78                                                            yieldUnescaped( asset.encodeAsHTML() )
79                                                        }
80                                                        a(href: assetSubItemCreateLink(asset.id)) {
81                                                            img(src: addImg(), alt: 'Add', title: 'Add Sub Item')
82                                                        }
83                                                        a(href: assetCopyLink(asset.id)) {
84                                                            img(src: copyImg(), alt: 'Add', title: 'Copy Asset')
85                                                        }
86                                                    } // li
87
88                                                    if(asset.assetSubItems) {
89                                                        div(id: divId,  style: 'display:none;') {
90                                                            ul() {
91                                                                for(assetSubItemL1 in asset.assetSubItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) {
92                                                                    li() {
93                                                                        if(assetSubItemL1.subItems) {
94                                                                            a(href: toggleBranch(nextDivId()) ) {
95                                                                                img(src: bulletTreePlusImg(), id: divId+'img' )
96                                                                            }
97                                                                        }
98                                                                        else
99                                                                            img(src: dashImg())
100                                                                        a( href: assetSubItemShowLink(assetSubItemL1.id) ) {
101                                                                            yieldUnescaped( assetSubItemL1.encodeAsHTML() )
102                                                                        }
103                                                                        a(href: assetSubItemCreateWithParentLink(assetSubItemL1.id)) {
104                                                                            img(src: addImg(), alt: 'Add', title: 'Add Sub Item')
105                                                                        }
106                                                                    } // li
107
108                                                                    if(assetSubItemL1.subItems) {
109                                                                        div(id: divId,  style: 'display:none;') {
110                                                                            ul() {
111                                                                                for(assetSubItemL2 in assetSubItemL1.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) {
112                                                                                    li() {
113                                                                                        if(assetSubItemL2.subItems) {
114                                                                                            a(href: toggleBranch(nextDivId()) ) {
115                                                                                                img( src: bulletTreePlusImg(), id: divId+'img' )
116                                                                                            }
117                                                                                        }
118                                                                                        else
119                                                                                            img(src: dashImg())
120                                                                                        a( href: assetSubItemShowLink(assetSubItemL2.id) ) {
121                                                                                            yieldUnescaped( assetSubItemL2.encodeAsHTML() )
122                                                                                        }
123                                                                                        a(href: assetSubItemCreateWithParentLink(assetSubItemL2.id)) {
124                                                                                            img(src: addImg(), alt: 'Add', title: 'Add Sub Item')
125                                                                                        }
126                                                                                    } // li
127
128                                                                                    if(assetSubItemL2.subItems) {
129                                                                                        div(id: divId,  style: 'display:none;') {
130                                                                                            ul() {
131                                                                                                for(assetSubItemL3 in assetSubItemL2.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) {
132                                                                                                    li() {
133                                                                                                        if(assetSubItemL3.subItems) {
134                                                                                                            a(href: toggleBranch(nextDivId()) ) {
135                                                                                                                img( src: bulletTreePlusImg(), id: divId+'img' )
136                                                                                                            }
137                                                                                                        }
138                                                                                                        else
139                                                                                                            img(src: dashImg())
140                                                                                                        a( href: assetSubItemShowLink(assetSubItemL3.id) ) {
141                                                                                                            yieldUnescaped( assetSubItemL3.encodeAsHTML() )
142                                                                                                        }
143                                                                                                        a(href: assetSubItemCreateWithParentLink(assetSubItemL3.id)) {
144                                                                                                            img(src: addImg(), alt: 'Add', title: 'Add Sub Item')
145                                                                                                        }
146                                                                                                    } // li
147
148                                                                                                    if(assetSubItemL3.subItems) {
149                                                                                                        div(id: divId,  style: 'display:none;') {
150                                                                                                            ul() {
151                                                                                                                for(assetSubItemL4 in assetSubItemL3.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }) {
152                                                                                                                    li() {
153//                                                                                                                         if(assetSubItemL4.subItems) {
154//                                                                                                                             a(href: toggleBranch(nextDivId()) ) {
155//                                                                                                                                 img( src: bulletTreePlusImg(), id: divId+'img' )
156//                                                                                                                             }
157//                                                                                                                         }
158//                                                                                                                         else
159                                                                                                                        img(src: dashImg())
160                                                                                                                        a( href: assetSubItemShowLink(assetSubItemL4.id) ) {
161                                                                                                                            yieldUnescaped( assetSubItemL4.encodeAsHTML() )
162                                                                                                                        }
163//                                                                                                                         a(href: assetSubItemCreateWithParentLink(assetSubItemL4.id)) {
164//                                                                                                                             img(src: addImg(), alt: 'Add', title: 'Add Sub Item')
165//                                                                                                                         }
166                                                                                                                    } // li
167
168                                                                                                                } // assetSubItemL4
169                                                                                                            } // ul
170                                                                                                        } // div
171                                                                                                    } // if(assetSubItemL3.subItems)
172
173
174                                                                                                } // assetSubItemL3
175                                                                                            } // ul
176                                                                                        } // div
177                                                                                    } // if(assetSubItemL2.subItems)
178
179                                                                                } // assetSubItemL2
180                                                                            } // ul
181                                                                        } // div
182                                                                    } // if(assetSubItemL1.subItems)
183
184                                                                } // assetSubItemL1
185                                                            } // ul
186                                                        } // div
187                                                    } // if(asset.assetSubItems)
188
189                                                } // assets
190                                            } // ul
191                                        } // div
192                                    } // if(section.assets)
193
194                                } //sections
195                            } // ul
196                        } // div
197                    } // if(site.sections)
198                } // sites
199            } // ul
200        } // mkp
201
202    } // assetTree
203
204
205
206    /** Imgs */
207
208    def treeRootImg() {
209        resource(dir:'images/skin',file:'chart_organisation.png').toString()
210    }
211    def addImg() {
212        resource(dir:'images/skin',file:'database_add.png').toString()
213    }
214    def copyImg() {
215        resource(dir:'images/skin',file:'page_copy.png').toString()
216    }
217    def bulletTreePlusImg() {
218        resource(dir:'images/skin',file:'bullet_tree_plus.png').toString()
219    }
220    // actually set in javascript function.
221    def bulletTreeMinusImg() {
222        resource(dir:'images/skin',file:'bullet_tree_minus.png').toString()
223    }
224    def dashImg() {
225        resource(dir:'images/skin',file:'hline_short.png').toString()
226    }
227
228    /** js calls */
229
230    def toggleBranch(divId) {
231        /// @todo: toggleBranch is in overlayPane.js and should be moved to tree.js
232       'javascript: toggleBranch(\"' + divId + '\", \"' + divId + 'img' + '\");'
233    }
234
235    /** Links */
236
237    def siteShowLink(id) {
238        createLink(controller: 'siteDetailed', action: 'show', params: ['id': id] ).toString()
239    }
240
241    def siteEditLink(id) {
242        createLink(controller: 'siteDetailed', action: 'edit', params: ['id': id] ).toString()
243    }
244
245    def sectionCreateLink(siteId) {
246        createLink(controller: 'sectionDetailed', action: 'create', params: ['site.id': siteId] ).toString()
247    }
248
249    def sectionShowLink(id) {
250        createLink(controller: 'sectionDetailed', action: 'show', params: ['id': id] ).toString()
251    }
252
253    def sectionEditLink(id) {
254        createLink(controller: 'sectionDetailed', action: 'edit', params: ['id': id] ).toString()
255    }
256
257    def assetCreateLink(sectionId) {
258        createLink(controller: 'assetDetailed', action: 'create', params: ['section.id': sectionId] ).toString()
259    }
260
261    def assetShowLink(id) {
262        createLink(controller: 'assetDetailed', action: 'show', id: id ).toString()
263    }
264
265    def assetEditLink(id) {
266        createLink(controller: 'assetDetailed', action: 'edit', id: id ).toString()
267    }
268
269    def assetCopyLink(id) {
270        createLink(controller: 'assetDetailed', action: 'copy', params: ['assetToCopy.id': id] ).toString()
271    }
272
273    def assetSubItemCreateLink(id) {
274        createLink(controller: 'assetSubItemDetailed', action: 'create', params: ['id': id] ).toString()
275    }
276
277    def assetSubItemCreateWithParentLink(parentItemId) {
278        createLink(controller: 'assetSubItemDetailed', action: 'create', params: ['parentItem.id': parentItemId] ).toString()
279    }
280
281    def assetSubItemShowLink(id) {
282        createLink(controller: 'assetSubItemDetailed', action: 'show', params: ['id': id] ).toString()
283    }
284
285    def assetSubItemEditLink(id) {
286        createLink(controller: 'assetSubItemDetailed', action: 'edit', params: ['id': id] ).toString()
287    }
288
289} // end class
Note: See TracBrowser for help on using the repository browser.