| 1 |
dojo.require("dojox.mobile.app.SceneAssistant"); |
| 2 |
|
| 3 |
dojo.declare("FlickrImageViewAssistant", dojox.mobile.app.SceneAssistant, { |
| 4 |
|
| 5 |
apiKey: "8c6803164dbc395fb7131c9d54843627", |
| 6 |
|
| 7 |
setup: function(){ |
| 8 |
|
| 9 |
|
| 10 |
this.controller.parse(); |
| 11 |
|
| 12 |
var viewer = this.viewer = dijit.byId("flickrImageView"); |
| 13 |
|
| 14 |
var _this = this; |
| 15 |
|
| 16 |
this.handlePhotoLoad = dojo.hitch(this, this.handlePhotoLoad); |
| 17 |
|
| 18 |
var loadingDiv = this.controller.query(".loading")[0]; |
| 19 |
|
| 20 |
|
| 21 |
var loadConn = dojo.connect(viewer, "onLoad", function(type, url, isSmall){ |
| 22 |
if(type == "center"){ |
| 23 |
dojo.disconnect(loadConn); |
| 24 |
dojo.destroy(loadingDiv); |
| 25 |
loadingDiv = null; |
| 26 |
} |
| 27 |
}); |
| 28 |
|
| 29 |
var reportDiv = this.controller.query(".report")[0]; |
| 30 |
|
| 31 |
this.connect(viewer, "onChange", function(direction){ |
| 32 |
_this.index += direction; |
| 33 |
|
| 34 |
|
| 35 |
if(_this.index > 0){ |
| 36 |
viewer.set("leftUrl", _this.urls[_this.index - 1]); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
if(_this.index < _this.urls.length - 1){ |
| 41 |
viewer.set("rightUrl", _this.urls[_this.index + 1]); |
| 42 |
} |
| 43 |
|
| 44 |
reportDiv.innerHTML = |
| 45 |
(_this.index + 1) + " of " + _this.urls.length |
| 46 |
+ " " + _this.urls[_this.index].title; |
| 47 |
}); |
| 48 |
}, |
| 49 |
|
| 50 |
activate: function(options){ |
| 51 |
|
| 52 |
this.dataType = (options && options.type ? options.type : "interesting"); |
| 53 |
console.log("In main assistant activate, dataType = ", this.dataType, options); |
| 54 |
|
| 55 |
switch(this.dataType){ |
| 56 |
case "interesting": |
| 57 |
this.loadInteresting(); |
| 58 |
break; |
| 59 |
case "group": |
| 60 |
this.loadGroup(options); |
| 61 |
break; |
| 62 |
case "data": |
| 63 |
|
| 64 |
|
| 65 |
this.useData(options.images, options.index || 0); |
| 66 |
break; |
| 67 |
|
| 68 |
} |
| 69 |
}, |
| 70 |
|
| 71 |
useData: function(images, startIndex){ |
| 72 |
this.urls = images; |
| 73 |
this.index = Math.min(images.length - 1, Math.max(0, startIndex)); |
| 74 |
|
| 75 |
if (this.index > 0) { |
| 76 |
this.viewer.set("leftUrl", images[this.index - 1]); |
| 77 |
} |
| 78 |
this.viewer.set("centerUrl", images[this.index]); |
| 79 |
|
| 80 |
if (this.index < images.length) { |
| 81 |
this.viewer.set("rightUrl", images[this.index + 1]); |
| 82 |
} |
| 83 |
}, |
| 84 |
|
| 85 |
loadInteresting: function(){ |
| 86 |
console.log("loading interesting"); |
| 87 |
|
| 88 |
var url = "http://api.flickr.com/services/rest/?method=" + |
| 89 |
"flickr.interestingness.getList"; |
| 90 |
|
| 91 |
|
| 92 |
var deferred = dojo.io.script.get({ |
| 93 |
url: url, |
| 94 |
content: { |
| 95 |
api_key: this.apiKey, |
| 96 |
format: "json" |
| 97 |
}, |
| 98 |
jsonp: "jsoncallback" |
| 99 |
}); |
| 100 |
deferred.addBoth(this.handlePhotoLoad); |
| 101 |
}, |
| 102 |
|
| 103 |
loadGroup: function(groupData){ |
| 104 |
console.log("loading group ", groupData); |
| 105 |
|
| 106 |
var url = "http://api.flickr.com/services/rest/?method=" + |
| 107 |
"flickr.groups.pools.getPhotos"; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
var deferred = dojo.io.script.get({ |
| 117 |
url: url, |
| 118 |
content: { |
| 119 |
api_key: this.apiKey, |
| 120 |
format: "json", |
| 121 |
group_id: groupData.nsid, |
| 122 |
per_page: 20 |
| 123 |
}, |
| 124 |
jsonp: "jsoncallback" |
| 125 |
}); |
| 126 |
deferred.addBoth(this.handlePhotoLoad); |
| 127 |
}, |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
handlePhotoLoad: function(res){ |
| 132 |
|
| 133 |
if(res && res.photos && res.photos.photo){ |
| 134 |
var images = res.photos.photo; |
| 135 |
|
| 136 |
var urls = []; |
| 137 |
|
| 138 |
var baseUrl; |
| 139 |
|
| 140 |
for(var i = 0; i < images.length; i++){ |
| 141 |
baseUrl = "http://farm" |
| 142 |
+ images[i].farm |
| 143 |
+ ".static.flickr.com/" |
| 144 |
+ images[i].server |
| 145 |
+ "/" |
| 146 |
+ images[i].id |
| 147 |
+ "_" |
| 148 |
+ images[i].secret; |
| 149 |
urls.push({ |
| 150 |
large: baseUrl + "_m.jpg", |
| 151 |
small: baseUrl + "_t.jpg", |
| 152 |
title: images[i].title |
| 153 |
}); |
| 154 |
} |
| 155 |
this.urls = urls; |
| 156 |
this.index = 0; |
| 157 |
|
| 158 |
this.viewer.set("centerUrl", urls[0]); |
| 159 |
this.viewer.set("rightUrl", urls[1]); |
| 160 |
}else{ |
| 161 |
console.log("didn't get photos"); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
}); |