-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncasUi.js
167 lines (150 loc) · 4.53 KB
/
ncasUi.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
//display display data already calcolated
function display(){
setNewDisplay();
displayName();
displayDie();
displayWinner();
displayTalent();
displayDominance();
}
function displaySession(){
let d = document.getElementById("url");
d.value = window.location.href;
}
function urlCopy(){
let d = document.getElementById("url");
d.focus();
d.select();
try {
d.setSelectionRange(0, 99999);
document.execCommand("copy");
navigator.clipboard.writeText(d.value.toString());
} catch (error) {
console.log("urlcopy: " + error);
}
if (navigator.share) {
navigator.share({
title: document.title,
text: "Condividi sessione",
url: window.location.href
})
.then(() => console.log('Successful share'))
.catch(error => console.log('Error sharing:', error));
}
}
//setNewDisplay
function setNewDisplay(){
const dT = new Date();
let time = dT.getTime();
o.time=time;
let d = getNcasE(o.time, "displayResult", true, true);
}
//displayName
function displayName(){
let d = getNcasE("name", o.time+"Display");
d.innerHTML = o.name;
displayCopyName();
}
//displayCopyName
function displayCopyName(){
let d = getNcasE("nameCopy", "diceDisplay", true);
}
//displayDice display all the dice results
function displayDie(){
for (let i = 0, pool = Object.keys(o.pool); i < pool.length; i++) {
displayDice(pool[i]);
}
}
//displayDie display single pool dice (ex. discipline or pain); parameter is the name of the pool
function displayDice(name){
let n = "";
if (o.pool[name].rolls && o.pool[name].rolls.length){
n = name+": "+o.pool[name].rolls;
}
let d = getNcasE(name, o.time+"Display");
d.innerHTML = n;
displayCopyDie(name);
}
//displayCopyDie
function displayCopyDie(name){
let n = " ";
if (o.pool[name].rolls && o.pool[name].rolls.length){
n = o.pool[name].rolls;
}
let d = getNcasE(name+"Copy" ,"diceDisplay", true);
d.innerHTML = n;
}
//displayWinner display if the player or the master win
function displayWinner(){
let d = getNcasE("winning", o.time+"Display");
d.innerHTML = o.success.winnerTxt;
}
//displayTalent display use of exhaustion talent
function displayTalent(){
let d = getNcasE("talent", o.time+"Display");
d.innerHTML = o.etalentTxt;
}
//displayDominance dominance
function displayDominance(){
let d = getNcasE("dominance", o.time+"Display");
if(o.dominance.str < 1){
d.innerHTML = "<p>" + o.dominance.attribute + "</p>";
return;
}
d.innerHTML = "<p class=\"dominanceHi "+o.dominance.attribute+"Class\">" + o.pool[o.dominance.attribute].txtArticle+o.pool[o.dominance.attribute].txt + " domina.</p>";
d.innerHTML += o.dominance.txt;
}
//getNcasE get html element, if the element is already here will return it, else it will be created as child of parent
//parameter name=ID of the html element, parent=ID of the parent where to append
function getNcasE(name, parent, checkOld, invert){
let d = document.getElementById(name+"Display");
if (checkOld && d){
return d;
}
let dP = document.getElementById(parent);
d = document.createElement('div');
d.id = name+"Display";
if (checkOld){
name = String(name);
name = name.substring(0, (name.length-4));
}
d.className = name+"Class";
if (checkOld && invert){
dP.insertBefore(d, dP.childNodes[0]);
return d;
}
dP.appendChild(d);
return d;
}
//click suppress click function for plus and minus, add or remove 1 from the sibiling input numer, find by id
function click(e){
if (!e || !e.target || !e.target.type || e.target.type != "button"){
return;
}
let id = getAllSiblings(e.target, inputFilter)[0].id;
let oldV = parseFloat(document.getElementById(id).value);
let newV = oldV;
if (e.target.value == "+"){
newV = oldV + 1;
} else if (e.target.value == "-" && oldV > 0) {
newV = oldV - 1;
} else{
return;
}
document.getElementById(id).value = newV;
}
function inputFilter(elem){
if (elem.nodeName.toUpperCase() == "INPUT" && elem.type.toUpperCase() == "NUMBER" ){
return true
}
return false;
}
function getAllSiblings(elem, filter) {
var sibs = [];
elem = elem.parentNode.firstChild;
do {
if (elem.nodeType === 3) continue; // text node
if (!filter || filter(elem)) sibs.push(elem);
} while (elem = elem.nextSibling)
return sibs;
}