-
Notifications
You must be signed in to change notification settings - Fork 0
/
Beholder.src.js
129 lines (92 loc) · 3.79 KB
/
Beholder.src.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
(function( $ ) {
$.fn.Beholder = function(buttonElement) {
/*arguments default to:
buttonElement = {
el : 'div',
className : 'BeholderButton',
html : '+'
}
*/
var obsEl = this,
butEl,
modalEl,
imgEl,
imgLinkEl,
closerEl,
viewButton = [];
buttonElement = buttonElement || { el : 'div', className : 'beholderButton', html : '+' };
//now generate button element from buttonElement argument object
butEl = document.createElement(buttonElement.el);
butEl.className = buttonElement.className;
butEl.insertAdjacentHTML('beforeend', buttonElement.html);
$(document).on('finishedObserved', function() {
//We then set the onclick event listener on the buttons to call modalbox
$('.' + buttonElement.className).on('click', function(e) {
//call the modal box if the button got clicked
//let's create the modal box
modalEl = document.createElement('div');
modalEl.className = 'beholderModalBox';
//and create the image
imgEl = document.createElement('img');
//take the src from data-url
imgEl.src = this.getAttribute('data-url');
//now append the image to modalEl the modal box
modalEl.appendChild(imgEl);
//setting done
//create the a for image link
imgLinkEl = document.createElement('a');
imgLinkEl.setAttribute('href', this.getAttribute('data-url'));
//put some text
imgLinkEl.textContent = 'Click here for the original source!';
//append it into the modal box
modalEl.appendChild(imgLinkEl);
imgEl.onload = function() {
var prefWindowWidth = $(window).width() - 120,
prefWindowHeight = $(window).height() - 120,
trueWindowWidth = $(window).width(),
trueWindowHeight = $(window).height();
if(imgEl.width >= trueWindowWidth) {
imgEl.style.width = prefWindowWidth + 'px';
imgEl.style.height = 'auto'
}
if(imgEl.height >= trueWindowHeight) {
imgEl.style.height = prefWindowHeight + 'px';
imgEl.style.width = 'auto';
}
};
$('body').append(modalEl).css('display', 'none').fadeIn(300, 'linear', function() {
//now if the image is larger than the width and height of the window, resize it
$(modalEl).on('click', function() {
$(modalEl).fadeOut(300, 'linear');
$(modalEl).remove();
});
});
});
});
var len = obsEl.find('a').length,
checkURLArr = [],
matcher;
//don't forget to get all the links on the obsEl to linksArr
obsEl.find('a').each(function(i, el) {
//now we reject the non image by checking the url's tail
checkURLArr = el.getAttribute('href').split('.');
matcher = checkURLArr.pop();
if(matcher === 'png' || matcher === 'jpg' || matcher === 'jpeg' || matcher === 'gif') {
//apply the data-url attr to each button
//but first, we must clone the button
viewButton[i-1] = butEl.cloneNode(true);
//now set the data-url
viewButton[i-1].setAttribute( 'data-url', el.getAttribute('href') );
//nice, now put the button just next to it's respective a element
//$(el).after(viewButton[i]);
el.insertAdjacentHTML('afterend', viewButton[i-1].outerHTML);
}
//this is lame, I know. If you got better method, just fork it and send me a pull request
//then trigger an event, so the buttons will be listened on click
if(i == len - 1) {
$(document).trigger('finishedObserved');
}
});
return this;
};
})( jQuery );