建立 Google 攝影獎作品集
我們最近在 Google 攝影獎網站上推出了「作品集」專區。相片庫會顯示從 Google+ 擷取的無限捲動相片清單。相片清單來自 AppEngine 應用程式,我們使用該應用程式管理相片庫中的相片清單。我們也將相片庫應用程式做為開放原始碼專案,發布在 Google Code 上。
作品集的後端是 AppEngine 應用程式,會使用 Google+ API 搜尋含有 Google 攝影獎主題標記 (例如 #megpp 和 #travelgpp) 的貼文。接著,應用程式會將這些貼文加入未審核相片清單。我們的內容團隊每週會檢查一次未經審核的相片清單,並標記違反內容規範的相片。點選「審核」按鈕後,未標記的相片就會新增至圖片庫頁面顯示的相片清單。
圖片庫前端
Gallery 前端是使用 Google Closure 程式庫建構而成。圖片庫小工具本身是 Closure 元件。在來源檔案頂端,我們告訴 Closure 這個檔案提供名為 photographyPrize.Gallery 的元件,並要求應用程式使用的 Closure 程式庫部分:
goog.provide('photographyPrize.Gallery');
goog.require('goog.debug.Logger');
goog.require('goog.dom');
goog.require('goog.dom.classes');
goog.require('goog.events');
goog.require('goog.net.Jsonp');
goog.require('goog.style');
圖片庫頁面有一小段 JavaScript,會使用 JSONP 從 App Engine 應用程式擷取相片清單。JSONP 是一種簡單的跨源 JavaScript 駭客技術,會插入類似 jsonpcallback("responseValue") 的指令碼。為了處理 JSONP 相關作業,我們使用 Closure 程式庫中的 goog.net.Jsonp 元件。
圖片庫指令碼會瀏覽相片清單,並為相片產生 HTML 元素,以便在圖片庫頁面上顯示。無限捲動功能會連結至視窗捲動事件,並在視窗捲動至接近頁面底部時載入新一批相片。載入新的相片清單片段後,相片庫指令碼會為相片建立元素,並將這些元素新增至相片庫元素,以便顯示相片。
顯示圖片清單
圖片清單顯示方法相當基本。它會瀏覽圖片清單,產生 HTML 元素和 +1 按鈕。下一步是將產生的清單區隔新增至圖片庫的主要圖片庫元素。您可以在下方程式碼中看到一些 Closure 編譯器慣例,請注意 JSDoc 註解中的類型定義和 @private 瀏覽權限。不公開方法的名稱後方會加上底線 (_)。
/**
* Displays images in imageList by putting them inside the section element.
* Edits image urls to scale them down to imageSize x imageSize bounding
* box.
*
* @param {Array.<Object>} imageList List of image objects to show. Retrieved
* by loadImages.
* @return {Element} The generated image list container element.
* @private
*/
photographyPrize.Gallery.prototype.displayImages_ = function(imageList) {
// find the images and albums from the image list
for (var j = 0; j < imageList.length; j++) {
// change image urls to scale them to photographyPrize.Gallery.MAX_IMAGE_SIZE
}
// Go through the image list and create a gallery photo element for each image.
// This uses the Closure library DOM helper, goog.dom.createDom:
// element = goog.dom.createDom(tagName, className, var_childNodes);
var category = goog.dom.createDom('div', 'category');
for (var k = 0; k < items.length; k++) {
var plusone = goog.dom.createDom('g:plusone');
plusone.setAttribute('href', photoPageUrl);
plusone.setAttribute('size', 'standard');
plusone.setAttribute('annotation', 'none');
var photo = goog.dom.createDom('div', {className: 'gallery-photo'}, ...)
photo.appendChild(plusone);
category.appendChild(photo);
}
this.galleryElement_.appendChild(category);
return category;
};
處理捲動事件
為了在訪客捲動至頁面底部時載入新圖片,圖片庫會連結至視窗物件的捲動事件。為彌補瀏覽器實作上的差異,我們使用 Closure 程式庫中一些實用的公用函式:goog.dom.getDocumentScroll() 傳回具有目前文件捲動位置的 {x, y} 物件、goog.dom.getViewportSize() 傳回視窗大小,而 goog.dom.getDocumentHeight() 則傳回 HTML 文件的高度。
/**
* Handle window scroll events by loading new images when the scroll reaches
* the last screenful of the page.
*
* @param {goog.events.BrowserEvent} ev The scroll event.
* @private
*/
photographyPrize.Gallery.prototype.handleScroll_ = function(ev) {
var scrollY = goog.dom.getDocumentScroll().y;
var height = goog.dom.getViewportSize().height;
var documentHeight = goog.dom.getDocumentHeight();
if (scrollY + height >= documentHeight - height / 2) {
this.tryLoadingNextImages_();
}
};
/**
* Try loading the next batch of images objects from the server.
* Only fires if we have already loaded the previous batch.
*
* @private
*/
photographyPrize.Gallery.prototype.tryLoadingNextImages_ = function() {
// ...
};
載入圖片
如要從伺服器載入圖片,請使用 goog.net.Jsonp 元件。它需要 goog.Uri 才能查詢。建立後,您可以使用查詢參數物件和成功回呼函式,將查詢傳送至 Jsonp 供應器。
/**
* Loads image list from the App Engine page and sets the callback function
* for the image list load completion.
*
* @param {string} tag Fetch images tagged with this.
* @param {number} limit How many images to fetch.
* @param {number} offset Offset for the image list.
* @param {function(Array.<Object>=)} callback Function to call
* with the loaded image list.
* @private
*/
photographyPrize.Gallery.prototype.loadImages_ = function(tag, limit, offset, callback) {
var jsonp = new goog.net.Jsonp(
new goog.Uri(photographyPrize.Gallery.IMAGE_LIST_URL));
jsonp.send({'tag': tag, 'limit': limit, 'offset': offset}, callback);
};
如上所述,圖片庫指令碼會使用 Closure 編譯器來編譯及壓縮程式碼。Closure 編譯器也可用於強制執行正確的輸入 (您在註解中使用 @type foo JSDoc 標記來設定屬性的類型),而且當您沒有方法的註解時,它也會通知您。
單元測試
我們也需要對圖片庫指令碼進行單元測試,因此 Closure 程式庫內建單元測試架構,非常方便。它遵循 jsUnit 慣例,因此很容易上手。
為了協助我編寫單元測試,我編寫了一個小型 Ruby 指令碼,用於剖析 JavaScript 檔案,並為圖片庫元件中的每個方法和屬性產生失敗的單元測試。假設有以下指令碼:
Foo = function() {}
Foo.prototype.bar = function() {}
Foo.prototype.baz = "hello";
測試產生器會為每個屬性產生空白測試:
function testFoo() {
fail();
Foo();
}
function testFooPrototypeBar = function() {
fail();
instanceFoo.bar();
}
function testFooPrototypeBaz = function() {
fail();
instanceFoo.baz;
}
這些自動產生的測試讓我能輕鬆開始編寫程式碼測試,而且所有方法和屬性預設都會涵蓋在內。測試失敗會產生良好的心理效果,我必須逐一檢查測試,並編寫適當的測試。搭配程式碼涵蓋率計量表,讓測試和涵蓋率都呈現綠色,就像玩遊戲一樣有趣。
摘要
Gallery+ 是一項開放原始碼專案,可顯示與 #主題標記相符的 Google+ 相片清單 (已審核)。這個工具是以 Go 和 Closure 程式庫建構而成。後端在 App Engine 上執行。Google 攝影獎網站使用 Gallery+ 顯示提交作品。本文介紹了前端指令碼的精華部分。App Engine 開發人員關係團隊的同事 Johan Euphrosine 正在撰寫第二篇文章,介紹後端應用程式。後端是以 Go 語言編寫,這是 Google 新推出的伺服器端語言。如果您想看 Go 程式碼的實際範例,請持續關注!