All Downloads are FREE. Search and download functionalities are using the official Maven repository.

groovity.portal.lib.expandingTree.grvt Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * © 2018 Disney | ABC Television Group
 *
 * Licensed under the Apache License, Version 2.0 (the "Apache License")
 * with the following modification; you may not use this file except in
 * compliance with the Apache License and the following modification to it:
 * Section 6. Trademarks. is deleted and replaced with:
 *
 * 6. Trademarks. This License does not grant permission to use the trade
 *     names, trademarks, service marks, or product names of the Licensor
 *     and its affiliates, except as required to comply with Section 4(c) of
 *     the License and to reproduce the content of the NOTICE file.
 *
 * You may obtain a copy of the Apache License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Apache License with the above modification is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the Apache License for the specific
 * language governing permissions and limitations under the Apache License.
 *******************************************************************************/
@Tag(
	info = "Create an interactive browser based on a tree",
	attrs = [
		@Attr(
			name = "id",
			info = "A unique identifier for this table to isolate interactions",
			required = true
		),
		@Attr(
			name = "map",
			info = "The nested map of tree data to display",
			required = true
		),
		@Attr(
			name = "title",
			info = "The title to apply to the table",
			required = false
		),
		@Attr(
			name = "selected",
			info = 'The currently selected key, defaults binding variable \'${id}_selected\'',
			required = false	
		),
		@Attr(
			name = "format",
			info = "A closure that can be passed in to control the formatting (e.g. linking) of items in the tree, takes two arguments 'key' and 'value'",
			required = false
		)
	]
)
class TreeBrowser{
	
	def p(String name){
		request.getParameter(name);
	}
	
	def tag(Map atts, Closure body){
		def id = atts.id;
		if(id instanceof Closure){
			id = id();
		}
		if(id==null){
			throw new RuntimeException("PaginatedTable requires a 'id' attribute");
		}
		def map = atts.map;
		if(map instanceof Closure){
			map = map();
		}
		if(map==null){
			throw new RuntimeException("TreeBrowser requires a 'map' attribute");
		}
		def title = atts.title;
		if(title instanceof Closure){
			title = title();
		}
		def selected = p("${id}_selected");
		if(selected==null){
			selected = atts.selected;
		}
		if(selected instanceof Closure){
			selected = selected();
		}
		if(!selected){
			selected="/";
		}
		//log.info("Selected is ${selected}")
		def format = atts.format;
		if(format instanceof Closure){
			try{
				def nformat = format();
				if(nformat instanceof Closure){
					format = nformat;
				}
			}
			catch(Exception e){}
		}
		def queryString = request.queryString ?: '';
		if(!format){
			
			format = { key, value -> 
				
				def href = uri(base:"${request.requestURI}?${queryString}",{ param(name:"${id}_selected",value:pathStack.join('')+key) });
				<~ ${key} ~>
			}
		} 
		pathStack = []
		recursiveTemplate = <~ 
			
				
				
				
  • ${format(entry.key,entry.value)}
    ${!(pathStack << entry.key) ?:'' }
    ${!pathStack.pop() ?:'' }
  • ${format(entry.key,entry.value)}
  • ~> currentMap = ["/":map]; write{<~
    ${title}
    ~>} } }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy