| 1 |
dojo.require("dojox.mobile.app.SceneAssistant"); |
| 2 |
|
| 3 |
dojo.declare("FlickrSearchTextAssistant", dojox.mobile.app.SceneAssistant, { |
| 4 |
|
| 5 |
setup: function(){ |
| 6 |
|
| 7 |
|
| 8 |
this.controller.parse(); |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
this.apiKey = "8c6803164dbc395fb7131c9d54843627"; |
| 13 |
|
| 14 |
this.listWidget = dijit.byId("searchList"); |
| 15 |
this.textWidget = dijit.byId("searchTextInput"); |
| 16 |
|
| 17 |
this.listWidget.set("items", []); |
| 18 |
|
| 19 |
var _this = this; |
| 20 |
|
| 21 |
this.search = dojo.hitch(this, this.search); |
| 22 |
this.handleGroupResults = dojo.hitch(this, this.handleGroupResults); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
this.connect(this.textWidget, "onChange", function(value){ |
| 28 |
|
| 29 |
|
| 30 |
if(!value || value.length == 0){ |
| 31 |
_this.listWidget.set("items", []); |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
if(!_this.timer){ |
| 36 |
_this.timer = setTimeout(_this.search, 300); |
| 37 |
} |
| 38 |
}); |
| 39 |
|
| 40 |
this.connect(this.listWidget, "onSelect", function(item, index, node){ |
| 41 |
console.log("select", arguments); |
| 42 |
|
| 43 |
_this.controller.stageController.pushScene("flickr-image-view", |
| 44 |
dojo.mixin({type: "group"}, item)); |
| 45 |
}); |
| 46 |
}, |
| 47 |
|
| 48 |
activate: function(searchType){ |
| 49 |
|
| 50 |
this.searchType = searchType; |
| 51 |
|
| 52 |
|
| 53 |
if(searchType){ |
| 54 |
var sceneHeader = dijit.byId("searchHeader"); |
| 55 |
switch(searchType){ |
| 56 |
case "group": |
| 57 |
sceneHeader.setLabel("Search Flickr Groups"); |
| 58 |
break; |
| 59 |
case "user": |
| 60 |
sceneHeader.setLabel("Search Flickr People"); |
| 61 |
break; |
| 62 |
case "tag": |
| 63 |
sceneHeader.setLabel("Search Flickr Tags"); |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
dijit.byId("searchTextInput").focus(); |
| 69 |
}, |
| 70 |
|
| 71 |
search: function(){ |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if(this.timer){ |
| 76 |
clearTimeout(this.timer); |
| 77 |
this.timer = null; |
| 78 |
} |
| 79 |
|
| 80 |
var searchText = this.textWidget.set("value"); |
| 81 |
|
| 82 |
if(!searchText || dojo.trim(searchText).length < 1){ |
| 83 |
this.listWidget.set("items", []); |
| 84 |
|
| 85 |
console.log("NOT SEARCHING"); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
var url = "http://api.flickr.com/services/rest/?method=flickr.groups.search"; |
| 90 |
|
| 91 |
var deferred = dojo.io.script.get({ |
| 92 |
url: url, |
| 93 |
content: { |
| 94 |
api_key: this.apiKey, |
| 95 |
format: "json", |
| 96 |
text: searchText, |
| 97 |
perPage: 10 |
| 98 |
}, |
| 99 |
jsonp: "jsoncallback" |
| 100 |
}); |
| 101 |
deferred.addBoth(this.handleGroupResults); |
| 102 |
}, |
| 103 |
|
| 104 |
handleGroupResults: function(res){ |
| 105 |
var groups; |
| 106 |
if(res && res.groups && res.groups.group){ |
| 107 |
groups = res.groups.group; |
| 108 |
|
| 109 |
for(var i = 0; i < groups.length; i++){ |
| 110 |
groups[i].label = groups[i].name; |
| 111 |
} |
| 112 |
}else{ |
| 113 |
groups = []; |
| 114 |
} |
| 115 |
this.listWidget.set("items", groups); |
| 116 |
} |
| 117 |
|
| 118 |
}); |