-
Notifications
You must be signed in to change notification settings - Fork 20
/
print.js
72 lines (68 loc) · 2.67 KB
/
print.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
var printSizes = document.querySelectorAll('[data-size]');
[].forEach.call(printSizes,function(elem){
elem.addEventListener('click',function(event){
event.preventDefault();
setPrintSize(elem.getAttribute('data-size'));
if(document.querySelector('[data-size].active')){
document.querySelector('[data-size].active').classList.remove('active');
}
elem.classList.add('active');
loadItems();
});
});
function setPrintSize(size){
[].forEach.call(printSizes,function(elem){
document.querySelector('body').classList.remove(elem.getAttribute('data-size'));
});
document.querySelector('body').classList.add(size);
makeStuffFit();
loadItems();
}
var rowCounts = document.querySelectorAll('[data-rowcount]');
[].forEach.call(rowCounts,function(elem){
elem.addEventListener('click',function(event){
event.preventDefault();
setRowCount(elem.getAttribute('data-rowcount'));
if(document.querySelector('[data-rowcount].active')){
document.querySelector('[data-rowcount].active').classList.remove('active');
}
elem.classList.add('active');
loadItems();
});
});
function setRowCount(count){
[].forEach.call(rowCounts,function(elem){
document.querySelector('body').classList.remove('per-row-'+elem.getAttribute('data-rowcount'));
});
document.querySelector('body').classList.add('per-row-'+count);
makeStuffFit();
loadItems();
}
// make the images fit
document.querySelector('body').insertAdjacentHTML("beforeend", '<style id="sizeCards"></style>');
var sizeCards = document.querySelector('#sizeCards');
function makeStuffFit(){
var cards = document.querySelectorAll('.card');
var breakout = false;
for(var x = 10; x < 100; x++){
sizeCards.innerHTML = '.card__item img { width:'+x+'%; }';
[].forEach.call(cards,function(card){
// console.debug(card);
var items = card.querySelectorAll('.card__item');
var cardBottomX = card.offsetTop + card.offsetHeight;
// console.log('cardBottomX',cardBottomX);
[].forEach.call(items,function(item){
// console.log(item);
var itemBottomX = item.offsetTop + item.offsetHeight;
// console.log('itemBottomX',itemBottomX);
if(itemBottomX > cardBottomX && !breakout){
// console.log("breakout!",item, x);
breakout = x-1; // go back one since that was the last to fit
}
});
});
if(breakout) {
sizeCards.innerHTML = '.card__item img { width:'+breakout+'%; }';
}
}
}