svghmi/gen_index_xhtml.xslt
branchsvghmi
changeset 2842 2f73f001955a
parent 2839 f4dd234faa11
child 2843 94696b3f69fb
equal deleted inserted replaced
2841:7c6050cde9e3 2842:2f73f001955a
   100       <xsl:apply-templates mode="inline_svg" select="@* | node()"/>
   100       <xsl:apply-templates mode="inline_svg" select="@* | node()"/>
   101     </xsl:copy>
   101     </xsl:copy>
   102   </xsl:template>
   102   </xsl:template>
   103   <xsl:template mode="inline_svg" match="svg:svg/@width"/>
   103   <xsl:template mode="inline_svg" match="svg:svg/@width"/>
   104   <xsl:template mode="inline_svg" match="svg:svg/@height"/>
   104   <xsl:template mode="inline_svg" match="svg:svg/@height"/>
   105   <xsl:template mode="inline_svg" match="svg:svg">
   105   <xsl:template xmlns="http://www.w3.org/2000/svg" mode="inline_svg" match="svg:svg">
   106     <xsl:copy>
   106     <svg>
   107       <xsl:attribute name="preserveAspectRatio">
   107       <xsl:attribute name="preserveAspectRatio">
   108         <xsl:text>none</xsl:text>
   108         <xsl:text>none</xsl:text>
   109       </xsl:attribute>
   109       </xsl:attribute>
   110       <xsl:attribute name="height">
   110       <xsl:attribute name="height">
   111         <xsl:text>100vh</xsl:text>
   111         <xsl:text>100vh</xsl:text>
   112       </xsl:attribute>
   112       </xsl:attribute>
   113       <xsl:attribute name="width">
   113       <xsl:attribute name="width">
   114         <xsl:text>100vw</xsl:text>
   114         <xsl:text>100vw</xsl:text>
   115       </xsl:attribute>
   115       </xsl:attribute>
   116       <xsl:apply-templates mode="inline_svg" select="@* | node()"/>
   116       <xsl:apply-templates mode="inline_svg" select="@* | node()"/>
   117     </xsl:copy>
   117     </svg>
   118   </xsl:template>
   118   </xsl:template>
   119   <xsl:template mode="inline_svg" match="svg:svg[@viewBox!=concat('0 0 ', @width, ' ', @height)]">
   119   <xsl:template mode="inline_svg" match="svg:svg[@viewBox!=concat('0 0 ', @width, ' ', @height)]">
   120     <xsl:message terminate="yes">
   120     <xsl:message terminate="yes">
   121       <xsl:text>ViewBox settings other than X=0, Y=0 and Scale=1 are not supported</xsl:text>
   121       <xsl:text>ViewBox settings other than X=0, Y=0 and Scale=1 are not supported</xsl:text>
   122     </xsl:message>
   122     </xsl:message>
   123   </xsl:template>
   123   </xsl:template>
   124   <xsl:template mode="inline_svg" match="sodipodi:namedview[@units!='px' or @inkscape:document-units!='px']">
   124   <xsl:template mode="inline_svg" match="sodipodi:namedview[@units!='px' or @inkscape:document-units!='px']">
   125     <xsl:message terminate="yes">
   125     <xsl:message terminate="yes">
   126       <xsl:text>All units must be set to "px" in Inkscape's document properties</xsl:text>
   126       <xsl:text>All units must be set to "px" in Inkscape's document properties</xsl:text>
   127     </xsl:message>
   127     </xsl:message>
       
   128   </xsl:template>
       
   129   <xsl:template xmlns="http://www.w3.org/2000/svg" mode="inline_svg" match="svg:use">
       
   130     <g>
       
   131       <xsl:attribute name="style">
       
   132         <xsl:value-of select="@style"/>
       
   133       </xsl:attribute>
       
   134       <xsl:attribute name="transform">
       
   135         <xsl:value-of select="@transform"/>
       
   136       </xsl:attribute>
       
   137       <xsl:attribute name="id">
       
   138         <xsl:value-of select="@id"/>
       
   139       </xsl:attribute>
       
   140       <xsl:variable name="targetid" select="substring-after(@xlink:href,'#')"/>
       
   141       <xsl:apply-templates mode="unlink_clone" select="//svg:*[@id = $targetid]"/>
       
   142     </g>
       
   143   </xsl:template>
       
   144   <xsl:template xmlns="http://www.w3.org/2000/svg" mode="unlink_clone" match="@*">
       
   145     <xsl:copy/>
       
   146   </xsl:template>
       
   147   <xsl:template xmlns="http://www.w3.org/2000/svg" mode="unlink_clone" match="svg:*">
       
   148     <xsl:choose>
       
   149       <xsl:when test="@id = $hmi_elements/@id">
       
   150         <use>
       
   151           <xsl:attribute name="xlink:href">
       
   152             <xsl:value-of select="concat('#',@id)"/>
       
   153           </xsl:attribute>
       
   154         </use>
       
   155       </xsl:when>
       
   156       <xsl:otherwise>
       
   157         <xsl:copy>
       
   158           <xsl:apply-templates mode="unlink_clone" select="@* | node()"/>
       
   159         </xsl:copy>
       
   160       </xsl:otherwise>
       
   161     </xsl:choose>
   128   </xsl:template>
   162   </xsl:template>
   129   <xsl:template match="/">
   163   <xsl:template match="/">
   130     <xsl:comment>
   164     <xsl:comment>
   131       <xsl:text>Made with SVGHMI. https://beremiz.org</xsl:text>
   165       <xsl:text>Made with SVGHMI. https://beremiz.org</xsl:text>
   132     </xsl:comment>
   166     </xsl:comment>
   137       <xsl:apply-templates mode="testtree" select="$hmitree"/>
   171       <xsl:apply-templates mode="testtree" select="$hmitree"/>
   138     </xsl:comment>
   172     </xsl:comment>
   139     <xsl:comment>
   173     <xsl:comment>
   140       <xsl:apply-templates mode="testtree" select="$indexed_hmitree"/>
   174       <xsl:apply-templates mode="testtree" select="$indexed_hmitree"/>
   141     </xsl:comment>
   175     </xsl:comment>
   142     <html xmlns="http://www.w3.org/1999/xhtml">
   176     <html xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/1999/xhtml">
   143       <head/>
   177       <head/>
   144       <body style="margin:0;overflow:hidden;">
   178       <body style="margin:0;overflow:hidden;">
   145         <xsl:apply-templates mode="inline_svg" select="svg:svg"/>
   179         <xsl:apply-templates mode="inline_svg" select="svg:svg"/>
   146         <script>
   180         <script>
   147           <xsl:call-template name="scripts"/>
   181           <xsl:call-template name="scripts"/>
   331       <xsl:variable name="all_page_ids" select="$page_ids | $page_sub_ids[not(. = $page_ids)]"/>
   365       <xsl:variable name="all_page_ids" select="$page_ids | $page_sub_ids[not(. = $page_ids)]"/>
   332       <xsl:text>    "</xsl:text>
   366       <xsl:text>    "</xsl:text>
   333       <xsl:value-of select="$desc/arg[1]/@value"/>
   367       <xsl:value-of select="$desc/arg[1]/@value"/>
   334       <xsl:text>": {
   368       <xsl:text>": {
   335 </xsl:text>
   369 </xsl:text>
   336       <xsl:text>        id: "</xsl:text>
   370       <xsl:text>        widget: hmi_widgets["</xsl:text>
   337       <xsl:value-of select="@id"/>
   371       <xsl:value-of select="@id"/>
   338       <xsl:text>",
   372       <xsl:text>"],
   339 </xsl:text>
   373 </xsl:text>
   340       <xsl:text>        bbox: [</xsl:text>
   374       <xsl:text>        bbox: [</xsl:text>
   341       <xsl:value-of select="$p/@x"/>
   375       <xsl:value-of select="$p/@x"/>
   342       <xsl:text>, </xsl:text>
   376       <xsl:text>, </xsl:text>
   343       <xsl:value-of select="$p/@y"/>
   377       <xsl:value-of select="$p/@y"/>
   822 </xsl:text>
   856 </xsl:text>
   823     <xsl:text>    let old_desc = page_desc[current_page];
   857     <xsl:text>    let old_desc = page_desc[current_page];
   824 </xsl:text>
   858 </xsl:text>
   825     <xsl:text>    let new_desc = page_desc[page_name];
   859     <xsl:text>    let new_desc = page_desc[page_name];
   826 </xsl:text>
   860 </xsl:text>
   827     <xsl:text>    /* TODO hide / show widgets */
   861     <xsl:text>
       
   862 </xsl:text>
       
   863     <xsl:text>    if(new_desc == undefined){
       
   864 </xsl:text>
       
   865     <xsl:text>        return;
       
   866 </xsl:text>
       
   867     <xsl:text>    }
   828 </xsl:text>
   868 </xsl:text>
   829     <xsl:text>
   869     <xsl:text>
   830 </xsl:text>
   870 </xsl:text>
   831     <xsl:text>    /* remove subsribers of previous page if any */
   871     <xsl:text>    /* remove subsribers of previous page if any */
   832 </xsl:text>
   872 </xsl:text>
   833     <xsl:text>    if(old_desc) for(let widget of old_desc.widgets){
   873     <xsl:text>    if(old_desc){
       
   874 </xsl:text>
       
   875     <xsl:text>        for(let widget of old_desc.widgets){
       
   876 </xsl:text>
       
   877     <xsl:text>            for(let index of widget.indexes){
       
   878 </xsl:text>
       
   879     <xsl:text>                subscribers[index].delete(widget);
       
   880 </xsl:text>
       
   881     <xsl:text>            }
       
   882 </xsl:text>
       
   883     <xsl:text>        }
       
   884 </xsl:text>
       
   885     <xsl:text>        old_desc.widget.element.style.display = "none";
       
   886 </xsl:text>
       
   887     <xsl:text>    } else {
       
   888 </xsl:text>
       
   889     <xsl:text>        /* initial page switch : set everybody hidden */
       
   890 </xsl:text>
       
   891     <xsl:text>        for(let name in page_desc){
       
   892 </xsl:text>
       
   893     <xsl:text>            if(name != new_desc){
       
   894 </xsl:text>
       
   895     <xsl:text>                page_desc[name].widget.element.style.display = "none";
       
   896 </xsl:text>
       
   897     <xsl:text>            }
       
   898 </xsl:text>
       
   899     <xsl:text>        }
       
   900 </xsl:text>
       
   901     <xsl:text>    }
       
   902 </xsl:text>
       
   903     <xsl:text>
       
   904 </xsl:text>
       
   905     <xsl:text>    /* add new subsribers if any */
       
   906 </xsl:text>
       
   907     <xsl:text>    for(let widget of new_desc.widgets){
   834 </xsl:text>
   908 </xsl:text>
   835     <xsl:text>        for(let index of widget.indexes){
   909     <xsl:text>        for(let index of widget.indexes){
   836 </xsl:text>
   910 </xsl:text>
   837     <xsl:text>            subscribers[index].delete(widget);
   911     <xsl:text>            subscribers[index].add(widget);
       
   912 </xsl:text>
       
   913     <xsl:text>            /* dispatch current cache in newly opened page widgets */
       
   914 </xsl:text>
       
   915     <xsl:text>            let cached_val = cache[index];
       
   916 </xsl:text>
       
   917     <xsl:text>            if(cached_val != undefined)
       
   918 </xsl:text>
       
   919     <xsl:text>                dispatch_value_to_widget(widget, index, cached_val, cached_val);
   838 </xsl:text>
   920 </xsl:text>
   839     <xsl:text>        }
   921     <xsl:text>        }
   840 </xsl:text>
   922 </xsl:text>
   841     <xsl:text>    }
   923     <xsl:text>    }
   842 </xsl:text>
   924 </xsl:text>
   843     <xsl:text>
   925     <xsl:text>
   844 </xsl:text>
   926 </xsl:text>
   845     <xsl:text>    if(new_desc) {
   927     <xsl:text>    new_desc.widget.element.style.display = "inline";
   846 </xsl:text>
   928 </xsl:text>
   847     <xsl:text>        /* add new subsribers if any */
   929     <xsl:text>
   848 </xsl:text>
   930 </xsl:text>
   849     <xsl:text>        for(let widget of new_desc.widgets){
   931     <xsl:text>    svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
   850 </xsl:text>
       
   851     <xsl:text>            for(let index of widget.indexes){
       
   852 </xsl:text>
       
   853     <xsl:text>                subscribers[index].add(widget);
       
   854 </xsl:text>
       
   855     <xsl:text>                let cached_val = cache[index];
       
   856 </xsl:text>
       
   857     <xsl:text>                if(cached_val != undefined)
       
   858 </xsl:text>
       
   859     <xsl:text>                    dispatch_value_to_widget(widget, index, cached_val, cached_val);
       
   860 </xsl:text>
       
   861     <xsl:text>            }
       
   862 </xsl:text>
       
   863     <xsl:text>        }
       
   864 </xsl:text>
       
   865     <xsl:text>        svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
       
   866 </xsl:text>
       
   867     <xsl:text>        // TODO dispatch current cache in newly opened page
       
   868 </xsl:text>
       
   869     <xsl:text>    }
       
   870 </xsl:text>
   932 </xsl:text>
   871     <xsl:text>    current_page = page_name;
   933     <xsl:text>    current_page = page_name;
   872 </xsl:text>
   934 </xsl:text>
   873     <xsl:text>
   935     <xsl:text>
   874 </xsl:text>
   936 </xsl:text>