Robert Carnecky 10 years ago
parent
commit
c76390b6c6
3 changed files with 91 additions and 34 deletions
  1. 77
    28
      generator/components/card-icon.html
  2. 13
    3
      generator/js/ui.ts
  3. 1
    3
      resources/custom-icons/mixed-swords.svg

+ 77
- 28
generator/components/card-icon.html View File

@@ -26,50 +26,99 @@
26 26
         var mainDoc = document;
27 27
         var importDoc = document.currentScript.ownerDocument;
28 28
 
29
-        var proto = Object.create(HTMLDivElement.prototype);
30
-        proto.loadSvg = function (src, callback) {
31
-            var isLocal = window.location.protocol === 'file:';
32
-            var httpRequest = new XMLHttpRequest();
29
+        var svgCache = {};
30
+
31
+        function loadSvgCached(src) {
32
+            if (src === undefined || src === null || src === "") {
33
+                return Promise.resolve(null);
34
+            } else if (svgCache[src]) {
35
+                return svgCache[src];
36
+            } else {
37
+                var promise = loadSvg(src);
38
+                svgCache[src] = promise;
39
+                return promise;
40
+            }
41
+        }
42
+        function loadSvg(src) {
43
+            console.log("Loading " + src);
44
+            return new Promise(function(resolve, reject) {
33 45
 
34
-            httpRequest.onreadystatechange = function () {
35
-                if (httpRequest.readyState === 4) {
46
+                var httpRequest = new XMLHttpRequest();
36 47
 
37
-                    if (httpRequest.status === 404 || httpRequest.responseXML === null) {
38
-                        throw new Error("Error loading SVG");
39
-                    }
48
+                httpRequest.onreadystatechange = function () {
49
+                    if (httpRequest.readyState === 4) {
50
+
51
+                        if (httpRequest.status === 404 || httpRequest.responseXML === null) {
52
+                            reject(Error("Error loading SVG from " + src));
53
+                        }
40 54
 
41
-                    if (httpRequest.status === 200 || (isLocal && httpRequest.status === 0)) {
42
-                        if (httpRequest.responseXML instanceof Document) {
43
-                            callback(httpRequest.responseXML.children[0]);
55
+                        if (httpRequest.status === 200) {
56
+                            if (httpRequest.responseXML instanceof Document) {
57
+                                var svg = httpRequest.responseXML.children[0];
58
+                                cleanSvg(svg);
59
+                                resolve(svg);
60
+                            }
44 61
                         }
45 62
                     }
46 63
                 }
64
+
65
+                httpRequest.open('GET', src);
66
+                if (httpRequest.overrideMimeType) httpRequest.overrideMimeType('text/xml');
67
+                httpRequest.send();
68
+            });
69
+        }
70
+        function cleanSvg(svg) {
71
+            // Remove all whitespace
72
+            for (var i = 0; i < svg.childNodes.length; ++i) {
73
+                var child = svg.childNodes[i];
74
+                if (child.nodeName == "#text") {
75
+                    svg.removeChild(child);
76
+                }
77
+            }
78
+
79
+            // Keep only the last path
80
+            while (svg.childNodes.length > 1) {
81
+                svg.removeChild(svg.firstChild);
47 82
             }
48 83
 
49
-            httpRequest.open('GET', src);
50
-            if (httpRequest.overrideMimeType) httpRequest.overrideMimeType('text/xml');
51
-            httpRequest.send();
84
+            // Remove the fill of the remaining paths
85
+            var path = svg.firstChild;
86
+            if (path) {
87
+                path.style.fill = "";
88
+            }
89
+
90
+            return svg;
91
+        }
92
+
93
+
94
+        var proto = Object.create(HTMLDivElement.prototype);
95
+        proto.setSvg = function (svg) {
96
+            var root = this.shadowRoot;
97
+            var container = root.getElementById("icon-container");
98
+
99
+            // Remove the previous SVG
100
+            while (container.firstChild) {
101
+                container.removeChild(container.firstChild);
102
+            }
103
+
104
+            // Replace it with the new one
105
+            if (svg !== null) {
106
+                container.appendChild(svg.cloneNode(true));
107
+            }
52 108
         }
53 109
         Object.defineProperty(proto, "src", {
54 110
             get: function() { 
55 111
                 return "";
56 112
             },
57 113
             set: function (value) {
114
+                console.log("Property src=" + value);
58 115
                 this.setAttribute('src', value);
59 116
 
60
-                var root = this.shadowRoot;
61
-                var container = root.getElementById("icon-container");
62
-                while (container.firstChild) {
63
-                    container.removeChild(container.firstChild);
64
-                }
65
-                this.loadSvg(value, function (svg) {
66
-                    container.appendChild(svg);
67
-
68
-                    // game-icons.net specific modifications
69
-                    var firstPath = root.querySelector("svg path");
70
-                    svg.removeChild(firstPath);
71
-                    var secondPath = root.querySelector("svg path");
72
-                    secondPath.style.fill = "";
117
+                var element = this;
118
+                loadSvgCached(value).then(function (svg) {
119
+                    element.setSvg(svg);
120
+                }, function (error) {
121
+                    // console.log(error);
73 122
                 });
74 123
             }
75 124
         });

+ 13
- 3
generator/js/ui.ts View File

@@ -13,6 +13,7 @@ module RpgCardsUI {
13 13
     var cardGenerator: RpgCards.CardHtmlGenerator = null;
14 14
     var pageGenerator: RpgCards.PageHtmlGenerator = null;
15 15
     var editor: AceAjax.Editor;
16
+    var update_in_progress: boolean = false;
16 17
 
17 18
     // ============================================================================
18 19
     // Seleted card
@@ -35,12 +36,16 @@ module RpgCardsUI {
35 36
         var size = deck.cards.length;
36 37
         if (size === 0) {
37 38
             $("#selected-card").val("");
39
+            update_selected_card();
38 40
         } else {
41
+
39 42
             index = Math.min(size - 1, index);
40 43
             index = Math.max(0, index);
41
-            $("#selected-card").val("" + index);
44
+            if (index != selected_card_index()) {
45
+                $("#selected-card").val("" + index);
46
+                update_selected_card();
47
+            }
42 48
         }
43
-        update_selected_card();
44 49
     }
45 50
 
46 51
     function select_first_card() {
@@ -69,6 +74,10 @@ module RpgCardsUI {
69 74
     // ============================================================================
70 75
 
71 76
     function render_selected_card() {
77
+        if (update_in_progress) {
78
+            return;
79
+        }
80
+
72 81
         var card = selected_card();
73 82
         $('#preview-container').empty();
74 83
         if (card) {
@@ -79,6 +88,7 @@ module RpgCardsUI {
79 88
     }
80 89
 
81 90
     function update_selected_card() {
91
+        update_in_progress = true;
82 92
         var card = selected_card();
83 93
         if (card) {
84 94
             $("#card-title").val(card.title);
@@ -103,6 +113,7 @@ module RpgCardsUI {
103 113
             $("#card-tags").val("");
104 114
             $("#card-color").val("").change();
105 115
         }
116
+        update_in_progress = false;
106 117
 
107 118
         render_selected_card();
108 119
     }
@@ -232,7 +243,6 @@ module RpgCardsUI {
232 243
     }
233 244
 
234 245
     function on_change_card_contents() {
235
-        // var value = $(this).val();
236 246
         var value = editor.getValue();
237 247
 
238 248
         var card = selected_card();

+ 1
- 3
resources/custom-icons/mixed-swords.svg View File

@@ -1,4 +1,2 @@
1
-<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
2
-  <path d="M0 0h512v512H0z"/>
3
-  <path d="M 15.517578 11.044922 C 25.0396 48.151325 39.984656 85.580645 57.109375 123.20703 C 74.605375 91.610028 98.321874 70.250328 128.67188 52.736328 C 90.863478 35.9246 53.13698 21.619872 15.517578 11.044922 z M 485.39062 14.4375 C 396.62562 63.9105 321.51587 122.75005 249.42188 188.12305 C 249.19787 188.32605 248.99158 188.54505 248.76758 188.74805 C 244.05947 193.02108 239.36424 197.32791 234.67773 201.65625 L 258.39844 224.97656 L 343.04492 140.34375 L 356.26562 153.5293 L 271.7207 238.07617 L 296.66602 262.60156 C 369.46936 186.63677 435.62047 108.30513 485.39062 14.4375 z M 104.66016 92.357422 C 101.08416 95.564422 97.680547 98.926606 94.435547 102.47461 L 460.67773 468.71875 L 471.21875 458.45898 L 104.66016 92.359375 L 104.66016 92.357422 z M 201.94336 232.4375 C 174.28427 258.86591 146.71758 286.17705 118.64258 314.21875 C 103.58558 295.64075 90.286875 275.478 79.171875 253.625 L 50.642578 282.1543 L 119.57812 351.09375 C 93.288124 394.76375 56.016875 427.47575 13.546875 454.96875 L 41.611328 483.0293 C 69.419328 440.8653 103.36808 404.85475 145.70508 377.21875 L 214.42188 445.93555 L 242.95508 417.40625 C 221.99108 405.29425 201.87358 391.90675 183.01758 377.09375 C 205.38258 354.76675 227.59488 332.77375 249.42188 310.84375 L 249.48438 310.90625 L 253.11133 307.15625 C 257.21027 303.03001 261.27866 298.90444 265.34766 294.77734 L 239.9707 269.82617 L 155.89062 353.90625 C 151.32563 349.66725 146.84633 345.31605 142.48633 340.87305 L 226.64648 256.72461 L 201.94336 232.4375 z M 417.36523 326.64844 L 417.36328 326.65039 L 365.69141 326.65039 L 433.80273 394.67578 L 485.39062 394.67578 L 417.36523 326.64844 z M 329.01367 363.48438 L 329.01367 415.00391 L 397.03906 483.02734 L 397.04297 431.50781 L 329.01367 363.48438 z " fill="#fff"></path>
1
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M0 0h512v512H0z"/><path d="M 15.517578 11.044922 C 25.0396 48.151325 39.984656 85.580645 57.109375 123.20703 C 74.605375 91.610028 98.321874 70.250328 128.67188 52.736328 C 90.863478 35.9246 53.13698 21.619872 15.517578 11.044922 z M 485.39062 14.4375 C 396.62562 63.9105 321.51587 122.75005 249.42188 188.12305 C 249.19787 188.32605 248.99158 188.54505 248.76758 188.74805 C 244.05947 193.02108 239.36424 197.32791 234.67773 201.65625 L 258.39844 224.97656 L 343.04492 140.34375 L 356.26562 153.5293 L 271.7207 238.07617 L 296.66602 262.60156 C 369.46936 186.63677 435.62047 108.30513 485.39062 14.4375 z M 104.66016 92.357422 C 101.08416 95.564422 97.680547 98.926606 94.435547 102.47461 L 460.67773 468.71875 L 471.21875 458.45898 L 104.66016 92.359375 L 104.66016 92.357422 z M 201.94336 232.4375 C 174.28427 258.86591 146.71758 286.17705 118.64258 314.21875 C 103.58558 295.64075 90.286875 275.478 79.171875 253.625 L 50.642578 282.1543 L 119.57812 351.09375 C 93.288124 394.76375 56.016875 427.47575 13.546875 454.96875 L 41.611328 483.0293 C 69.419328 440.8653 103.36808 404.85475 145.70508 377.21875 L 214.42188 445.93555 L 242.95508 417.40625 C 221.99108 405.29425 201.87358 391.90675 183.01758 377.09375 C 205.38258 354.76675 227.59488 332.77375 249.42188 310.84375 L 249.48438 310.90625 L 253.11133 307.15625 C 257.21027 303.03001 261.27866 298.90444 265.34766 294.77734 L 239.9707 269.82617 L 155.89062 353.90625 C 151.32563 349.66725 146.84633 345.31605 142.48633 340.87305 L 226.64648 256.72461 L 201.94336 232.4375 z M 417.36523 326.64844 L 417.36328 326.65039 L 365.69141 326.65039 L 433.80273 394.67578 L 485.39062 394.67578 L 417.36523 326.64844 z M 329.01367 363.48438 L 329.01367 415.00391 L 397.03906 483.02734 L 397.04297 431.50781 L 329.01367 363.48438 z " fill="#fff"></path>
4 2
 </svg>

Loading…
Cancel
Save