-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.wtfstockphotos.js
80 lines (78 loc) · 2.33 KB
/
jquery.wtfstockphotos.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
// Generated by CoffeeScript 1.3.3
(function() {
(function($) {
return $.wtfstockphotos = function(el, options) {
var defaults, getNextImageUrl, init, plugin, replaceImages, shuffle;
defaults = {
limit: 30
};
plugin = this;
plugin.settings = {};
plugin.urls = [];
getNextImageUrl = function() {
var url;
url = plugin.urls.pop();
plugin.urls.unshift(url);
return url;
};
replaceImages = function(urls) {
var _i, _len, _ref, _results;
_ref = $(plugin.el);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
el = _ref[_i];
el.onerror = function(err) {
console.log("bad url");
return el.src = getNextImageUrl();
};
_results.push(el.src = getNextImageUrl());
}
return _results;
};
shuffle = function(array) {
var i, j, temp, _i, _ref;
for (i = _i = _ref = array.length - 1; _ref <= 1 ? _i <= 1 : _i >= 1; i = _ref <= 1 ? ++_i : --_i) {
j = Math.floor(Math.random() * (i + 1));
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
};
init = function() {
var req;
plugin.settings = $.extend({}, defaults, options);
plugin.el = el;
req = $.ajax({
url: "http://reddit.com/r/wtfstockphotos.json?limit=" + plugin.settings.limit,
dataType: "jsonp",
jsonp: "jsonp"
});
return req.done(function(res) {
var post, posts;
posts = (function() {
var _i, _len, _ref, _results;
_ref = res.data.children;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
post = _ref[_i];
_results.push(post);
}
return _results;
})();
plugin.urls = shuffle((function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = posts.length; _i < _len; _i++) {
post = posts[_i];
_results.push(post.data.url);
}
return _results;
})());
return replaceImages();
});
};
return init();
};
})(jQuery);
}).call(this);