forked from adrianengine/jquery-spectragram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectragram.js
162 lines (135 loc) · 5.02 KB
/
spectragram.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*!
* jQuery - Spectragram by Adrian Quevedo
* http://adrianquevedo.com/ - http://lab.adrianquevedo.com/ - http://elnucleo.com.co/
*
* Dual licensed under the MIT or GPL Version 2 licenses.
* You are free to use this plugin in commercial projects as long as the copyright header is left intact.
*
* This plugin uses the Instagram(tm) API and is not endorsed or certified by Instagram or Burbn, inc.
* All Instagram(tm) logos and trademarks displayed on this plugin are property of Burbn, Inc.
*
* Date: Thu Jul 15 14:05:02 2012 -0500
*/
// Utility for older browsers
if (typeof Object.create !== 'function') {
Object.create = function (obj) {
function F() {};
F.prototype = obj;
return new F();
};
}
(function ($, window, document, undefined) {
var Instagram = {
//Initialize function
init: function (options, elem) {
var self = this;
self.elem = elem;
self.$elem = $(elem);
self.api = 'https://api.instagram.com/v1',
self.accessData = $.fn.spectragram.accessData,
self.options = $.extend({}, $.fn.spectragram.options, options);
},
//Users
//Get the most recent media published by a user.
getRecentMedia: function ( userID ) {
var self = this,
getData = '/users/' + userID + '/media/recent/?' + self.accessData.clientID + '&access_token='+ self.accessData.accessToken +'';
self.fetch(getData).done(function ( results ) {
self.display(results);
});
},
//Search for a user by name.
getUserFeed: function () {
var self = this,
getData = '/users/search?q=' + self.options.query + '&count=' + self.options.max + '&client_id=' + self.accessData.clientID + '';
self.fetch(getData).done(function ( results ) {
if(results.data.length){
self.getRecentMedia(results.data[0].id);
}else{
$.error('Spectagram.js - Error: the username ' + self.options.query + ' does not exist.');
};
});
},
//Media
//Get a list of what media is most popular at the moment
getPopular: function () {
var self = this,
getData = '/media/popular?client_id=' + self.accessData.clientID + '&access_token='+ self.accessData.accessToken + '';
self.fetch(getData).done(function ( results ) {
self.display(results);
});
},
//Tags
//Get a list of recently tagged media
getRecentTagged: function () {
var self = this,
getData = '/tags/' + self.options.query + '/media/recent?client_id=' + self.accessData.clientID + '&access_token='+ self.accessData.accessToken + '';
self.fetch(getData).done(function ( results ) {
if(results.data.length){
self.display(results);
}else{
$.error('Spectagram.js - Error: the tag ' + self.options.query + ' does not have results.');
};
});
},
fetch: function (getData) {
var self = this,
getUrl = self.api + getData;
return $.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: getUrl
});
},
display: function (results) {
var self = this,
setSize = self.options.size,
size, max = (self.options.max >= results.data.length) ? results.data.length : self.options.max;
if (results.data.length === 0) {
self.$elem.append($(self.options.wrapEachWith).append(self.options.notFoundMsg));
}
else {
for (var i = 0; i < max; i++) {
if (setSize == "small") {
size = results.data[i].images.thumbnail.url;
}
else if (setSize == "medium") {
size = results.data[i].images.low_resolution.url;
}
else {
size = results.data[i].images.standard_resolution.url;
}
self.$elem.append($(self.options.wrapEachWith).append("<a target='_blank' href='" + results.data[i].link + "'><img src='" + size + "'></img></a>"));
}
}
}
};
jQuery.fn.spectragram = function ( method, options ) {
if(jQuery.fn.spectragram.accessData.clientID){
this.each( function () {
var instagram = Object.create( Instagram );
instagram.init( options, this );
if( instagram[method] ) {
return instagram[method]( this );
}else{
$.error( 'Method ' + method + ' does not exist on jQuery.spectragram' );
}
});
}else{
$.error( 'You must define an accessToken and a clientID on jQuery.spectragram' );
}
};
//Plugin Default Options
jQuery.fn.spectragram.options = {
max: 10,
query: 'coffee',
size: 'medium',
wrapEachWith: '<li></li>'
};
//Instagram Access Data
jQuery.fn.spectragram.accessData = {
accessToken: null,
clientID: null
};
})(jQuery, window, document);