PicassXMLAlbum.js # Javascript library that handle Picassa XML output
28/01/2009
/* * -------------------------------------------------------------------------- * PicassaXMLAlbum Javascript library, version 1.0 * (c) 2006 Gabriel DROMARD <gabriel_dromard@yahoo.fr> * * * This library is build under album XML output generated by Picassa v2.1.0 * This library require prototype.js v1.4.0 * -------------------------------------------------------------------------- */ /* * Exemple of use: * * -------------------------------------------------------------------------- myAjaxrequest = new Ajax.Request( 'http://localhost/MyAlbum/index.xml', { method: 'get', onFailure: displayThumbnailsError, onComplete: setThumbnails}); function setThumbnails() { var album = new PicassaXMLAlbum(request.responseText); var testElmt = document.getElementById('test'); testElmt.innerHTML = 'Album Name: '+album.getName()+'<br>'; testElmt.innerHTML += 'Album nb images: '+album.getImageCount(); var images = album.getImages(); for(i=0; i<images.length; ++i) { testElmt.innerHTML += '<br>Images n°'+i+' '+images[i].getThumbnail(); } } function displayThumbnailsError(error) { document.getElementById('test').innerHTML = '<font color="darkred"><b>Sorry. There was an error while loading thumbnails. </b>('+error+')</font>'; } * -------------------------------------------------------------------------- * * */ /** * PicassaXMLAlbum Object that represent the album object. */ var PicassaXMLAlbum = Class.create(); PicassaXMLAlbum.prototype = { /** * Constructor * @param xml The XML string representing the Picassa XML album file content. */ initialize: function(url, options) { this.xml = url; this.options = options; }, /** * Retreive the album name. */ dispatchError: function(error) { try { if(typeof(this.options.onFailure) == 'function') { this.options.onFailure(error); } else { alert(error); } } catch(e) { alert(error); } }, /** * Retreive the album name. */ getName: function() { try { if(this.name == undefined) { this.name = RegExp('<albumName>([^<>]*)</albumName>').exec(this.xml)[1]; } return this.name; } catch(e) { this.dispatchError(e); } }, /** * Retreive the image count of the album. */ getImageCount: function() { try { if(this.imageCount == undefined) { this.imageCount = RegExp('<albumItemCount>([^<>]*)</albumItemCount>').exec(this.xml)[1]; } return this.imageCount; } catch(e) { this.dispatchError(e); } }, /** * Get images of the albums. * @return an array of PicassaXMLImage objects. */ getImages: function() { try { if(this.images == undefined) { var imagesArray = new Array(); var xmlTmp = this.xml; for(i=0; i<this.getImageCount(); ++i) { var begin = xmlTmp.indexOf('<image>'); xmlTmp = xmlTmp.substr(begin); var end = xmlTmp.indexOf('</image>'); imagesArray[i] = xmlTmp.substr(0, end); xmlTmp = xmlTmp.substr(end); } this.images = new Array(); for(i=0; i<imagesArray.length; ++i) { var image = new PicassaXMLImage(imagesArray[i], {onFailure: this.dispatchError}); this.images[i] = image; } } return this.images; } catch(e) { this.dispatchError(e); } } }; /** * PicassaXMLImage object that represent one image. */ var PicassaXMLImage = Class.create(); PicassaXMLImage.prototype = { /** * Constructor */ initialize: function(xml, options) { this.xml = xml; this.options = options; }, /** * Is it the first image ? */ isFirstImage: function() { try { if(this.firstImage == undefined) { this.firstImage = RegExp('<isFirstImage>([^<>]*)</isFirstImage>').exec(this.xml)[1]; } return this.firstImage; } catch(e) { this.dispatchError(e); } }, /** * Is it the last image ? */ isLastImage: function() { try { if(this.lastImage == undefined) { this.lastImage = RegExp('<isLastImage>([^<>]*)</isLastImage>').exec(this.xml)[1]; } return this.lastImage; } catch(e) { this.dispatchError(e); } }, /** * Get the image url. */ getImage: function() { if(this.image == undefined) { this.image = RegExp('<itemLargeImage>([^<>]*)</itemLargeImage>').exec(this.xml)[1]; } return this.image; }, /** * Get the image name. */ getName: function() { if(this.name == undefined) { this.name = RegExp('<itemName>([^<>]*)</itemName>').exec(this.xml)[1]; } return this.name; }, /** * Get the image caption. */ getCaption: function() { if(this.caption == undefined) { this.caption = RegExp('<itemCaption>([^<>]*)</itemCaption>').exec(this.xml)[1]; } return this.caption; }, /** * Get the width of image. */ getImageWidth: function() { if(this.imageWidth == undefined) { this.imageWidth = RegExp('<itemWidth>([^<>]*)</itemWidth>').exec(this.xml)[1]; } return this.imageWidth; }, /** * Get the width of image. */ getImageHeight: function() { if(this.imageHeight == undefined) { this.imageHeight = RegExp('<itemHeight>([^<>]*)</itemHeight>').exec(this.xml)[1]; } return this.imageHeight; }, /** * Get the name of the previous image. */ getPrevImageName: function() { if(this.prevImageName == undefined) { this.prevImageName = RegExp('<prevImage>([^<>]*)</prevImage>').exec(this.xml)[1]; } return this.prevImageName; }, /** * Get the name of the next image. */ getNextImageName: function() { if(this.nextImageName == undefined) { this.nextImageName = RegExp('<nextImage>([^<>]*)</nextImage>').exec(this.xml)[1]; } return this.nextImageName; }, /** * Get the url of the image thumbnail. */ getThumbnail: function() { if(this.thumbnail == undefined) { this.thumbnail = RegExp('<itemThumbnailImage>([^<>]*)</itemThumbnailImage>').exec(this.xml)[1]; } return this.thumbnail; }, /** * Get the width of image. */ getThumbnailWidth: function() { if(this.thumbnailWidth == undefined) { this.thumbnailWidth = RegExp('<itemThumbnailWidth>([^<>]*)</itemThumbnailWidth>').exec(this.xml)[1]; } return this.thumbnailWidth; }, /** * Get the width of image. */ getThumbnailHeight: function() { if(this.thumbnailHeight == undefined) { this.thumbnailHeight = RegExp('<itemThumbnailHeight>([^<>]*)</itemThumbnailHeight>').exec(this.xml)[1]; } return this.thumbnailHeight; }, /** * Retreive the album name. */ dispatchError: function(error) { try { if(typeof(this.options.onFailure) == 'function') { this.options.onFailure(error); } else { alert(error); } } catch(e) { alert(error); } } };