-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdexs.js
228 lines (171 loc) · 6.12 KB
/
dexs.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
// Dexs Modal JS Library v1.0.2
// github.com/PolarComputer/Dexs-Modal
// this file has been updeated for ES5
// Add Shade When Done Loading
document.addEventListener("DOMContentLoaded", function(){
var shadeElm = document.createElement( "div" );
shadeElm.classList.add('dexs-shade');
shadeElm.classList.add('fade');
document.body.appendChild( shadeElm );
document.getElementsByClassName( "dexs-shade" )[0].addEventListener( "click", function(){
dexs.close();
});
});
// Open Shade Function
const shade = [];
shade.show = function() {
var shadeElm = document.getElementsByClassName( "dexs-shade" );
if ( shadeElm.length != 1 ) {
console.error( "Dexs Modal: Failed to reveal Dexs Modal Shade. There must be exactly one element with the class name 'dexs-shade'." );
} else {
shadeElm = shadeElm[0];
shadeElm.classList.add( "show" );
}
}
shade.hide = function() {
var shadeElm = document.getElementsByClassName( "dexs-shade" );
if ( shadeElm.length != 1 ) {
console.error( "Dexs Modal: Failed to suppress Dexs Modal Shade. There must be exactly one element with the class name 'dexs-shade'." );
} else {
shadeElm = shadeElm[0];
shadeElm.classList.remove( "show" );
}
}
// dexs modal
const dexs = [];
/*================ Open ================
Opens a modal based on ID. Add
existing open modal to stack and close
them.
======================================*/
dexs.open = function( _id ) {
var dexsElm = document.getElementById( _id );
// Checks to see if element exists
if ( !dexsElm ) {
console.error( "Dexs Modal: Failed to locate #" + _id + ", because element doesn't exist. Please use the element ID." );
} else {
// Open Shade
shade.show();
// check for other decks
var activeStacks = document.querySelectorAll( "[ data-stack-level ]" );
// Close current open modal and add stack level
var openModals = document.querySelectorAll( "modal.dexs.open" );
if ( openModals.length == 1 && openModals[0].id != _id ) { // stops adding deck level to current open modal
openModals = openModals[0];
// Add Stack Level attr to element
if ( !openModals.hasAttribute( "disable-stack" ) ) {
var highestStackLevel = this.stack();
if ( highestStackLevel.length != 0 ) {
highestStackLevel = highestStackLevel[0].level + 1;
} else {
highestStackLevel = 0;
}
openModals.dataset.stackLevel = highestStackLevel;
}
openModals.classList.remove( "open" );
}
// Opens New element
dexsElm.classList.add( "open" );
}
}
/*============== closeAll ==============
The close all function will close all
open modals and clear and current
open stacks.
======================================*/
dexs.closeAll = function() {
shade.hide(); // Close Shade
this.clearStack(); // Clear Stack
// Close all possible modals
var openModals = document.querySelectorAll( "modal.dexs.open" );
for ( var i = 0; i < openModals.length; i++ ) {
openModals[i].classList.remove( "open" );
if ( openModals[i].hasAttribute( "destroy-on-close" ) ) {
openModals.parentNode.removeChild( openModals );
}
}
}
/*=============== Close ================
Close only the current modal, and
remove it from the stack, and open the
next modal in the stack. If the
element has the attribute "destroy-on-close",
it will be removed from the HTML, upon
closing the modal.
@return - false if failed to close
======================================*/
dexs.close = function() {
var openModals = document.querySelectorAll( "modal.dexs.open" ); // get current open modal
// Check if modal is open
if ( openModals.length != 1 ) {
return false;
}
openModals = openModals[0]; // select the only element
this.removeFromStack( openModals.id ); // remove this element from stack => shouldn't be there - precation
openModals.classList.remove( "open" ); // close the modaul
setTimeout(function(){
if ( openModals.hasAttribute( "destroy-on-close" ) ) {
openModals.parentNode.removeChild( openModals );
}
}, 200);
// Hide shade if required
var stack = this.stack();
if ( this.stack().length == 0 ) {
shade.hide();
} else {
this.open( stack[0].id );
}
return true;
}
/*============= Is Open ================
returns boolean on if the modal based
on the id is open or closed
@return - false - modal closed or error
@return - true - if modal open
======================================*/
dexs.isOpen = function( _id ) {
if ( document.getElementById( _id ) && document.getElementById( _id ).classList.contains( "open" ) ) {
return true;
}
return false;
}
/*=============== Stack ================
Returns list of arrays of modals in
stack, with newest in stack first.
Each object has the level number and
the stack ID.
@return - array of modal id's in stack
======================================*/
dexs.stack = function() {
var activeStacks = document.querySelectorAll( "[ data-stack-level ]" ); // get all active stacks
// Create Array of objects from active Stacks
var resultStacks = [];
for ( var i = 0; i < activeStacks.length; i++ ) {
resultStacks.push( { id: activeStacks[ i ].id, level: parseInt( activeStacks[ i ].getAttribute( "data-stack-level" ) ) } );
}
// Sort Array of objects with highest level number first
resultStacks.sort(function(a, b){
if(a.level < b.level) return 1;
if(a.level > b.level) return -1;
return 0;
});
return resultStacks;
}
/*============ Clear Stack ==============
Removes all modals from history stack.
======================================*/
dexs.clearStack = function() {
var activeStacks = document.querySelectorAll( "[ data-stack-level ]" );
for ( var i = 0; i < activeStacks.length; i++ ) {
activeStacks[i].removeAttribute( "data-stack-level" );
}
}
/*========= Remove From Stack ==========
Removes the stack level attribute
from the element with the id that
is passed.
======================================*/
dexs.removeFromStack = function( _id ) {
document.getElementById( _id ).removeAttribute( "data-stack-level" );
}
export { dexs };