misc/flags_js.xhtml
changeset 16007 20adaa127663
parent 16006 f81fe8250ed9
equal deleted inserted replaced
16006:f81fe8250ed9 16007:20adaa127663
    54 }
    54 }
    55     </style>
    55     </style>
    56     <script type="application/ecmascript">
    56     <script type="application/ecmascript">
    57 //<![CDATA[
    57 //<![CDATA[
    58 "use strict";
    58 "use strict";
    59 var IS_LOCAL=false; // set to true to fetch flags locally. Useful for testing.
    59 let IS_LOCAL=false; // set to true to fetch flags locally. Useful for testing.
    60 var flags;
    60 let flags;
    61 if (IS_LOCAL) {
    61 if (IS_LOCAL) {
    62 /* JavaScript version of a sprite sheet - this could be pretty trivially done in pure HTML, but maintenance
    62 /* JavaScript version of a sprite sheet - this could be pretty trivially done in pure HTML, but maintenance
    63 would be easier with a server-side portion. list of sprites could be gotten from server, but would require XSS whitelisting */
    63 would be easier with a server-side portion. list of sprites could be gotten from server, but would require XSS whitelisting */
    64 // Last updated: 1.0.0
    64 // Last updated: 1.0.0
    65 flags=["afghanistan","albania","algeria","american_samoa","andorra","angola","anguilla","antigua_and_barbuda","arabemirates",
    65 flags=["afghanistan","albania","algeria","american_samoa","andorra","angola","anguilla","antigua_and_barbuda","arabemirates",
    96 else
    96 else
    97 {
    97 {
    98 flags = [];
    98 flags = [];
    99 }
    99 }
   100 
   100 
   101 var on_xml_loaded = function(ex)
   101 let on_xml_loaded = function(ex)
   102 {
   102 {
   103     var resp = this.responseText;
   103     let resp = this.responseText;
   104     var r = />([^<]*).png</g;
   104     let r = />([^<]*).png</g;
   105     var x;
   105     let x;
   106     while(x = r.exec(resp))
   106     while(x = r.exec(resp))
   107     {
   107     {
   108         flags.push(x[1]);
   108         flags.push(x[1]);
   109     }
   109     }
   110     on_flags_loaded();
   110     on_flags_loaded();
   111 }
   111 }
   112 
   112 
   113 var on_xml_error = function()
   113 let on_xml_error = function()
   114 {
   114 {
   115     var p = document.createElement("p");
   115     let p = document.createElement("p");
   116     p.appendChild(document.createTextNode("ERROR: List of flags could not be fetched from the server!"));
   116     p.appendChild(document.createTextNode("ERROR: List of flags could not be fetched from the server!"));
   117     document.body.appendChild(p);
   117     document.body.appendChild(p);
   118 }
   118 }
   119 
   119 
   120 var on_flags_loaded;
   120 let on_flags_loaded;
   121 
   121 
   122 window.onload = function()
   122 window.onload = function()
   123 {
   123 {
   124     // Load list of flags
   124     // Load list of flags
   125     if (!IS_LOCAL) {
   125     if (!IS_LOCAL) {
   126         // Request list of flags from repository URL
   126         // Request list of flags from repository URL
   127         var xml=new XMLHttpRequest();
   127         let xml=new XMLHttpRequest();
   128         xml.open("GET", "//hg.hedgewars.org/hedgewars/file/tip/share/hedgewars/Data/Graphics/Flags/");
   128         xml.open("GET", "//hg.hedgewars.org/hedgewars/file/tip/share/hedgewars/Data/Graphics/Flags/");
   129         xml.addEventListener("error", on_xml_error);
   129         xml.addEventListener("error", on_xml_error);
   130         xml.onload = on_xml_loaded;
   130         xml.onload = on_xml_loaded;
   131         xml.send();
   131         xml.send();
   132     }
   132     }
   137 }
   137 }
   138 
   138 
   139 on_flags_loaded = function()
   139 on_flags_loaded = function()
   140 {
   140 {
   141     // Sort flags
   141     // Sort flags
   142     var flag_compare = function(a, b)
   142     let flag_compare = function(a, b)
   143     {
   143     {
   144         if (a === "hedgewars")
   144         if (a === "hedgewars")
   145             return -1;
   145             return -1;
   146         else if (b === "hedgewars")
   146         else if (b === "hedgewars")
   147             return 1;
   147             return 1;
   158     }
   158     }
   159 
   159 
   160     flags.sort(flag_compare);
   160     flags.sort(flag_compare);
   161 
   161 
   162     // Render flags
   162     // Render flags
   163     var img;
   163     let img;
   164     var j = 0;
   164     let j = 0;
   165     var toDelete = [];
   165     let toDelete = [];
   166     var a = document.createElement("a");
   166     let a = document.createElement("a");
   167     a.className="flag";
   167     a.className="flag";
   168     a.appendChild(document.createElement("div"));
   168     a.appendChild(document.createElement("div"));
   169     a.lastChild.appendChild(document.createTextNode(""));
   169     a.lastChild.appendChild(document.createTextNode(""));
   170 
   170 
   171     var flagState = 0; // 0 = hedgewars, 1 = country flag, 2 = community flag ("cm_")
   171     let flagState = 0; // 0 = hedgewars, 1 = country flag, 2 = community flag ("cm_")
   172 
   172 
   173     for (var i=0;i<flags.length;i++)
   173     for (let i=0;i<flags.length;i++)
   174     {
   174     {
   175         var flag = flags[i];
   175         let flag = flags[i];
   176         // Skip CPU flags
   176         // Skip CPU flags
   177         if (flag === "cpu" || flag === "cpu_plain")
   177         if (flag === "cpu" || flag === "cpu_plain")
   178             continue;
   178             continue;
   179 
   179 
   180         var oldFlagState = flagState;
   180         let oldFlagState = flagState;
   181         if (flagState === 0 && flag !== "hedgewars")
   181         if (flagState === 0 && flag !== "hedgewars")
   182             flagState++;
   182             flagState++;
   183         else if (flagState === 1 && flag.startsWith("cm_"))
   183         else if (flagState === 1 && flag.startsWith("cm_"))
   184             flagState++;
   184             flagState++;
   185         if (flagState !== oldFlagState)
   185         if (flagState !== oldFlagState)
   188             document.body.appendChild(document.createElement("br"));
   188             document.body.appendChild(document.createElement("br"));
   189             document.body.appendChild(document.createElement("br"));
   189             document.body.appendChild(document.createElement("br"));
   190             document.body.appendChild(document.createElement("br"));
   190             document.body.appendChild(document.createElement("br"));
   191         }
   191         }
   192 
   192 
   193         var h = document.body.appendChild(a.cloneNode(true));
   193         let h = document.body.appendChild(a.cloneNode(true));
   194         if (IS_LOCAL)
   194         if (IS_LOCAL)
   195             h.href = "../share/hedgewars/Data/Graphics/Flags/"+flag+".png";
   195             h.href = "../share/hedgewars/Data/Graphics/Flags/"+flag+".png";
   196         else
   196         else
   197             h.href = "//hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Graphics/Flags/"+flag+".png";
   197             h.href = "//hg.hedgewars.org/hedgewars/raw-file/tip/share/hedgewars/Data/Graphics/Flags/"+flag+".png";
   198 
   198 
   199         img = new Image();
   199         img = new Image();
   200         img.onload = function() {
   200         img.onload = function() {
   201             var name = this.id.substr(7);
   201             let name = this.id.substr(7);
   202             if (this.height === 32) {
   202             if (this.height === 32) {
   203                 staticMasks[name] = true;
   203                 staticMasks[name] = true;
   204             }
   204             }
   205             this.remove();
   205             this.remove();
   206         }
   206         }