This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
forked from jamwise/ghostHunter
-
Notifications
You must be signed in to change notification settings - Fork 12
/
jquery.ghostHunter-nodependency.js
182 lines (153 loc) · 5.01 KB
/
jquery.ghostHunter-nodependency.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* ghostHunter - 0.3.5
* Copyright (C) 2014 Jamal Neufeld ([email protected])
* MIT Licensed
* @license
*/
(function( $ ) {
/* Include the Lunr library */
var lunr=require('./lunr.min.js');
//This is the main plugin definition
$.fn.ghostHunter = function( options ) {
//Here we use jQuery's extend to set default values if they weren't set by the user
var opts = $.extend( {}, $.fn.ghostHunter.defaults, options );
if( opts.results )
{
pluginMethods.init( this , opts );
return pluginMethods;
}
};
$.fn.ghostHunter.defaults = {
resultsData : false,
onPageLoad : false,
onKeyUp : false,
result_template : "<a href='{{link}}'><p><h2>{{title}}</h2><h4>{{prettyPubDate}}</h4></p></a>",
info_template : "<p>Number of posts found: {{amount}}</p>",
displaySearchInfo : true,
zeroResultsInfo : true,
before : false,
onComplete : false,
includepages : false,
filterfields : false
};
var prettyDate = function(date) {
var d = new Date(date);
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return d.getDate() + ' ' + monthNames[d.getMonth()] + ' ' + d.getFullYear();
};
var pluginMethods = {
isInit : false,
init : function( target , opts ){
var that = this;
this.target = target;
this.results = opts.results;
this.blogData = {};
this.result_template = opts.result_template;
this.info_template = opts.info_template;
this.zeroResultsInfo = opts.zeroResultsInfo;
this.displaySearchInfo = opts.displaySearchInfo;
this.before = opts.before;
this.onComplete = opts.onComplete;
this.includepages = opts.includepages;
this.filterfields = opts.filterfields;
//This is where we'll build the index for later searching. It's not a big deal to build it on every load as it takes almost no space without data
this.index = lunr(function () {
this.field('title', {boost: 10})
this.field('description')
this.field('link')
this.field('markdown', {boost: 5})
this.field('pubDate')
this.field('tag')
this.ref('id')
});
if ( opts.onPageLoad ) {
that.loadAPI();
} else {
target.focus(function(){
that.loadAPI();
});
}
target.closest("form").submit(function(e){
e.preventDefault();
that.find(target.val());
});
if( opts.onKeyUp ) {
target.keyup(function() {
that.find(target.val());
});
}
},
loadAPI : function(){
if(this.isInit) return false;
/* Here we load all of the blog posts to the index.
This function will not call on load to avoid unnecessary heavy
operations on a page if a visitor never ends up searching anything. */
var index = this.index,
blogData = this.blogData;
obj = {limit: "all", include: "tags"};
if ( this.includepages ){
obj.filter="(page:true,page:false)";
}
$.get(ghost.url.api('posts',obj)).done(function(data){
searchData = data.posts;
searchData.forEach(function(arrayItem){
var tag_arr = arrayItem.tags.map(function(v) {
return v.name; // `tag` object has an `name` property which is the value of tag. If you also want other info, check API and get that property
})
if(arrayItem.meta_description == null) { arrayItem.meta_description = '' };
var category = tag_arr.join(", ");
if (category.length < 1){
category = "undefined";
}
var parsedData = {
id : String(arrayItem.id),
title : String(arrayItem.title),
description : String(arrayItem.meta_description),
markdown : String(arrayItem.markdown),
pubDate : String(arrayItem.created_at),
tag : category,
link : String(arrayItem.url)
}
parsedData.prettyPubDate = prettyDate(parsedData.pubDate);
var tempdate = prettyDate(parsedData.pubDate);
index.add(parsedData)
blogData[arrayItem.id] = {title: arrayItem.title, description: arrayItem.meta_description, pubDate: tempdate, link: arrayItem.url};
});
});
this.isInit = true;
},
find : function(value){
var searchResult = this.index.search(value);
var results = $(this.results);
var resultsData = [];
results.empty();
if(this.before) {
this.before();
};
if(this.zeroResultsInfo || searchResult.length > 0)
{
if(this.displaySearchInfo) results.append(this.format(this.info_template,{"amount":searchResult.length}));
}
for (var i = 0; i < searchResult.length; i++)
{
var lunrref = searchResult[i].ref;
var postData = this.blogData[lunrref];
results.append(this.format(this.result_template,postData));
resultsData.push(postData);
}
if(this.onComplete) {
this.onComplete(resultsData);
};
},
clear : function(){
$(this.results).empty();
this.target.val("");
},
format : function (t, d) {
return t.replace(/{{([^{}]*)}}/g, function (a, b) {
var r = d[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
}
}
})( jQuery );