-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample-popup-ui.js
163 lines (122 loc) · 3.74 KB
/
example-popup-ui.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
(function($){
// Set elements for the popup UI
var popupUI = {
close : $('<span class="popup-close">×</span>'),
next : $('<span class="popup-next ">→</span>'),
prev : $('<span class="popup-prev ">←</span>'),
nav : $('<div class="popup-nav"></div>'),
overlay : $('<div class="popup-overlay"></div>')
};
// Trigger close, and prevent propagation
// so no click events bubble up
function quitPopup(e,instance){
e.preventDefault();
e.stopImmediatePropagation();
instance.quitImageLightbox();
}
// Switch to previous image,
// if it's the first then back to the last
function routePrev(self){
if(self.current === 0 ){
self.switchImageLightbox(self.length - 1);
self.current = self.length -1;
} else {
self.switchImageLightbox(self.current - 1);
self.current--;
}
return false;
}
// Switch to next image,
// if it's the last then back to the first
function routeNext(self){
if(self.current === (self.length - 1) ){
self.switchImageLightbox(0);
self.current = 0;
} else {
self.switchImageLightbox(self.current + 1);
self.current++;
}
return false;
}
// Adds active class to the nav items
function setNavItemActive(i){
navItems = popupUI.nav.find('.popup-nav-item');
navItems.removeClass('active');
navItems.eq(i).addClass('active');
}
// Get the index of the current image being displayed
// !! You cannot have multiple instances of the same image in the collection
function getCurrentIndex(instance){
var $target = instance.filter( '[href="' + $( '#imagelightbox' ).attr( 'src' ) + '"]' ),
index = instance.index($target);
return index;
}
// Binds listeners to the nav, so that they are clickable
function setNav(instance){
var self = instance;
setNavItemActive(self.current);
popupUI.nav.on('click', '.popup-nav-item', function(){
var $this = $(this);
if($this.hasClass('active')){
return false;
}
popupUI.nav.find('.popup-nav-item').removeClass('active');
var index = $this.index();
if(self.current != index){
self.switchImageLightbox(index);
self.current = index;
self.setNavItemActive(self.current);
}
return false;
});
}
// Adds all the UI Elements and neccessary listeners
function addUI(instance){
for(var k in popupUI){
popupUI[k].appendTo('body');
}
var self = instance;
popupUI.prev.on('click',function(e){
e.stopImmediatePropagation();
routePrev(self);
});
popupUI.next.on('click',function(e){
e.stopImmediatePropagation();
routeNext(self);
});
self.each(function(){
$('<span class="popup-nav-item"><span>').appendTo(popupUI.nav);
});
popupUI.close.on('click touchend',function(e){
quitPopup(e,self);
});
popupUI.overlay.on('click touchend',function(e){
quitPopup(e,self);
});
setNav(instance);
}
// Remove all UI Elements and children
function removeUI(){
popupUI.nav.children().remove();
for(var k in popupUI){
popupUI[k].remove();
}
}
function updateUI(instance){
var currentIndex = getCurrentIndex(instance);
popupGallery.current = currentIndex;
setNavItemActive(currentIndex); // Update the nav when the image changes
}
var popupGallery = $('a').imageLightbox({
onStart : function(){
addUI(popupGallery);
},
onEnd : function(){
removeUI();
},
onLoadStart: false,
onLoadEnd: function(){
updateUI(popupGallery);
}
});
})(jQuery);