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
|
<!doctype html>
<html>
<head>
<title>Photoblaster Search Widget</title>
<style type="text/css">
form { display: inline-block; }
#images img { max-width: 200px; height: 90px; margin: 5px; }
#result { height: 500px; }
#result img { max-height: 500px; }
</style>
<body>
<div id="ui">
<form id="search">
<input type="text" id="search-query" value="duck bill">
<button id="search">DUMP SEARCH</button>
</form>
<button id="random">IM RANDOM</button>
<span id="status"></span>
<div id="images"></div>
</div>
<div id="result">
<input type="text" id="url">
<div id="image"></div>
</div>
</body>
<script type="text/javascript" src="js/vendor/jquery/jquery.min.js"></script>
<script type="text/javascript">
var ui = {}
ui.init = function(){
ui.bind()
}
ui.bind = function(){
$("#random").click(asdf.random)
$("#search").submit(dumpfm.search)
$(document).on("click", "#images img", gallery.choose)
status("ready")
}
//
// gallery .. the most basic image picker
var gallery = {}
gallery.load = function(ims){
status("loading " + ims.length + " images");
$("#images").empty();
for (var i = 0; i < ims.length; i++) {
gallery.image(ims[i]);
}
}
gallery.image = function(im){
var img = new Image ();
img.onload = function(){
$("#images").append(img);
}
try { img.src = im.url; }
catch(e){ return; }
if (img.complete) img.onload();
}
gallery.choose = function(){
$("#image").empty().append( $(this).clone() )
$("#result #url").val(this.src);
}
//
// asdf .. asdf.us/im search api
var asdf = { fetching: false }
asdf.random = function (e){
status("fetching random photoblasts")
e.preventDefault();
opt = {};
opt.data = { random: 1 };
opt.success = gallery.load;
asdf.fetch(opt);
}
asdf.fetch = function (opt){
if (asdf.fetching) return;
asdf.fetching = true;
var params = {random:1};
$.ajax({
'url': "http://asdf.us/cgi-bin/im/list",
'data': params,
'dataType': "jsonp",
'success': function(urls){
console.log(urls);
asdf.fetching = false;
opt.success && opt.success(urls)
},
'error': opt.error || function(err){
console.log(err);
}
});
}
//
// dumpfm .. dump.fm/search api
var dumpfm = {}
dumpfm.search = function (e){
e.preventDefault();
var term = $("#search-query").val().toLowerCase()
var rawTokens = term.split(" ")
var tokens = []
rawTokens.forEach(function(t){ if (t.length > 2) tokens.push(t) })
if (tokens.length == 0 || tokens[0] == "undefined") {
status("search query too small")
} else {
status("searching for '"+tokens.join(" and ")+"'")
dumpfm.fetch({
query: tokens.join("+"),
success: gallery.load
})
}
};
dumpfm.fetch = function(opt){
if (dumpfm.fetching) return;
dumpfm.fetching = true;
var params = {};
$.ajax({
'url': "http://dump.fm/cmd/search/" + opt.query,
'data': params,
'dataType': "jsonp",
'success': function(urls){
dumpfm.fetching = false;
for (var i = 0, len = urls.length; i < len; i++) {
var url = urls[i].url;
if (url.match(/^\d+\//)) {
url = "http://dump.fm/images/" + url;
}
else {
url = "http://" + url;
}
urls[i].url = url;
}
opt.success && opt.success(urls)
},
'error': opt.error || function(err){
console.log(err);
}
});
}
function status(s){ $("#status").html(s); console.log(s) }
$(ui.init);
</script>
</html>
|