public
Description: Visual eBay search.
Homepage: http://apps.yahoo.com/-5WLKJH5c
Clone URL: git://github.com/reid/shopper.git
shopper / index.html
100644 334 lines (260 sloc) 8.04 kb
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<script>
var IRShopper = function () {
 
    var defaults = {};
 
    var $ = function (el) {
        return document.getElementById(el);
    };
 
    var attachEvent = function (ev, el, cb) {
        if (el.addEventListener) {
            el.addEventListener(ev, cb, false);
        } else if (el.attachEvent) {
            el.attachEvent('on' + ev, cb);
        }
    };
 
    var defaultInput = function (args) {
        var el = $(args.id);
 
        if (!el) return false;
 
        if (el.value == '') el.value = args.value;
        if (args.classBlur) el.className = args.classBlur;
 
        attachEvent('blur', el, function () {
            if (el.value !== '') return;
            el.value = args.value;
            if (args.classBlur) el.className = args.classBlur;
        });
 
        attachEvent('focus', el, function () {
            if (el.value !== args.value) return;
            el.value = '';
            if (args.classFocus) el.className = args.classFocus;
        });
    };
 
    var submit = function () {
        query(
            $('item').value,
            $('min_price').value,
            $('max_price').value
        );
    };
 
    var query = function (q, min, max) {
 
        if (q.length === 0) {
            $('results').innerHTML = 'Please supply an item name!';
            return;
        }
 
        $('shopper-spinner').style.display = 'inline';
        $('shopper-status').innerHTML = 'Fetching...';
 
        var callback = function (req) {
 
            $('shopper-status').innerHTML = 'Processing...';
 
            var complete = function (results) {
 
                if (!results) {
                    results = 'No items, try again?';
                } else {
                    results = '<div class="clear"></div>' + results;
                }
 
                $('results').innerHTML = results;
                $('shopper-status').innerHTML = '';
                $('shopper-spinner').style.display = 'none';
                $('item').focus();
 
            };
 
            if (
                !req.hasOwnProperty("data") || !req.data
            ) return complete(false);
 
            var data = req.data,
                results = '',
                auctions = [],
                items = data.value.items,
                i = 0,
                l = items.length;
 
            if (!data.count) complete(false);
 
            $('shopper-status').innerHTML = 'Processing ' + data.count + ' items...';
 
            results += 'Found ' + data.count + ' items on eBay. Auctions ending sooner appear first.';
 
            for (; i < l ; i++) auctions.push(items[i].description);
 
            var START = '<div class="ebay-item">',
                END = '</div>';
 
            results += START + auctions.join(END + START) + END;
 
            complete(results);
 
        };
 
        if (min.charAt(0) === 'M') min = 0;
        if (max.charAt(0) === 'M') max = 9999999;
 
        q = encodeURIComponent(q);
        min = encodeURIComponent(min);
        max = encodeURIComponent(max);
 
        var url = 'http://pipes.yahoo.com/pipes/pipe.run?MaxPrice=' + max + '&MinPrice=' + min + '&_id=1c67a784df61eb40cf46224adf6f5011&_render=json&textinput1=' + q;
 
        var params = {};
        params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
        params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.NONE;
 
        gadgets.io.makeRequest(url, callback, params);
    };
 
    var announce = function () {
 
        var callback = function (req) {
            if (req.hadError()) $('shopper-status').innerHTML = 'Not shared, login first!';
        };
 
        $('shopper-status').innerHTML = 'Posted!';
 
        var activity = {},
            item = $('item').value;
 
        if (item.length === 0) {
            $('shopper-status').innerHTML = 'Nothing to share!';
            return;
        }
 
        activity[opensocial.Activity.Field.TITLE] = 'Found a great deal on ' + item + ' using Shopper!';
        activity[opensocial.Activity.Field.BODY] = 'Search eBay and get what you want with Shopper!';
        activity[opensocial.Activity.Field.URL] = '';
 
        opensocial.requestCreateActivity(
            opensocial.newActivity(activity),
            opensocial.CreateActivityPriority.HIGH,
            callback
        );
 
    };
 
    var setQuery = function (q) {
        defaults.query = q;
    };
 
    var init = function () {
 
        $('results').innerHTML = 'Type an item!';
 
        attachEvent('submit', $('shop'), function (e) {
            e.preventDefault();
            submit();
        });
 
        attachEvent('click', $('shop-announce'), announce);
 
        defaultInput({
            id: 'min_price',
            value: 'Minimum Price ($)',
            classFocus: 'shop-focus',
            classBlur: 'shop-blur'
        });
 
        defaultInput({
            id: 'max_price',
            value: 'Maximum Price ($)',
            classFocus: 'shop-focus',
            classBlur: 'shop-blur'
        });
 
        // ImageCache workaround
        var img = document.createElement('img');
        img.src = 'http://apps.idearefuge.com/spin.gif';
        $('shopper-spinner').appendChild(img);
 
        if (defaults.hasOwnProperty("query")) {
            $('item').value = defaults.query;
            // Invoking submit immediately causes unexpected behavior.
            // setTimeout makes submit work as expected.
            window.setTimeout(submit, 0);
            return;
        }
 
        $('item').focus();
    };
 
    return {
        init: init,
        setQuery: setQuery
    };
 
}();
 
gadgets.util.registerOnLoadHandler(IRShopper.init);
 
</script>
<style>
body {
    margin: 0;
    padding: 0;
}
 
#ir-app {
    font-family: Helvetica, Arial, sans-serif;
}
 
h1, h2, h3 {
    margin: 0;
    padding: 0;
}
 
a {
    color: #369;
}
 
#hd {
    color: #369;
    background: #d4ebe0;
    padding: 15px 15px 10px;
    border-bottom: 1px solid #ccc;
}
 
#shopper {
    padding: 5px;
    margin: 0;
}
 
#shopper form {
    margin: 0;
}
 
h1 {
    letter-spacing: -1px;
}
 
#ir-app h1, #hd span {
    font-style: italic;
    display: inline;
    padding-right: 5px;
}
 
#listings {
    padding: 3px 5px 3px;
    border-top: 1px solid #ccc;
}
 
#ft {
    padding: 20px 5px;
    text-align: center;
    text-transform: uppercase;
    font-size: 70%;
    color: #ccc;
}
 
#ft a {
    color: #bbb;
}
 
#ft, .clear {
    clear: both;
}
 
.ebay-item {
    float: left;
}
 
.ebay-item td {
    padding: 0;
    margin: 0;
    font-size: 70%;
    display: block;
    width: 100px;
    height: 100px;
}
 
.shop-blur {
    color: #666;
    text-transform: uppercase;
}
 
.shop-focus {
    color: #000;
    text-transform: none;
}
 
.hide {
    display: none;
}
 
#shopper-spinner {
    margin: 5px auto;
    text-align: center;
}
</style>
<div id="ir-app">
<div id="hd">
    <h1>Shopper!</h1>
    <span>Fast visual eBay shopping</span>
</div>
<div id="bd">
    <div id="shopper">
    <form id="shop">
            <label for="item">Find: </label>
            <input type="text" id="item">
            <label for="min_price" class="hide">Min: $</label>
            <input type="text" id="min_price" value="" size="15">
            <label for="max_price" class="hide">Max: $</label>
            <input type="text" id="max_price" value="" size="15">
            <input type="submit" value="Go!">
            <input type="button" value="Announce" id="shop-announce">
            <span id="shopper-spinner" style="display:none;"></span>
            <span id="shopper-status"></span>
        </form>
    </div>
    <div id="listings">
        <div id="results">
            Loading...
        </div>
    </div>
</div>
<div id="ft">
    A <a href="http://reidburke.com/">Reid Burke</a> App
    &mdash; <a href="http://github.com/reid/shopper">Source on GitHub</a>
    &mdash; <a href="http://pipes.yahoo.com/">Yahoo! Pipes</a>, <a href="http://ebay.com/">eBay</a>, and <a href="http://developer.yahoo.com/yap/">YAP</a>
    &mdash; &copy; 2009
</div>
</div>