Accueil > JavaScript > PicassXMLAlbum.js # Javascript library that handle Picassa XML output

PicassXMLAlbum.js # Javascript library that handle Picassa XML output

28/01/2009
  1. /* 
  2.  * -------------------------------------------------------------------------- 
  3.  * PicassaXMLAlbum Javascript library, version 1.0 
  4.  * (c) 2006 Gabriel DROMARD <gabriel_dromard@yahoo.fr> 
  5.  * 
  6.  * 
  7.  * This library is build under album XML output generated by Picassa v2.1.0 
  8.  * This library require prototype.js v1.4.0 
  9.  * -------------------------------------------------------------------------- 
  10.  */  
  11.   
  12. /* 
  13.  * Exemple of use: 
  14.  * 
  15.  * -------------------------------------------------------------------------- 
  16. myAjaxrequest = new Ajax.Request( 'http://localhost/MyAlbum/index.xml', { method: 'get', onFailure: displayThumbnailsError, onComplete: setThumbnails}); 
  17.  
  18. function setThumbnails() { 
  19.     var album = new PicassaXMLAlbum(request.responseText); 
  20.     var testElmt = document.getElementById('test'); 
  21.     testElmt.innerHTML = 'Album Name: '+album.getName()+'<br>'; 
  22.     testElmt.innerHTML += 'Album nb images: '+album.getImageCount(); 
  23.     var images = album.getImages(); 
  24.     for(i=0; i<images.length; ++i) { 
  25.         testElmt.innerHTML += '<br>Images n°'+i+' '+images[i].getThumbnail(); 
  26.     } 
  27. } 
  28.  
  29. function displayThumbnailsError(error) { 
  30.     document.getElementById('test').innerHTML = '<font color="darkred"><b>Sorry. There was an error while loading thumbnails. </b>('+error+')</font>'; 
  31. } 
  32.  * -------------------------------------------------------------------------- 
  33.  * 
  34.  * 
  35.  */  
  36.   
  37. /** 
  38.  * PicassaXMLAlbum Object that represent the album object. 
  39.  */  
  40. var PicassaXMLAlbum = Class.create();  
  41.   
  42. PicassaXMLAlbum.prototype = {  
  43.   
  44.     /** 
  45.      * Constructor 
  46.      * @param xml The XML string representing the Picassa XML album file content. 
  47.      */  
  48.     initialize: function(url, options) {  
  49.         this.xml = url;  
  50.         this.options = options;  
  51.     },  
  52.   
  53.     /** 
  54.      * Retreive the album name. 
  55.      */  
  56.     dispatchError: function(error) {  
  57.         try {  
  58.             if(typeof(this.options.onFailure) == 'function') {  
  59.                 this.options.onFailure(error);  
  60.             } else {  
  61.                 alert(error);  
  62.             }  
  63.         } catch(e) {  
  64.             alert(error);  
  65.         }  
  66.     },  
  67.   
  68.     /** 
  69.      * Retreive the album name. 
  70.      */  
  71.     getName: function() {  
  72.         try {  
  73.             if(this.name == undefined) {  
  74.                 this.name = RegExp('<albumName>([^<>]*)</albumName>').exec(this.xml)[1];  
  75.             }  
  76.             return this.name;  
  77.         } catch(e) {  
  78.             this.dispatchError(e);  
  79.         }  
  80.     },  
  81.   
  82.     /** 
  83.      * Retreive the image count of the album. 
  84.      */  
  85.     getImageCount: function() {  
  86.         try {  
  87.             if(this.imageCount == undefined) {  
  88.                 this.imageCount = RegExp('<albumItemCount>([^<>]*)</albumItemCount>').exec(this.xml)[1];  
  89.             }  
  90.             return this.imageCount;  
  91.         } catch(e) {  
  92.             this.dispatchError(e);  
  93.         }  
  94.     },  
  95.   
  96.     /** 
  97.      * Get images of the albums. 
  98.      * @return an array of PicassaXMLImage objects. 
  99.      */  
  100.     getImages: function() {  
  101.         try {  
  102.             if(this.images == undefined) {  
  103.                 var imagesArray = new Array();  
  104.                 var xmlTmp = this.xml;  
  105.                 for(i=0; i<this.getImageCount(); ++i) {  
  106.                     var begin = xmlTmp.indexOf('<image>');  
  107.                     xmlTmp = xmlTmp.substr(begin);  
  108.                     var end = xmlTmp.indexOf('</image>');  
  109.                     imagesArray[i] = xmlTmp.substr(0, end);  
  110.                     xmlTmp = xmlTmp.substr(end);  
  111.                 }  
  112.                 this.images = new Array();  
  113.                 for(i=0; i<imagesArray.length; ++i) {  
  114.                     var image = new PicassaXMLImage(imagesArray[i], {onFailure: this.dispatchError});  
  115.                     this.images[i] = image;  
  116.                 }  
  117.             }  
  118.             return this.images;  
  119.         } catch(e) {  
  120.             this.dispatchError(e);  
  121.         }  
  122.     }  
  123. };  
  124.   
  125. /** 
  126.  * PicassaXMLImage object that represent one image. 
  127.  */  
  128. var PicassaXMLImage = Class.create();  
  129.   
  130. PicassaXMLImage.prototype = {  
  131.   
  132.     /** 
  133.      * Constructor 
  134.      */  
  135.     initialize: function(xml, options) {  
  136.         this.xml = xml;  
  137.         this.options = options;  
  138.     },  
  139.   
  140.     /** 
  141.      * Is it the first image ? 
  142.      */  
  143.     isFirstImage: function() {  
  144.         try {  
  145.             if(this.firstImage == undefined) {  
  146.                 this.firstImage = RegExp('<isFirstImage>([^<>]*)</isFirstImage>').exec(this.xml)[1];  
  147.             }  
  148.             return this.firstImage;  
  149.         } catch(e) {  
  150.             this.dispatchError(e);  
  151.         }  
  152.     },  
  153.   
  154.     /** 
  155.      * Is it the last image ? 
  156.      */  
  157.     isLastImage: function() {  
  158.         try {  
  159.             if(this.lastImage == undefined) {  
  160.                 this.lastImage = RegExp('<isLastImage>([^<>]*)</isLastImage>').exec(this.xml)[1];  
  161.             }  
  162.             return this.lastImage;  
  163.         } catch(e) {  
  164.             this.dispatchError(e);  
  165.         }  
  166.     },  
  167.   
  168.     /** 
  169.      * Get the image url. 
  170.      */  
  171.     getImage: function() {  
  172.         if(this.image == undefined) {  
  173.             this.image = RegExp('<itemLargeImage>([^<>]*)</itemLargeImage>').exec(this.xml)[1];  
  174.         }  
  175.         return this.image;  
  176.     },  
  177.   
  178.     /** 
  179.      * Get the image name. 
  180.      */  
  181.     getName: function() {  
  182.         if(this.name == undefined) {  
  183.             this.name = RegExp('<itemName>([^<>]*)</itemName>').exec(this.xml)[1];  
  184.         }  
  185.         return this.name;  
  186.     },  
  187.   
  188.     /** 
  189.      * Get the image caption. 
  190.      */  
  191.     getCaption: function() {  
  192.         if(this.caption == undefined) {  
  193.             this.caption = RegExp('<itemCaption>([^<>]*)</itemCaption>').exec(this.xml)[1];  
  194.         }  
  195.         return this.caption;  
  196.     },  
  197.   
  198.     /** 
  199.      * Get the width of image. 
  200.      */  
  201.     getImageWidth: function() {  
  202.         if(this.imageWidth == undefined) {  
  203.             this.imageWidth = RegExp('<itemWidth>([^<>]*)</itemWidth>').exec(this.xml)[1];  
  204.         }  
  205.         return this.imageWidth;  
  206.     },  
  207.   
  208.     /** 
  209.      * Get the width of image. 
  210.      */  
  211.     getImageHeight: function() {  
  212.         if(this.imageHeight == undefined) {  
  213.             this.imageHeight = RegExp('<itemHeight>([^<>]*)</itemHeight>').exec(this.xml)[1];  
  214.         }  
  215.         return this.imageHeight;  
  216.     },  
  217.   
  218.     /** 
  219.      * Get the name of the previous image. 
  220.      */  
  221.     getPrevImageName: function() {  
  222.         if(this.prevImageName == undefined) {  
  223.             this.prevImageName = RegExp('<prevImage>([^<>]*)</prevImage>').exec(this.xml)[1];  
  224.         }  
  225.         return this.prevImageName;  
  226.     },  
  227.   
  228.     /** 
  229.      * Get the name of the next image. 
  230.      */  
  231.     getNextImageName: function() {  
  232.         if(this.nextImageName == undefined) {  
  233.             this.nextImageName = RegExp('<nextImage>([^<>]*)</nextImage>').exec(this.xml)[1];  
  234.         }  
  235.         return this.nextImageName;  
  236.     },  
  237.   
  238.     /** 
  239.      * Get the url of the image thumbnail. 
  240.      */  
  241.     getThumbnail: function() {  
  242.         if(this.thumbnail == undefined) {  
  243.             this.thumbnail = RegExp('<itemThumbnailImage>([^<>]*)</itemThumbnailImage>').exec(this.xml)[1];  
  244.         }  
  245.         return this.thumbnail;  
  246.     },  
  247.   
  248.     /** 
  249.      * Get the width of image. 
  250.      */  
  251.     getThumbnailWidth: function() {  
  252.         if(this.thumbnailWidth == undefined) {  
  253.             this.thumbnailWidth = RegExp('<itemThumbnailWidth>([^<>]*)</itemThumbnailWidth>').exec(this.xml)[1];  
  254.         }  
  255.         return this.thumbnailWidth;  
  256.     },  
  257.   
  258.     /** 
  259.      * Get the width of image. 
  260.      */  
  261.     getThumbnailHeight: function() {  
  262.         if(this.thumbnailHeight == undefined) {  
  263.             this.thumbnailHeight = RegExp('<itemThumbnailHeight>([^<>]*)</itemThumbnailHeight>').exec(this.xml)[1];  
  264.         }  
  265.         return this.thumbnailHeight;  
  266.     },  
  267.   
  268.     /** 
  269.      * Retreive the album name. 
  270.      */  
  271.     dispatchError: function(error) {  
  272.         try {  
  273.             if(typeof(this.options.onFailure) == 'function') {  
  274.                 this.options.onFailure(error);  
  275.             } else {  
  276.                 alert(error);  
  277.             }  
  278.         } catch(e) {  
  279.             alert(error);  
  280.         }  
  281.     }  
  282. };  

JavaScript

Les commentaires sont fermés.