svghmi/gen_index_xhtml.xslt
branchsvghmi
changeset 2797 c5ba1e77f054
parent 2796 c7a22ce8c156
child 2798 ddb2c4668a6b
equal deleted inserted replaced
2796:c7a22ce8c156 2797:c5ba1e77f054
     1 <?xml version="1.0"?>
     1 <?xml version="1.0"?>
     2 <xsl:stylesheet xmlns:func="http://exslt.org/functions" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:svg="http://www.w3.org/2000/svg" xmlns:str="http://exslt.org/strings" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:exsl="http://exslt.org/common" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" extension-element-prefixes="ns func" version="1.0" exclude-result-prefixes="ns str regexp exsl func">
     2 <xsl:stylesheet xmlns:func="http://exslt.org/functions" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:svg="http://www.w3.org/2000/svg" xmlns:str="http://exslt.org/strings" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:exsl="http://exslt.org/common" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" extension-element-prefixes="ns func" version="1.0" exclude-result-prefixes="ns str regexp exsl func">
     3   <xsl:output method="xml" cdata-section-elements="script"/>
     3   <xsl:output method="xml" cdata-section-elements="script"/>
     4   <xsl:variable name="geometry" select="ns:GetSVGGeometry()"/>
     4   <xsl:variable name="geometry" select="ns:GetSVGGeometry()"/>
     5   <xsl:variable name="hmitree" select="ns:GetHMITree()"/>
     5   <xsl:variable name="hmitree" select="ns:GetHMITree()"/>
     6   <xsl:variable name="hmi_elements" select="//*[starts-with(@inkscape:label, 'HMI:')]"/>
     6   <xsl:variable name="hmi_elements" select="//svg:*[starts-with(@inkscape:label, 'HMI:')]"/>
     7   <xsl:variable name="hmi_geometry" select="$geometry[@Id = $hmi_elements/@id]"/>
     7   <xsl:variable name="hmi_geometry" select="$geometry[@Id = $hmi_elements/@id]"/>
     8   <xsl:variable name="hmi_pages" select="$hmi_elements[func:parselabel(@inkscape:label)/widget/@type = 'Page']"/>
     8   <xsl:variable name="hmi_pages" select="$hmi_elements[func:parselabel(@inkscape:label)/widget/@type = 'Page']"/>
     9   <xsl:variable name="default_page">
     9   <xsl:variable name="default_page">
    10     <xsl:choose>
    10     <xsl:choose>
    11       <xsl:when test="count($hmi_pages) &gt; 1">
    11       <xsl:when test="count($hmi_pages) &gt; 1">
   177       </xsl:if>
   177       </xsl:if>
   178     </xsl:variable>
   178     </xsl:variable>
   179     <func:result select="exsl:node-set($ast)"/>
   179     <func:result select="exsl:node-set($ast)"/>
   180   </func:function>
   180   </func:function>
   181   <xsl:template name="scripts">
   181   <xsl:template name="scripts">
   182     <xsl:text>var hmi_index = {
   182     <xsl:text>var hmi_hash = [</xsl:text>
   183 </xsl:text>
   183     <xsl:value-of select="$hmitree/@hash"/>
   184     <xsl:variable name="svg" select="/"/>
   184     <xsl:text>]; 
   185     <xsl:for-each select="$indexed_hmitree/*">
   185 </xsl:text>
   186       <xsl:value-of select="@index"/>
   186     <xsl:text>var hmi_widgets = {
       
   187 </xsl:text>
       
   188     <xsl:for-each select="$hmi_elements">
       
   189       <xsl:variable name="widget" select="func:parselabel(@inkscape:label)/widget"/>
       
   190       <xsl:value-of select="@id"/>
   187       <xsl:text>: {
   191       <xsl:text>: {
   188 </xsl:text>
   192 </xsl:text>
   189       <xsl:text>    name: "</xsl:text>
   193       <xsl:text>    type: "</xsl:text>
   190       <xsl:value-of select="@name"/>
   194       <xsl:value-of select="$widget/@type"/>
   191       <xsl:text>",
   195       <xsl:text>",
   192 </xsl:text>
   196 </xsl:text>
   193       <xsl:text>    hmipath: "</xsl:text>
   197       <xsl:text>    frequency: </xsl:text>
   194       <xsl:value-of select="@hmipath"/>
   198       <xsl:apply-templates mode="refresh_frequency" select="$widget"/>
   195       <xsl:text>"
   199       <xsl:text>,
   196 </xsl:text>
   200 </xsl:text>
   197       <xsl:text>    ids: [
   201       <xsl:text>    args: [
   198 </xsl:text>
   202 </xsl:text>
   199       <xsl:variable name="hmipath" select="@hmipath"/>
   203       <xsl:for-each select="$widget/arg">
   200       <xsl:for-each select="$svg//*[substring-after(@inkscape:label,'@') = $hmipath]">
       
   201         <xsl:text>        "</xsl:text>
   204         <xsl:text>        "</xsl:text>
   202         <xsl:value-of select="@id"/>
   205         <xsl:value-of select="@value"/>
   203         <xsl:text>"</xsl:text>
   206         <xsl:text>"</xsl:text>
   204         <xsl:if test="position()!=last()">
   207         <xsl:if test="position()!=last()">
   205           <xsl:text>,</xsl:text>
   208           <xsl:text>,</xsl:text>
   206         </xsl:if>
   209         </xsl:if>
   207         <xsl:text>
   210         <xsl:text>
   208 </xsl:text>
   211 </xsl:text>
   209       </xsl:for-each>
   212       </xsl:for-each>
       
   213       <xsl:text>    ],
       
   214 </xsl:text>
       
   215       <xsl:text>    paths: [
       
   216 </xsl:text>
       
   217       <xsl:for-each select="$widget/path">
       
   218         <xsl:variable name="hmipath" select="@value"/>
       
   219         <xsl:variable name="hmitree_match" select="$indexed_hmitree/*[@hmipath = $hmipath]"/>
       
   220         <xsl:if test="count($hmitree_match) = 0">
       
   221           <xsl:message terminate="yes">
       
   222             <xsl:text>No match for HMI </xsl:text>
       
   223             <xsl:value-of select="$hmipath"/>
       
   224             <xsl:text>;</xsl:text>
       
   225           </xsl:message>
       
   226         </xsl:if>
       
   227         <xsl:text>        </xsl:text>
       
   228         <xsl:value-of select="$hmitree_match/@index"/>
       
   229         <xsl:if test="position()!=last()">
       
   230           <xsl:text>,</xsl:text>
       
   231         </xsl:if>
       
   232         <xsl:text>
       
   233 </xsl:text>
       
   234       </xsl:for-each>
   210       <xsl:text>    ]
   235       <xsl:text>    ]
   211 </xsl:text>
   236 </xsl:text>
   212       <xsl:text>}</xsl:text>
   237       <xsl:text>}</xsl:text>
   213       <xsl:if test="position()!=last()">
   238       <xsl:if test="position()!=last()">
   214         <xsl:text>,</xsl:text>
   239         <xsl:text>,</xsl:text>
   218     </xsl:for-each>
   243     </xsl:for-each>
   219     <xsl:text>}
   244     <xsl:text>}
   220 </xsl:text>
   245 </xsl:text>
   221     <xsl:text>
   246     <xsl:text>
   222 </xsl:text>
   247 </xsl:text>
       
   248     <xsl:text>var hmi_index = [
       
   249 </xsl:text>
       
   250     <xsl:variable name="svg" select="/"/>
       
   251     <xsl:for-each select="$indexed_hmitree/*">
       
   252       <xsl:text>{   /* </xsl:text>
       
   253       <xsl:value-of select="@index"/>
       
   254       <xsl:text>  </xsl:text>
       
   255       <xsl:value-of select="@hmipath"/>
       
   256       <xsl:text> */
       
   257 </xsl:text>
       
   258       <xsl:text>    type: "</xsl:text>
       
   259       <xsl:value-of select="local-name()"/>
       
   260       <xsl:text>",
       
   261 </xsl:text>
       
   262       <xsl:text>    ids: [
       
   263 </xsl:text>
       
   264       <xsl:variable name="hmipath" select="@hmipath"/>
       
   265       <xsl:for-each select="$svg//*[substring-after(@inkscape:label,'@') = $hmipath]">
       
   266         <xsl:text>        hmi_widgets["</xsl:text>
       
   267         <xsl:value-of select="@id"/>
       
   268         <xsl:text>"]</xsl:text>
       
   269         <xsl:if test="position()!=last()">
       
   270           <xsl:text>,</xsl:text>
       
   271         </xsl:if>
       
   272         <xsl:text>
       
   273 </xsl:text>
       
   274       </xsl:for-each>
       
   275       <xsl:text>    ]
       
   276 </xsl:text>
       
   277       <xsl:text>}</xsl:text>
       
   278       <xsl:if test="position()!=last()">
       
   279         <xsl:text>,</xsl:text>
       
   280       </xsl:if>
       
   281       <xsl:text>
       
   282 </xsl:text>
       
   283     </xsl:for-each>
       
   284     <xsl:text>]
       
   285 </xsl:text>
       
   286     <xsl:text>
       
   287 </xsl:text>
   223     <xsl:text>var page_desc = {
   288     <xsl:text>var page_desc = {
   224 </xsl:text>
   289 </xsl:text>
   225     <xsl:for-each select="$hmi_pages">
   290     <xsl:for-each select="$hmi_pages">
   226       <xsl:variable name="desc" select="func:parselabel(@inkscape:label)/widget"/>
   291       <xsl:variable name="desc" select="func:parselabel(@inkscape:label)/widget"/>
       
   292       <xsl:variable name="page" select="."/>
       
   293       <xsl:variable name="p" select="$hmi_geometry[@Id = $page/@id]"/>
       
   294       <xsl:variable name="page_ids" select="$hmi_geometry[@Id != $page/@id and &#10;                                @x &gt;= $p/@x and @y &gt;= $p/@y and &#10;                                @x+@w &lt;= $p/@x+$p/@w and @y+@h &lt;= $p/@y+$p/@h]/@Id"/>
       
   295       <xsl:variable name="page_elements" select="$hmi_elements[@id = $page_ids]"/>
   227       <xsl:text>    "</xsl:text>
   296       <xsl:text>    "</xsl:text>
   228       <xsl:value-of select="$desc/arg[1]/@value"/>
   297       <xsl:value-of select="$desc/arg[1]/@value"/>
   229       <xsl:text>": {
   298       <xsl:text>": {
   230 </xsl:text>
   299 </xsl:text>
   231       <xsl:text>        id: "</xsl:text>
   300       <xsl:text>        id: "</xsl:text>
   232       <xsl:value-of select="@id"/>
   301       <xsl:value-of select="@id"/>
   233       <xsl:text>",
   302       <xsl:text>",
   234 </xsl:text>
   303 </xsl:text>
   235       <xsl:text>        widgets: [
   304       <xsl:text>        widgets: [
   236 </xsl:text>
   305 </xsl:text>
   237       <xsl:variable name="page" select="."/>
   306       <xsl:for-each select="$page_ids">
   238       <xsl:variable name="p" select="$hmi_geometry[@Id = $page/@id]"/>
       
   239       <xsl:for-each select="$hmi_geometry[@Id != $page/@id and &#10;                       @x &gt;= $p/@x and @y &gt;= $p/@y and &#10;                       @x+@w &lt;= $p/@x+$p/@w and @y+@h &lt;= $p/@y+$p/@h]">
       
   240         <xsl:text>            "</xsl:text>
   307         <xsl:text>            "</xsl:text>
   241         <xsl:value-of select="@Id"/>
   308         <xsl:value-of select="."/>
   242         <xsl:text>"</xsl:text>
   309         <xsl:text>"</xsl:text>
   243         <xsl:if test="position()!=last()">
   310         <xsl:if test="position()!=last()">
   244           <xsl:text>,</xsl:text>
   311           <xsl:text>,</xsl:text>
   245         </xsl:if>
   312         </xsl:if>
   246         <xsl:text>
   313         <xsl:text>
   247 </xsl:text>
   314 </xsl:text>
   248       </xsl:for-each>
   315       </xsl:for-each>
   249       <xsl:text>        ]
   316       <xsl:text>        ]
   250 </xsl:text>
   317 </xsl:text>
       
   318       <xsl:text>        subscriptions: [
       
   319 </xsl:text>
       
   320       <xsl:for-each select="$page_elements">
       
   321         <xsl:variable name="hmipaths" select="func:parselabel(@inkscape:label)/widget/path/@value"/>
       
   322         <xsl:variable name="notlast" select="position()!=last()"/>
       
   323         <xsl:for-each select="$hmipaths">
       
   324           <xsl:variable name="hmipath" select="."/>
       
   325           <xsl:text>            </xsl:text>
       
   326           <xsl:value-of select="$indexed_hmitree/*[@hmipath = $hmipath]/@index"/>
       
   327           <xsl:if test="$notlast or position()!=last()">
       
   328             <xsl:text>,</xsl:text>
       
   329           </xsl:if>
       
   330           <xsl:text>
       
   331 </xsl:text>
       
   332         </xsl:for-each>
       
   333       </xsl:for-each>
       
   334       <xsl:text>        ]
       
   335 </xsl:text>
       
   336       <xsl:text>    }</xsl:text>
       
   337       <xsl:if test="position()!=last()">
       
   338         <xsl:text>,</xsl:text>
       
   339       </xsl:if>
       
   340       <xsl:text>
       
   341 </xsl:text>
   251     </xsl:for-each>
   342     </xsl:for-each>
   252     <xsl:text>}
   343     <xsl:text>}
   253 </xsl:text>
   344 </xsl:text>
   254     <xsl:text>
   345     <xsl:text>
   255 </xsl:text>
   346 </xsl:text>
   319 </xsl:text>
   410 </xsl:text>
   320     <xsl:text>    const subscriptions = new Map();
   411     <xsl:text>    const subscriptions = new Map();
   321 </xsl:text>
   412 </xsl:text>
   322     <xsl:text>
   413     <xsl:text>
   323 </xsl:text>
   414 </xsl:text>
   324     <xsl:text>
       
   325 </xsl:text>
       
   326     <xsl:text>    // subscription state as needed by widget now
   415     <xsl:text>    // subscription state as needed by widget now
   327 </xsl:text>
   416 </xsl:text>
   328     <xsl:text>    // expected {index:[widgets]};
   417     <xsl:text>    // expected {index:[widgets]};
   329 </xsl:text>
   418 </xsl:text>
   330     <xsl:text>    var subscribers = {};
   419     <xsl:text>    var subscribers = new Map();
   331 </xsl:text>
   420 </xsl:text>
   332     <xsl:text>
   421     <xsl:text>
   333 </xsl:text>
   422 </xsl:text>
   334     <xsl:text>    // return the diff in between curently subscribed and subscription
   423     <xsl:text>    // return the diff in between curently subscribed and subscription
   335 </xsl:text>
   424 </xsl:text>
   336     <xsl:text>    function update_subscriptions() {
   425     <xsl:text>    function update_subscriptions() {
   337 </xsl:text>
   426 </xsl:text>
   338     <xsl:text>        let delta = [];
   427     <xsl:text>        let delta = [];
   339 </xsl:text>
   428 </xsl:text>
   340     <xsl:text>        Object.keys(subscribers).forEach(index =&gt; {
   429     <xsl:text>        for(let [index, widgets] in subscribers.entries()){
   341 </xsl:text>
   430 </xsl:text>
   342     <xsl:text>
   431     <xsl:text>
       
   432 </xsl:text>
       
   433     <xsl:text>            // periods are in ms
   343 </xsl:text>
   434 </xsl:text>
   344     <xsl:text>            let previous_period = subscriptions.get(index);
   435     <xsl:text>            let previous_period = subscriptions.get(index);
   345 </xsl:text>
   436 </xsl:text>
   346     <xsl:text>            delete subscriptions[index];
   437     <xsl:text>
   347 </xsl:text>
   438 </xsl:text>
   348     <xsl:text>
   439     <xsl:text>            let new_period = Math.min(...widgets.map(widget =&gt; 1000/widget.frequency));
   349 </xsl:text>
   440 </xsl:text>
   350     <xsl:text>            let new_period = Math.min(...widgets.map(widget =&gt; widget.period));
   441     <xsl:text>
   351 </xsl:text>
   442 </xsl:text>
   352     <xsl:text>
   443     <xsl:text>            if(previous_period != new_period) {
   353 </xsl:text>
   444 </xsl:text>
   354     <xsl:text>            if(previous_period != new_period) 
   445     <xsl:text>                subscriptions.set(index, new_period);
   355 </xsl:text>
   446 </xsl:text>
   356     <xsl:text>                delta.push({index: index, period: new_period});
   447     <xsl:text>                delta.push({index: index, period: new_period});
   357 </xsl:text>
   448 </xsl:text>
       
   449     <xsl:text>            }
       
   450 </xsl:text>
       
   451     <xsl:text>            
       
   452 </xsl:text>
   358     <xsl:text>        })
   453     <xsl:text>        })
   359 </xsl:text>
   454 </xsl:text>
   360     <xsl:text>        return result;
   455     <xsl:text>        return result;
   361 </xsl:text>
   456 </xsl:text>
   362     <xsl:text>    }
   457     <xsl:text>    }
   371 </xsl:text>
   466 </xsl:text>
   372     <xsl:text>    };
   467     <xsl:text>    };
   373 </xsl:text>
   468 </xsl:text>
   374     <xsl:text>
   469     <xsl:text>
   375 </xsl:text>
   470 </xsl:text>
       
   471     <xsl:text>    var current_page = default_page;
       
   472 </xsl:text>
       
   473     <xsl:text>
       
   474 </xsl:text>
   376     <xsl:text>    function switch_page(page_name) {
   475     <xsl:text>    function switch_page(page_name) {
       
   476 </xsl:text>
       
   477     <xsl:text>        let new_desc = page_desc[page_name];
       
   478 </xsl:text>
       
   479     <xsl:text>        let old_desc = page_desc[page_name];
       
   480 </xsl:text>
       
   481     <xsl:text>        /* TODO hide / show widgets */
       
   482 </xsl:text>
       
   483     <xsl:text>        /* TODO move viewport */
       
   484 </xsl:text>
       
   485     <xsl:text>
       
   486 </xsl:text>
       
   487     <xsl:text>        /* Update subscribers */
       
   488 </xsl:text>
       
   489     <xsl:text>        /* Update subscriptions */
       
   490 </xsl:text>
       
   491     <xsl:text>
   377 </xsl:text>
   492 </xsl:text>
   378     <xsl:text>
   493     <xsl:text>
   379 </xsl:text>
   494 </xsl:text>
   380     <xsl:text>    };
   495     <xsl:text>    };
   381 </xsl:text>
   496 </xsl:text>
   454       <xsl:with-param name="indent">
   569       <xsl:with-param name="indent">
   455         <xsl:value-of select="concat($indent,'&gt;')"/>
   570         <xsl:value-of select="concat($indent,'&gt;')"/>
   456       </xsl:with-param>
   571       </xsl:with-param>
   457     </xsl:apply-templates>
   572     </xsl:apply-templates>
   458   </xsl:template>
   573   </xsl:template>
       
   574   <xsl:template mode="refresh_frequency" match="widget">
       
   575     <xsl:text>10</xsl:text>
       
   576   </xsl:template>
       
   577   <xsl:template mode="refresh_frequency" match="widget[@type='Meter']">
       
   578     <xsl:text>10</xsl:text>
       
   579   </xsl:template>
       
   580   <xsl:template mode="refresh_frequency" match="widget[@type='Display']">
       
   581     <xsl:text>5</xsl:text>
       
   582   </xsl:template>
       
   583   <xsl:template mode="refresh_frequency" match="widget[@type='Input']">
       
   584     <xsl:text>5</xsl:text>
       
   585   </xsl:template>
   459 </xsl:stylesheet>
   586 </xsl:stylesheet>