PageSourceSearch

https://terrazacarmona.com/js/vendor/spectragram.js

js terrazacarmona.com collected 2026-10-02 13:29:05 UTC 6,942 bytes, 185 lines download raw bytes

1/*!
2 * jQuery - Spectragram by Adrian Quevedo
3 * http://adrianquevedo.com/  - http://lab.adrianquevedo.com/ - http://elnucleo.com.co/
4 *
5 * Dual licensed under the MIT or GPL Version 2 licenses.
6 * You are free to use this plugin in commercial projects as long as the copyright header is left intact.
7 *
8 * This plugin uses the Instagram(tm) API and is not endorsed or certified by Instagram or Burbn, inc. 
9 * All Instagram(tm) logos and trademarks displayed on this plugin are property of Burbn, Inc.
10 *
11 * Date: Thu Jul 15 14:05:02 2012 -0500
12 */
13
14// Utility for older browsers
15if (typeof Object.create !== 'function') {
16    Object.create = function(obj) {
17        function F() {};
18        F.prototype = obj;
19        return new F();
20    };
21}
22
23(function($, window, document, undefined) {
24
25    var Instagram = {
26
27        //Initialize function
28        init: function(options, elem) {
29            var self = this;
30
31            self.elem = elem;
32            self.$elem = $(elem);
33
34            self.api = 'https://api.instagram.com/v1',
35
36                self.accessData = $.fn.spectragram.accessData,
37
38                self.options = $.extend({}, $.fn.spectragram.options, options);
39        },
40
41        //Users		
42        //Get the most recent media published by a user.
43        getRecentMedia: function(userID) {
44            var self = this,
45                getData = '/users/' + userID + '/media/recent/?' + self.accessData.clientID + '&access_token=' 
45+ self.accessData.accessToken + '';
46
47            self.fetch(getData).done(function(results) {
48                self.display(results);
49            });
50        },
51
52        //Search for a user by name.
53        getUserFeed: function() {
54            var self = this,
55                getData = '/users/search?q=' + self.options.query + '&count=' + self.options.max + '&access_token=' + self.accessData.accessToken + '';
56
57            self.fetch(getData).done(function(results) {
58                if (results.data.length) {
59                    // only request media for exact match, otherwise 400 error
60                    for (var length = results.data.length, i = 0; i < length; i++) {
61                        if (results.data[i].username === self.options.query) {
62                            self.getRecentMedia(results.data[i].id);
63                        }
64                    }
65                } else {
66                    $.error('Spectagram.js - Error: the username ' + self.options.query + ' does not exist.');
67                };
68            });
69        },
70
71        //Media
72        //Get a list of what media is most popular at the moment
73        getPopular: function() {
74            var self = this,
75                getData = '/media/popular?client_id=' + self.accessData.clientID + '&access_token=' 
75+ self.accessData.accessToken + '';
76
77            self.fetch(getData).done(function(results) {
78                self.display(results);
79            });
80        },
81
82        //Tags
83        //Get a list of recently tagged media
84        getRecentTagged: function() {
85            var self = this,
86                getData = '/tags/' + self.options.query + '/media/recent?client_id=' + self.accessData.clientID + '&access_token=' + self.accessData.accessToken + '';
87
88            self.fetch(getData).done(function(results) {
89                if (results.data.length) {
90                    self.display(results);
91                } else {
92                    $.error('Spectagram.js - Error: the tag ' + self.options.query + ' does not have results.');
93                };
94            });
95        },
96
97        fetch: function(getData) {
98            var self = this,
99                getUrl = self.api + getData;
100
101            return $.ajax({
102                type: "GET",
103                dataType: "jsonp",
104                cache: false,
105                url: getUrl
106            });
107        },
108
109        display: function(results) {
110            var self = this,
111                setSize = self.options.size,
112                size, max = (self.options.max >= results.data.length) ? results.data.length : self.options.max;
113
114            if (results.data.length === 0) {
115                self.$elem.append($(self.options.wrapEachWith).append(self.options.notFoundMsg));
116            } else {
117                for (var i = 0; i < max; i++) {
118                    if (setSize == "small") {
119                        size = results.data[i].images.thumbnail.url;
120                    } else if (setSize == "medium") {
121                        size = results.data[i].images.low_resolution.url;
122                    } else {
123                        size = results.data[i].images.standard_resolution.url;
124                    }
125
126                    var titleIMG;
127                    // Skip if the caption is empty.
128                    if (results.data[i].caption != null) {
129                        /**
130                         * 1. First it creates a dummy element <span/>
131                         * 2. And then puts the caption inside the element created previously.
132                         * 3. Extracts the html caption (this allows html codes to be included).
133                         * 4. Lastly, the most important part, create the Title attribute using double quotes
134                         * to enclose the text. This fixes the bug when the caption retrieved from Instagram 
135                         * includes single quotes which breaks the Title attribute.
136                         */
137                        titleIMG = 'title="' + $('<span/>').text(results.data[i].caption.text).html() + '"';
138                    }
139
140                    // Now concatenate the titleIMG generated.
141                    self.$elem.append($(self.options.wrapEachWith).append("<a " + titleIMG + " target='_blank' href='" + results.data[i].link + "'><img src='" + size + "'></img></a>"));
142                }
143            }
144
145            if (typeof self.options.complete === 'function') {
146                self.options.complete.call(self);
147            }
148        }
149    };
150
151    jQuery.fn.spectragram = function(method, options) {
152
153        if (jQuery.fn.spectragram.accessData.clientID) {
154
155            this.each(function() {
156                var instagram = Object.create(Instagram);
157                instagram.init(options, this);
158                if (instagram[method]) {
159                    return instagram[method](this);
160                } else {
161                    $.error('Method ' + method + ' does not exist on jQuery.spectragram');
162                }
163            });
164
165        } else {
166            $.error('You must define an accessToken and a clientID on jQuery.spectragram');
167        }
168    };
169
170    //Plugin Default Options
171    jQuery.fn.spectragram.options = {
172        max: 10,
173        query: 'coffee',
174        size: 'medium',
175        wrapEachWith: '<li></li>',
176        complete: null
177    };
178
179    //Instagram Access Data
180    jQuery.fn.spectragram.accessData = {
181        accessToken: null,
182        clientID: null
183    };
184
185})(jQuery, window, document);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.