Commits

Nick Freear  committed a3ca980

maltwiki: Bug #451 Update to latest jquery-oembed (Google code r6:r19)

git-svn-id: https://wahoo.open.ac.uk/svn/repos/incubator/maltwiki_org@4252931b2e54-a526-0410-9ac3-d7400825cc31

  • Participants
  • Parent commits 3bd15e0

Comments (0)

Files changed (2)

File trunk/system/application/hooks/jquery.oembed.js

 /**
  oEmbed Javascript, with MALT Wiki integration.
 
- #link http://maltwiki.org/developer
- #link http://code.google.com/p/jquery-oembed/
+ http://maltwiki.org/developer
+ http://code.google.com/p/jquery-oembed/#r19-fix
 */
 (function($) {
-    $.fn.oembed = function(url, options) {		
-	
-		options = $.extend({}, $.fn.oembed.defaults, options);
-		
-        return this.each(function() {      
-			
-			var container = $(this), 
-				target = (url != null) ? url : container.attr("href"), 
-				provider;		
-			
-			if (target != null) {			
-				
-				provider = getOEmbedProvider(target);
+    $.fn.oembed = function(url, options, callback) {
 
-				if (provider != null) {
-					provider.maxWidth = options.maxWidth;
-					provider.maxHeight = options.maxHeight;               
-					
-					provider.embedCode(target, function(code) { container.html(code); });		
-				}
-			}
+        options = $.extend(true, $.fn.oembed.defaults, options);
+
+        return this.each(function() {
+
+            var container = $(this),
+				resourceURL = (url != null) ? url : container.attr("href"),
+				provider;
+
+            if (!callback) callback = function(container, oembed) {			
+				 $.fn.oembed.insertCode(container, options.embedMethod, oembed);
+            };
+
+            if (resourceURL != null) {
+                provider = getOEmbedProvider(resourceURL);
+
+                if (provider != null) {
+//ou-specific
+                    options.maltwiki = { client:'org.maltwiki:jquery.oembed', ref:escape(location) };
+//ou-specific ends.
+					provider.params = getNormalizedParams(options[provider.name]) || {};
+                    provider.maxWidth = options.maxWidth;
+                    provider.maxHeight = options.maxHeight;										
+                    provider.embedCode(container, resourceURL, callback);
+                    return;
+                }
+            }
+
+            callback(container, null);
         });
     };
 
     // Plugin defaults
     $.fn.oembed.defaults = {
-        maxWidth: 500,
-        maxHeight: 400
+        maxWidth: null,
+        maxHeight: null,
+		embedMethod: "replace" // "auto", "append", "fill"
     };
+	
+	$.fn.oembed.insertCode = function(container, embedMethod, oembed) {
+		if (oembed == null)
+			return;
+		switch(embedMethod)
+		{
+			case "auto":				
+                if (container.attr("href") != null) {
+					insertCode(container, "append", oembed);
+				}
+				else {
+					insertCode(container, "replace", oembed);
+				};
+				break;
+			case "replace":	
+				container.replaceWith(oembed.code);
+				break;
+			case "fill":
+				container.html(oembed.code);
+				break;
+			case "append":
+                var oembedContainer = container.next();
+				if (oembedContainer == null || !oembedContainer.hasClass("oembed-container")) {
+					oembedContainer = container
+						.after('<div class="oembed-container"></div>')
+						.next(".oembed-container");
+					if (oembed != null && oembed.provider_name != null)
+					    oembedContainer.toggleClass("oembed-container-" + oembed.provider_name);		
+				}
+				oembedContainer.html(oembed.code);				
+				break;			
+		}
+	}	
 
     $.fn.oembed.getPhotoCode = function(url, data) {
-//ou-specific
+//ou-specific, Words in place of dashes.
         var alt = data.title ? data.title : 'Photo';
         alt += data.author_name ? ', by '+data.author_name :'';
         alt += data.provider_name ? ' on '+data.provider_name :'';
         var code= '<a class="photo" href="'+ url +'"><img src="'+ data.url +'" alt="'+ alt +'" title="'+ alt +'" /></a>';
+        //var code = '<div><a href="' + url + '" target="_blank"><img src="' + data.url + '" alt="' + alt + '"/></a></div>';
 //ou-specific ends.
         if (data.html)
             code += "<div>" + data.html + "</div>";
 
     $.fn.oembed.getGenericCode = function(url, data) {
         var title = (data.title != null) ? data.title : url,
-			code = '<a href="' + url + '">' + title + '</a>';			
+			code = '<a href="' + url + '">' + title + '</a>';
         if (data.html)
             code += "<div>" + data.html + "</div>";
-		return code;
+        return code;
     };
 
     $.fn.oembed.isAvailable = function(url) {
         var provider = getOEmbedProvider(url);
         return (provider != null);
     };
-	
+
     /* Private Methods */
     function getOEmbedProvider(url) {
         for (var i = 0; i < providers.length; i++) {
         }
         return null;
     }
+	
+	function getNormalizedParams(params) {
+		if (params == null)
+			return null;
+		var normalizedParams = {};
+		for (var key in params) {
+			if (key != null)
+				normalizedParams[key.toLowerCase()] = params[key];
+		}
+		return normalizedParams;
+	}
 
     var providers = [
         new OEmbedProvider("fivemin", "5min.com"),
         new OEmbedProvider("amazon", "amazon.com"),
-        new OEmbedProvider("flickr", "flickr", "http://www.flickr.com/services/oembed", "jsoncallback"),
+        new OEmbedProvider("flickr", "flickr", "http://flickr.com/services/oembed", "jsoncallback"),    
         new OEmbedProvider("googlevideo", "video.google."),
         new OEmbedProvider("hulu", "hulu.com"),
         new OEmbedProvider("imdb", "imdb.com"),
         new OEmbedProvider("metacafe", "metacafe.com"),
         new OEmbedProvider("qik", "qik.com"),
-        new OEmbedProvider("revision3", "revision3.com"), //"http://revision3.com/api/oembed" - No callback param.
+        new OEmbedProvider("revision3", "revision3.com"), //http://revision3.com/api/oembed: No callback param.
         new OEmbedProvider("slideshare", "slideshare.net"),
         new OEmbedProvider("twitpic", "twitpic.com"),
         new OEmbedProvider("viddler", "viddler.com"),
         new OEmbedProvider("vimeo", "vimeo.com", "http://vimeo.com/api/oembed.json"),
         new OEmbedProvider("wikipedia", "wikipedia.org"),
         new OEmbedProvider("wordpress", "wordpress.com"),
-//ou-specific,  #todo Experimental.
+//ou-specific,
         new OEmbedProvider("NFB", "nfb.ca"),
         new OEmbedProvider("blip","blip.tv"),
-        new OEmbedProvider("cohere", "cohere.open.ac.uk", "http://olnet.org/oembed"),
-        new OEmbedProvider("youtube", "youtube.com", "http://maltwiki.org/oembed")
-        //new OEmbedProvider("youtube", "youtube.com")
+        new OEmbedProvider("maltwiki", "mathtran.org", "http://olnet.org/oembed"),
+        new OEmbedProvider("maltwiki", "cohere.open.ac.uk", "http://olnet.org/oembed"),
+        new OEmbedProvider("maltwiki", "youtube.com", "http://maltwiki.org/oembed"),
+        //new OEmbedProvider("youtube", "youtube.com"),
 //ou-specific ends.
+        new OEmbedProvider("vids.myspace.com", "vids.myspace.com", "http://vids.myspace.com/index.cfm?fuseaction=oembed"),
+		new OEmbedProvider("screenr", "screenr.com", "http://screenr.com/api/oembed.json")
     ];
 
     function OEmbedProvider(name, urlPattern, oEmbedUrl, callbackparameter) {
 
             if (url.indexOf("?") <= 0)
                 url = url + "?";
+			else
+				url = url + "&";
 
-            url += "maxwidth=" + this.maxWidth + 
-//ou-specific
-                "&maxheight=" + this.maxHeight +
-                //#todo Bug "&maxHeight=" + this.maxHeight + 
-                "&client=" + "org.maltwiki:jquery.oembed.js" +
-                "&ref=" + escape(location) +  //Comment for DEV. servers.
-//ou-specific ends.
-                "&format=json" + 
-                "&url=" + escape(externalUrl) + 
-                "&" + this.callbackparameter + "=?";
+			var qs = "";
+			
+			if (this.maxWidth != null && this.params["maxwidth"] == null)
+				this.params["maxwidth"] = this.maxWidth;				
+				
+			if (this.maxHeight != null && this.params["maxheight"] == null)
+				this.params["maxheight"] = this.maxHeight;
+
+			for (var i in this.params) {
+                // We don't want them to jack everything up by changing the callback parameter
+                if (i == this.callbackparameter)
+                  continue;
+                
+				// allows the options to be set to null, don't send null values to the server as parameters
+                if (this.params[i] != null)
+                	qs += "&" + escape(i) + "=" + this.params[i];
+            }
+
+			url += "format=json&url=" + escape(externalUrl) + 
+					qs + 
+					"&" + this.callbackparameter + "=?";
             return url;
         }
 
-        this.embedCode = function(externalUrl, embedCallback) {
+        this.embedCode = function(container, externalUrl, callback) {
 
             var request = this.getRequestUrl(externalUrl);
 
             $.getJSON(request, function(data) {
-                
-				var code, type = data.type;
+
+                var oembed = $.extend(data);
+
+                var code, type = data.type;
 
                 switch (type) {
                     case "photo":
-                        code = $.fn.oembed.getPhotoCode(externalUrl, data);
+                        oembed.code = $.fn.oembed.getPhotoCode(externalUrl, data);
                         break;
                     case "video":
-                        code = $.fn.oembed.getVideoCode(externalUrl, data);
+                        oembed.code = $.fn.oembed.getVideoCode(externalUrl, data);
                         break;
                     case "rich":
-                        code = $.fn.oembed.getRichCode(externalUrl, data);
+                        oembed.code = $.fn.oembed.getRichCode(externalUrl, data);
                         break;
                     default:
-                        code = $.fn.oembed.getGenericCode(externalUrl, data);
+                        oembed.code = $.fn.oembed.getGenericCode(externalUrl, data);
                         break;
                 }
 
-                embedCallback(code);
+                callback(container, oembed);
             });
         }
     }
 })(jQuery);
-

File trunk/system/application/hooks/jquery.oembed_ORIG.js

 (function($) {
-    $.fn.oembed = function(url, options) {		
-	
-		options = $.extend({}, $.fn.oembed.defaults, options);
-		
-        return this.each(function() {      
-			
-			var container = $(this), 
-				target = (url != null) ? url : container.attr("href"), 
-				provider;		
-			
-			if (target != null) {			
-				
-				provider = getOEmbedProvider(target);
+    $.fn.oembed = function(url, options, callback) {
 
-				if (provider != null) {
-					provider.maxWidth = options.maxWidth;
-					provider.maxHeight = options.maxHeight;               
-					
-					provider.embedCode(target, function(code) { container.html(code); });		
-				}
-			}
+        options = $.extend(true, $.fn.oembed.defaults, options);
+
+        return this.each(function() {
+
+            var container = $(this),
+				resourceURL = (url != null) ? url : container.attr("href"),
+				provider;
+
+            if (!callback) callback = function(container, oembed) {			
+				 $.fn.oembed.insertCode(container, options.embedMethod, oembed);
+            };
+
+            if (resourceURL != null) {
+                provider = getOEmbedProvider(resourceURL);
+
+                if (provider != null) {						
+					provider.params = getNormalizedParams(options[provider.name]) || {};
+                    provider.maxWidth = options.maxWidth;
+                    provider.maxHeight = options.maxHeight;										
+                    provider.embedCode(container, resourceURL, callback);
+                    return;
+                }
+            }
+
+            callback(container, null);
         });
     };
 
     // Plugin defaults
     $.fn.oembed.defaults = {
-        maxWidth: 500,
-        maxHeight: 400
+        maxWidth: null,
+        maxHeight: null,
+		embedMethod: "replace" // "auto", "append", "fill"
     };
+	
+	$.fn.oembed.insertCode = function(container, embedMethod, oembed) {
+		if (oembed == null)
+			return;
+		switch(embedMethod)
+		{
+			case "auto":				
+                if (container.attr("href") != null) {
+					insertCode(container, "append", oembed);
+				}
+				else {
+					insertCode(container, "replace", oembed);
+				};
+				break;
+			case "replace":	
+				container.replaceWith(oembed.code);
+				break;
+			case "fill":
+				container.html(oembed.code);
+				break;
+			case "append":
+                var oembedContainer = container.next();
+				if (oembedContainer == null || !oembedContainer.hasClass("oembed-container")) {
+					oembedContainer = container
+						.after('<div class="oembed-container"></div>')
+						.next(".oembed-container");
+					if (oembed != null && oembed.provider_name != null)
+					    oembedContainer.toggleClass("oembed-container-" + oembed.provider_name);		
+				}
+				oembedContainer.html(oembed.code);				
+				break;			
+		}
+	}	
 
     $.fn.oembed.getPhotoCode = function(url, data) {
-        var code = '<div><a href="' + url + '" target="_blank"><img src="' + data.url + '"/></a></div>';
+	    var alt = data.title ? data.title : '';
+        alt += data.author_name ? ' - ' + data.author_name : '';
+        alt += data.provider_name ? ' - ' +data.provider_name : '';
+        var code = '<div><a href="' + url + '" target="_blank"><img src="' + data.url + '" alt="' + alt + '"/></a></div>';
         if (data.html)
             code += "<div>" + data.html + "</div>";
         return code;
 
     $.fn.oembed.getGenericCode = function(url, data) {
         var title = (data.title != null) ? data.title : url,
-			code = '<a href="' + url + '">' + title + '</a>';			
+			code = '<a href="' + url + '">' + title + '</a>';
         if (data.html)
             code += "<div>" + data.html + "</div>";
-		return code;
+        return code;
     };
 
     $.fn.oembed.isAvailable = function(url) {
         var provider = getOEmbedProvider(url);
         return (provider != null);
     };
-	
+
     /* Private Methods */
     function getOEmbedProvider(url) {
         for (var i = 0; i < providers.length; i++) {
         }
         return null;
     }
+	
+	function getNormalizedParams(params) {
+		if (params == null)
+			return null;
+		var normalizedParams = {};
+		for (var key in params) {
+			if (key != null)
+				normalizedParams[key.toLowerCase()] = params[key];
+		}
+		return normalizedParams;
+	}
 
     var providers = [
         new OEmbedProvider("fivemin", "5min.com"),
         new OEmbedProvider("amazon", "amazon.com"),
-        new OEmbedProvider("flickr", "flickr", "http://flickr.com/services/oembed", "jsoncallback"),
+        new OEmbedProvider("flickr", "flickr", "http://flickr.com/services/oembed", "jsoncallback"),    
         new OEmbedProvider("googlevideo", "video.google."),
         new OEmbedProvider("hulu", "hulu.com"),
         new OEmbedProvider("imdb", "imdb.com"),
         new OEmbedProvider("vimeo", "vimeo.com", "http://vimeo.com/api/oembed.json"),
         new OEmbedProvider("wikipedia", "wikipedia.org"),
         new OEmbedProvider("wordpress", "wordpress.com"),
-        new OEmbedProvider("youtube", "youtube.com")
+        new OEmbedProvider("youtube", "youtube.com"),
+        new OEmbedProvider("vids.myspace.com", "vids.myspace.com", "http://vids.myspace.com/index.cfm?fuseaction=oembed"),
+		new OEmbedProvider("screenr", "screenr.com", "http://screenr.com/api/oembed.json")
     ];
 
     function OEmbedProvider(name, urlPattern, oEmbedUrl, callbackparameter) {
 
             if (url.indexOf("?") <= 0)
                 url = url + "?";
+			else
+				url = url + "&";
+
+			var qs = "";
+			
+			if (this.maxWidth != null && this.params["maxwidth"] == null)
+				this.params["maxwidth"] = this.maxWidth;				
+				
+			if (this.maxHeight != null && this.params["maxheight"] == null)
+				this.params["maxheight"] = this.maxHeight;
 
-            url += "maxwidth=" + this.maxWidth + 
-						"&maxHeight=" + this.maxHeight + 
-						"&format=json" + 
-						"&url=" + escape(externalUrl) + 
-						"&" + this.callbackparameter + "=?";						
+			for (var i in this.params) {
+                // We don't want them to jack everything up by changing the callback parameter
+                if (i == this.callbackparameter)
+                  continue;
+                
+				// allows the options to be set to null, don't send null values to the server as parameters
+                if (this.params[i] != null)
+                	qs += "&" + escape(i) + "=" + this.params[i];
+            }			
+            
+				
+			url += "format=json&url=" + escape(externalUrl) + 			
+					qs + 
+					"&" + this.callbackparameter + "=?";
+					
             return url;
         }
 
-        this.embedCode = function(externalUrl, embedCallback) {
+        this.embedCode = function(container, externalUrl, callback) {
 
             var request = this.getRequestUrl(externalUrl);
 
             $.getJSON(request, function(data) {
-                
-				var code, type = data.type;
+
+                var oembed = $.extend(data);
+
+                var code, type = data.type;
 
                 switch (type) {
                     case "photo":
-                        code = $.fn.oembed.getPhotoCode(externalUrl, data);
+                        oembed.code = $.fn.oembed.getPhotoCode(externalUrl, data);
                         break;
                     case "video":
-                        code = $.fn.oembed.getVideoCode(externalUrl, data);
+                        oembed.code = $.fn.oembed.getVideoCode(externalUrl, data);
                         break;
                     case "rich":
-                        code = $.fn.oembed.getRichCode(externalUrl, data);
+                        oembed.code = $.fn.oembed.getRichCode(externalUrl, data);
                         break;
                     default:
-                        code = $.fn.oembed.getGenericCode(externalUrl, data);
+                        oembed.code = $.fn.oembed.getGenericCode(externalUrl, data);
                         break;
                 }
 
-                embedCallback(code);
+                callback(container, oembed);
             });
         }
     }
-})(jQuery);
-
+})(jQuery);