/public/javascripts/dojo/dojox/grid/compat/tests/yahooSearch.js

http://enginey.googlecode.com/ · JavaScript · 137 lines · 121 code · 8 blank · 8 comment · 12 complexity · c8b27a8a6faabaec2a23d76720302ab8 MD5 · raw file

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