Created
April 23, 2018 06:16
-
-
Save danshan/86beb183492da88d5f259d8eb120a370 to your computer and use it in GitHub Desktop.
jquery-tapir.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
var el; | |
var settings = {}; | |
var methods = { | |
init: function(options) { | |
el = this; | |
settings = { | |
token: false, | |
query_param: 'query' | |
}; | |
if(options) { | |
$.extend(settings, options); | |
} | |
if(!settings.token || settings.query_param == '') { | |
return this; | |
} | |
$.getJSON('http://tapirgo.com/api/1/search.json?token=' + settings.token | |
+ '&query=' + paramValue(settings.query_param) + '&callback=?', | |
function(data) { | |
if(settings['complete']) { | |
settings.complete() | |
} | |
$.each(data, function(key, val) { | |
el.append('<div class="result"><h3><a href="' + val.link + '">' | |
+ val.title + '</a></h3><p>' + val.summary + '</p></div>'); | |
}); | |
}); | |
return this; | |
} | |
}; | |
function paramValue(query_param) { | |
var results = new RegExp('[\\?&]' + query_param | |
+ '=([^&#]*)').exec(window.location.href); | |
return results ? results[1] : false; | |
} | |
$.fn.tapir = function(method) { | |
if(methods[method]) { | |
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); | |
} else if(typeof method === 'object' || !method) { | |
return methods.init.apply(this, arguments); | |
} else { | |
$.error('Method ' + method + ' does not exist on jQuery.tapir'); | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment