forked from potomak/jquery-instagram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.instagram.js
136 lines (116 loc) · 3.99 KB
/
jquery.instagram.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
130
131
132
133
134
135
136
/*
* Instagram jQuery plugin
* v0.2.1
* http://potomak.github.com/jquery-instagram/
* Copyright (c) 2012 Giovanni Cappellotto
* Licensed MIT
*/
(function ($){
$.fn.instagram = function (options) {
var that = this,
apiEndpoint = "https://api.instagram.com/v1",
settings = {
hash: null
, userId: null
, locationId: null
, search: null
, accessToken: null
, clientId: null
, show: null
, onLoad: null
, onComplete: null
, maxId: null
, minId: null
, next_url: null
, image_size: null
, photoLink: true
};
options && $.extend(settings, options);
function createPhotoElement(photo) {
var image_url = photo.images.thumbnail.url;
if (settings.image_size == 'low_resolution') {
image_url = photo.images.low_resolution.url;
}
else if (settings.image_size == 'thumbnail') {
image_url = photo.images.thumbnail.url;
}
else if (settings.image_size == 'standard_resolution') {
image_url = photo.images.standard_resolution.url;
}
var innerHtml = $('<img>')
.addClass('instagram-image')
.attr('src', image_url);
if (settings.photoLink) {
innerHtml = $('<a>')
.attr('target', '_blank')
.attr('href', photo.link)
.append(innerHtml);
}
return $('<div>')
.addClass('instagram-placeholder')
.attr('id', photo.id)
.append(innerHtml);
}
function createEmptyElement() {
return $('<div>')
.addClass('instagram-placeholder')
.attr('id', 'empty')
.append($('<p>').html('No photos for this query'));
}
function composeRequestURL() {
var url = apiEndpoint,
params = {};
if (settings.next_url != null) {
return settings.next_url;
}
if (settings.hash != null) {
url += "/tags/" + settings.hash + "/media/recent";
}
else if (settings.search != null) {
url += "/media/search";
params.lat = settings.search.lat;
params.lng = settings.search.lng;
settings.search.max_timestamp != null && (params.max_timestamp = settings.search.max_timestamp);
settings.search.min_timestamp != null && (params.min_timestamp = settings.search.min_timestamp);
settings.search.distance != null && (params.distance = settings.search.distance);
}
else if (settings.userId != null) {
url += "/users/" + settings.userId + "/media/recent";
}
else if (settings.locationId != null) {
url += "/locations/" + settings.locationId + "/media/recent";
}
else {
url += "/media/popular";
}
settings.accessToken != null && (params.access_token = settings.accessToken);
settings.clientId != null && (params.client_id = settings.clientId);
settings.minId != null && (params.min_id = settings.minId);
settings.maxId != null && (params.max_id = settings.maxId);
settings.show != null && (params.count = settings.show);
url += "?" + $.param(params)
return url;
}
settings.onLoad != null && typeof settings.onLoad == 'function' && settings.onLoad();
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: composeRequestURL(),
success: function (res) {
var length = typeof res.data != 'undefined' ? res.data.length : 0;
var limit = settings.show != null && settings.show < length ? settings.show : length;
if (limit > 0) {
for (var i = 0; i < limit; i++) {
that.append(createPhotoElement(res.data[i]));
}
}
else {
that.append(createEmptyElement());
}
settings.onComplete != null && typeof settings.onComplete == 'function' && settings.onComplete(res.data, res);
}
});
return this;
};
})(jQuery);