2150 |
mathias |
1 |
// model that works with Yahoo Search API
|
|
|
2 |
dojo.declare("dojox.grid.data.yahooSearch", dojox.grid.data.dynamic,
|
|
|
3 |
function(inFields, inData, inSearchNode) {
|
|
|
4 |
this.rowsPerPage = 20;
|
|
|
5 |
this.searchNode = inSearchNode;
|
|
|
6 |
this.fieldNames = [];
|
|
|
7 |
for (var i=0, f; (f=inFields[i]); i++)
|
|
|
8 |
this.fieldNames.push(f.name);
|
|
|
9 |
}, {
|
|
|
10 |
// server send / receive
|
|
|
11 |
encodeParam: function(inName, inValue) {
|
|
|
12 |
return dojo.string.substitute('&${0}=${1}', [inName, inValue]);
|
|
|
13 |
},
|
|
|
14 |
getQuery: function() {
|
|
|
15 |
return dojo.byId(this.searchNode).value.replace(/ /g, '+');
|
|
|
16 |
},
|
|
|
17 |
getParams: function(inParams) {
|
|
|
18 |
var url = this.url;
|
|
|
19 |
url += '?appid=foo';
|
|
|
20 |
inParams = inParams || {};
|
|
|
21 |
inParams.output = 'json';
|
|
|
22 |
inParams.results = this.rowsPerPage;
|
|
|
23 |
inParams.query = this.getQuery();
|
|
|
24 |
for (var i in inParams)
|
|
|
25 |
if (inParams[i] != undefined)
|
|
|
26 |
url += this.encodeParam(i, inParams[i]);
|
|
|
27 |
return url;
|
|
|
28 |
},
|
|
|
29 |
send: function(inAsync, inParams, inOnReceive, inOnError) {
|
|
|
30 |
var
|
|
|
31 |
p = this.getParams(inParams),
|
|
|
32 |
d = dojo.xhrPost({
|
|
|
33 |
url: "support/proxy.php",
|
|
|
34 |
content: {url: p },
|
|
|
35 |
contentType: "application/x-www-form-urlencoded; charset=utf-8",
|
|
|
36 |
handleAs: 'json-comment-filtered',
|
|
|
37 |
sync: !inAsync
|
|
|
38 |
});
|
|
|
39 |
d.addCallbacks(dojo.hitch(this, "receive", inOnReceive, inOnError), dojo.hitch(this, "error", inOnError));
|
|
|
40 |
this.onSend(inParams);
|
|
|
41 |
return d;
|
|
|
42 |
},
|
|
|
43 |
receive: function(inOnReceive, inOnError, inData) {
|
|
|
44 |
try {
|
|
|
45 |
inData = inData.ResultSet;
|
|
|
46 |
inOnReceive(inData);
|
|
|
47 |
this.onReceive(inData);
|
|
|
48 |
} catch(e) {
|
|
|
49 |
if (inOnError)
|
|
|
50 |
inOnError(inData);
|
|
|
51 |
}
|
|
|
52 |
},
|
|
|
53 |
error: function(inOnError, inErr) {
|
|
|
54 |
var m = 'io error: ' + inErr.message;
|
|
|
55 |
alert(m);
|
|
|
56 |
if (inOnError)
|
|
|
57 |
inOnError(m);
|
|
|
58 |
},
|
|
|
59 |
fetchRowCount: function(inCallback) {
|
|
|
60 |
this.send(true, inCallback );
|
|
|
61 |
},
|
|
|
62 |
// request data
|
|
|
63 |
requestRows: function(inRowIndex, inCount) {
|
|
|
64 |
inRowIndex = (inRowIndex == undefined ? 0 : inRowIndex);
|
|
|
65 |
var params = {
|
|
|
66 |
start: inRowIndex + 1
|
|
|
67 |
}
|
|
|
68 |
this.send(true, params, dojo.hitch(this, this.processRows));
|
|
|
69 |
},
|
|
|
70 |
// server callbacks
|
|
|
71 |
processRows: function(inData) {
|
|
|
72 |
for (var i=0, l=inData.totalResultsReturned, s=inData.firstResultPosition; i<l; i++) {
|
|
|
73 |
this.setRow(inData.Result[i], s - 1 + i);
|
|
|
74 |
}
|
|
|
75 |
// yahoo says 1000 is max results to return
|
|
|
76 |
var c = Math.min(1000, inData.totalResultsAvailable);
|
|
|
77 |
if (this.count != c) {
|
|
|
78 |
this.setRowCount(c);
|
|
|
79 |
this.allChange();
|
|
|
80 |
this.onInitializeData(inData);
|
|
|
81 |
}
|
|
|
82 |
},
|
|
|
83 |
getDatum: function(inRowIndex, inColIndex) {
|
|
|
84 |
var row = this.getRow(inRowIndex);
|
|
|
85 |
var field = this.fields.get(inColIndex);
|
|
|
86 |
return (inColIndex == undefined ? row : (row ? row[field.name] : field.na));
|
|
|
87 |
},
|
|
|
88 |
// events
|
|
|
89 |
onInitializeData: function() {
|
|
|
90 |
},
|
|
|
91 |
onSend: function() {
|
|
|
92 |
},
|
|
|
93 |
onReceive: function() {
|
|
|
94 |
}
|
|
|
95 |
});
|
|
|
96 |
|
|
|
97 |
// report
|
|
|
98 |
modelChange = function() {
|
|
|
99 |
var n = dojo.byId('rowCount');
|
|
|
100 |
if (n)
|
|
|
101 |
n.innerHTML = dojo.string.substitute('about ${0} row(s)', [model.count]);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
// some data formatters
|
|
|
106 |
getCellData = function(inCell, inRowIndex, inField) {
|
|
|
107 |
var m = inCell.grid.model;
|
|
|
108 |
return m.getDatum(inRowIndex, inField);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
formatLink = function(inData, inRowIndex) {
|
|
|
112 |
if (!inData)
|
|
|
113 |
return ' ';
|
|
|
114 |
var text = getCellData(this, inRowIndex, this.extraField);
|
|
|
115 |
return dojo.string.substitute('<a target="_blank" href="${href}">${text}</a>', {href: inData, text: text });
|
|
|
116 |
};
|
|
|
117 |
|
|
|
118 |
formatImage = function(inData, inRowIndex) {
|
|
|
119 |
if (!inData)
|
|
|
120 |
return ' ';
|
|
|
121 |
var info = getCellData(this, inRowIndex, this.extraField);
|
|
|
122 |
var o = {
|
|
|
123 |
href: inData,
|
|
|
124 |
src: info.Url,
|
|
|
125 |
width: info.Width,
|
|
|
126 |
height: info.Height
|
|
|
127 |
}
|
|
|
128 |
return dojo.string.substitute('<a href="${href}" target="_blank"><img border=0 src="${src}" width="${width}" height="${height}"></a>', o);
|
|
|
129 |
};
|
|
|
130 |
|
|
|
131 |
formatDate = function(inDatum, inRowIndex) {
|
|
|
132 |
if (!inDatum)
|
|
|
133 |
return ' ';
|
|
|
134 |
var d = new Date(inDatum * 1000);
|
|
|
135 |
return dojo.string.substitute("${0}/${1}/${2}",[d.getMonth()+1, d.getDate(), d.getFullYear()])
|
|
|
136 |
};
|
|
|
137 |
|
|
|
138 |
formatDimensions = function(inData, inRowIndex) {
|
|
|
139 |
if (!inData)
|
|
|
140 |
return ' ';
|
|
|
141 |
var w = inData, h = getCellData(this, inRowIndex, this.extraField);
|
|
|
142 |
return w + ' x ' + h;
|
|
|
143 |
}
|