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