/
core.js
192 lines (158 loc) · 6.11 KB
/
core.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
183
184
185
186
187
188
189
190
191
192
/*!
* jQuery Lifestream Plug-in
* @version 0.3.5
* Show a stream of your online activity
*
* Copyright 2011, Christian Vuerings - http://denbuzze.com
*/
/*globals jQuery, $ */
;(function( $ ){
"use strict";
/**
* Initialize the lifestream plug-in
* @param {Object} config Configuration object
*/
$.fn.lifestream = function( config ) {
// Make the plug-in chainable
return this.each(function() {
// The element where the lifestream is linked to
var outputElement = $(this),
// Extend the default settings with the values passed
settings = jQuery.extend({
// The name of the main lifestream class
// We use this for the main ul class e.g. lifestream
// and for the specific feeds e.g. lifestream-twitter
classname: "lifestream",
// Callback function which will be triggered when a feed is loaded
feedloaded: null,
// The amount of feed items you want to show
limit: 10,
// An array of feed items which you want to use
list: []
}, config),
// The data object contains all the feed items
data = {
count: settings.list.length,
items: []
},
// We use the item settings to pass the global settings variable to
// every feed
itemsettings = jQuery.extend( true, {}, settings ),
/**
* This method will be called every time a feed is loaded. This means
* that several DOM changes will occur. We did this because otherwise it
* takes to look before anything shows up.
* We allow 1 request per feed - so 1 DOM change per feed
* @private
* @param {Array} inputdata an array containing all the feeditems for a
* specific feed.
*/
finished = function( inputdata ) {
// Merge the feed items we have from other feeds, with the feeditems
// from the new feed
$.merge( data.items, inputdata );
// Sort the feeditems by date - we want the most recent one first
data.items.sort( function( a, b ) {
return ( b.date - a.date );
});
var items = data.items,
// We need to check whether the amount of current feed items is
// smaller than the main limit. This parameter will be used in the
// for loop
length = ( items.length < settings.limit ) ?
items.length :
settings.limit,
i = 0, item,
// We create an unordered list which will create all the feed
// items
ul = $('<ul class="' + settings.classname + '"/>');
// Run over all the feed items + add them as list items to the
// unordered list
for ( ; i < length; i++ ) {
item = items[i];
if ( item.html ) {
$('<li class="'+ settings.classname + '-' +
item.config.service + '">').data( "name", item.config.service )
.data( "url", item.url || "#" )
.data( "time", item.date )
.append( item.html )
.appendTo( ul );
}
}
// Change the innerHTML with a list of all the feeditems in
// chronological order
outputElement.html( ul );
// Trigger the feedloaded callback, if it is a function
if ( $.isFunction( settings.feedloaded ) ) {
settings.feedloaded();
}
},
/**
* Fire up all the feeds and pass them the right arugments.
* @private
*/
load = function() {
var i = 0, j = settings.list.length;
// We don't pass the list array to each feed because this will create
// a recursive JavaScript object
delete itemsettings.list;
// Run over all the items in the list
for( ; i < j; i++ ) {
var config = settings.list[i];
// Check whether the feed exists, if the feed is a function and if a
// user has been filled in
if ( $.fn.lifestream.feeds[config.service] &&
$.isFunction( $.fn.lifestream.feeds[config.service] ) &&
config.user) {
// You'll be able to get the global settings by using
// config._settings in your feed
config._settings = itemsettings;
// Call the feed with a config object and finished callback
$.fn.lifestream.feeds[config.service]( config, finished );
}
}
};
// Load the jQuery templates plug-in if it wasn't included in the page.
// At then end we call the load method.
if( !jQuery.tmpl ) {
jQuery.getScript(
"http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js",
load);
} else {
load();
}
});
};
/**
* Create a valid YQL URL by passing in a query
* @param {String} query The query you want to convert into a valid yql url
* @return {String} A valid YQL URL
*/
$.fn.lifestream.createYqlUrl = function( query ) {
return ( "http://query.yahooapis.com/v1/public/yql?q=__QUERY__&env=" +
"store://datatables.org/alltableswithkeys&format=json")
.replace( "__QUERY__" , encodeURIComponent( query ) );
};
/**
* A big container which contains all available feeds
*/
$.fn.lifestream.feeds = $.fn.lifestream.feeds || {};
/**
* Add compatible Object.keys support in older environments that do not natively support it
* https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys#section_6
*/
if(!Object.keys) {
Object.keys = function(o){
if (o !== Object(o)){
throw new TypeError('Object.keys called on non-object');
}
var ret=[],p;
for(p in o) {
if(Object.prototype.hasOwnProperty.call(o,p)) {
ret.push(p);
}
}
return ret;
};
}
}( jQuery ));