Add embedly-jquery

Adding embedly-query.  Library and code located and downloaded from
https://github.com/embedly/embedly-jquery.  npm test passes. file
versions match package.json
This commit is contained in:
Michael Payne
2012-09-13 08:05:07 -07:00
parent f2379e1cc8
commit c2d1e5defe
3 changed files with 304 additions and 0 deletions
@@ -0,0 +1,254 @@
/*
* Embedly JQuery v2.2.0
* ==============
* This library allows you to easily embed objects on any page.
*
* Requirements:
* -------------
* jquery-1.3 or higher
*
* Usage:
* ------
* There are two ways to interact with this lib. One exposes a simple method to call embedly directly
*
* >>> $.embedly('http://www.youtube.com/watch?v=LfamTmY5REw', {}, function(oembed){ alert(oembed.title);});
*
* The oembed is either a json object or null
*
* You can also reference it this way, which will try and replace every link on the page with an embed
*
* Documentation is availiable at http://github.com/embedly/embedly-jquery
*
* $('a').embedly();
*
* The Options Are as Follows
*
* endpoint: 'oembed', // default endpoint is oembed (preview and objectify available too)
* chars: null, // Default number of characters in description
* words: null, // Default number of words in description
* maxWidth: null, // force a maxWidth on all returned media
* maxHeight: null, // force a maxHeight on all returned media
* secure: false, // use https endpoint vs http
* frame: false, // serves all embeds within an iframe to avoid XSS issues
* wmode: 'opaque', // for flash elements set a wmode
* autoplay: null, // tell videos to autoplay
* width: null, // force a width on all video/rich media
* method: 'replace', // embed handling option for standard callback
* addImageStyles: true, // add style="" attribute to images for maxWidth and maxHeight
* wrapElement: 'div', // standard wrapper around all returned embeds
* className: 'embed', // class on the wrapper element
* urlRe: null, // custom regex function
* key: null, // an embed.ly key
* elems: [], // array to hold nodes
* success: null, // default callback
* error: null // error-handling function
*
* http://api.embed.ly/tools/generator - generate your own regex for only sources you want
*
*/
(function($){
window.embedlyURLre = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
$.embedly = $.embedly || {};
if ( $.embedly.version ) { return; }
$.extend({
embedly: function(urls, options, callback){
var elems = [];
var path = "http://api.embed.ly/";
var settings;
options = options ? options : {};
settings = $.extend({}, $.embedly.defaults, options);
if (!settings.urlRe) {settings.urlRe = window.embedlyURLre; }
if (typeof urls === "string"){ urls = new Array(urls); }
if (typeof callback !== "undefined"){ settings.success = callback; }
if (settings.secure){ path = 'https://api.embed.ly/';}
if (!settings.success) {
settings.success = function(oembed, dict){
var _a, elem = $(dict.node);
if (! (oembed) ) { return null; }
if ((_a = settings.method) === 'replace') { return elem.replaceWith(oembed.code); }
else if (_a === 'after') { return elem.after(oembed.code); }
else if (_a === 'afterParent') { return elem.parent().after(oembed.code); }
else if (_a === 'replaceParent') { return elem.parent().replaceWith(oembed.code); }
};
}
if (!settings.error) {
settings.error = function(node, dict){
// we don't by default handle error cases
// node is the jQuery representation of the <a> tag
// dict contains error information
};
}
var urlValid = function(url){
return settings.urlRe.test(url);
};
var getParams = function(urls){
var _p = "urls=" + urls;
if (settings.maxWidth) {_p += '&maxwidth=' + settings.maxWidth;}
else if (typeof dimensions !== "undefined") { _p += '&maxwidth=' + dimensions.width;}
if (settings.maxHeight) {_p += '&maxheight=' +settings.maxHeight;}
if (settings.chars) {_p += '&chars=' + settings.chars;}
if (settings.words) {_p += '&words=' + settings.words;}
if (settings.secure) {_p += '&secure=true';}
if (settings.frame) {_p += '&frame=true';}
_p += '&wmode=' + settings.wmode;
if (typeof settings.key === "string"){ _p += "&key=" + settings.key;}
if (typeof settings.autoplay === "string" || typeof settings.autoplay === "boolean"){ _p += "&autoplay=" + settings.autoplay;}
if (settings.width){_p += "&width=" + settings.width;}
return _p;
};
var getUrl = function(){
if (typeof settings.key === "string"){
if (settings.endpoint.search(/objectify/i) >= 0){
return path + '2/objectify';
}
else if (settings.endpoint.search(/preview/i) >= 0){
return path + '1/preview';
}
}
return path + "1/oembed";
};
var createImageStyle = function() {
var style = [];
if (settings.addImageStyles) {
if (settings.maxWidth) {
units = isNaN(parseInt(settings.maxWidth, 10)) ? '' : 'px';
style.push("max-width: " + (settings.maxWidth)+units);
}
if (settings.maxHeight) {
units = isNaN(parseInt(settings.maxHeight,10)) ? '' : 'px';
style.push("max-height: " + (settings.maxHeight)+units);
}
}
return style.join(';');
}
var processEmbed = function(oembed, dict) {
// bypass any embed processing for preview, objectify endpoints
// for advanced users only
if(settings.endpoint !== 'oembed'){
return settings.success(oembed, dict);
}
var _a, code, style, title, units, thumb, provider, description;
if ((_a = oembed.type) === 'photo') {
title = oembed.title || '';
code = "<a href='" + dict.url + "' target='_blank'><img style='" + createImageStyle() + "' src='" + oembed.url + "' alt='" + title + "' /></a>";
} else if (_a === 'video') {
code = oembed.html;
} else if (_a === 'rich') {
code = oembed.html;
} else {
title = oembed.title || dict.url;
thumb = oembed.thumbnail_url ? "<img src='" + oembed.thumbnail_url + "' class='thumb' style='" + createImageStyle() + "'/>" : "";
description = oembed.description ? '<div class="description">' + oembed.description + '</div>' : '';
provider = oembed.provider_name ? "<a href='" + oembed.provider_url + "' class='provider'>" + oembed.provider_name + "</a>" : "";
code = thumb + "<a href='" + dict.url + "'>" + title + "</a>";
code += provider;
code += description;
}
if (settings.wrapElement && settings.wrapElement === 'div' && $.browser.msie && $.browser.version < 9){
settings.wrapElement = 'span';
}
if (settings.wrapElement) {
code = '<' + settings.wrapElement+ ' class="' + settings.className + '">' + code + '</' + settings.wrapElement + '>';
}
oembed.code = code;
// for DOM elements we add the oembed object as a data field to that element and trigger a custom event called oembed
// with the custom event, developers can do any number of custom interactions with the data that is returned.
if (typeof dict.node !== "undefined") { $(dict.node).data('oembed', oembed).trigger('embedly-oembed', [oembed]); }
return settings.success(oembed, dict);
};
var processBatch = function(batch){
var data, embed, urls, dimensions, node;
urls = $.map(batch,
function(e, i) {
if (i === 0) {
if ( e.node !== null){
node = $(e.node);
dimensions = {
"width": node.parent().width(),
"height": node.parent().height()
};
}
}
return encodeURIComponent(e.url);
}).join(',');
$.ajax({
url: getUrl(),
dataType: 'jsonp',
data: getParams(urls),
success: function(data) {
return $.each(data,
function(index, elem) {
return elem.type !== 'error' ? processEmbed(elem, batch[index]) : settings.error(batch[index].node, elem);
});
}
});
};
$.each(urls, function(i, v){
var node = typeof settings.elems !== "undefined" ? settings.elems[i] : null;
if(typeof node !== "undefined" && !urlValid(v)){
$(node).data('oembed', false);
}
var err = {url: v, error_code:400, error_message:'HTTP 400: Bad Request', type:'error'};
return (v && urlValid(v)) ? elems.push({'url':v, 'node':node }) : settings.error(node, err);
});
var _a = [];
var _b = elems.length;
for (var i = 0; (0 <= _b ? i < _b: i > _b); i += 20) {
_a = _a.concat(processBatch(elems.slice(i, i + 20)));
}
if(settings.elems){
return settings.elems;
} else {
return this;
}
}
});
// Versions
$.embedly.version = "2.2.0";
// Once the function is we can add defaults as an attribute
$.embedly.defaults = {
endpoint: 'oembed', // default endpoint is oembed (preview and objectify available too)
secure: false, // use https endpoint vs http
frame: false, // serves all embeds within an iframe to avoid XSS issues
wmode: 'opaque', // for flash elements set a wmode
method: 'replace', // embed handling option for standard callback
addImageStyles: true, // add style="" attribute to images for maxWidth and maxHeight
wrapElement: 'div', // standard wrapper around all returned embeds
className: 'embed', // class on the wrapper element
elems: []
};
$.fn.embedly = function(options, callback){
var settings = typeof options !== "undefined" ? options : {};
// callback is a legacy option, we should be moving towards including a success method in the options
if (typeof callback !== "undefined") {options.success = callback; }
//settings.elems = this;
var urls = new Array();
var nodes = new Array();
this.each(function(){
if (typeof $(this).attr('href') !== "undefined"){
urls.push($(this).attr('href'));
nodes.push($(this));
} else {
$(this).find('a').each(function(){
urls.push($(this).attr('href'));
nodes.push($(this));
});
}
settings.elems = nodes;
});
var elems = $.embedly(urls, settings);
return this;
};
})(jQuery);
+1
View File
@@ -0,0 +1 @@
(function(a){window.embedlyURLre=/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;a.embedly=a.embedly||{};if(a.embedly.version){return}a.extend({embedly:function(k,q,n){var c=[];var p="http://api.embed.ly/";var d;q=q?q:{};d=a.extend({},a.embedly.defaults,q);if(!d.urlRe){d.urlRe=window.embedlyURLre}if(typeof k==="string"){k=new Array(k)}if(typeof n!=="undefined"){d.success=n}if(d.secure){p="https://api.embed.ly/"}if(!d.success){d.success=function(i,t){var r,s=a(t.node);if(!(i)){return null}if((r=d.method)==="replace"){return s.replaceWith(i.code)}else{if(r==="after"){return s.after(i.code)}else{if(r==="afterParent"){return s.parent().after(i.code)}else{if(r==="replaceParent"){return s.parent().replaceWith(i.code)}}}}}}if(!d.error){d.error=function(i,r){}}var m=function(i){return d.urlRe.test(i)};var o=function(r){var i="urls="+r;if(d.maxWidth){i+="&maxwidth="+d.maxWidth}else{if(typeof dimensions!=="undefined"){i+="&maxwidth="+dimensions.width}}if(d.maxHeight){i+="&maxheight="+d.maxHeight}if(d.chars){i+="&chars="+d.chars}if(d.words){i+="&words="+d.words}if(d.secure){i+="&secure=true"}if(d.frame){i+="&frame=true"}i+="&wmode="+d.wmode;if(typeof d.key==="string"){i+="&key="+d.key}if(typeof d.autoplay==="string"||typeof d.autoplay==="boolean"){i+="&autoplay="+d.autoplay}if(d.width){i+="&width="+d.width}return i};var j=function(){if(typeof d.key==="string"){if(d.endpoint.search(/objectify/i)>=0){return p+"2/objectify"}else{if(d.endpoint.search(/preview/i)>=0){return p+"1/preview"}}}return p+"1/oembed"};var b=function(){var i=[];if(d.addImageStyles){if(d.maxWidth){units=isNaN(parseInt(d.maxWidth,10))?"":"px";i.push("max-width: "+(d.maxWidth)+units)}if(d.maxHeight){units=isNaN(parseInt(d.maxHeight,10))?"":"px";i.push("max-height: "+(d.maxHeight)+units)}}return i.join(";")};var g=function(v,t){if(d.endpoint!=="oembed"){return d.success(v,t)}var w,s,r,y,u,i,x,z;if((w=v.type)==="photo"){y=v.title||"";s="<a href='"+t.url+"' target='_blank'><img style='"+b()+"' src='"+v.url+"' alt='"+y+"' /></a>"}else{if(w==="video"){s=v.html}else{if(w==="rich"){s=v.html}else{y=v.title||t.url;i=v.thumbnail_url?"<img src='"+v.thumbnail_url+"' class='thumb' style='"+b()+"'/>":"";z=v.description?'<div class="description">'+v.description+"</div>":"";x=v.provider_name?"<a href='"+v.provider_url+"' class='provider'>"+v.provider_name+"</a>":"";s=i+"<a href='"+t.url+"'>"+y+"</a>";s+=x;s+=z}}}if(d.wrapElement&&d.wrapElement==="div"&&a.browser.msie&&a.browser.version<9){d.wrapElement="span"}if(d.wrapElement){s="<"+d.wrapElement+' class="'+d.className+'">'+s+"</"+d.wrapElement+">"}v.code=s;if(typeof t.node!=="undefined"){a(t.node).data("oembed",v).trigger("embedly-oembed",[v])}return d.success(v,t)};var e=function(i){var t,v,u,s,r;u=a.map(i,function(x,w){if(w===0){if(x.node!==null){r=a(x.node);s={width:r.parent().width(),height:r.parent().height()}}}return encodeURIComponent(x.url)}).join(",");a.ajax({url:j(),dataType:"jsonp",data:o(u),success:function(w){return a.each(w,function(x,y){return y.type!=="error"?g(y,i[x]):d.error(i[x].node,y)})}})};a.each(k,function(s,r){var u=typeof d.elems!=="undefined"?d.elems[s]:null;if(typeof u!=="undefined"&&!m(r)){a(u).data("oembed",false)}var t={url:r,error_code:400,error_message:"HTTP 400: Bad Request",type:"error"};return(r&&m(r))?c.push({url:r,node:u}):d.error(u,t)});var l=[];var h=c.length;for(var f=0;(0<=h?f<h:f>h);f+=20){l=l.concat(e(c.slice(f,f+20)))}if(d.elems){return d.elems}else{return this}}});a.embedly.version="2.2.0";a.embedly.defaults={endpoint:"oembed",secure:false,frame:false,wmode:"opaque",method:"replace",addImageStyles:true,wrapElement:"div",className:"embed",elems:[]};a.fn.embedly=function(d,g){var e=typeof d!=="undefined"?d:{};if(typeof g!=="undefined"){d.success=g}var f=new Array();var c=new Array();this.each(function(){if(typeof a(this).attr("href")!=="undefined"){f.push(a(this).attr("href"));c.push(a(this))}else{a(this).find("a").each(function(){f.push(a(this).attr("href"));c.push(a(this))})}e.elems=c});var b=a.embedly(f,e);return this}})(jQuery);
+49
View File
@@ -0,0 +1,49 @@
{
"name": "embedly-jquery",
"version": "2.2.0",
"filename": "jquery.embedly.min.js",
"description": "Embedly - jQuery is a jQuery Library for Embedly that will replace links with content. It follows the oEmbed spec (oembed.com) for content retrieval, while utilizing http://api.embed.ly as a single endpoint.",
"homepage": "https://github.com/embedly/embedly-jquery",
"maintainers": [
{
"name": "Andrew Pellett",
"url": "https://github.com/anrope"
},
{
"name": "Art Gibson",
"url": "https://github.com/artgibson"
},
{
"name": "Bob Corsaro",
"url": "https://github.com/dokipen"
},
{
"name": "John Emhoff",
"url": "https://github.com/JohnEmhoff"
},
{
"name": "Sean Creeley",
"url": "https://github.com/screeley"
}
],
"repositories": [
{
"type": "git",
"url": "https://github.com/embedly/embedly-jquery"
}
],
"licenses": [
{
"type": "BSD",
"url": "http://github.com/embedly/embedly-jquery/tree/master/LICENSE/"
}
],
"dependencies": {
"jquery": "1.3.1 or greater"
},
"keywords": [
"oEmbed",
"jQuery",
"embed"
]
}