-
Notifications
You must be signed in to change notification settings - Fork 0
/
edcss.js
189 lines (184 loc) · 5.95 KB
/
edcss.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
(function(){//alert close
let alerts=document.querySelectorAll('.alert .close');
[].forEach.call(alerts,function(alert){
alert.addEventListener('click',function(e){
e.preventDefault();
e.target.parentNode.parentNode.removeChild(e.target.parentNode);
});
});
})();
(function(){//nav
//hamburger
let navs=document.querySelector('.navbar-toggle');
if(navs!=null){
navs.addEventListener('click',function(e){
e.preventDefault();
let navbars=document.querySelectorAll('.navbar-nav');
[].forEach.call(navbars,function(navbar){
navbar.classList.toggle('nav-open');
});
});
}
//dropdown
let nav_dds=document.querySelectorAll('.dropdown-toggle');
[].forEach.call(nav_dds,function(nav_dd){
nav_dd.addEventListener('click',function(e){
e.preventDefault();
let nextEl=this.nextElementSibling;
//mobile
nextEl.classList.toggle('dropdown-open');
//desktop: @media(min-width:768px){.dropdown:hover>.dropdown-menu{display:block}}
});
});
})();
(function(){//tab
let tabs=document.querySelectorAll('.nav-tabs li');
[].forEach.call(tabs,function(tab){
tab.addEventListener('click',function(e){
e.preventDefault();
let self=this;
if(self.classList.contains('disabled')==false){
[].forEach.call(self.parentNode.children,function(el){
el.classList.remove('active');
});
self.classList.add('active');
let id=e.target.getAttribute('href').replace(/.*(?=#[^\s]*$)/,'');
let tabnext=self.parentNode.nextElementSibling;
[].forEach.call(tabnext.children,function(el){
el.style.display="none";
});
tabnext.querySelector(id).style.display="block";
}
});
});
})();
(function(){//modal
let modals=document.querySelectorAll('[data-toggle="modal"]');
[].forEach.call(modals,function(modal){
modal.addEventListener('click',function(e){
let modal_target=this.getAttribute('data-target');
let tagetEl=document.getElementById(modal_target);
tagetEl.style.display="none";
if(this.tagName=='A') e.preventDefault();
document.body.style.overflow="hidden";
tagetEl.style.display="block";
let dismiss=tagetEl.querySelectorAll('[data-dismiss="modal"]');
[].forEach.call(dismiss,function(dismis){
dismis.addEventListener('click',function(e){
document.body.style.overflow="auto";
tagetEl.style.display="none";
});
});
});
});
})();
(function(){//carousel
let time=7000;
let carouselTimer=setInterval(function(){carousel();},time);
let indicators=document.querySelectorAll('.indicator');
[].forEach.call(indicators,function(indicator){
indicator.addEventListener('click',function(e){
let idx=Array.from(this.parentNode.children).indexOf(this);
carousel(idx);
clearInterval(carouselTimer);
carouselTimer=setInterval(function(){carousel();},time);
});
});
function carousel(idx){//slide
let items=document.querySelectorAll('.carousel-inner .item'), activEl=document.querySelector('.carousel-inner .item.active');
if(activEl!=null){
let active=Array.from(items).indexOf(activEl);
[].forEach.call(items,function(item){
item.classList.remove('active');
});
if(active >= items.length-1) active=-1;
active=active+1;
if(idx != null) active=idx;
items[active].classList.add('active');
let indicators=document.querySelectorAll('.carousel-indicators .indicator');
[].forEach.call(indicators,function(indicator){
indicator.classList.remove('active');
});
indicators[active].classList.add('active');
}
}
})();
(function(){//gallery
let prevImg=document.querySelector('#previous-img'), nextImg=document.querySelector('#next-img');
let winSize=function(){
let w=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth;
let h=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight;
return w+'x'+h;
}
function disableButtons(count_max,count_current){
prevImg.style.display="block";
nextImg.style.display="block";
if(count_max == count_current){
nextImg.style.display="none";
}else if(count_current == 1){
prevImg.style.display="none";
}
}
let curr_img,selector,count=0,setIDs=true,setClick='a.thumbnail',target;
if(prevImg!=null){
prevImg.addEventListener('click',function(){
curr_img--;
selector=document.querySelector('[data-id="'+curr_img+'"]');
updateGallery(selector);
});
}
if(nextImg!=null){
nextImg.addEventListener('click',function(){
curr_img++;
selector=document.querySelector('[data-id="'+curr_img+'"]');
updateGallery(selector);
});
}
function key(e){
if(e.keyCode==27 || e.which==27){
document.querySelector('#'+target).style.display="none";
document.body.style.overflow="auto";
document.removeEventListener('keyup', key);
}
if(curr_img >= count && (e.keyCode==39||e.which==39)) return false;
if(e.keyCode==39||e.which==39){
curr_img++;
selector=document.querySelector('[data-id="'+curr_img+'"]');
updateGallery(selector);
}
if(curr_img <= 1 && (e.keyCode==37||e.which==37)) return false;
else if(e.keyCode==37||e.which==37){
curr_img--;
selector=document.querySelector('[data-id="'+curr_img+'"]');
updateGallery(selector);
}
}
function size(sel){
let img=new Image();
img.src=sel.children[0].getAttribute("src");
let imgW=img.width,imgH=img.height,siz=winSize().split('x'),w=siz[0],h=siz[1],left=(imgW<w ? (w/2-imgW/2):0);
document.querySelector('#'+target+' .gallery-img').setAttribute('style','width:'+(imgW<w?imgW:w)+'px;left:'+left+'px;height:'+h+'px;background:center / contain no-repeat url("'+img.src+'")');
}
function updateGallery(selector){
let sel=selector;
curr_img=sel.getAttribute('data-id');
disableButtons(count,curr_img);
size(sel);
window.addEventListener("resize",function(){size(sel)});
document.addEventListener('keyup',key);
}
let thumbs=document.querySelectorAll(setClick);
[].forEach.call(thumbs,function(thumb){
count++;
if(setIDs==true){
let attr=document.createAttribute('data-id');
attr.value=count;
thumb.setAttributeNode(attr);
}else thumb.setAttribute('data-id',count);
thumb.addEventListener('click',function(){
let id=this.getAttribute('data-id');
target=this.getAttribute('data-target');
updateGallery(document.querySelector('[data-id="'+id+'"]'));
});
});
})();