-
Notifications
You must be signed in to change notification settings - Fork 1
/
mosaic.js
103 lines (89 loc) · 2.53 KB
/
mosaic.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
/**
* @param {Object} data
* @param {string} data.src
* @param {string} data.title
* @param {string} data.caption
* @param {string} data.href
* @param {Element} rootElement
*/
function Mosaic(data, rootElement) {
this.data = data
this.container = rootElement
this.margin = 0
this.breakpoints = {
small: 576,
medium: 768,
large: 992,
xlarge: 1200,
default: 99999999
}
this.container.classList.add('mosaic-gallery')
this.calcImageWidth = function () {
const vp = this.getCurrentViewport()
const margin = this.margin[vp]
const imgPerRow = this.imagePerRow[vp]
return (this.container.clientWidth - (margin * imgPerRow * 2)) / imgPerRow
}
this.getCurrentViewport = function () {
const k = Object.keys(this.breakpoints)
for (let i = 0; i < k.length; i++) {
const sizeName = k[i]
const sizeValue = this.breakpoints[sizeName]
if (this.container.clientWidth <= sizeValue) {
return sizeName
}
}
return 'large'
}
}
Mosaic.prototype.render = function () {
const w = this.calcImageWidth()
const vp = this.getCurrentViewport()
this.data.forEach(image => {
const template = `
<div class="mosaic-item" style="width: ${w}px; height: ${w}px; margin: ${this.margin[vp]}px">
<a href="${image.href}">
<img src="${image.src}" />
<div class="caption">
<p>${image.title}</p>
${image.caption ? '<p class="d-none d-sm-block">' + image.caption + '</p>' : ''}
</div>
</a>
</div>
`
this.container.innerHTML += template
})
document.addEventListener('DOMContentLoaded', () => {
window.addEventListener('resize', () => {
const w = this.calcImageWidth()
const vp = this.getCurrentViewport()
this.container.querySelectorAll('.mosaic-item').forEach(img => {
img.style.width = `${w}px`
img.style.height = `${w}px`
img.style.margin = `${this.margin[vp]}px`
})
})
})
}
/**
* @param {Object} margin
* @param {number} margin.default
* @param {number} margin.small
* @param {number} margin.medium
* @param {number} margin.large
* @param {number} margin.xlarge
*/
Mosaic.prototype.setMargin = function (margin) {
this.margin = margin
}
/**
* @param {Object} quantity
* @param {number} quantity.default
* @param {number} quantity.small
* @param {number} quantity.medium
* @param {number} quantity.large
* @param {number} quantity.xlarge
*/
Mosaic.prototype.imagesPerRow = function (quantity) {
this.imagePerRow = quantity
}