This repository was archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery-insta-bam.js
177 lines (137 loc) · 5.03 KB
/
jquery-insta-bam.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/** @preserve
* insta-bam
*
* Created at: 2012-11-20 21:22:09 +0100
* Updated at: 2013-11-19 16:21:40 +0100
*
* Author: @clrblnd (+ @ivow for the way of the code)
* Version: 1.2.2
*
*/
/*global jQuery:false console window*/
(function($, window) {
"use strict";
var tmpl_feed = '<a href="{{instagram_url}}" class="instabam"><img src="{{instagram}}" alt="{{caption}}" data-likes="{{likes}}" /><time datetime="{{created_at_iso}}" title="{{created_at_formatted}}">{{created_at_formatted}}</time></a>';
var plugin_name = 'instaBam',
defaults = {
doneCallback : null,
list : null,
tmpl_feed : tmpl_feed
},
ISODateString;
function InstaFeed(element, url, options){
this.element = element;
this.options = $.extend( {}, defaults, options );
this.url = url;
this.id = this.url.hashCode();
this.cache = window.sessionStorage[plugin_name + this.id];
this.init();
}
InstaFeed.prototype.init = function(){
var _this = this;
if ( window.JSON && window.Storage && _this.cache !== undefined) {
_this.output( $.parseJSON(_this.cache) );
} else {
$.ajax({
dataType: 'jsonp',
success: function( response_data ){
// Check if API response isn't an error
// http://instagram.com/developer/endpoints/
if ( response_data.meta.code !== 200 ) {
if ( $.isFunction(_this.options.onErrorAPI) ) {
_this.options.onErrorAPI.call( _this.element, response_data );
}
} else {
if ( window.JSON && window.Storage ) {
window.sessionStorage[plugin_name + _this.id] = JSON.stringify( response_data );
}
_this.output( response_data );
}
},
url: _this.url
});
}
};
InstaFeed.prototype.output = function( data ){
var _this = this,
$this = $(this.element),
feed_result = [],
$ul = this.options.list ? this.options.list : $('<ul />');
// check for whitelist data
if(_this.options.whitelist) {
if(_this.url.indexOf('https://api.instagram.com/v1/tags/') != -1) {
$.each( data.data, function(idx, value){
if ( $.inArray(value.user.username,_this.options.whitelist) != -1 ) {
feed_result.push(value);
}
});
} else if (_this.url.indexOf('https://api.instagram.com/v1/users/') != -1) {
$.each( data.data, function(idx, value){
$.each( value.tags, function(idy, tagvalue){
if ( $.inArray(tagvalue,_this.options.whitelist) != -1 ) {
feed_result.push(value);
}
});
});
} else {
// feed is all
feed_result = data.data;
}
} else {
// feed is all
feed_result = data.data;
}
// read out result
$.each( feed_result, function(idx, value) {
var $li = $('<li />'),
template = _this.options.tmpl_feed,
created_at = new window.Date( parseInt(value.created_time * 1000, 10) ),
created_at_formatted = created_at.getFullYear() + '-' + (created_at.getMonth() + 1) + '-' + created_at.getDate(),
created_at_iso = _this.createdAtISO( created_at ),
caption = value.caption ? value.caption.text : "";
template = template
.replace(/\{\{instagram_url\}\}/g , value.link)
.replace(/\{\{instagram\}\}/g , value.images.standard_resolution.url)
.replace(/\{\{caption\}\}/g , caption)
.replace(/\{\{likes\}\}/g , value.likes.count)
.replace(/\{\{created_at_iso\}\}/g , created_at_iso)
.replace(/\{\{created_at_formatted\}\}/g , created_at_formatted);
$li
.append( $(template) )
.appendTo( $ul );
});
// append content to element
$ul.appendTo( $this );
if ( $.isFunction( this.options.doneCallback) ) {
this.options.doneCallback.call( this.element );
}
};
InstaFeed.prototype.createdAtISO = function( date ) {
function pad(n) {
return n < 10 ? '0' + n : n;
}
return date.getUTCFullYear() + '-' + pad( date.getUTCMonth() + 1 ) + '-' + pad( date.getUTCDate() )+ 'T' + pad( date.getUTCHours() ) + ':' + pad( date.getUTCMinutes() ) + ':' + pad( date.getUTCSeconds() ) + 'Z';
};
// jQuery function
$.fn.instafeed = function( url, options ){
return this.each( function() {
$(this).data( plugin_name, new InstaFeed(this, url, options) );
var instabam_instance = $(this).data( 'instaBam' );
});
};
// Helpers
String.prototype.hashCode = function(){
var hash = 0,
i,
character;
if (this.length === 0) {
return hash;
}
for (i = 0; i < this.length; i++) {
character = this.charCodeAt(i);
hash = ((hash<<5)-hash)+character;
hash = hash & hash; // Convert to 32bit integer
}
return Math.abs(hash);
};
}(jQuery, window));