ghost-ocular-implants
Version:
A jquery plugin for ghost blog and allows the visitors to search for posts or tags
2 lines (1 loc) • 3.7 kB
JavaScript
!function(t){t.fn.ghostOcularImplants=function(e){function n(t){return 10>t&&(t="0"+t),t}var r=this,s={},i=t(document),u=t("body");s.postlist=[],s.searchstring="",s.searchterms="",s.settings=t.extend({resultBox:"#results",noresults:"<p>No post name or tags were found</p>",template:'<li><a href="{{url}}">{{title}} - tags: [ {{tags}} ] on {{date: MM-DD-YY}}</a></li>',hidePostsSelector:".post-wrap"},e);var l=r.parent("form"),a=r,o=i.find(s.settings.resultBox)||u,c=i.find(s.settings.hidePostsSelector);return s.getposts=function(){var e={limit:"all",include:"author,tags",filter:"(page:true,page:false)"};t.get(ghost.url.api("posts",e)).done(function(t){s.postlist=t.posts})},s.unique=function(t){for(var e=[],n=0;n<t.length;n+=1)-1==e.indexOf(t[n])&&e.push(t[n]);return e},s.mergeResults=function(t,e){var n=[];return n=n.concat(t),n=n.concat(e),s.unique(n)},s.emptyResults=function(){o.empty()},s.objresolve=function(t,e){return t.split(".").reduce(function(t,e){return t?t[e]:void 0},e||r)},s.returnTagsList=function(t,e){var n="",r=e.tags,s=e.tags.length,i=t.includes("_class"),u=t.split(":"),l="undefined"!=typeof u[1]?u[1].slice(1):"",a=i?" ":", ",o="/tag/";if(0>=s)n=i?"no-tags":"no tags";else{for(var c=0;s>c;++c)n+=i?l+r[c].name+a:'<a href="'+o+r[c].name+'/">'+l+r[c].name+"</a>"+a;n=i?n.slice(0,-1):n.slice(0,-2)}return n},s.returnPrettyTime=function(t,e){for(var r="",s=e.created_at,i=new Date(s),u=t.split(":"),l="undefined"!=typeof u[1]?u[1].slice(1):"HH-MM-SS",a=l.split("-"),o={HH:function(t){return t.getHours()},MM:function(t){return n(t.getMinutes())},SS:function(t){return n(t.getSeconds())}},c=0;c<a.length;c++)r+=o[a[c]](i)+":";return r=r.slice(0,-1)},s.returnExcerpt=function(t,e){var n=t.split(":"),r="undefined"!=typeof n[1]?n[1].slice(1):"26",s=e.html.split(" ");s.length;return s.splice(0,r).join(" ").substring(3)},s.returnPrettyDate=function(t,e){for(var r="",s=e.created_at,i=new Date(s),u=t.split(":"),l="undefined"!=typeof u[1]?u[1].slice(1):"MM-DD-YYYY",a=l.split("-"),o="en-us",c={YY:function(t){return t.getFullYear().toString().substr(2,2)},YYYY:function(t){return t.getFullYear()},MM:function(t){return n(t.getMonth()+1)},MMM:function(t){return t.toLocaleString(o,{month:"long"})},DD:function(t){return n(t.getDate())}},f=0;f<a.length;f++)r+=c[a[f]](i)+"-";return r=r.slice(0,-1)},s.parseTemplate=function(t,e){var n=/\{\{(.*?)\}\}/g;return t.replace(n,function(t){var n=t.slice(2,-2);return n.includes("tags")?s.returnTagsList(n,e):n.includes("date")?s.returnPrettyDate(n,e):n.includes("time")?s.returnPrettyTime(n,e):n.includes("excerpt")?s.returnExcerpt(n,e):s.objresolve(n,e)})},s.buildResults=function(t){var e="",n=t.merged.length;if(s.emptyResults(),t.merged.length<=0&&s.searchterms.length>=1)o.html(s.settings.noresults);else for(var r=0;n>r;++r)e=s.parseTemplate(s.settings.template,t.merged[r]),o.append(e)},s.find=function(t){var e={byTitle:[],byTag:[],merged:[]},n=[],r=null,i=null,u=[],l=[];u=t.split(" "),s.searchstring=t,s.searchterms=u,e.byTitle=s.postlist.filter(function(t){return l=t.title.split(" "),i=l.filter(function(t){return u.indexOf(t)>=0}),i.length>0&&"published"===t.status}),e.byTag=s.postlist.filter(function(t){return n=t.tags,r=n.filter(function(t){return u.indexOf(t.name)>=0}),r.length>0&&"published"===t.status}),e.merged=s.mergeResults(e.byTitle,e.byTag),s.buildResults(e)},s.setupSearchInput=function(){l.on("submit",function(t){t.preventDefault()}),a.on("keyup",function(e){var n=t(this),r=n.val();e.preventDefault(),""===r?(s.emptyResults(),c.removeClass("hidden").addClass("visible")):(s.find(r),c.removeClass("visible").addClass("hidden"))})},s.init=function(){s.getposts(),s.setupSearchInput()},s.init()}}(jQuery);