-
Notifications
You must be signed in to change notification settings - Fork 1
/
nl_embed.js
1 lines (1 loc) · 4.78 KB
/
nl_embed.js
1
if(void 0===Neighborland)var Neighborland={};Neighborland.EmbedRenderer=function(){"use strict";var e=function(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")},t=function(t){var n="";return null!==t.i_want_text&&(n+="<div class='idea' id='"+t.id+"'><div class='ideatext'><a href='"+t.alternate_url+"'>“"+e(t.i_want_text)+"”</a></div>",n+="<div class='embed-pointer'></div>"),n+="<div class='embed-meta cf'><div class='embed-sayer'><img src='"+t.neighbor.avatar_thumb_url+"' width='36' height='36' /></div>",n+="<div class='embed-says'>says <a href='"+t.neighbor.alternate_url+"'>"+e(t.neighbor.name)+"</a> in <a href='"+t.city.alternate_url+"'>"+e(t.city.name)+", "+e(t.city.state)+"</a><br />",n+="<span class='embed-support'><strong>"+t.support_count,n+=1===t.support_count?"</strong> neighbor wants it</span></div></div>":"</strong> neighbors want it</span></div></div>",n+="</div>",n+="</div>"},n=function(t){var n="";return n+="<div class='idea cf' id='"+t.id+"'>",n+="<div class='embed-sayer-compact'><img src='"+t.neighbor.avatar_thumb_url+"' width='36' height='36' /></div>",n+="<div class='compact-wrap'>",null!==t.i_want_text&&(n+="<div class='ideatext-compact'><a href='"+t.alternate_url+"'>“"+e(t.i_want_text)+"”</a></div>"),n+="<div class='embed-meta-compact'>says <a href='"+t.neighbor.alternate_url+"'>"+e(t.neighbor.name)+"</a> in <a href='"+t.city.alternate_url+"'>"+e(t.city.name)+", "+e(t.city.state)+"</a>",n+=" / <strong>"+t.support_count,n+=1===t.support_count?"</strong> wants it</div>":"</strong> want it</div>",n+="</div>",n+="</div>"};return{renderIdeas:function(e,i){var r,o="";for(o+=this.renderTitle(e,i),r=0;r<e.length;r++)switch(i.style){case"compact":o+=n(e[r]);break;case"fancy":o+=t(e[r])}return o+="<div class='widget-footer cf'>Powered by <a href='https://neighborland.com'>Neighborland</a></div>"},renderTitle:function(t,n){var i="";if(t.length>0&&"question"!==n.context){switch(i+="<div class='cf widget-title-"+n.style+"'>",n.context){case"city":i+="Ideas in "+e(t[0].city.name);break;case"neighbor":i+="Ideas from "+e(t[0].neighbor.name)}i+="</div>"}return i},renderQuestionTitle:function(t,n){var i="";return i+="<div class='cf widget-title-"+n.style+"'>",i+=e(t.question_text),i+="</div>"}}}(),Neighborland.embedUrlBuilder=function(e){var t=e+"api/v1/",n=function(e){return!!e&&("string"==typeof e&&e.length>=1)},i=function(e){var t="";return e&&(e.limit&&e.limit>0?t+="?per_page="+e.limit:t+="?per_page=5",n(e.filter)&&(t+="&filter="+encodeURIComponent(e.filter))),t};return{cityUrl:function(e,r){if(e&&n(e))return t+"cities/"+encodeURIComponent(e)+"/ideas"+i(r)},questionUrl:function(e,r){if(e&&n(e))return t+"questions/"+encodeURIComponent(e)+"/ideas"+i(r)},neighborUrl:function(e,r){if(e&&n(e))return t+"neighbors/"+encodeURIComponent(e)+"/ideas"+i(r)},oneQuestionUrl:function(e){if(e&&n(e))return t+"questions/"+encodeURIComponent(e)}}},Neighborland.nlEmbedBuilder=function(e){function t(e){var t,n;for((t=document.createElement("link")).rel="stylesheet",t.type="text/css",t.href=e,t.media="all",n=document.lastChild;1!==n.nodeType;)n=n.previousSibling;for(n=n.firstChild;1!==n.nodeType;)n=n.nextSibling;n.appendChild(t)}function n(e){return e&&e.rootId?e.rootId:"nl_embed"}function i(i,r,o){t(e+"static/nl_embed.css?cachebuster=2"),document.write("<div class='neighborland_embed' id='"+n(r)+"' style='display: none'></div>");var a=new XMLHttpRequest;a.open("GET",i),a.onload=function(e){4===a.readyState&&(200===a.status?o(JSON.parse(a.responseText)):console.error(a.statusText))},a.send()}function r(e){return e&&e.style?e.style:"fancy"}function o(e){return e&&e.width?e.width:"330px"}var a=Neighborland.embedUrlBuilder(e);return{cityIdeas:function(e,t){var d=this;i(a.cityUrl(e,t),t,function(e){d.renderIdeas(e,{style:r(t),context:"city",width:o(t),rootId:n(t)})})},questionIdeas:function(e,t){var d=this;i(a.questionUrl(e,t),t,function(i){var s;d.renderIdeas(i,{style:r(t),context:"question",width:o(t),rootId:n(t)}),(s=new XMLHttpRequest).open("GET",a.oneQuestionUrl(e)),s.onload=function(e){4===s.readyState&&(200===s.status?d.renderQuestionTitle(JSON.parse(s.responseText),{style:r(t),context:"question",width:o(t),rootId:n(t)}):console.error(s.statusText))},s.send()})},neighborIdeas:function(e,t){var d=this;i(a.neighborUrl(e,t),t,function(e){d.renderIdeas(e,{style:r(t),context:"neighbor",width:o(t),rootId:n(t)})})},renderQuestionTitle:function(e,t){if(e){var n=document.getElementById(t.rootId),i="",r=n.innerHTML;i=Neighborland.EmbedRenderer.renderQuestionTitle(e,t),n.innerHTML=i+r}},renderIdeas:function(e,t){if(e){var n=document.getElementById(t.rootId),i="";i=Neighborland.EmbedRenderer.renderIdeas(e,t),n.innerHTML=n.innerHTML+i,n.style.width=t.width,n.style.display="block",n.style.visibility="visible"}}}};var NlEmbed=Neighborland.nlEmbedBuilder("https://neighborland.com/");