1 | <!-- |
---|
2 | This file is a demo of the OpenSearchStore, a simple wrapper to any OpenSearch compliant |
---|
3 | search engine. |
---|
4 | |
---|
5 | Note, the simple proxy requires a curl-enabled PHP install |
---|
6 | --> |
---|
7 | <html> |
---|
8 | <head> |
---|
9 | <title>Demo of OpenSearchStore</title> |
---|
10 | <style type="text/css"> |
---|
11 | |
---|
12 | @import "../../../dijit/themes/tundra/tundra.css"; |
---|
13 | @import "../../../dojo/resources/dojo.css"; |
---|
14 | @import "../../../dijit/tests/css/dijitTests.css"; |
---|
15 | @import "./openSearchDemo.css"; |
---|
16 | </style> |
---|
17 | |
---|
18 | <script type="text/javascript" src="../../../dojo/dojo.js" djConfig="isDebug: true, parseOnLoad: true"></script> |
---|
19 | |
---|
20 | <script type="text/javascript"> |
---|
21 | dojo.require("dojo.parser"); |
---|
22 | dojo.require("dijit.form.TextBox"); |
---|
23 | dojo.require("dijit.form.Button"); |
---|
24 | dojo.require("dijit.form.FilteringSelect"); |
---|
25 | dojo.require("dijit.form.CheckBox"); |
---|
26 | dojo.require("dijit.form.NumberSpinner"); |
---|
27 | dojo.require("dijit.Tree"); |
---|
28 | dojo.require("dojox.data.OpenSearchStore"); |
---|
29 | |
---|
30 | function init(){ |
---|
31 | var fViewWidgets = []; |
---|
32 | |
---|
33 | //Set up an onComplete handler for OpenSearchData |
---|
34 | function onComplete(items, request){ |
---|
35 | if(items.length > 0){ |
---|
36 | var ul = dojo.byId("searchResults"); |
---|
37 | var test; |
---|
38 | var li; |
---|
39 | for(var i=0; i<items.length; i++){ |
---|
40 | li = dojo.doc.createElement("li"); |
---|
41 | li.innerHTML = openSearchStore.getValue(items[i], "content"); |
---|
42 | ul.appendChild(li); |
---|
43 | } |
---|
44 | } |
---|
45 | statusWidget.attr('value', "PROCESSING COMPLETE."); |
---|
46 | } |
---|
47 | //What to do if a search fails... |
---|
48 | function onError(error, request){ |
---|
49 | statusWidget.attr('value', "PROCESSING ERROR."); |
---|
50 | } |
---|
51 | |
---|
52 | //Function to invoke the search of the openSearchStore |
---|
53 | function invokeSearch(){ |
---|
54 | var tbody = dojo.byId("searchResults"); |
---|
55 | while(tbody.childNodes.length){ |
---|
56 | var node = tbody.childNodes.item(0); |
---|
57 | node.parentNode.removeChild(node); |
---|
58 | } |
---|
59 | var request = { |
---|
60 | query: {}, |
---|
61 | onComplete: onComplete, |
---|
62 | onError: onError |
---|
63 | }; |
---|
64 | if(searchTermsWidget){ |
---|
65 | var searchTerms = searchTermsWidget.attr('value'); |
---|
66 | if(searchTerms && searchTerms !== ""){ |
---|
67 | var searchTermsArray = searchTerms.split(" "); |
---|
68 | searchTerms = ""; |
---|
69 | for(var i = 0; i < searchTermsArray.length; i++){ |
---|
70 | searchTerms = searchTerms + searchTermsArray[i]; |
---|
71 | if(i < (searchTermsArray.length - 1)){ |
---|
72 | searchTerms += "," |
---|
73 | } |
---|
74 | } |
---|
75 | request.query.searchTerms = searchTerms; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | if(countWidget){ |
---|
80 | request.count = countWidget.attr('value'); |
---|
81 | } |
---|
82 | |
---|
83 | if(statusWidget){ |
---|
84 | statusWidget.attr('value', "PROCESSING REQUEST"); |
---|
85 | } |
---|
86 | |
---|
87 | openSearchStore.fetch(request); |
---|
88 | } |
---|
89 | |
---|
90 | //Lastly, link up the search event. |
---|
91 | dojo.connect(dijit.byId("searchButton"), 'onClick', invokeSearch); |
---|
92 | var currentArgs = {url: 'http://intertwingly.net/search/'}; |
---|
93 | var oldProcess = null; |
---|
94 | function setTransform(state){ |
---|
95 | if(state){ |
---|
96 | oldProcess = openSearchStore.processItem; |
---|
97 | switch(currentArgs.url){ |
---|
98 | case 'http://intertwingly.net/search/': |
---|
99 | openSearchStore.processItem = intertwinglyTransform; |
---|
100 | break; |
---|
101 | case 'http://www.shutterpoint.com/opensearch.xml': |
---|
102 | openSearchStore.processItem = shutterpointTransform; |
---|
103 | break; |
---|
104 | case 'http://technorati.com/osd.xml': |
---|
105 | openSearchStore.processItem = technoratiTransform; |
---|
106 | break; |
---|
107 | } |
---|
108 | }else if(oldProcess !== null){ |
---|
109 | openSearchStore.processItem = oldProcess; |
---|
110 | } |
---|
111 | } |
---|
112 | dojo.connect(dijit.byId('transformItem'), 'onChange', function(state){ |
---|
113 | setTransform(state); |
---|
114 | }); |
---|
115 | dojo.connect(dijit.byId('urlSelector'), 'onChange', function(args){ |
---|
116 | currentArgs = dojo.fromJson(args); |
---|
117 | currentArgs.url = 'openSearchProxy.php?osd=true&url='+currentArgs.url; |
---|
118 | openSearchStore.close(); |
---|
119 | openSearchStore = new dojox.data.OpenSearchStore(currentArgs); |
---|
120 | if(dijit.byId('transformItem').checked){ |
---|
121 | setTransform(true); |
---|
122 | } |
---|
123 | }); |
---|
124 | |
---|
125 | var intertwinglyTransform = function(item, attribute){ |
---|
126 | function removeAll(/*NodeList*/list){ |
---|
127 | while(list.length) { |
---|
128 | var node = list.item(0); |
---|
129 | node.parentNode.removeChild(node); |
---|
130 | } |
---|
131 | } |
---|
132 | var content = item.node.getElementsByTagName("content").item(0); |
---|
133 | // Remove all blockquote elements |
---|
134 | removeAll(content.getElementsByTagName("blockquote")); |
---|
135 | // Remove all pre-formatted elements |
---|
136 | removeAll(content.getElementsByTagName("pre")); |
---|
137 | return openSearchStore._getNodeXml(content, true); |
---|
138 | }; |
---|
139 | |
---|
140 | var shutterpointTransform = function(item, attribute){ |
---|
141 | var description = item.node.getElementsByTagName("description").item(0); |
---|
142 | var div = dojo.doc.createElement("div"); |
---|
143 | div.innerHTML = description.childNodes.item(0).nodeValue; |
---|
144 | //Of the description children, remove the divs (to only leave the images) |
---|
145 | for(var i=0; i<div.childNodes.length; i++){ |
---|
146 | var node = div.childNodes.item(i); |
---|
147 | if(node.tagName.toLowerCase() === "div") |
---|
148 | node.parentNode.removeChild(node); |
---|
149 | } |
---|
150 | return openSearchStore._getNodeXml(div, true); |
---|
151 | }; |
---|
152 | |
---|
153 | var technoratiTransform = function(item, attribute){ |
---|
154 | function removeAll(/*NodeList*/list){ |
---|
155 | while(list.length) { |
---|
156 | var node = list.item(0); |
---|
157 | node.parentNode.removeChild(node); |
---|
158 | } |
---|
159 | } |
---|
160 | removeAll(item.node.getElementsByTagName("blockquote")); |
---|
161 | return item.innerHTML; |
---|
162 | }; |
---|
163 | } |
---|
164 | dojo.addOnLoad(init); |
---|
165 | </script> |
---|
166 | </head> |
---|
167 | |
---|
168 | <body class="tundra"> |
---|
169 | <h1>DEMO: OpenSearchStore Search</h1> |
---|
170 | <hr /> |
---|
171 | <h3>Description:</h3> |
---|
172 | <p> |
---|
173 | This simple demo shows how services, such as an OpenSearch compliant search service, can be wrapped by the datastore API. In this demo, you can search public search engines through a simple OpenSearchStore by specifying a series of search terms (separated by spaces) to search on. The results will be displayed below the search box. |
---|
174 | </p> |
---|
175 | <p> |
---|
176 | <b>NOTE: This demo makes use of a simple PHP based proxy script. The proxy script requires cURL support in PHP. Without cURL support, the demo will throw errors.</b> |
---|
177 | </p> |
---|
178 | <label for="urlSelector">URL of OpenSearchDocument:</label> |
---|
179 | <select dojoType="dijit.form.FilteringSelect" |
---|
180 | id="urlSelector" |
---|
181 | name="urlSelector" |
---|
182 | autoComplete="true"> |
---|
183 | <option value="{url: 'http://intertwingly.net/search/'}">http://intertwingly.net/search/</option> |
---|
184 | <option value="{url: 'http://www.shutterpoint.com/opensearch.xml'}">http://www.shutterpoint.com/opensearch.xml</option> |
---|
185 | <option value="{url: 'http://technorati.com/osd.xml', itemPath: '.hentry'}">http://technorati.com/osd.xml</option> |
---|
186 | </select> |
---|
187 | <label for="transformItem">Apply transform function?</label> |
---|
188 | <input dojoType="dijit.form.CheckBox" |
---|
189 | type="checkbox" |
---|
190 | id="transformItem" |
---|
191 | name="transformItem"> |
---|
192 | </input> |
---|
193 | <hr /> |
---|
194 | <label for="status">Status:</label> |
---|
195 | <div dojoType="dijit.form.TextBox" maxLength="50" id="status" name="status" jsId="statusWidget" disabled="true"></div> |
---|
196 | <label for="searchTerms">Search For:</label> |
---|
197 | <div dojoType="dijit.form.TextBox" maxLength="50" id="searchTerms" name="searchTerms" jsId="searchTermsWidget" value="javascript"></div> |
---|
198 | <label for="count">Number of Results:</label> |
---|
199 | <div id="count" |
---|
200 | name="count" |
---|
201 | jsId="countWidget" |
---|
202 | dojoType="dijit.form.NumberSpinner" |
---|
203 | value="20" |
---|
204 | constraints="{min:1,max:20}"> |
---|
205 | </div> |
---|
206 | <div dojoType="dijit.form.Button" label="Search" id="searchButton" jsId="searchButtonWidget"></div> |
---|
207 | <hr/> |
---|
208 | <div dojoType="dojox.data.OpenSearchStore" |
---|
209 | url="openSearchProxy.php?osd=true&url=http://intertwingly.net/search/" |
---|
210 | jsId="openSearchStore"> |
---|
211 | </div> |
---|
212 | <ul id="searchResults"></ul> |
---|
213 | |
---|
214 | </body> |
---|
215 | </html> |
---|