svghmi/gen_index_xhtml.xslt
branchsvghmi
changeset 2808 dc78ffa5253d
parent 2807 7fa21b3b5f9f
child 2810 63b9a37b73c7
equal deleted inserted replaced
2807:7fa21b3b5f9f 2808:dc78ffa5253d
     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:xhtml="http://www.w3.org/1999/xhtml" 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:xhtml="http://www.w3.org/1999/xhtml" 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="xhtml:script"/>
     3   <xsl:output method="xml" cdata-section-elements="xhtml: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="svg_root_id" select="/svg:svg/@id"/>
     6   <xsl:variable name="hmi_elements" select="//svg:*[starts-with(@inkscape:label, 'HMI:')]"/>
     7   <xsl:variable name="hmi_elements" select="//svg:*[starts-with(@inkscape:label, 'HMI:')]"/>
     7   <xsl:variable name="hmi_geometry" select="$geometry[@Id = $hmi_elements/@id]"/>
     8   <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']"/>
     9   <xsl:variable name="hmi_pages" select="$hmi_elements[func:parselabel(@inkscape:label)/widget/@type = 'Page']"/>
     9   <xsl:variable name="default_page">
    10   <xsl:variable name="default_page">
    10     <xsl:choose>
    11     <xsl:choose>
    95       <xsl:with-param name="parentpath">
    96       <xsl:with-param name="parentpath">
    96         <xsl:value-of select="$parentpath"/>
    97         <xsl:value-of select="$parentpath"/>
    97       </xsl:with-param>
    98       </xsl:with-param>
    98     </xsl:apply-templates>
    99     </xsl:apply-templates>
    99   </xsl:template>
   100   </xsl:template>
   100   <xsl:template mode="identity_svg" match="@* | node()">
   101   <xsl:template mode="inline_svg" match="@* | node()">
   101     <xsl:copy>
   102     <xsl:copy>
   102       <xsl:apply-templates mode="identity_svg" select="@* | node()"/>
   103       <xsl:apply-templates mode="inline_svg" select="@* | node()"/>
       
   104     </xsl:copy>
       
   105   </xsl:template>
       
   106   <xsl:template mode="inline_svg" match="svg:svg/@width"/>
       
   107   <xsl:template mode="inline_svg" match="svg:svg/@height"/>
       
   108   <xsl:template mode="inline_svg" match="svg:svg">
       
   109     <xsl:copy>
       
   110       <xsl:attribute name="preserveAspectRatio">
       
   111         <xsl:text>none</xsl:text>
       
   112       </xsl:attribute>
       
   113       <xsl:attribute name="height">
       
   114         <xsl:text>100vh</xsl:text>
       
   115       </xsl:attribute>
       
   116       <xsl:attribute name="width">
       
   117         <xsl:text>100vw</xsl:text>
       
   118       </xsl:attribute>
       
   119       <xsl:apply-templates mode="inline_svg" select="@* | node()"/>
   103     </xsl:copy>
   120     </xsl:copy>
   104   </xsl:template>
   121   </xsl:template>
   105   <xsl:template match="/">
   122   <xsl:template match="/">
   106     <xsl:comment>
   123     <xsl:comment>
   107       <xsl:text>Made with SVGHMI. https://beremiz.org</xsl:text>
   124       <xsl:text>Made with SVGHMI. https://beremiz.org</xsl:text>
   108     </xsl:comment>
   125     </xsl:comment>
   109     <html xmlns="http://www.w3.org/1999/xhtml">
   126     <html xmlns="http://www.w3.org/1999/xhtml">
   110       <head/>
   127       <head/>
   111       <body style="margin:0;">
   128       <body style="margin:0;overflow:hidden;">
   112         <xsl:copy>
   129         <xsl:apply-templates mode="inline_svg" select="svg:svg"/>
   113           <xsl:comment>
       
   114             <xsl:apply-templates mode="testgeo" select="$hmi_geometry"/>
       
   115           </xsl:comment>
       
   116           <xsl:comment>
       
   117             <xsl:apply-templates mode="testtree" select="$hmitree"/>
       
   118           </xsl:comment>
       
   119           <xsl:comment>
       
   120             <xsl:apply-templates mode="testtree" select="$indexed_hmitree"/>
       
   121           </xsl:comment>
       
   122           <xsl:apply-templates mode="identity_svg" select="@* | node()"/>
       
   123         </xsl:copy>
       
   124         <script>
   130         <script>
   125           <xsl:call-template name="scripts"/>
   131           <xsl:call-template name="scripts"/>
   126         </script>
   132         </script>
   127       </body>
   133       </body>
   128     </html>
   134     </html>
   286 </xsl:text>
   292 </xsl:text>
   287       <xsl:text>        id: "</xsl:text>
   293       <xsl:text>        id: "</xsl:text>
   288       <xsl:value-of select="@id"/>
   294       <xsl:value-of select="@id"/>
   289       <xsl:text>",
   295       <xsl:text>",
   290 </xsl:text>
   296 </xsl:text>
       
   297       <xsl:text>        bbox: [</xsl:text>
       
   298       <xsl:value-of select="$p/@x"/>
       
   299       <xsl:text>, </xsl:text>
       
   300       <xsl:value-of select="$p/@y"/>
       
   301       <xsl:text>, </xsl:text>
       
   302       <xsl:value-of select="$p/@w"/>
       
   303       <xsl:text>, </xsl:text>
       
   304       <xsl:value-of select="$p/@h"/>
       
   305       <xsl:text>],
       
   306 </xsl:text>
   291       <xsl:text>        widgets: [
   307       <xsl:text>        widgets: [
   292 </xsl:text>
   308 </xsl:text>
   293       <xsl:for-each select="$page_ids">
   309       <xsl:for-each select="$page_ids">
   294         <xsl:text>            hmi_widgets.</xsl:text>
   310         <xsl:text>            hmi_widgets.</xsl:text>
   295         <xsl:value-of select="."/>
   311         <xsl:value-of select="."/>
   314 </xsl:text>
   330 </xsl:text>
   315     <xsl:text>var default_page = "</xsl:text>
   331     <xsl:text>var default_page = "</xsl:text>
   316     <xsl:value-of select="$default_page"/>
   332     <xsl:value-of select="$default_page"/>
   317     <xsl:text>";
   333     <xsl:text>";
   318 </xsl:text>
   334 </xsl:text>
       
   335     <xsl:text>var svg_root = document.getElementById("</xsl:text>
       
   336     <xsl:value-of select="$svg_root_id"/>
       
   337     <xsl:text>");
       
   338 </xsl:text>
   319     <xsl:text>// svghmi.js
   339     <xsl:text>// svghmi.js
   320 </xsl:text>
   340 </xsl:text>
   321     <xsl:text>
   341     <xsl:text>
   322 </xsl:text>
   342 </xsl:text>
   323     <xsl:text>var cache = hmitree_types.map(_ignored =&gt; undefined);
   343     <xsl:text>var cache = hmitree_types.map(_ignored =&gt; undefined);
   694 </xsl:text>
   714 </xsl:text>
   695     <xsl:text>        }
   715     <xsl:text>        }
   696 </xsl:text>
   716 </xsl:text>
   697     <xsl:text>    }
   717     <xsl:text>    }
   698 </xsl:text>
   718 </xsl:text>
   699     <xsl:text>    /* add new subsribers if any */
   719     <xsl:text>
   700 </xsl:text>
   720 </xsl:text>
   701     <xsl:text>    if(new_desc) for(let widget of new_desc.widgets){
   721     <xsl:text>    if(new_desc) {
   702 </xsl:text>
   722 </xsl:text>
   703     <xsl:text>        for(let index of widget.indexes){
   723     <xsl:text>        /* add new subsribers if any */
   704 </xsl:text>
   724 </xsl:text>
   705     <xsl:text>            subscribers[index].add(widget);
   725     <xsl:text>        for(let widget of new_desc.widgets){
       
   726 </xsl:text>
       
   727     <xsl:text>            for(let index of widget.indexes){
       
   728 </xsl:text>
       
   729     <xsl:text>                subscribers[index].add(widget);
       
   730 </xsl:text>
       
   731     <xsl:text>            }
   706 </xsl:text>
   732 </xsl:text>
   707     <xsl:text>        }
   733     <xsl:text>        }
   708 </xsl:text>
   734 </xsl:text>
       
   735     <xsl:text>        svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
       
   736 </xsl:text>
   709     <xsl:text>    }
   737     <xsl:text>    }
   710 </xsl:text>
       
   711     <xsl:text>
       
   712 </xsl:text>
   738 </xsl:text>
   713     <xsl:text>    current_page = page_name;
   739     <xsl:text>    current_page = page_name;
   714 </xsl:text>
   740 </xsl:text>
   715     <xsl:text>
   741     <xsl:text>
   716 </xsl:text>
   742 </xsl:text>
   827       <xsl:with-param name="indent">
   853       <xsl:with-param name="indent">
   828         <xsl:value-of select="concat($indent,'&gt;')"/>
   854         <xsl:value-of select="concat($indent,'&gt;')"/>
   829       </xsl:with-param>
   855       </xsl:with-param>
   830     </xsl:apply-templates>
   856     </xsl:apply-templates>
   831   </xsl:template>
   857   </xsl:template>
   832   <xsl:template mode="widget_defs" match="widget[@type='Display']">
   858   <xsl:template name="defs_by_labels">
       
   859     <xsl:param name="labels" select="''"/>
       
   860     <xsl:param name="mandatory" select="'yes'"/>
   833     <xsl:param name="hmi_element"/>
   861     <xsl:param name="hmi_element"/>
   834     <xsl:text>frequency: 5,
   862     <xsl:for-each select="str:split($labels)">
   835 </xsl:text>
       
   836     <xsl:text>dispatch: function(value) {
       
   837 </xsl:text>
       
   838     <xsl:choose>
       
   839       <xsl:when test="$hmi_element[self::svg:text]">
       
   840         <xsl:text>  this.element.textContent = String(value);
       
   841 </xsl:text>
       
   842       </xsl:when>
       
   843       <xsl:otherwise>
       
   844         <xsl:message terminate="yes">
       
   845           <xsl:text>Display widget as a group not implemented</xsl:text>
       
   846         </xsl:message>
       
   847       </xsl:otherwise>
       
   848     </xsl:choose>
       
   849     <xsl:text>},
       
   850 </xsl:text>
       
   851   </xsl:template>
       
   852   <xsl:template mode="widget_defs" match="widget[@type='Meter']">
       
   853     <xsl:param name="hmi_element"/>
       
   854     <xsl:text>frequency: 10,
       
   855 </xsl:text>
       
   856     <xsl:for-each select="str:split('value min max needle range')">
       
   857       <xsl:variable name="name" select="."/>
   863       <xsl:variable name="name" select="."/>
   858       <xsl:variable name="elt_id" select="$hmi_element//*[@inkscape:label=$name][1]/@id"/>
   864       <xsl:variable name="elt_id" select="$hmi_element//*[@inkscape:label=$name][1]/@id"/>
   859       <xsl:if test="not($elt_id)">
   865       <xsl:if test="$mandatory='yes' and not($elt_id)">
   860         <xsl:message terminate="yes">
   866         <xsl:message terminate="yes">
   861           <xsl:text>Meter widget must have a </xsl:text>
   867           <xsl:text>Meter widget must have a </xsl:text>
   862           <xsl:value-of select="$name"/>
   868           <xsl:value-of select="$name"/>
   863           <xsl:text> element</xsl:text>
   869           <xsl:text> element</xsl:text>
   864         </xsl:message>
   870         </xsl:message>
   867       <xsl:text>_elt: document.getElementById("</xsl:text>
   873       <xsl:text>_elt: document.getElementById("</xsl:text>
   868       <xsl:value-of select="$elt_id"/>
   874       <xsl:value-of select="$elt_id"/>
   869       <xsl:text>"),
   875       <xsl:text>"),
   870 </xsl:text>
   876 </xsl:text>
   871     </xsl:for-each>
   877     </xsl:for-each>
       
   878   </xsl:template>
       
   879   <xsl:template mode="widget_defs" match="widget[@type='Display']">
       
   880     <xsl:param name="hmi_element"/>
       
   881     <xsl:text>frequency: 5,
       
   882 </xsl:text>
       
   883     <xsl:text>dispatch: function(value) {
       
   884 </xsl:text>
       
   885     <xsl:choose>
       
   886       <xsl:when test="$hmi_element[self::svg:text]">
       
   887         <xsl:text>  this.element.textContent = String(value);
       
   888 </xsl:text>
       
   889       </xsl:when>
       
   890       <xsl:otherwise>
       
   891         <xsl:message terminate="yes">
       
   892           <xsl:text>Display widget as a group not implemented</xsl:text>
       
   893         </xsl:message>
       
   894       </xsl:otherwise>
       
   895     </xsl:choose>
       
   896     <xsl:text>},
       
   897 </xsl:text>
       
   898   </xsl:template>
       
   899   <xsl:template mode="widget_defs" match="widget[@type='Meter']">
       
   900     <xsl:param name="hmi_element"/>
       
   901     <xsl:text>frequency: 10,
       
   902 </xsl:text>
       
   903     <xsl:call-template name="defs_by_labels">
       
   904       <xsl:with-param name="hmi_element" select="$hmi_element"/>
       
   905       <xsl:with-param name="labels">
       
   906         <test>value min max needle range</test>
       
   907       </xsl:with-param>
       
   908     </xsl:call-template>
   872     <xsl:text>dispatch: function(value) {
   909     <xsl:text>dispatch: function(value) {
   873 </xsl:text>
   910 </xsl:text>
   874     <xsl:text>    this.value_elt.textContent = String(value);
   911     <xsl:text>    this.value_elt.textContent = String(value);
   875 </xsl:text>
   912 </xsl:text>
   876     <xsl:text>    let [min,max,totallength] = this.range;
   913     <xsl:text>    let [min,max,totallength] = this.range;
   898   </xsl:template>
   935   </xsl:template>
   899   <xsl:template mode="widget_defs" match="widget[@type='Input']">
   936   <xsl:template mode="widget_defs" match="widget[@type='Input']">
   900     <xsl:param name="hmi_element"/>
   937     <xsl:param name="hmi_element"/>
   901     <xsl:text>frequency: 5,
   938     <xsl:text>frequency: 5,
   902 </xsl:text>
   939 </xsl:text>
   903     <xsl:variable name="value_elt_id" select="$hmi_element//*[self::svg:text][@inkscape:label='value'][1]/@id"/>
   940     <xsl:call-template name="defs_by_labels">
   904     <xsl:if test="not($value_elt_id)">
   941       <xsl:with-param name="hmi_element" select="$hmi_element"/>
   905       <xsl:message terminate="yes">
   942       <xsl:with-param name="labels">
   906         <xsl:text>Input widget must have a text element</xsl:text>
   943         <test>value</test>
   907       </xsl:message>
   944       </xsl:with-param>
   908     </xsl:if>
   945     </xsl:call-template>
   909     <xsl:text>value_elt: document.getElementById("</xsl:text>
       
   910     <xsl:value-of select="$value_elt_id"/>
       
   911     <xsl:text>"),
       
   912 </xsl:text>
       
   913     <xsl:text>dispatch: function(value) {
   946     <xsl:text>dispatch: function(value) {
   914 </xsl:text>
   947 </xsl:text>
   915     <xsl:text>    this.value_elt.textContent = String(value);
   948     <xsl:text>    this.value_elt.textContent = String(value);
   916 </xsl:text>
   949 </xsl:text>
   917     <xsl:text>},
   950     <xsl:text>},
   953   </xsl:template>
   986   </xsl:template>
   954   <xsl:template mode="widget_defs" match="widget[@type='Change']">
   987   <xsl:template mode="widget_defs" match="widget[@type='Change']">
   955     <xsl:text>    frequency: 5,
   988     <xsl:text>    frequency: 5,
   956 </xsl:text>
   989 </xsl:text>
   957   </xsl:template>
   990   </xsl:template>
       
   991   <xsl:template mode="widget_defs" match="widget[@type='Jump']">
       
   992     <xsl:text>init: function() {
       
   993 </xsl:text>
       
   994     <xsl:text>    this.element.addEventListener(
       
   995 </xsl:text>
       
   996     <xsl:text>        "click", 
       
   997 </xsl:text>
       
   998     <xsl:text>        evt =&gt; switch_page(this.args[0]));
       
   999 </xsl:text>
       
  1000     <xsl:text>},
       
  1001 </xsl:text>
       
  1002   </xsl:template>
   958 </xsl:stylesheet>
  1003 </xsl:stylesheet>