-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram-widget.js
88 lines (74 loc) · 2.77 KB
/
instagram-widget.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
var container;
var numCol;
//resize height on chagnes
$( window ).resize(function() {
setDimensions();
});
//main instagram method
function instagram(obj, tag, row, col) {
container = obj;
container.empty();
numCol = col;
var toAdd = '';
var counter = 0;
//set up the divs and images
for (var i = 0; i < row; i++) {
toAdd = toAdd + '<div class=\"instarow\">';
for (var j = 0; j < col; j++) {
toAdd = toAdd + '<div id=\"instaWrapper' + counter +'\"' + 'class="instaWrapper" > <div class="instaOverlay"><div id="instaText' + counter + '" class="instaTextClass"><div class="instaTextInner"></div></div></div></div>';
counter = counter + 1;
}
toAdd = toAdd + '</div>';
}
obj.append(toAdd);
setDimensions();
//make api call
getPictures(row * col, tag);
}
function setDimensions() {
//adjust width and height
$('.instaWrapper').css('width', (100/numCol) + '%');
var width = $(".instaWrapper")[0].getBoundingClientRect().width
$('.instaWrapper').css('width', width
- parseInt($(".instaWrapper").css("marginLeft"))
- parseInt($(".instaWrapper").css("marginRight")));
$('.instaWrapper').css('height', $('.instaWrapper').width());
$('.instaTextInner').css('max-height', $('.instaWrapper').height());
$('.instaTextInner').css('width', $('.instaWrapper').width());
$('.instaTextInner').css('font-size', getInstaFontSize()+'px');
}
//api call with number of pictures and the tag to load
function getPictures(num, tag) {
$.ajax({
url: "https://agile-taiga-1109.herokuapp.com/getData/" + tag,
dataType: 'jsonp',
type: 'GET',
success: function(result){
var jQueryData = jQuery.parseJSON(result.data);
setData(jQueryData.data, num);
},
error: function(xhr, status, error) {
}
});
}
function getInstaFontSize() {
return $('.instaWrapper').width() / 12 ;
}
//update web page with pictures
function setData(data, num) {
for (i=0; i < num && i < data.length; i++) {
$('#instaWrapper'+i).css("background","url("+data[i].images.low_resolution.url+")");
$('#instaWrapper'+i).css("background-size","cover");
var link = data[i].link;
$('#instaWrapper' + i).attr("href", link);
$('#instaWrapper' + i).click(function() {
window.location.href = $(this).attr("href");
});
var caption = data[i].caption.text;
$('#instaText'+i + ' .instaTextInner').text(caption);
}
//for now, upper bounded by number of pics returned from api
for (j=i; j < num; j++) {
$('#instaWrapper'+j).css("display","none");
}
}