| 1 |
dojo.require("dojox.mobile.app.SceneAssistant"); |
| 2 |
|
| 3 |
dojo.declare("FlickrImageThumbViewAssistant", dojox.mobile.app.SceneAssistant, { |
| 4 |
|
| 5 |
apiKey: "8c6803164dbc395fb7131c9d54843627", |
| 6 |
|
| 7 |
setup: function(){ |
| 8 |
|
| 9 |
|
| 10 |
this.controller.parse(); |
| 11 |
|
| 12 |
this.handlePhotoLoad = dojo.hitch(this, this.handlePhotoLoad); |
| 13 |
this.search = dojo.hitch(this, this.search); |
| 14 |
this.resizeViewer = dojo.hitch(this, this.resizeViewer); |
| 15 |
|
| 16 |
this.textWidget = dijit.byId("searchTextThumbInput"); |
| 17 |
|
| 18 |
var viewer = this.viewer = dijit.byId("flickrImageThumbView"); |
| 19 |
|
| 20 |
|
| 21 |
viewer.labelParam = "ownername"; |
| 22 |
|
| 23 |
var _this = this; |
| 24 |
|
| 25 |
this.connect(viewer, "onSelect", function(item, index){ |
| 26 |
console.log("selected ", item, index); |
| 27 |
|
| 28 |
_this.controller.stageController.pushScene("flickr-image-view",{ |
| 29 |
type: "data", |
| 30 |
images: _this.urls, |
| 31 |
index: index |
| 32 |
}); |
| 33 |
}); |
| 34 |
|
| 35 |
this.connect(this.textWidget, "onChange", function(value){ |
| 36 |
if(!value || value.length == 0){ |
| 37 |
_this.viewer.set("items", []); |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if(!_this.timer){ |
| 42 |
_this.timer = setTimeout(_this.search, 1000); |
| 43 |
} |
| 44 |
}); |
| 45 |
|
| 46 |
this.connect(dijit.byId("btnSmall"), "onClick", function(){ |
| 47 |
_this.setThumbSize("small"); |
| 48 |
}); |
| 49 |
this.connect(dijit.byId("btnMedium"), "onClick", function(){ |
| 50 |
_this.setThumbSize("medium"); |
| 51 |
}); |
| 52 |
this.connect(dijit.byId("btnLarge"), "onClick", function(){ |
| 53 |
_this.setThumbSize("large"); |
| 54 |
}); |
| 55 |
|
| 56 |
var resizeTimer; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
this.connect(window, "resize", function(){ |
| 62 |
if(resizeTimer){ |
| 63 |
clearTimeout(resizeTimer); |
| 64 |
} |
| 65 |
resizeTimer = setTimeout(_this.resizeViewer, 300); |
| 66 |
}); |
| 67 |
}, |
| 68 |
|
| 69 |
activate: function(options){ |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
if (!this.dataType) { |
| 74 |
|
| 75 |
this.dataType = (options ? options.type : "interesting"); |
| 76 |
|
| 77 |
switch (this.dataType) { |
| 78 |
case "interesting": |
| 79 |
|
| 80 |
dojo.style(this.textWidget.domNode, "display", "none"); |
| 81 |
this.loadInteresting(); |
| 82 |
break; |
| 83 |
case "text": |
| 84 |
dojo.style(this.textWidget.domNode, "visibility", "visible"); |
| 85 |
this.loadText(this.textWidget.set("value")); |
| 86 |
break; |
| 87 |
case "tag": |
| 88 |
|
| 89 |
|
| 90 |
dojo.style(this.textWidget.domNode, "visibility", "visible"); |
| 91 |
this.loadTags(this.textWidget.set("value")); |
| 92 |
break; |
| 93 |
default: |
| 94 |
console.log("unknown type " + this.dataType, options); |
| 95 |
} |
| 96 |
} |
| 97 |
}, |
| 98 |
|
| 99 |
setThumbSize: function(cls){ |
| 100 |
dojo.removeClass(this.viewer.domNode, ["small", "medium", "large"]); |
| 101 |
dojo.addClass(this.viewer.domNode, cls); |
| 102 |
this.resizeViewer(); |
| 103 |
}, |
| 104 |
|
| 105 |
resizeViewer: function(){ |
| 106 |
this.viewer.resize(); |
| 107 |
}, |
| 108 |
|
| 109 |
search: function(){ |
| 110 |
if(this.timer){ |
| 111 |
clearTimeout(this.timer); |
| 112 |
this.timer = null; |
| 113 |
} |
| 114 |
|
| 115 |
var searchText = this.textWidget.set("value"); |
| 116 |
|
| 117 |
if(!searchText || dojo.trim(searchText).length < 1){ |
| 118 |
this.viewer.set("items", []); |
| 119 |
|
| 120 |
console.log("NOT SEARCHING"); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
console.log("search", searchText); |
| 125 |
switch(this.dataType){ |
| 126 |
case "text": |
| 127 |
this.loadText(searchText); |
| 128 |
break; |
| 129 |
case "tag": |
| 130 |
|
| 131 |
|
| 132 |
this.loadTags(searchText); |
| 133 |
break; |
| 134 |
} |
| 135 |
}, |
| 136 |
|
| 137 |
loadInteresting: function(){ |
| 138 |
console.log("loading interesting"); |
| 139 |
|
| 140 |
var url = "http://api.flickr.com/services/rest/?method=" + |
| 141 |
"flickr.interestingness.getList"; |
| 142 |
|
| 143 |
var deferred = dojo.io.script.get({ |
| 144 |
url: url, |
| 145 |
content: { |
| 146 |
api_key: this.apiKey, |
| 147 |
format: "json", |
| 148 |
per_page: 20 |
| 149 |
}, |
| 150 |
jsonp: "jsoncallback" |
| 151 |
}); |
| 152 |
deferred.addBoth(this.handlePhotoLoad); |
| 153 |
}, |
| 154 |
|
| 155 |
loadText: function(text){ |
| 156 |
console.log("loading text ", text); |
| 157 |
|
| 158 |
var url = "http://api.flickr.com/services/rest/?method=" + |
| 159 |
"flickr.photos.search"; |
| 160 |
|
| 161 |
var deferred = dojo.io.script.get({ |
| 162 |
url: url, |
| 163 |
content: { |
| 164 |
api_key: this.apiKey, |
| 165 |
format: "json", |
| 166 |
text: text, |
| 167 |
extras: "owner_name", |
| 168 |
per_page: 20 |
| 169 |
}, |
| 170 |
jsonp: "jsoncallback" |
| 171 |
}); |
| 172 |
deferred.addBoth(this.handlePhotoLoad); |
| 173 |
}, |
| 174 |
|
| 175 |
loadTags: function(text){ |
| 176 |
console.log("loading tags ", text); |
| 177 |
|
| 178 |
var url = "http://api.flickr.com/services/rest/?method=" + |
| 179 |
"flickr.photos.search"; |
| 180 |
|
| 181 |
var deferred = dojo.io.script.get({ |
| 182 |
url: url, |
| 183 |
content: { |
| 184 |
api_key: this.apiKey, |
| 185 |
format: "json", |
| 186 |
tags: text, |
| 187 |
per_page: 20 |
| 188 |
}, |
| 189 |
jsonp: "jsoncallback" |
| 190 |
}); |
| 191 |
deferred.addBoth(this.handlePhotoLoad); |
| 192 |
}, |
| 193 |
|
| 194 |
handlePhotoLoad: function(res){ |
| 195 |
console.log("got photos", res); |
| 196 |
if(res && res.photos && res.photos.photo){ |
| 197 |
var images = this.images = res.photos.photo; |
| 198 |
|
| 199 |
var urls = []; |
| 200 |
|
| 201 |
var baseUrl; |
| 202 |
|
| 203 |
for(var i = 0; i < images.length; i++){ |
| 204 |
baseUrl = "http://farm" |
| 205 |
+ images[i].farm |
| 206 |
+ ".static.flickr.com/" |
| 207 |
+ images[i].server |
| 208 |
+ "/" |
| 209 |
+ images[i].id |
| 210 |
+ "_" |
| 211 |
+ images[i].secret; |
| 212 |
urls.push({ |
| 213 |
large: baseUrl + ".jpg", |
| 214 |
small: baseUrl + "_t.jpg", |
| 215 |
thumb: baseUrl + "_s.jpg", |
| 216 |
ownername: images[i].ownername, |
| 217 |
title: images[i].title |
| 218 |
}); |
| 219 |
} |
| 220 |
this.urls = urls; |
| 221 |
this.index = 0; |
| 222 |
this.viewer.set("selectedIndex", this.index); |
| 223 |
|
| 224 |
this.viewer.set("items", urls); |
| 225 |
}else{ |
| 226 |
this.viewer.set("items", []); |
| 227 |
console.log("didn't get photos"); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
}); |