-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmitbutton.js
145 lines (123 loc) · 4.46 KB
/
submitbutton.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
function hasClass( target, className )
{ // a helper function to test if a target contains a specific class property
return new RegExp('(\\s|^)' + className + '(\\s|$)').test(target.className);
}
function clamp(val, min, max) {
// a helper function to clamp values
return val > max ? max : val < min ? min : val;
}
function sendForm() {
manageButton("sending");
// put here backend code to send data
// manageButton("success") - when google confirms all good (status: 200)
setTimeout(function(){
// manageButton( "success"; )
},2300);
// manageButton("error") - when google returns an error code
setTimeout(function(){
manageButton("success");
},2300);
// display your error/confirmation messages, ask what to do then go idle.
// manageButton("idle");
}
function manageButton(command) {
// target submit button
var submitButton = document.getElementById("submitButton");
// since we are now undergoing some kind of command,
// disable the button from being clicked again, until otherwise applicable
submitButton.onclick = "";
// setup a static/(global) variable flag that will be applied when the animation timing is right
submitButtonFlag = command;
// maybe remember previous state of button in orer to go back to it when finished?
// var submitButtonPreviousState = submitButton.className;
// create a pair of transition functions to psuedo blend.
/* Old version. to delete. new version triggers a css animation
function transitionOn() {
let elem = document.getElementById("psuedo-transition-layer");
elem.style.opacity = 0;
elem.style.width = "100%";
elem.style.height = "100%";
elem.style.borderRadius = "4px";
elem.style.backgroundColor = "#ffffff";
elem.style.opacity = 1;
}
function transitionOff() {
let elem = document.getElementById("psuedo-transition-layer");
let full = 35;
let opac = full;
let id2 = setInterval(frame2, 50);
// this method is buggy because it spawns a thread, repeated spawns don't override old ones
// so if you make a rapid change then you have two animations occuring at the same time
function frame2() {
if (opac > 0) {
opac -= 6;
elem.style.opacity = clamp(opac/full,0,1);
}
if (opac <= 0) clearInterval(id2);
}
}
*/
// create functions for the different states
function setIdle()
{
submitButton.style.pointerEvents = "auto";
if (hasClass(submitButton,'active')) { submitButton.classList.remove('active'); }
if (!hasClass(submitButton,'blue')){
submitButton.className = "progress-btn button blue";
let sbtxt = document.getElementById('submitButtonText');
flash(sbtxt);
}
document.getElementById("submitButtonText").innerHTML = "Send";
// make the button clickable again
submitButton.onclick = function(){ sendForm(); };
}
function setSuccess()
{
submitButton.style.pointerEvents = "none";
submitButton.classList.remove('active');
if (!hasClass(submitButton,'green')){
submitButton.className = "progress-btn button green";
let sbtxt = document.getElementById('submitButtonText');
flash(sbtxt);
}
document.getElementById("submitButtonText").innerHTML = "Sent";
}
function setError()
{
submitButton.style.pointerEvents = "none";
submitButton.classList.remove('active');
if (!hasClass(submitButton,'red')){
submitButton.className = "progress-btn button red";
let sbtxt = document.getElementById('submitButtonText');
flash(sbtxt);
}
document.getElementById("submitButtonText").innerHTML = "Error";
}
function setSending()
{
submitButton.style.pointerEvents = "none";
if (!hasClass(submitButton,'active')) {
submitButton.classList.add('active');
document.getElementById("submitButtonText").innerHTML = "Sending...";
}
}
///////// Manage commands/button states /////////
function manageStates() {
if (submitButtonFlag == "idle") {setIdle();}
else if (submitButtonFlag == "sending") {setSending();}
else if (submitButtonFlag == "error") {setError();}
else if (submitButtonFlag == "success") {setSuccess();}
}
manageStates();
///////////
// Disabled the below because I can't have the button change asynchronously
// otherwise it will look out of sync with the other GUI events
// occuring to indicate success or faliure
//////////
/*
// if not active, just jump straight, but if active, use an eventlistener
if (!hasClass(submitButton,'active')) { manageStates(); }
// Create a listener that will change button status when animation ready
else submitButton.addEventListener('animationiteration', function() {manageStates();},{once:true});
*/
}