Skip to content

Commit 8e58938

Browse files
author
Bernhard Grünewaldt
committed
0.0.10 and esm module
1 parent e9cb66b commit 8e58938

9 files changed

+309
-11
lines changed
File renamed without changes.

example/systemjs/index.html

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>cc-lightbox</title>
6+
7+
</head>
8+
<body>
9+
10+
11+
12+
<h2>Demo Gallery #01</h2>
13+
14+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/images/DSC05051.JPG" target="_blank"><img
15+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/thumbs/DSC05051.JPG"
16+
style="width:200px;border:1px solid #ccc"
17+
data-cc-lightbox="gallery-01"
18+
data-cc-title="Image One"
19+
/></a>
20+
21+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/images/DSC05052.JPG" target="_blank"><img
22+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/thumbs/DSC05052.JPG"
23+
style="width:200px;border:1px solid #ccc"
24+
data-cc-lightbox="gallery-01"
25+
data-cc-title="Image Two"
26+
/></a>
27+
28+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/images/DSC05053.JPG" target="_blank"><img
29+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/thumbs/DSC05053.JPG"
30+
style="width:200px;border:1px solid #ccc"
31+
data-cc-lightbox="gallery-01"
32+
data-cc-title="Image Three"
33+
/></a>
34+
35+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/images/DSC05054.JPG" target="_blank"><img
36+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/thumbs/DSC05054.JPG"
37+
style="width:200px;border:1px solid #ccc"
38+
data-cc-lightbox="gallery-01"
39+
data-cc-title="Image Four"
40+
/></a>
41+
42+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/images/DSC05055.JPG" target="_blank"><img
43+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/thumbs/DSC05055.JPG"
44+
style="width:200px;border:1px solid #ccc"
45+
data-cc-lightbox="gallery-01"
46+
data-cc-title="Image Five"
47+
/></a>
48+
49+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/images/DSC05057.JPG" target="_blank"><img
50+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/thumbs/DSC05057.JPG"
51+
style="width:200px;border:1px solid #ccc"
52+
data-cc-lightbox="gallery-01"
53+
data-cc-title="Image Six"
54+
/></a>
55+
56+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/images/DSC05069.JPG" target="_blank"><img
57+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-01/thumbs/DSC05069.JPG"
58+
style="width:200px;border:1px solid #ccc"
59+
data-cc-lightbox="gallery-01"
60+
data-cc-title="Image Seven"
61+
/></a>
62+
63+
<hr>
64+
65+
<h2>Demo Gallery #02</h2>
66+
67+
68+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-02/images/DSC05078.JPG" target="_blank"><img
69+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-02/thumbs/DSC05078.JPG"
70+
style="width:200px;border:1px solid #ccc"
71+
data-cc-lightbox="gallery-02"
72+
data-cc-title="Image One"
73+
/></a>
74+
75+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-02/images/DSC05084.JPG" target="_blank"><img
76+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-02/thumbs/DSC05084.JPG"
77+
style="width:200px;border:1px solid #ccc"
78+
data-cc-lightbox="gallery-02"
79+
data-cc-title="Image Two"
80+
/></a>
81+
82+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-02/images/DSC05089.JPG" target="_blank"><img
83+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-02/thumbs/DSC05089.JPG"
84+
style="width:200px;border:1px solid #ccc"
85+
data-cc-lightbox="gallery-02"
86+
data-cc-title="Image Three"
87+
/></a>
88+
89+
<a href="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-02/images/DSC05104.JPG" target="_blank"><img
90+
src="https://codeclou.github.io/cc-image-lightbox/demo/demo-gallery-02/thumbs/DSC05104.JPG"
91+
style="width:200px;border:1px solid #ccc"
92+
data-cc-lightbox="gallery-02"
93+
data-cc-title="Image Four"
94+
/></a>
95+
96+
97+
<link href="https://cdn.rawgit.com/codeclou/cc-image-lightbox/e9cb66b7/src/components.es6/ccImageLightbox.css" rel="stylesheet" />
98+
<script src="https://cdn.rawgit.com/systemjs/systemjs/0.20.9/dist/system.js"></script>
99+
<script>
100+
SystemJS.config({
101+
map: {
102+
'plugin-babel': 'https://cdn.rawgit.com/systemjs/plugin-babel/0.0.21/plugin-babel.js',
103+
'systemjs-babel-build': 'https://cdn.rawgit.com/systemjs/plugin-babel/0.0.21/systemjs-babel-browser.js',
104+
'cc-image-lightbox': 'https://cdn.rawgit.com/codeclou/cc-image-lightbox/e9cb66b7/src/components.es6/ccImageLightbox.js'
105+
},
106+
transpiler: 'plugin-babel'
107+
});
108+
SystemJS.import('./main.js');
109+
</script>
110+
</body>
111+
</html>

example/systemjs/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import CCImageLightbox from 'cc-image-lightbox';
2+
3+
new CCImageLightbox();

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cc-image-lightbox",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "Lightweight vanilla-JS image lightbox crafted with CSS3 and inline SVG icons for modern browsers",
55
"repository": {
66
"type": "git",
@@ -9,8 +9,9 @@
99
"author": "codeclou",
1010
"license": "MIT",
1111
"scripts": {
12-
"build": "npm run build:browser",
13-
"build:browser": "webpack --config webpack.browser.config.js && MINIFY=1 webpack --optimize-minimize --config webpack.browser.config.js"
12+
"build": "npm run build:browser && npm run build:module:esm",
13+
"build:browser": "webpack --config webpack.browser.config.js && MINIFY=1 webpack --optimize-minimize --config webpack.browser.config.js",
14+
"build:module:esm": "cp src/ccImageLightbox.js build/cc-image-lightbox.esm.js"
1415
},
1516
"devDependencies": {
1617
"babel-cli": "^6.23.0",

src/build.browser.js

-7
This file was deleted.
File renamed without changes.

src/ccImageLightbox.js

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
class CCImageLightbox {
2+
constructor() {
3+
this.store = {
4+
current: {
5+
galleryId: null,
6+
index: null
7+
},
8+
galleries: []
9+
};
10+
this.init();
11+
}
12+
13+
_setCurrentOpenImage(galleryId, index) {
14+
this.store.current.galleryId = galleryId;
15+
this.store.current.index = index;
16+
}
17+
_getCurrentOpenImage() {
18+
return this.store.current;
19+
}
20+
_clearCurrentOpenImage() {
21+
this.store.current.galleryId = null;
22+
this.store.current.index = null;
23+
}
24+
_isCurrentOpenImage() {
25+
return this.store.current.galleryId !== null;
26+
}
27+
28+
_getGallery(galleryId) {
29+
if (this.store[galleryId] === undefined || this.store[galleryId] === null) {
30+
this.store[galleryId] = [];
31+
}
32+
return this.store[galleryId];
33+
}
34+
35+
_getImage(galleryId, index) {
36+
const gallery = this._getGallery(galleryId);
37+
if (gallery[index] === undefined || gallery[index] === null) {
38+
gallery[index] = {};
39+
}
40+
return gallery[index];
41+
}
42+
43+
_isImage(galleryId, index) {
44+
const image = this._getImage(galleryId, index);
45+
return !(image.src === undefined || image.src === null);
46+
}
47+
48+
_renderNextOrPreviousButton(galleryId, index, direction) {
49+
var self = this;
50+
const button = document.createElement('div');
51+
button.setAttribute('class', 'cc-lightbox--' + direction);
52+
if (self._isImage(galleryId, index)) {
53+
button.setAttribute('class', direction === 'left' ? 'cc-lightbox--left cc-lightbox--left--has-previous' : 'cc-lightbox--right cc-lightbox--right--has-next');
54+
button.onclick = () => self.open(galleryId, index);
55+
}
56+
return button;
57+
};
58+
59+
_generateId(galleryId, index) {
60+
return 'cc-lightbox--' + galleryId + '--' + index;
61+
}
62+
63+
64+
closeIfOpen() {
65+
const self = this;
66+
const lightboxWrapper = document.getElementsByClassName('cc-lightbox-wrapper');
67+
if (lightboxWrapper[0] !== undefined && lightboxWrapper[0] !== null) {
68+
lightboxWrapper[0].remove();
69+
}
70+
self._clearCurrentOpenImage();
71+
}
72+
73+
open(galleryId, index) {
74+
this.closeIfOpen();
75+
this._setCurrentOpenImage(galleryId, index);
76+
this.create(galleryId, index);
77+
}
78+
79+
create(galleryId, index) {
80+
const self = this;
81+
const indexInt = parseInt(index, 10);
82+
83+
// WRAPPER
84+
const wrapper = document.createElement('div');
85+
wrapper.setAttribute('class', 'cc-lightbox-wrapper');
86+
wrapper.setAttribute('data-cc-lightbox-gallery-id', galleryId);
87+
document.body.appendChild(wrapper);
88+
89+
// TOPBAR
90+
const topBar = document.createElement('div');
91+
topBar.setAttribute('class', 'cc-lightbox--top');
92+
wrapper.appendChild(topBar);
93+
94+
// TITLEBAR
95+
const titleBar = document.createElement('div');
96+
titleBar.setAttribute('class', 'cc-lightbox--top-title');
97+
titleBar.innerHTML = self._getImage(galleryId, index).title;
98+
topBar.appendChild(titleBar);
99+
100+
// CLOSEBUTTON
101+
const closeButton = document.createElement('div');
102+
closeButton.setAttribute('class', 'cc-lightbox--top-close');
103+
closeButton.onclick = () => self.closeIfOpen();
104+
topBar.appendChild(closeButton);
105+
106+
// PREVIOUS BUTTON
107+
wrapper.appendChild(self._renderNextOrPreviousButton(galleryId, (indexInt - 1), 'left'));
108+
109+
// IMAGE
110+
const image = document.createElement('div');
111+
image.setAttribute('class', 'cc-lightbox--image');
112+
wrapper.appendChild(image);
113+
const imageInner = document.createElement('div');
114+
imageInner.setAttribute('class', 'cc-lightbox--image-inner');
115+
image.appendChild(imageInner);
116+
const imageInnerWrap = document.createElement('div');
117+
imageInnerWrap.setAttribute('class', 'cc-lightbox--image-inner-wrap');
118+
imageInner.appendChild(imageInnerWrap);
119+
const img = document.createElement('img');
120+
img.onload = function () {
121+
image.setAttribute('class', 'cc-lightbox--image cc-lightbox--image-loaded');
122+
};
123+
img.setAttribute('src', self._getImage(galleryId, index).src);
124+
img.setAttribute('class', 'cc-lightbox--image-img');
125+
img.setAttribute('id', self._generateId(galleryId, index));
126+
imageInnerWrap.appendChild(img);
127+
128+
// NEXT BUTTON
129+
wrapper.appendChild(self._renderNextOrPreviousButton(galleryId, (indexInt + 1), 'right'));
130+
131+
return false;
132+
}
133+
134+
init() {
135+
const self = this;
136+
const lightboxElements = document.querySelectorAll('[data-cc-lightbox]');
137+
138+
for (let i = 0; i < lightboxElements.length; i++) {
139+
const lightboxElement = lightboxElements[i];
140+
const galleryId = lightboxElement.getAttribute('data-cc-lightbox');
141+
const nextIndex = self._getGallery(galleryId).length;
142+
const nextImage = self._getImage(galleryId, nextIndex);
143+
nextImage.title = lightboxElement.getAttribute('data-cc-title');
144+
nextImage.src = lightboxElement.parentNode.getAttribute('href');
145+
146+
//
147+
// THUMBNAIL CLICK OPENS LIGHTBOX
148+
//
149+
lightboxElement.parentNode.onclick = () => {
150+
self.open(galleryId, nextIndex);
151+
return false;
152+
};
153+
}
154+
155+
//
156+
// CLOSE ON ESCAPE KEY PRESS
157+
//
158+
document.addEventListener('keydown', function (event) {
159+
if (event.keyCode === 27) {
160+
self.closeIfOpen();
161+
}
162+
if (event.keyCode === 37) { // left
163+
if (self._isCurrentOpenImage()) {
164+
const current = self._getCurrentOpenImage();
165+
if (self._isImage(current.galleryId, current.index - 1)) {
166+
self.open(current.galleryId, current.index - 1);
167+
}
168+
}
169+
}
170+
if (event.keyCode === 39) { // right
171+
if (self._isCurrentOpenImage()) {
172+
const current = self._getCurrentOpenImage();
173+
if (self._isImage(current.galleryId, current.index + 1)) {
174+
self.open(current.galleryId, current.index + 1);
175+
}
176+
}
177+
}
178+
}, false);
179+
180+
};
181+
};
182+
183+
export default CCImageLightbox;

webpack.browser.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var MINIFY = JSON.parse(process.env.MINIFY || '0');
88
var jsExtension = MINIFY ? '.min.js' : '.js';
99
var cssExtension = MINIFY ? '.min.css' : '.css';
1010
module.exports = {
11-
entry: './src/build.browser.js',
11+
entry: './webpack.browser.main.js',
1212
output: {
1313
path: path.join(__dirname, 'build'),
1414
filename: 'cc-image-lightbox' + jsExtension

webpack.browser.main.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* USED TO GENERATE A BROWSER VERSION WITH WEBPACK.
3+
* SEE build/* FILES
4+
*/
5+
import './src/ccImageLightbox.css';
6+
import CCImageLightbox from './src/ccImageLightbox.js';
7+
new CCImageLightbox();

0 commit comments

Comments
 (0)