/*! Name: Instagram Lite Dependencies: jQuery Author: Michael Lynch Author URL: http://michaelynch.com Date Created: January 14, 2014 Licensed under the MIT license */ ;(function($) { $.fn.instagramLite = function(options) { // return if no element was bound // so chained events can continue if(!this.length) { return this; } // define plugin plugin = this; // define default parameters plugin.defaults = { accessToken: null, limit: null, list: true, videos: false, urls: false, captions: false, date: false, likes: false, comments_count: false, element_class: '', error: function() {}, success: function() {} } // vars var s = $.extend({}, plugin.defaults, options), el = $(this); var formatCaption = function(caption) { var words = caption.split(' '), newCaption = ''; for(var i = 0; i < words.length; i++) { var word; if(words[i][0] == '@') { var a = ''+words[i]+''; word = a; } else if(words[i][0] == '#') { var a = ''+words[i]+''; word = a; } else { word = words[i] } newCaption += word + ' '; } return newCaption; }; var constructMedia = function(data) { // for each piece of media returned for(var i = 0; i < data.length; i++) { // define media namespace var thisMedia = data[i], item; // if media type is image or videos is set to false if(thisMedia.type === 'image' || !s.videos) { // construct image item = 'Instagram Image'; item += ''; if(s.captions || s.date || s.likes || s.comments_count) { item += ''; } // if caption setting is true if(s.captions && thisMedia.caption) { item += ''+formatCaption(thisMedia.caption.text)+''; } // if date setting is true if(s.date) { var date = new Date(thisMedia.created_time * 1000), day = date.getDate(), month = date.getMonth() + 1, year = date.getFullYear(), hours = date.getHours(), minutes = date.getMinutes(), seconds = date.getSeconds(); date = month +'/'+ day +'/'+ year; item += ''+date+''; } // if likes setting is true if(s.likes) { item += ''; } // if caption setting is true if(s.comments_count && thisMedia.comments) { item += ' '+thisMedia.comments.count+''; } if(s.captions || s.date || s.likes || s.comments_count) { item += ''; } item += ''; // if url setting is true if(s.urls) { item = ''+item+''; } } if(thisMedia.type === 'video' && s.videos) { if(thisMedia.videos) { var src; if(thisMedia.videos.standard_resolution) { src = thisMedia.videos.standard_resolution.url; } else if(thisMedia.videos.low_resolution) { src = thisMedia.videos.low_resolution.url; } else if(thisMedia.videos.low_bandwidth) { src = thisMedia.videos.low_bandwidth.url; } item = ''; } } // if list setting is true if(s.list && item) { // redefine item with wrapping list item item = '
'+item+'
'; } // append image / video if(item !== '') { el.append(item); } } } var loadContent = function() { // if access token if(s.accessToken) { // construct API endpoint var url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token='+s.accessToken+'&count='+s.limit; $.ajax({ type: 'GET', url: url, dataType: 'jsonp', success: function(data) { // if success status if(data.meta.code === 200 && data.data.length) { // construct media constructMedia(data.data); // execute success callback s.success.call(this); } else { // execute error callback s.error.call(this); } }, error: function() { // execute error callback s.error.call(this); } }); } } // init loadContent(); } })(jQuery);