sysmodellibs/sysmodelgen/src/old/svg/Overlay.xsl
changeset 6 5b32dc297d05
parent 1 b538b70cbe51
equal deleted inserted replaced
3:e7e0ae78773e 6:5b32dc297d05
       
     1 <?xml version="1.0"?>
       
     2 
       
     3 <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:doc="tooldoc"  xmlns:s="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
       
     4 	<xsl:output method="xml"/>
       
     5 	<xsl:param name="Data" doc:desc="The data to be overlayed onto the SVG mode"/>
       
     6 	<xsl:param name="Prefix" doc:desc="The prefix for the ID, in case the plain ID is already taken"><xsl:apply-templates select="/" mode="my-prefix"/></xsl:param>
       
     7 	<xsl:param name="Event" doc:desc="The behaviour for opening the ID section -- click, popup, or mouseover. 'click' will display the group when the component is clicked. 'popup' will display the group when the component is moved over, with only one group showing at a time. 'mouseover' will display the group only when the mouse is over the component.."><xsl:apply-templates select="/" mode="my-event"/></xsl:param>
       
     8 	 <xsl:key name="id" match="s:g" use="@id"/> <!-- find id of only groups -->
       
     9 
       
    10 <!-- should override the following:
       
    11 
       
    12 	<xsl:template match="*" mode="my-script"/>	for any custom scripts (called on root node in $Data)
       
    13 	<xsl:template match="*" mode="my-defs"/>	for any custom defs (styles, shapes, etc)  (called on root node in $Data)
       
    14 	 <xsl:template match="/" mode="my-legend"/>	for any additional legend areas (called on the top-level document in $Data)
       
    15  	<xsl:template match="*" mode="my-release-version"/>	for custom release version text (called on tspan element containing text)
       
    16 	<xsl:template match="s:g" mode="my-overlay"><xsl:with-param name="id"/> 	the content of the group to be displayed on an event
       
    17 	<xsl:template match="/" mode="my-prefix"/>		in case the plain ID is taken, this prefix can be applied to the new groups (called on the top-level document in SVG model)
       
    18 	<xsl:template match="/" mode="my-event"/>	events can be popup, click or mouseover (the default). Also can be set by parameter
       
    19 	<xsl:template match="*" mode="is-present"><xsl:param name="id"/>	return '1' if component with $id exists, or leave empty if not
       
    20 -->
       
    21 
       
    22 <!-- no custom scripts or defs by default -->
       
    23 <xsl:template match="*" mode="my-script"/>
       
    24 <xsl:template match="*" mode="my-defs"/>
       
    25 
       
    26  <xsl:template match="/" mode="my-legend"/> <!-- no new legend -->
       
    27  <xsl:template match="*" mode="my-release-version"><xsl:value-of select="."/> </xsl:template> <!-- just use existing text -->
       
    28 
       
    29 <xsl:template match="*" mode="my-overlay"/>	<!-- no content by default -->
       
    30 <xsl:template match="/" mode="my-prefix"/>	<!-- no prefix by default -->
       
    31 <xsl:template match="/" mode="my-additional-content"/>	<!-- no content by default -->
       
    32 
       
    33 <xsl:template match="/" mode="my-event">mouseover</xsl:template> <!-- default event is mouseover -->
       
    34 <xsl:template match="*" mode="is-present"><xsl:param name="id"/>1</xsl:template> <!-- always present by default -->
       
    35 
       
    36 <xsl:template match="/" mode="my-valid-items">component collection block subblock layer</xsl:template>
       
    37 
       
    38 <!-- ======= main code follows ======= -->
       
    39 
       
    40  <xsl:template match="/s:svg">
       
    41  	<s:svg><xsl:apply-templates select="@*|node()"/>
       
    42 		<xsl:apply-templates select="//s:g[@id]" mode="overlay"/>
       
    43 		<xsl:apply-templates select="/" mode="my-additional-content"/>		
       
    44  	</s:svg>
       
    45  </xsl:template>
       
    46  
       
    47  <!-- print custom scripts last -->
       
    48 <xsl:template match="s:script[count(following::s:script)=0]">
       
    49 	<xsl:copy><xsl:copy-of select="@*|node()"/></xsl:copy>
       
    50 	<xsl:variable name="scripts" select="//s:script"/>
       
    51 	<xsl:choose>
       
    52 		<xsl:when test="$Data!=''">
       
    53 			<xsl:apply-templates select="document($Data,/)/*" mode="my-script">
       
    54 				<xsl:with-param name="scripts" select="$scripts"/>
       
    55 			</xsl:apply-templates>
       
    56 		</xsl:when>
       
    57 		<xsl:otherwise>
       
    58 			<xsl:apply-templates select="." mode="my-script">
       
    59 				<xsl:with-param name="scripts" select="$scripts"/>
       
    60 			</xsl:apply-templates>			
       
    61 		</xsl:otherwise>
       
    62 	</xsl:choose>
       
    63 </xsl:template>
       
    64 
       
    65 <!-- print custom defs last -->
       
    66 <xsl:template match="s:defs[count(following::s:defs)=0]">
       
    67 	<xsl:copy><xsl:copy-of select="@*|node()"/></xsl:copy>
       
    68 	<xsl:variable name="defs" select="//s:defs"/>
       
    69 	<xsl:choose>
       
    70 		<xsl:when test="$Data!=''">
       
    71 			<xsl:apply-templates select="document($Data,/)/*" mode="my-defs">
       
    72 				<xsl:with-param name="defs" select="$defs"/>
       
    73 			</xsl:apply-templates>
       
    74 		</xsl:when>
       
    75 		<xsl:otherwise>
       
    76 			<xsl:apply-templates select="." mode="my-defs">
       
    77 				<xsl:with-param name="defs" select="$defs"/>
       
    78 			</xsl:apply-templates>
       
    79 		</xsl:otherwise>
       
    80 	</xsl:choose>
       
    81 </xsl:template>
       
    82 
       
    83 
       
    84  <xsl:template match="s:g[(@class='component' or @class='layer-detail' or @class='block' or @class='subblock' or @class='collection')]">
       
    85 	<xsl:variable name="id">
       
    86 		<xsl:choose>
       
    87 			<xsl:when test="@id"><xsl:value-of select="@id"/></xsl:when>
       
    88 			<xsl:when test="@name"><xsl:value-of select="translate(@name,' ','')"/></xsl:when>
       
    89 		</xsl:choose>
       
    90 	 </xsl:variable>
       
    91   	<xsl:copy>
       
    92 	 	<xsl:apply-templates select="@*[name()!='id']"/>
       
    93 	 	<xsl:variable name="items"><xsl:apply-templates select="/" mode="my-valid-items"/></xsl:variable>
       
    94 		<xsl:variable name="found">
       
    95 			<xsl:choose>
       
    96   			<xsl:when test="$Data='' and not(contains(concat(' ',$items,' '),concat(' ',@class,' ')))"/>			
       
    97 			<xsl:when test="$Data=''">1</xsl:when>	
       
    98 			<xsl:otherwise>
       
    99 				<xsl:apply-templates select="document($Data,/)/*" mode="is-present">
       
   100 					<xsl:with-param name="id" select="$id"/>
       
   101 				</xsl:apply-templates>
       
   102 			</xsl:otherwise>
       
   103 			</xsl:choose>
       
   104 		</xsl:variable>
       
   105 		<xsl:if test="$id!='' and $found!=''">
       
   106 			<xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute>
       
   107 		</xsl:if>
       
   108 	 	<xsl:choose>
       
   109 		 	<xsl:when test="$found='' "/>
       
   110 		 	<xsl:when test="$Event='click' ">
       
   111 			 	<xsl:attribute name="onclick">on(clear('<xsl:value-of select="concat($Prefix,$id)"/>'))</xsl:attribute>
       
   112 		 	</xsl:when>
       
   113 		 	<xsl:when test="$Event='popup' ">
       
   114 			 	<xsl:attribute name="onmouseover">on(clear('<xsl:value-of select="concat($Prefix,$id)"/>'))</xsl:attribute>
       
   115 		 	</xsl:when>
       
   116 		 	<xsl:otherwise> 	
       
   117 			 	<xsl:attribute name="onmouseover">on('<xsl:value-of select="concat($Prefix,$id)"/>')</xsl:attribute>
       
   118 			 	<xsl:attribute name="onmouseout">off('<xsl:value-of select="concat($Prefix,$id)"/>')</xsl:attribute>
       
   119 			 </xsl:otherwise>
       
   120 		</xsl:choose>
       
   121 	<xsl:apply-templates select="node()"/>
       
   122  	</xsl:copy>
       
   123 </xsl:template>
       
   124 
       
   125  <xsl:template match="s:g" mode="overlay">
       
   126 	<xsl:variable name="id" select="@id"/>
       
   127 	<xsl:variable name="found">
       
   128 		<xsl:apply-templates select="document($Data,/)/*" mode="is-present">
       
   129 			<xsl:with-param name="id" select="$id"/>
       
   130 		</xsl:apply-templates>
       
   131 	</xsl:variable> <!--  no overlay if no data file -->
       
   132 	<xsl:if test="$Data!='' and $found!=''">
       
   133 		<s:g visibility="hidden" id="{concat($Prefix,$id)}">
       
   134 			<xsl:apply-templates select="." mode="my-overlay">
       
   135 				<xsl:with-param name="id" select="$id"/>
       
   136 			</xsl:apply-templates>
       
   137 		</s:g>
       
   138 	</xsl:if>
       
   139  </xsl:template>
       
   140  
       
   141  <xsl:template match="node()|@*">
       
   142  	<xsl:copy>
       
   143  	<xsl:apply-templates select="node()|@*"/>
       
   144  	</xsl:copy>
       
   145 </xsl:template>
       
   146 
       
   147 
       
   148 <!-- ====== positions in model ===============-->
       
   149 
       
   150 
       
   151  <xsl:template match="s:g" mode="item-width">
       
   152 	<xsl:choose>
       
   153 		<xsl:when test="@class='component'"><xsl:value-of select="s:use[1]/@width"/></xsl:when>
       
   154 		<xsl:otherwise><xsl:value-of select="s:rect[1]/@width"/></xsl:otherwise>
       
   155 	</xsl:choose>
       
   156 </xsl:template>
       
   157 
       
   158  <xsl:template match="s:g" mode="item-pos">
       
   159 	<xsl:choose>
       
   160 		<xsl:when test="@class='component'"><xsl:apply-templates select="s:use[1]" mode="position"/></xsl:when>
       
   161 		<xsl:otherwise><xsl:apply-templates select="s:rect[1]" mode="position"/></xsl:otherwise>
       
   162 	</xsl:choose>
       
   163 </xsl:template>
       
   164 
       
   165  <xsl:template match="s:g" mode="height">
       
   166 	<xsl:choose>
       
   167 		<xsl:when test="@class='component'"><xsl:value-of select="s:use[1]/@height"/></xsl:when>
       
   168 		<xsl:otherwise><xsl:value-of select="s:rect[1]/@height"/></xsl:otherwise>
       
   169 	</xsl:choose>
       
   170 </xsl:template>
       
   171 
       
   172 <xsl:template match="s:rect|s:use" mode="position">
       
   173 <xsl:variable name="pos">
       
   174 	<xsl:call-template name="sumpos"><xsl:with-param name="list">
       
   175 	   <xsl:value-of select="concat(@x,' ',@y)"/> <xsl:apply-templates select="ancestor::s:g[@transform]" mode="position"/>
       
   176 	   </xsl:with-param>
       
   177 	 </xsl:call-template>
       
   178 </xsl:variable>
       
   179 	<xsl:value-of select="concat(substring-before($pos, ' ') + @width *0.5 ,',',substring-after($pos, ' ') + @height *0.5 )"/>
       
   180 </xsl:template>
       
   181 
       
   182 <xsl:template match="s:g" mode="position">
       
   183 	<xsl:variable name="pos" select="normalize-space(substring-before(substring-after(substring-after(@transform,'translate'),'('),')'))"/>
       
   184 	<xsl:choose>
       
   185 		<xsl:when test="contains($pos,' ')"> + <xsl:value-of select="$pos"/></xsl:when>
       
   186 		<xsl:otherwise> + <xsl:value-of select="$pos"/> 0</xsl:otherwise>
       
   187 	</xsl:choose>
       
   188 </xsl:template>
       
   189 
       
   190 
       
   191 <xsl:template name="sumpos"><xsl:param name="list"/>
       
   192 	<xsl:variable name="cur" select="normalize-space(substring-before($list,'+'))"/>
       
   193 	<xsl:choose>
       
   194 		<xsl:when test="$cur=''"><xsl:value-of select="normalize-space($list)"/></xsl:when>
       
   195 		<xsl:otherwise>
       
   196 			<xsl:variable name="x" select="substring-before($cur,' ')"/>
       
   197 			<xsl:variable name="y" select="substring-after($cur,' ')"/>
       
   198 			<xsl:variable name="next">
       
   199 				<xsl:call-template name="sumpos">
       
   200 					<xsl:with-param name="list" select="substring-after($list,'+')"/>
       
   201 				</xsl:call-template>
       
   202 			</xsl:variable>	
       
   203 			<xsl:variable name="x1" select="substring-before($next,' ')"/>
       
   204 			<xsl:variable name="y1" select="substring-after($next,' ')"/>
       
   205 			<xsl:value-of select="concat($x1 +  $x,' ', $y1 + $y)"/>
       
   206 		</xsl:otherwise>
       
   207 	</xsl:choose>
       
   208 </xsl:template>
       
   209 
       
   210 <!-- cleanup empty links -->
       
   211 <xsl:template match="s:a">
       
   212 	<xsl:choose>
       
   213 		<xsl:when test="@*"><xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy></xsl:when>
       
   214 		<xsl:otherwise>
       
   215 			<xsl:apply-templates/>	
       
   216 		</xsl:otherwise>
       
   217 	</xsl:choose>
       
   218 </xsl:template>
       
   219 
       
   220 
       
   221 <!-- ====== legend stuff ===============-->
       
   222 
       
   223 <xsl:template match="*" mode="legend-ext-width">20</xsl:template>
       
   224  <xsl:template match="s:g[@class='legend']">
       
   225  <xsl:call-template name="insert-legend"><xsl:with-param name="width"><xsl:apply-templates select="." mode="legend-ext-width"/></xsl:with-param>
       
   226  </xsl:call-template>
       
   227 </xsl:template>
       
   228 
       
   229 <xsl:template name="insert-legend"><xsl:param name="width"/>
       
   230 	<xsl:copy>
       
   231 		<xsl:copy-of select="@*[name()!='transform']"/>
       
   232 		<xsl:attribute name="transform">
       
   233 			<xsl:value-of select="substring-before(@transform,'(')"/>
       
   234 			<xsl:variable name="t" select="normalize-space(substring-after(@transform,'('))"/>
       
   235 			<xsl:value-of select="concat('(',substring-before($t,' ') - $width ,' ')"/>
       
   236 			<xsl:value-of select="substring-after($t,' ')"/>
       
   237 		</xsl:attribute>
       
   238 		<xsl:apply-templates mode="insert-legend">
       
   239 			<xsl:with-param name="width" select="$width"/>
       
   240  		</xsl:apply-templates >
       
   241 	</xsl:copy>
       
   242 </xsl:template>
       
   243 
       
   244  <xsl:template match="*" mode="insert-legend"><xsl:copy-of select="."/></xsl:template>
       
   245 
       
   246  <xsl:template match="s:text|s:tspan" mode="insert-legend"><xsl:param name="width"/>
       
   247  	<xsl:copy>
       
   248 		<xsl:copy-of select="@*[name()!='x']"/>
       
   249 		<xsl:attribute name="x"><xsl:value-of select="@x +  $width"/></xsl:attribute>	
       
   250 		<xsl:choose>
       
   251 			<xsl:when test="@id='release-version'">
       
   252 				<xsl:apply-templates select="." mode="my-release-version"/>
       
   253 			 </xsl:when>
       
   254 			<xsl:otherwise>
       
   255 				<xsl:apply-templates mode="insert-legend"><xsl:with-param name="width" select="$width"/></xsl:apply-templates>
       
   256 			</xsl:otherwise>
       
   257 		</xsl:choose>	
       
   258 	</xsl:copy>
       
   259  </xsl:template>
       
   260  
       
   261  <xsl:template match="s:rect" mode="insert-legend"><xsl:param name="width"/>
       
   262  	<xsl:copy>
       
   263 		<xsl:copy-of select="@*[name()!='width']"/>
       
   264 		<xsl:attribute name="width"><xsl:value-of select="@width +  $width"/></xsl:attribute>	
       
   265 		<xsl:copy-of select="*|text()"/>
       
   266 	</xsl:copy>
       
   267  </xsl:template>
       
   268 
       
   269  <xsl:template match="s:g" mode="insert-legend"><xsl:param name="width"/>
       
   270  	<xsl:copy><xsl:copy-of select="@*"/>
       
   271 		<xsl:apply-templates mode="insert-legend"><xsl:with-param name="width" select="$width"/></xsl:apply-templates>
       
   272 	</xsl:copy>
       
   273  </xsl:template>
       
   274  
       
   275 
       
   276  <xsl:template match="s:g[@id='legend-box']/s:g" mode="insert-legend"><xsl:param name="width"/>
       
   277  	<xsl:copy>
       
   278 		<xsl:copy-of select="@*"/>
       
   279 		<xsl:apply-templates/>
       
   280 		<xsl:if test="$width!=0">
       
   281 			<s:g>
       
   282 				<xsl:attribute name="transform">translate(<xsl:value-of select="preceding-sibling::s:rect[1]/@width - 5"/>)</xsl:attribute>
       
   283 				<xsl:variable name="legend" select="."/>
       
   284 				<xsl:choose>
       
   285 					<xsl:when test="$Data!=''">
       
   286 						<xsl:apply-templates select="document($Data,/)" mode="my-legend">
       
   287 							<xsl:with-param name="legend" select="$legend"/>
       
   288 						</xsl:apply-templates>
       
   289 					</xsl:when>
       
   290 					<xsl:otherwise>
       
   291 						<xsl:apply-templates select="/" mode="my-legend">
       
   292 							<xsl:with-param name="legend" select="$legend"/>
       
   293 						</xsl:apply-templates>			
       
   294 					</xsl:otherwise>
       
   295 				</xsl:choose>			
       
   296 			</s:g>
       
   297 		</xsl:if>
       
   298 	</xsl:copy>
       
   299  </xsl:template> 
       
   300  
       
   301 <!-- ======= an default implementation for SVG ======= -->
       
   302 
       
   303 <!-- find all SVG groups with an ID in the model and make them mouse-overs -->
       
   304 
       
   305 
       
   306 <!-- copy all scripts from other SVG -->
       
   307 <xsl:template match="/s:svg" mode="my-script"><xsl:copy-of select="//s:script"/></xsl:template>
       
   308 
       
   309 <!-- copy all defs from other SVG -->
       
   310 <xsl:template match="/s:svg" mode="my-defs"><xsl:copy-of select="//s:defs"/></xsl:template>
       
   311 
       
   312 <!-- position over the component -->
       
   313 <xsl:template match="s:g" mode="my-overlay"><xsl:param name="id"/>
       
   314 	<xsl:variable name="pos"><xsl:apply-templates select="." mode="item-pos"/></xsl:variable>
       
   315 	<xsl:variable name="w"><xsl:apply-templates select="." mode="item-width"/></xsl:variable>
       
   316 	<xsl:attribute name="transform">translate(<xsl:value-of select="concat(substring-before($pos,','), ' ',substring-after($pos,',') + $w *0.5)"/>) <xsl:value-of select="@transform"/></xsl:attribute>
       
   317 	<xsl:apply-templates select="document($Data,/)/*" mode="my-content">
       
   318 		<xsl:with-param name="id" select="$id"/>		
       
   319 	</xsl:apply-templates>
       
   320 </xsl:template>
       
   321 
       
   322  <xsl:template match="/s:svg" mode="my-content"><xsl:param name="id"/>
       
   323  	<xsl:copy-of select="key('id',$id)"/>
       
   324  </xsl:template>
       
   325  
       
   326  <xsl:template match="/s:svg" mode="is-present"><xsl:param name="id"/>
       
   327   	<xsl:if test="key('id',$id)">1</xsl:if>
       
   328  </xsl:template>
       
   329 
       
   330  
       
   331 </xsl:stylesheet>