grails-app/taglib/SimileTagLib.groovy
author Josh Reed <jareed@andrill.org>
Wed Mar 12 23:00:06 2008 -0500 (2 years ago)
changeset 0 f374b63c0b59
child 2 faafd83ba160
permissions -rw-r--r--
Initial commit of the Simile grails plugin
     1 import org.codehaus.groovy.grails.plugins.PluginManagerHolder
     2 
     3 /**
     4  * A taglib to ease the loading of Simile libraries 
     5  */
     6 class SimileTagLib {
     7 	static namespace = 'simile'
     8 	
     9 	def ajax = { attrs ->
    10 		out.println pluginJS(request?.contextPath, 'ajax/simile-ajax', attrs?.bundle);
    11 	}
    12 	
    13 	def timeline = { attrs ->
    14 		out.println pluginJS(request?.contextPath, 'timeline/timeline', attrs?.bundle);
    15 	}
    16 	
    17 	def timelineExt = { attrs ->
    18 		out.println pluginJS(request?.contextPath, "timeline/ext/${attrs?.name}/${attrs?.name}", attrs?.bundle);
    19 	}
    20 	
    21 	def pluginJS(contextPath, lib, bundle) {
    22 		def p = PluginManagerHolder?.pluginManager.getGrailsPlugin('simile')
    23 		return "<script type='text/javascript' src='${contextPath}/plugins/${p.name}-${p.version}/js/${lib}-api.js?bundle=${bundle == 'true'}'></script>"
    24 	}
    25 	
    26 	def renderTimeline = { attrs ->
    27 		// get our timeline
    28 		def timeline = attrs.timeline
    29 		
    30 		// figure out our unit type
    31 		def unitType = Timeline.TYPE_UNITS[timeline.type]
    32 		def eventSource = "0"
    33 		if (unitType) { eventSource = "new SimileAjax.EventIndex(${unitType})" }
    34 		
    35 		def unitObject = ""
    36 		switch (timeline.type) {
    37 			case Timeline.DEPTH_TYPE: 		unitObject = ".Depth"; break;
    38 			case Timeline.GEOCHRONO_TYPE:	unitObject = ".Geochrono"; break;
    39 		}
    40 			
    41 		
    42 		out.println """\
    43 		    <script>
    44         var tl;
    45         function onLoad() {
    46         var eventSource = new Timeline.DefaultEventSource(${eventSource});
    47             
    48             var theme = Timeline.ClassicTheme.create();
    49             theme.event.bubble.width = 320;
    50             theme.event.bubble.height = 220;
    51             var bandInfos = [
    52                 Timeline${unitObject}.createBandInfo({
    53                     eventSource:    eventSource,
    54                     width:          "80%", 
    55                     intervalUnit:   Timeline.${timeline.detailUnit}, 
    56                     intervalPixels: ${timeline.detailPixels}
    57                 }),
    58                 Timeline${unitObject}.createBandInfo({
    59                     eventSource:    eventSource,
    60                     width:          "20%", 
    61                     intervalUnit:   Timeline.${timeline.overviewUnit}, 
    62                     intervalPixels: ${timeline.overviewPixels},
    63                     overview:       true,
    64                     convertToBaseUnit: true
    65                 })
    66             ];
    67             bandInfos[1].syncWith = 0;
    68             bandInfos[1].highlight = true;
    69             
    70             tl = Timeline.create(document.getElementById("${attrs?.element}"), bandInfos, Timeline.HORIZONTAL${unitType == "" ? "" : ", " + unitType});
    71             tl.loadXML("${request?.contextPath}/timeline/eventsXML/${timeline.id}", function(xml, url) {
    72                 eventSource.loadXML(xml, url);
    73             });
    74         }
    75         var resizeTimerID = null;
    76         function onResize() {
    77             if (resizeTimerID == null) {
    78                 resizeTimerID = window.setTimeout(function() {
    79                     resizeTimerID = null;
    80                     tl.layout();
    81                 }, 500);
    82             }
    83         }
    84     </script>
    85 		"""
    86 	}
    87 }