-
Notifications
You must be signed in to change notification settings - Fork 17
/
shieldcommon.js
350 lines (311 loc) · 11.9 KB
/
shieldcommon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* Copyright 2010-2021 Karl Wilcox, Mattias Basaglia
This file is part of the DrawShield.net heraldry image creation program
DrawShield is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DrawShield is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DrawShield. If not, see https://www.gnu.org/licenses/.
*/
// Javascript shield common bits
var xmlhttp;
var asText;
var useId;
var useHTMLId;
var shieldsize = 500;
var printable = false;
var shieldtarget = 'shieldimg';
var captiontarget = 'shieldcaption';
var tabletarget = 'resultstable';
var targetURL = '/include/shield/drawshield.php?';
var messageCallback;
function updateHTML() {
if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
var item = document.getElementById(useHTMLId);
while ( item.hasChildNodes() ) {
item.removeChild(item.firstChild);
}
if ( xmlhttp.responseXML == null ) {
errorText = document.createTextNode(xmlhttp.responseText);
errorPara = document.createElement('p');
errorPara.appendChild(errorText);
item.insertBefore(errorPara,null);
} else {
item.innerHTML = xmlhttp.responseText;
}
}
}
function updateSVG() {
if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
var shieldImg = document.getElementById(useId);
while ( shieldImg.hasChildNodes() ) {
shieldImg.removeChild(shieldImg.firstChild);
}
if ( xmlhttp.responseXML == null ) {
errorText = document.createTextNode(xmlhttp.responseText);
errorPara = document.createElement('p');
errorPara.appendChild(errorText);
shieldImg.insertBefore(errorPara,null);
} else {
if ( (navigator.userAgent.indexOf( "iPad" ) > 0) ||
(navigator.userAgent.indexOf( "iPod" ) > 0) ||
(navigator.userAgent.indexOf( "iPhone" ) > 0)
) {
var svg = document.importNode(xmlhttp.responseXML.firstChild, true);
} else if (navigator.userAgent.indexOf('Edge') > 0){
var svg = xmlhttp.responseXML.documentElement;
} else {
var svg = xmlhttp.responseXML.documentElement;
svg = cloneToDoc(svg);
//shieldImg.innerHTML = xmlhttp.responseText;
}
shieldImg.appendChild(svg);
}
asText = xmlhttp.responseText;
if (messageCallback != null) {
messageCallback(xmlhttp.responseXML);
}
}
}
function requestHTML(url,id) {
if (!xmlhttp) xmlhttp = new XMLHttpRequest();
if (!xmlhttp) return;
useHTMLId = id;
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = updateHTML;
xmlhttp.send(null);
}
function requestSVG(url,id,messageFunc) {
if (!xmlhttp) xmlhttp = new XMLHttpRequest();
if (!xmlhttp) return;
useId = id;
messageCallback = messageFunc;
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = updateSVG;
xmlhttp.send(null);
}
function saveshield(url) {
if ( typeof(url) !== 'undefined' && url !== null ) targetURL = url;
blazonText = document.getElementById('blazon').value;
window.location.replace( targetURL + 'asfile=1' + getOptions() + '&blazon=' + encodeURIComponent(blazonText));
}
// Function provided by http://stackoverflow.com/users/405017/phrogz
function cloneToDoc(node,doc){
if (!doc) doc=document;
var clone = doc.createElementNS(node.namespaceURI,node.nodeName);
for (var i=0,len=node.attributes.length;i<len;++i){
var a = node.attributes[i];
if (/^xmlns\b/.test(a.nodeName)) continue; // IE can't create these
clone.setAttributeNS(a.namespaceURI,a.nodeName,a.nodeValue);
}
for (var i=0,len=node.childNodes.length;i<len;++i){
var c = node.childNodes[i];
clone.insertBefore(
c.nodeType===1 ? cloneToDoc(c,doc) : doc.createTextNode(c.nodeValue),
null
); }
return clone;
}
function getOptions() {
// Look for options being set
var options = '';
var radioValue = 'png';
var radioButtons = document.getElementsByName('format');
for ( var i = 0; i < radioButtons.length; i++ ) {
var option = radioButtons[i];
if ( option.checked ) {
radioValue = option.value;
break;
}
}
options += '&saveformat=' + radioValue;
radioValue = 'drawshield';
radioButtons = document.getElementsByName('scheme');
for ( i = 0; i < radioButtons.length; i++ ) {
option = radioButtons[i];
if ( option.checked ) {
radioValue = option.value;
break;
}
}
options += '&palette=' + radioValue;
radioValue = 'drawshield';
radioButtons = document.getElementsByName('shape');
for ( i = 0; i < radioButtons.length; i++ ) {
option = radioButtons[i];
if ( option.checked ) {
radioValue = option.value;
break;
}
}
options += '&shape=' + radioValue;
radioValue = 'shiny';
radioButtons = document.getElementsByName('visual');
for ( i = 0; i < radioButtons.length; i++ ) {
option = radioButtons[i];
if ( option.checked ) {
radioValue = option.value;
break;
}
}
options += '&effect=' + radioValue;
option = document.getElementById('printable');
if ( option != null && option.checked == true) {
options += '&printable=1';
printable = true;
}
for ( var misc of ["webcols", "whcols", "tartancols"] )
if ( document.getElementById(misc).checked )
options += `&${misc}=yes`;
var customPaletteArea = document.getElementById("customPalette");
var paletteItems = customPaletteArea.value.split("\n").map(x => x.split("=")).map(x => x.map(a => a.trim()));
for ( var [key, val] of paletteItems )
{
if ( key == "" || val == "" )
continue;
if ( key.search("/") == -1 )
key = "heraldic/" + key;
options += `&customPalette[${encodeURIComponent(key)}]=${encodeURIComponent(val)}`;
}
return options;
}
function randomShieldCallback(callback)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', "randomblazon.php", true);
xmlhttp.onreadystatechange = function() {
if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
callback(xmlhttp.responseText.replace("created by", "// created by"));
};
xmlhttp.send(null);
}
function drawshield(url, messageFunc) {
if ( typeof(url) !== 'undefined' && url != null ) targetURL = url;
// Isolate blazon
shieldCaption = document.getElementById(captiontarget);
blazonText = document.getElementById('blazon').value;
eol1 = blazonText.indexOf('#');
eol2 = blazonText.indexOf('/');
if ( eol1 == -1 ) { eol = eol2; }
else if ( eol2 == -1 ) { eol = eol1; }
else { eol = Math.min(eol1, eol2); }
if ( eol != -1 ) { captionText = blazonText.slice(0,eol); }
else { captionText = blazonText; }
shieldCaption.firstChild.nodeValue= captionText ;
shieldCaption.setAttribute("href","http://drawshield.net/create/?blazon=" + encodeURIComponent(captionText));
// Add a random number to force resend (avoids caching)
myOpts = getOptions(); // set printable as side effect
if ( printable )
window.open(targetURL + myOpts + '&size=1000' + '&blazon=' + encodeURIComponent(blazonText),'_blank');
else
requestSVG(targetURL + getOptions() + '&size=' + shieldsize + '&rand=' + Math.random()
+ '&blazon=' + encodeURIComponent(blazonText),shieldtarget, messageFunc);
}
function dbquery() {
searchTerm = document.getElementById('searchterm').value;
requestHTML('/include/shield/dbquery.php?term=' + encodeURIComponent(searchTerm),tabletarget);
}
// Arguments are:
// target - name of the div element that holds the shield image
// size - horizontal size of the shield, in pixels, if null = 500
// initial - the initial blazon to use
// caption - name of the paragraph element that contains the shield caption
function setupshield(target, size, initial, caption) {
initBlazon = "";
if (typeof(target) !== 'undefined' && target != null) shieldtarget = target;
if (typeof(caption) !== 'undefined' && caption != null) captiontarget = caption;
shieldCaption = document.getElementById(captiontarget);
if (typeof(initial) !== 'undefined' && initial != null) {
initBlazon = '&blazon=' + encodeURIComponent(initial);
document.getElementById('blazon').value = initial;
shieldCaption.setAttribute("href", "http://drawshield.net/create/?blazon=" + encodeURIComponent(initial));
} else
initial = "Your shield here";
if (typeof(size) !== 'undefined' && size > 0) shieldsize = size;
shieldCaption.firstChild.nodeValue = initial;
requestSVG('/include/shield/drawshield.php?&highlight=1&size=' + shieldsize + initBlazon, shieldtarget);
}
function displayMessages(svg, messageTarget="messageList", blazonTarget="resultstable", message_types=["warning", "alert", "licence", "blazon"]) {
var messageText = '';
var errorList = svg.getElementsByTagNameNS('*','message');
for ( var i = 0; i < errorList.length; i++ ) {
var errorItem = errorList[i];
var category = errorItem.getAttribute('category');
if ( message_types.indexOf(category) == -1 )
continue;
var context = errorItem.getAttribute('context');
var lineno = errorItem.getAttribute('linerange');
var message = errorItem.innerHTML;
if (context != null) message += ' ' + context;
if (lineno != null) message += ' near ' + lineno;
switch (category) {
case 'licence':
case 'links':
case 'legal':
default:
messageText += "<li>" + message + "</li>";
break;
case 'warning':
messageText += "<li><span style='color:orange;'>WARNING</span> " + message + "</li>";
break;
case 'alert':
messageText += "<li><span style='color:red'>" + message + "</span></li>";
break;
}
}
document.getElementById(messageTarget).innerHTML = "<ul>" + messageText + "</ul>";
if ( blazonTarget )
format_blazon_ml(svg.querySelector("blazon"), blazonTarget);
}
function format_blazon_ml(element, target_id)
{
var target = document.getElementById(target_id);
while ( target.hasChildNodes() )
target.removeChild(target.firstChild);
if ( !element )
return;
function add_text(target, text)
{
target.appendChild(document.createTextNode(text));
}
function add_text_element(target, tag, text)
{
add_text(target.appendChild(document.createElement(tag)), text);
}
function add_element(target, tag)
{
return target.appendChild(document.createElement(tag));
}
function format_impl(parent, element, indent)
{
var target = add_element(parent, "div");
add_text_element(target, "strong", element.tagName);
for ( var att of element.attributes )
{
var row = add_element(target, "span");
add_text_element(row, "em", att.name);
add_text(row, " = " + att.value);
}
for ( var child of element.childNodes )
{
if ( child instanceof Element )
{
// if ( child.tagName != "blazonML:input" )
format_impl(target, child, indent+1);
}
else if ( child instanceof Text )
{
var text = child.textContent.trim();
if ( text != "" )
{
add_text_element(target, "span", text);
}
}
}
}
format_impl(target, element, 0);
}