forked from embedly/jquery-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.preview.min.js
12 lines (12 loc) · 16.4 KB
/
jquery.preview.min.js
1
2
3
4
5
6
7
8
9
10
11
12
(function(e){function b(){if(e.preview!==undefined&&e.preview.debug&&window.console){console.log(Array.prototype.slice.call(arguments))}}
/*!
* linkify - v0.3 - 6/27/2009
* http://benalman.com/code/test/js-linkify/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*
* Some regexps adapted from http://userscripts.org/scripts/review/7122
*/
window.linkify=(function(){var k="[a-z\\d.-]+://",m="(?:(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.){3}(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])",l="(?:(?:[^\\s!@#$%^&*()_=+[\\]{}\\\\|;:'\",.<>/?]+)\\.)+",s="(?:ac|ad|aero|ae|af|ag|ai|al|am|an|ao|aq|arpa|ar|asia|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|biz|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|cat|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|coop|com|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|info|int|in|io|iq|ir|is|it|je|jm|jobs|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mobi|mo|mp|mq|mr|ms|mt|museum|mu|mv|mw|mx|my|mz|name|na|nc|net|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pro|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|travel|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--80akhbyknj4f|xn--9t4b11yi5a|xn--deba0ad|xn--g6w251d|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--jxalpdlp|xn--kgbechtv|xn--zckzah|ye|yt|yu|za|zm|zw)",h="(?:"+l+s+"|"+m+")",t="(?:[;/][^#?<>\\s]*)?",i="(?:\\?[^#<>\\s]*)?(?:#[^<>\\s]*)?",j="\\b"+k+"[^<>\\s]+",g="\\b"+h+t+i+"(?!\\w)",r="mailto:",n="(?:"+r+")?[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@"+h+i+"(?!\\w)",f=new RegExp("(?:"+j+"|"+g+"|"+n+")","ig"),p=new RegExp("^"+k,"i"),q={"'":"`",">":"<",")":"(","]":"[","}":"{","»":"«","›":"‹"},o={callback:function(v,u){return u?'<a href="'+u+'" title="'+u+'">'+v+"</a>":v},punct_regexp:/(?:[!?.,:;'"]|(?:&|&)(?:lt|gt|quot|apos|raquo|laquo|rsaquo|lsaquo);)$/};return function(A,J){J=J||{};var C,z,E,v,B="",y=[],x,I,H,u,w,G,F,D;for(z in o){if(J[z]===undefined){J[z]=o[z]}}while(C=f.exec(A)){E=C[0];I=f.lastIndex;H=I-E.length;if(/[\/:]/.test(A.charAt(H-1))){continue}do{u=E;D=E.substr(-1);F=q[D];if(F){w=E.match(new RegExp("\\"+F+"(?!$)","g"));G=E.match(new RegExp("\\"+D,"g"));if((w?w.length:0)<(G?G.length:0)){E=E.substr(0,E.length-1);I--}}if(J.punct_regexp){E=E.replace(J.punct_regexp,function(K){I-=K.length;return""})}}while(E.length&&E!==u);v=E;if(!p.test(v)){v=(v.indexOf("@")!==-1?(!v.indexOf(r)?"":r):!v.indexOf("irc.")?"irc://":!v.indexOf("ftp.")?"ftp://":"http://")+v}if(x!=H){y.push([A.slice(x,H)]);x=I}y.push([E,v])}y.push([A.substr(x)]);for(z=0;z<y.length;z++){B+=J.callback.apply(window,y[z])}return B||A}})();function a(h,f){var g={selector:".selector",type:"small",template:null,elem:null,partials:{images_small:['<div class="thumbnail">','<div class="controls">','<a class="left" href="#">◀</a>','<a class="right" href="#">▶</a>','<a class="nothumb" href="#">✕</a>',"</div>",'<div class="items">','<ul class="images">',"{{#images}}",'<li><img src="{{url}}"/></li>',"{{/images}}","</ul>","</div>","</div>"].join(""),images_large:['<div class="thumbnail">','<a class="left" href="#">◀</a>','<div class="items">','<ul class="images">',"{{#images}}",'<li><img src="{{url}}"/></li>',"{{/images}}","</ul>","</div>",'<a class="right" href="#">▶</a>','<a class="nothumb" href="#">✕</a>',"</div>"].join(""),attributes:['<a class="title" href="#">{{title}}</a>','<p><a class="description" href="#">{{description}}</a></p>'].join(""),title:'<a class="title" href="#">{{title}}</a>',description:'<p><a class="description" href="#">{{description}}</a></p>',favicon:'<img class="favicon" src="{{favicon_url}}">'},templates:{small:['<div class="selector small">',"{{>images_small}}",'<div class="attributes">',"{{>attributes}}",'<span class="meta">',"{{>favicon}}",'<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>",'<div class="action"><a href="#" class="close">✕</a></div>',"</div>"].join(""),large:['<div class="selector large">',"{{>title}}","{{>images_large}}",'<div class="attributes">',"{{>description}}",'<span class="meta">',"{{>favicon}}",'<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>","</div>"].join(""),rich:{video:['<div class="selector rich">',"{{>title}}","{{>object}}",'<div class="attributes">',"{{>description}}",'<span class="meta">',"{{>favicon}}",'<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>","</div>"].join(""),rich:['<div class="selector rich">',"{{>title}}","{{>object}}",'<div class="attributes">',"{{>description}}",'<span class="meta">',"{{>favicon}}",'<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>","</div>"].join(""),photo:['<div class="selector rich">',"{{>title}}","{{>object}}",'<div class="attributes">',"{{>description}}",'<span class="meta">',"{{>favicon}}",'<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>","</div>"].join(""),link:['<div class="selector rich">',"{{>images_small}}",'<div class="attributes">',"{{>attributes}}",'<span class="meta">',"{{>favicon}}",'<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>","</div>"].join("")}},render:function(m){var l=null;if(this.template!==null){l=this.template}else{l=this.templates[this.type]}if(_.isObject(l)){l=l[m.object_type]}var i=this.toView(m);var k=this.toPartials(m);var j=Mustache.to_html(l,i,k);if(h.find(this.selector).length){this.elem=h.find(this.selector).replaceWith(j)}else{this.elem=h.append(j)}this.elem=h.find(this.selector);this.elem.show();if(m.images.length>0){h.find("#id_thumbnail_url").val(m.images[0].url)}else{this.elem.find(".thumbnail").hide()}if(m.images.length===1){this.elem.find(".left, .right").hide()}this.bind()},toView:function(i){return i},toPartials:function(j){var i=e.extend(true,{},this.partials);i.object="";if(j.object&&(j.object.type==="video"||j.object.type==="rich")){i.object='<div class="media">{{{html}}}</div>'}else{if(j.object&&j.object.type==="photo"){i.object='<div class="media"><img src="{{url}}"/></div>'}}if(!j.favion_url){i.favicon=""}return i},clear:function(i){if(i!==undefined){i.preventDefault()}this.elem.html("");this.elem.hide();h.find('input[type="hidden"].preview_input').remove()},scroll:function(l,o){o.preventDefault();var j=this.elem.find(".images");var m=parseInt(j.find("li").css("width"),10);var n=parseInt(j.css("left"),10);var i=j.find("img").length*m;if(l==="left"){n=parseInt(n,10)+m;if(n>0){return false}}else{if(l==="right"){n=parseInt(n,10)-m;if(n<=-i){return false}}else{b("not a valid direction: "+l);return false}}var k=j.find("img").eq((n/-m)).attr("src");h.find("#id_thumbnail_url").val(k);j.css("left",n+"px")},nothumb:function(i){i.preventDefault();this.elem.find(".thumbnail").hide();h.find("#id_thumbnail_url").val("")},title:function(k){k.preventDefault();var j=e("<input/>").attr({value:e(k.target).text(),"class":"title",type:"text"});e(k.target).replaceWith(j);j.focus();var i=this.title;j.one("blur",function(n){var m=e(n.target);h.find("#id_title").val(m.val());var l=e("<a/>").attr({"class":"title",href:"#"}).text(m.val());e(n.target).replaceWith(l);l.bind("click",i)})},description:function(j){j.preventDefault();var i=e("<textarea/>").attr({"class":"description"}).text(e(j.target).text());e(j.target).replaceWith(i);i.focus();var k=this.description;i.one("blur",function(n){var m=e(n.target);h.find("#id_description").val(m.val());var l=e("<a/>").attr({"class":"description",href:"#"}).text(m.val());e(n.target).replaceWith(l);l.bind("click",k)})},update:function(i){this.elem.find("."+e(i.target).attr("name")).text(e(i.target).val())},bind:function(){this.elem.find(".left").bind("click",_.bind(this.scroll,{},"left"));this.elem.find(".right").bind("click",_.bind(this.scroll,{},"right"));this.elem.find(".nothumb").bind("click",this.nothumb);this.elem.find(".action .close").bind("click",this.clear);this.elem.bind("mouseenter mouseleave",function(){e(this).find(".action").toggle()});this.elem.find(".thumbnail").one("mouseenter",function(){e(this).bind("mouseenter mouseleave",function(){e(this).find(".controls").toggle()})});this.elem.find(".title").bind("click",this.title);this.elem.find(".description").bind("click",this.description)}};_.extend(g,f);_.bindAll(g);return g}function d(g){var f={selector:"#feed",type:"small",template:null,partials:{thumbnail:['<div class="thumbnail {{object_type}}">','<a href="{{original_url}}" target="_blank">','<img title="{{title}}" src="{{thumbnail_url}}"/>','<span class="overlay"></span>',"</a>","</div>"].join("")},templates:{small:['<div class="item">',"{{>thumbnail}}",'<div class="attributes">','<a class="title" href="{{original_url}}" target="_blank">{{title}}</a>','<p class="description">{{description}}</p>','<span class="meta">','<img class="favicon" src="{{favicon_url}}"/>','<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>",'<div class="clearfix"></div>',"</div>"].join(""),status:['<div class="item">','<div class="status">{{{status_linked}}}</div>',"{{>thumbnail}}",'<div class="attributes">','<a class="title" href="{{original_url}}" target="_blank">{{title}}</a>','<p class="description">{{description}}</p>','<span class="meta">','<img class="favicon" src="{{favicon_url}}"/>','<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>",'<div class="clearfix"></div>',"</div>"].join(""),rich:{video:['<div class="item video">','<a class="title" href="{{original_url}}" target="_blank">{{title}}</a>',"{{>object}}",'<div class="attributes">','<p class="description">{{description}}</p>','<span class="meta">','<img class="favicon" src="{{favicon_url}}"/>','<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>",'<div class="clearfix"></div>',"</div>"].join(""),rich:['<div class="item rich">','<a class="title" href="{{original_url}}" target="_blank">{{title}}</a>',"{{>object}}",'<div class="attributes">','<p class="description">{{description}}</p>','<span class="meta">','<img class="favicon" src="{{favicon_url}}"/>','<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>",'<div class="clearfix"></div>',"</div>"].join(""),photo:['<div class="item photo">','<a class="title" href="{{original_url}}" target="_blank">{{title}}</a>',"{{>object}}",'<div class="attributes">','<p class="description">{{description}}</p>','<span class="meta">','<img class="favicon" src="{{favicon_url}}"/>','<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>",'<div class="clearfix"></div>',"</div>"].join(""),link:['<div class="item link">',"{{>thumbnail}}",'<div class="attributes">','<a class="title" href="{{original_url}}" target="_blank">{{title}}</a>','<p class="description">{{description}}</p>','<span class="meta">','<img class="favicon" src="{{favicon_url}}"/>','<a class="provider" href="{{provider_url}}">{{provider_display}}</a>',"</span>","</div>",'<div class="clearfix"></div>',"</div>"].join("")}},toView:function(h){if(h.hasOwnProperty("status")){h.status_linked=linkify(h.status)}return h},toPartials:function(i){var h=e.extend(true,{},this.partials);if(!i.thumbnail_url){h.thumbnail=""}h.object="";if(i.object_type==="video"||i.object_type==="rich"){h.object='<div class="media video">{{{html}}}</div>'}else{if(i.object_type==="photo"||i.type==="image"){h.object='<div class="media image"><img alt="{{title}}" src="{{image_url}}"/></div>'}}if(this.type==="rich"&&i.object_type!=="link"){h.thumbnail=""}return h},create:function(m){var k=null;if(this.template!==null){k=this.template}else{k=this.templates[this.type]}if(_.isObject(k)){k=k[m.object_type]}var h=this.toView(m);var j=this.toPartials(m);var i=Mustache.to_html(k,h,j);var l=e(this.selector).prepend(i).children().first();l.data("preview",m)},play:function(h){},bind:function(){e(".thumbnail.video a, .thumbnail.rich a").live("click",function(i){i.preventDefault();var h=e(this).parents(".item").data("preview");e(this).parents(".item").replaceWith(h.html)})}};_.extend(f,g);_.bindAll(f);f.bind();return f}function c(h,f){var g={api_args:["key","maxwidth","maxheight","width","wmode","autoplay","videosrc","allowscripts","words","chars","secure","frame"],display_attrs:["type","original_url","url","title","description","favicon_url","provider_url","provider_display","provider_name","safe","html","thumbnail_url","object_type","image_url"],default_data:{},debug:false,form:null,type:"link",loading_selector:".loading",options:{debug:false,selector:{},field:null,display:{},preview:{},wmode:"opaque",words:30,maxwidth:560},init:function(j,i){this.options=_.extend(this.options,typeof i!=="undefined"?i:{});var k={};_.each(_.intersection(_.keys(this.options),this.api_args),function(m){var l=i[m];if(!(_.isNull(l)||_.isUndefined(l))){k[m]=l}});this.default_data=k;this.form=null;if(j){this.form=f.form?f.form:j.parents("form")}this.debug=this.options.debug;this.selector=a(this.form,this.options.selector);this.display=d(this.options.display);_.extend(this,this.options.preview);if(j){this.bind()}},getStatusUrl:function(m){var j=h.val();if(j===""){return null}var i=/^http(s?):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i;var l=j.match(i);var k=l?l[0]:null;if(k===null){i=/[-\w]+(\.[a-z]{2,})+(\S+)?(\/|\/[\w#!:.?+=&%@!\-\/])?/gi;l=j.match(i);k=l?"http://"+l[0]:null}return k},toggleLoading:function(){this.form.find(this.loading_selector).toggle()},callback:function(i){},_callback:function(j){this.toggleLoading();b(j);if(!j.hasOwnProperty("type")){b("Embedly returned an invalid response");this.error(j);return false}if(j.type==="error"){b("URL ("+j.url+") returned an error: "+j.error_message);this.error(j);return false}if(!j.safe){b("URL ("+j.url+") was deemed unsafe: "+j.safe_message);this.error(j);return false}if(!(j.type in {html:"",image:""})){b("URL ("+j.url+") returned a type ("+j.type+") not handled");this.error(j);return false}this.form.find('input[type="hidden"].preview_input').remove();var i=this.form;_.each(this.display_attrs,function(o){var k=null;if(o==="object_type"){if(j.hasOwnProperty("object")&&j.object.hasOwnProperty("type")){k=j.object.type}else{k="link"}j.object_type=k}else{if(o==="html"){if(j.hasOwnProperty("object")&&j.object.hasOwnProperty("html")){k=j.object.html}}else{if(o==="image_url"){if(j.hasOwnProperty("object")&&j.object.hasOwnProperty("type")&&j.object.type==="photo"){k=j.object.url}else{if(j.type==="image"){k=j.url}}j.image_url=k}else{k=j[o]}}}var m={name:o,type:"hidden",id:"id_"+o,value:k};var l=i.find("#id_"+o);if(l.length){if(l.attr("type")==="hidden"){delete m.type;l.attr(m)}else{if(!l.val()){l.val(j[o])}else{j[o]=l.val()}l.bind("keyup",function(n){e.preview.selector.update(n)})}l.addClass("preview_input")}else{m["class"]="preview_input";i.append(e("<input />").attr(m))}});this.selector.render(j);this.callback(j)},error:function(){b("error");b(arguments)},ajax:function(i){e.ajax({url:"http://api.embed.ly/1/preview",dataType:"jsonp",data:i,success:this._callback,error:this.error})},fetch:function(i){if(typeof i==="undefined"||typeof i!=="string"){i=this.getStatusUrl()}if(i===null){return true}i=e.trim(i);var k=this.form.find("#id_original_url").val();if(k===i){return true}this.toggleLoading();var j=_.clone(this.default_data);j.url=i;this.ajax(j);return true},keyUp:function(j){if(j.which!==32&&j.which!==13){return null}var i=this.getStatusUrl();if(i===null){return null}b("onKeyUp url:"+i);e(this.status_selector).unbind("keyup");this.fetch(i)},paste:function(i){_.delay(this.fetch,200)},_submit:function(j){var i={};this.form.find("textarea, input").not('input[type="submit"]').each(function(k,l){var m=e(l).attr("name");if(m!==undefined){i[m]=e(l).val()}});this.selector.clear();this.submit(j,i);h.val("");this.form.find('input[type="hidden"].preview_input').remove()},submit:function(k,j){k.preventDefault();var i=e(k.target);e.ajax({type:"post",url:i.attr("action"),data:e.param(j),dataType:"json",success:this.display.create})},bind:function(){b("Starting Bind");h.bind("blur",this.fetch);h.bind("keyup",this.keyUp);h.bind("paste",this.paste);this.form.bind("submit",this._submit);h.bind("attach",this.fetch)}};_.bindAll(g);g.init(h,f);return g}e.preview=new c(null,{});e.fn.preview=function(f,g){e(this).each(function(h,j){e.preview=new c(e(this),f)});return this}})(jQuery);