master
1(function($){
2 var el;
3 var settings = {};
4
5 var methods = {
6 init: function(options) {
7 el = this;
8
9 settings = {
10 token: false,
11 query_param: 'query'
12 };
13
14 if (options) {
15 $.extend(settings, options);
16 }
17
18 if (!settings.token || settings.query_param == '') {
19 return this;
20 }
21
22 $.getJSON(
23 'http://tapirgo.com/api/1/search.json?token=' + settings.token + '&query=' + paramValue(settings.query_param) + '&callback=?', function(data){
24 if(settings['complete']) { settings.complete() }
25 $.each(data, function(key, val) {
26 document.getElementById('search_results').style.display="block";
27 document.getElementById('search_results').style.height="100%";
28 document.getElementById('search_results').style.overflow="hidden";
29 var str1 = val.content;
30 var str2 = str1.substr(1, 250);
31 str2 = str2.substr(0, Math.min(str2.length, str2.lastIndexOf(" ")));
32 el.append('<div id="content" class="inner"><article class="post"><h2 class="title"><a href="' + val.link + '">' + val.title + '</a></h2><div class="entry-content"><div class="date">Published on: ' + (val.published_on).substr(0,10) + '</div></div><br><p>' + str2 + '...</p><br><a href="' + val.link +'">Read on →</a></article></div>');
33 if(key >= (data.length-1)){
34 el.append('<div id="content" class="inner"><center><h2 style="border-bottom: 1px solid #ddd;">Result ends</h2></center><br><br></div>');
35 }
36 });
37 }
38 );
39
40 return this;
41 }
42 };
43
44 // Extract the param value from the URL.
45 function paramValue(query_param) {
46 var results = new RegExp('[\\?&]' + query_param + '=([^&#]*)').exec(window.location.href);
47 return results ? results[1] : false;
48 }
49
50 $.fn.tapir = function(method) {
51 if (methods[method]) {
52 return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1));
53 } else if (typeof method === 'object' || ! method) {
54 return methods.init.apply(this, arguments);
55 } else {
56 $.error('Method ' + method + ' does not exist on jQuery.tapir');
57 }
58 };
59
60})( jQuery );