Index: trunk/tools/wp-photocommons/search.js |
— | — | @@ -1,117 +1,118 @@ |
2 | 2 | // Debug |
3 | 3 | window.log = (console && console.log) ? console.log : function(){}; |
4 | 4 | |
5 | | -if ( typeof Photocommons == 'undefined' ) { |
| 5 | +if (!window.Photocommons) window.Photocommons = {}; |
6 | 6 | |
7 | | - window.Photocommons = { |
8 | | - |
9 | | - getQueryUrl: function( type, args ) { |
10 | | - var queries = { |
11 | | - 'pagesearch' : function(q) { |
12 | | - return { |
13 | | - 'action' : 'opensearch', |
14 | | - 'search' : q.search |
15 | | - }; |
16 | | - }, |
17 | | - |
18 | | - 'pageimages' : function(q) { |
19 | | - return { |
20 | | - 'action' : 'query', |
21 | | - 'prop' : 'images', |
22 | | - 'indexpageids' : '1', |
23 | | - 'titles' : q.title |
24 | | - }; |
25 | | - }, |
26 | | - |
27 | | - 'thumbs' : function(q) { |
28 | | - return { |
29 | | - 'action' : 'query', |
30 | | - 'prop' : 'imageinfo', |
31 | | - 'iiprop' : 'url', |
32 | | - 'iiurlwidth' : q.width, |
33 | | - 'indexpageids' : '1', |
34 | | - 'titles' : q.image |
35 | | - }; |
36 | | - } |
37 | | - }; |
| 7 | +$.extend( Photocommons, { |
| 8 | + |
| 9 | + getQueryUrl: function( type, args ) { |
| 10 | + var queries = { |
| 11 | + 'pagesearch' : function(q) { |
| 12 | + return { |
| 13 | + 'action' : 'opensearch', |
| 14 | + 'search' : q.search |
| 15 | + }; |
| 16 | + }, |
38 | 17 | |
39 | | - if (!queries[type]) { |
40 | | - throw new Error('Unknown query type'); |
41 | | - } |
| 18 | + 'pageimages' : function(q) { |
| 19 | + return { |
| 20 | + 'action' : 'query', |
| 21 | + 'prop' : 'images', |
| 22 | + 'indexpageids' : '1', |
| 23 | + 'titles' : q.title |
| 24 | + }; |
| 25 | + }, |
42 | 26 | |
43 | | - return Photocommons.makeUrl(queries[type](args)); |
44 | | - }, |
45 | | - makeUrl: function( args ) { |
46 | | - // default arguments |
47 | | - args = $.extend({ |
48 | | - 'format' : 'json', |
49 | | - 'callback' : '!noencode!?' |
50 | | - }, args); |
51 | | - |
52 | | - var url = 'http://commons.wikimedia.org/w/api.php'; |
53 | | - var first = true; |
54 | | - for (var key in args) { |
55 | | - var value = args[key]; |
56 | | - url += (first) ? '?' : '&'; |
57 | | - first = false; |
58 | | - |
59 | | - if (value.indexOf('!noencode!') === 0 && typeof value === 'string') { |
60 | | - value = value.slice(10); |
61 | | - } else { |
62 | | - value = encodeURIComponent(value); |
| 27 | + 'thumbs' : function(q) { |
| 28 | + return { |
| 29 | + 'action' : 'query', |
| 30 | + 'prop' : 'imageinfo', |
| 31 | + 'iiprop' : 'url', |
| 32 | + 'iiurlwidth' : q.width, |
| 33 | + 'indexpageids' : '1', |
| 34 | + 'titles' : q.image |
| 35 | + }; |
63 | 36 | } |
| 37 | + }; |
| 38 | + |
| 39 | + if (!queries[type]) { |
| 40 | + throw new Error('Unknown query type'); |
| 41 | + } |
| 42 | + |
| 43 | + return Photocommons.makeUrl(queries[type](args)); |
| 44 | + }, |
| 45 | + makeUrl: function( args ) { |
| 46 | + // default arguments |
| 47 | + args = $.extend({ |
| 48 | + 'format' : 'json', |
| 49 | + 'callback' : '!noencode!?' |
| 50 | + }, args); |
| 51 | + |
| 52 | + var url = 'http://commons.wikimedia.org/w/api.php'; |
| 53 | + var first = true; |
| 54 | + for (var key in args) { |
| 55 | + var value = args[key]; |
| 56 | + url += (first) ? '?' : '&'; |
| 57 | + first = false; |
| 58 | + |
| 59 | + if (value.indexOf('!noencode!') === 0 && typeof value === 'string') { |
| 60 | + value = value.slice(10); |
| 61 | + } else { |
| 62 | + value = encodeURIComponent(value); |
| 63 | + } |
| 64 | + |
| 65 | + url += key + '=' + value; |
| 66 | + } |
| 67 | + return url; |
| 68 | + }, |
| 69 | + init: function() { |
| 70 | + |
| 71 | + $('#search').autocomplete({ |
| 72 | + source : function(request, response) { |
| 73 | + var url = Photocommons.getQueryUrl('pagesearch', { |
| 74 | + 'search' : $('#search').val() |
| 75 | + }); |
64 | 76 | |
65 | | - url += key + '=' + value; |
66 | | - } |
67 | | - return url; |
68 | | - }, |
69 | | - init: function() { |
70 | | - |
71 | | - $('#search').autocomplete({ |
72 | | - source : function(request, response) { |
73 | | - var url = Photocommons.getQueryUrl('pagesearch', { |
74 | | - 'search' : $('#search').val() |
75 | | - }); |
| 77 | + $.getJSON(url, function(data) { |
| 78 | + response(data[1]); |
| 79 | + }); |
| 80 | + }, |
| 81 | + |
| 82 | + select : function(event, ui) { |
| 83 | + $('#images').empty(); |
| 84 | + $('#loading').show(); |
76 | 85 | |
77 | | - $.getJSON(url, function(data) { |
78 | | - response(data[1]); |
79 | | - }); |
80 | | - }, |
81 | | - |
82 | | - select : function(event, ui) { |
83 | | - $('#images').empty(); |
84 | | - $('#loading').show(); |
| 86 | + var url = Photocommons.getQueryUrl('pageimages', { |
| 87 | + 'title' : ui.item.value |
| 88 | + }); |
| 89 | + |
| 90 | + $.getJSON(url, function(data) { |
| 91 | + var pageid = data.query.pageids[0], |
| 92 | + query = data.query.pages[pageid].images; |
85 | 93 | |
86 | | - var url = Photocommons.getQueryUrl('pageimages', { |
87 | | - 'title' : ui.item.value |
88 | | - }); |
| 94 | + if (!query) { |
| 95 | + $('#images').html('No images found :('); |
| 96 | + } |
89 | 97 | |
90 | | - $.getJSON(url, function(data) { |
91 | | - var pageid = data.query.pageids[0], |
92 | | - query = data.query.pages[pageid].images; |
| 98 | + $.each(query, function() { |
| 99 | + var url = Photocommons.getQueryUrl('thumbs', { |
| 100 | + width : '200', |
| 101 | + image : this.title |
| 102 | + }); |
93 | 103 | |
94 | | - if (!query) { |
95 | | - $('#images').html('No images found :('); |
96 | | - } |
97 | | - |
98 | | - $.each(query, function() { |
99 | | - var url = Photocommons.getQueryUrl('thumbs', { |
100 | | - width : '200', |
101 | | - image : this.title |
102 | | - }); |
103 | | - |
104 | | - $.getJSON(url, function(data) { |
105 | | - var pageid = data.query.pageids[0], |
106 | | - src = data.query.pages[pageid].imageinfo[0].thumburl; |
107 | | - $('#images').append('<img src="' + src + '" style="display:none;"/>').find('img').fadeIn(); |
108 | | - }); |
| 104 | + $.getJSON(url, function(data) { |
| 105 | + var pageid = data.query.pageids[0], |
| 106 | + src = data.query.pages[pageid].imageinfo[0].thumburl; |
| 107 | + $('#images').append('<img src="' + src + '" style="display:none;"/>').find('img').fadeIn(); |
109 | 108 | }); |
110 | | - |
111 | | - $('#loading').hide(); |
112 | 109 | }); |
113 | | - } |
114 | | - }); |
115 | | - } |
| 110 | + |
| 111 | + $('#loading').hide(); |
| 112 | + }); |
| 113 | + } |
| 114 | + }); |
116 | 115 | } |
117 | | - $( document ).ready( Photocommons.init ); |
118 | | -} |
\ No newline at end of file |
| 116 | +}); |
| 117 | + |
| 118 | +// Init |
| 119 | +$( document ).ready( Photocommons.init ); |
\ No newline at end of file |