PageRenderTime 71ms CodeModel.GetById 57ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/source/app/NOS.Wall/script/monitter-start.js

http://github.com/agross/netopenspace
JavaScript | 278 lines | 246 code | 28 blank | 4 comment | 8 complexity | 56b12c9145d7c849441e61759f845553 MD5 | raw file
  1$(document).ready(function()
  2{
  3	var mobypicture = {
  4		name: "mobypicture",
  5		match: function (remainder)
  6		{
  7			return remainder.match(/^http:\/\/moby\.to\/([a-z0-9]+)/i);
  8		},
  9		process: function(remainder, link) {
 10			var anchor = $("<a>")
 11				.attr("href", link)
 12				.attr("target", "_blank");
 13			return anchor
 14					.clone()
 15					.append($("<img>")
 16						.attr("src", link + ":thumb")
 17						.data("preview", link + ":medium"))
 18				.prependTo(remainder.append(anchor.text(link)));
 19		}
 20	};
 21	
 22	var yfrog = {
 23		name: "yfrog",
 24		match: function (remainder)
 25		{
 26			return remainder.match(/^http:\/\/yfrog\.com\/([a-z0-9]+)/i);
 27		},
 28		process: function(remainder, link) {
 29			var anchor = $("<a>")
 30				.attr("href", link)
 31				.attr("target", "_blank");
 32			return anchor
 33					.clone()
 34					.append($("<img>")
 35						.attr("src", link + ".th.jpg")
 36						.data("preview", link + ":iphone"))
 37				.prependTo(remainder.append(anchor.text(link)));
 38		}
 39	};
 40	
 41	var twitpic = {
 42		name: "twitpic",
 43		match: function (remainder)
 44		{
 45			return remainder.match(/^http:\/\/twitpic\.com\/([a-z0-9]+)/i);
 46		},
 47		process: function(remainder, link) {
 48			var rematch = twitpic.match(link);
 49			var img_id = RegExp.$1;
 50			var anchor = $("<a>")
 51				.attr("href", link)
 52				.attr("target", "_blank");
 53			return anchor
 54					.clone()
 55					.append($("<img>")
 56						.attr("src", "http://twitpic.com/show/mini/" + img_id)
 57						.data("preview", "http://twitpic.com/show/thumb/" + img_id))
 58				.prependTo(remainder.append(anchor.text(link)));
 59		}
 60	};
 61
 62	var picTwitter = {
 63		name: "picTwitter",
 64		match: function (remainder, tweet)
 65		{
 66			return tweet.entities && tweet.entities.media;
 67		},
 68		process: function(remainder, ignored, tweet) {
 69			var media = tweet.entities.media[0].media_url;
 70			var link = tweet.entities.media[0].url;
 71			var anchor = $("<a>")
 72				.attr("href", link)
 73				.attr("target", "_blank");
 74			return anchor
 75					.clone()
 76					.append($("<img>")
 77						.attr("src", media)
 78						.data("preview", media))
 79				.prependTo(remainder.append(anchor.text(link)));
 80		}
 81	};
 82
 83	var link = {
 84		name: "link",
 85		match: function (remainder) {
 86			return remainder.match(/^[a-z]+:\/\/[a-z0-9-_]+\.[a-z0-9-_:%&\?\/.=#]+/i);
 87		},
 88		process: function(remainder, link) {
 89			return remainder.append($("<a>")
 90				.attr("href", link)
 91				.attr("target", "_blank")
 92				.text(link));
 93		}
 94	};
 95	
 96	var user = {
 97		name: "user",
 98		match: function (remainder) {
 99			return remainder.match(/^@[\w-äöüß]+/i);
100		},
101		process: function(remainder, user) {
102			var u = user.replace("@", "")
103			return remainder.append($("<a>")
104				.attr("href", "http://twitter.com/" + u)
105				.attr("target", "_blank")
106				.text(user));
107		}
108	};
109	
110	var tag = {
111		name: "tag",
112		match: function (remainder) {
113			return remainder.match(/^#[\w-äöüß]+/i);
114		},
115		process: function(remainder, tag) {
116			var t = tag.replace("#", "%23")
117			return remainder
118				.append($("<a>")
119				.attr("href", "http://search.twitter.com/search?q=" + t)
120				.attr("target", "_blank")
121				.text(tag));
122		}
123	};
124	
125	var whitespace = {
126		name: "whitespace",
127		match: function (remainder) {
128			return remainder.match(/^\s+/i);
129		},
130		process: function(remainder, text) {
131			return remainder
132				.append(text);
133		}
134	};
135	
136	var text = {
137		name: "text",
138		match: function (text) {
139			return text.match(/^\s*\S+\s*/i);
140		},
141		process: function(remainder, text) {
142			return remainder
143				.append(text);
144		}
145	};
146	
147	$.fn.tweet = function(tweet) {
148		var remainder = tweet.text;
149		var result = $("<span>");
150		var parsers = [whitespace, mobypicture, yfrog, twitpic, link, tag, user, text];
151		
152		// console.log("remainder: <" + remainder + ">");
153		while(remainder)
154		{
155			for(var k = 0; k < parsers.length; k++)
156			{
157				var parser = parsers[k];
158				var match = parser.match(remainder, tweet);
159				if (match)
160				{
161					match = match[0];
162					
163					// console.log(parser.name + ": <" + match + "> ");
164					parser.process(result, match, tweet);
165					
166					remainder = remainder.substring(match.length);
167					// console.log("remainder: <" + remainder + ">");
168					break;
169				}
170			}
171		};
172	
173	if (picTwitter.match(remainder, tweet))
174	{
175		picTwitter.process(result, "", tweet);
176	}
177	
178		// console.log("result: <" + result.html() + ">");	
179		return $(this).html(result);
180	};
181	
182	$.getJSON('http://netopenspace.de/all-net-open-spaces.json?json=?',
183		function(json)
184		{
185			var developmentFilter = function(item)
186			{
187				return !(!(/^http:\/\/.*nos\.local/i.test(document.URL)) && item.Private);
188			}
189			
190			json = $.grep(json, developmentFilter);
191						
192			var query = $(json).map(function() {
193				return this.TwitterSearch;
194			});
195			
196			query = $.makeArray(query).join(' OR ');
197			
198			$('.loading').fadeOut();
199			
200			var prefixWithZero = function(value)
201			{
202				if (value < 10)
203				{
204					return '0' + value;
205				}
206				return value;
207			}
208			
209			var opacity = function(i, limit, opaqueCount, minOpacity, maxOpacity)
210			{
211				if (i <= opaqueCount)
212				{
213					return maxOpacity;
214				}
215				
216				i = i - opaqueCount;
217				limit = limit - opaqueCount;
218				
219				return (-1 * (maxOpacity - minOpacity) / limit)  * i + maxOpacity;
220			};
221			
222			$('#tweets')
223				.monitter({
224					query: query,
225					limit: 12,
226					timeout: 10000,
227					buildTweet: function() {
228						var element = 
229							$('<div>')
230							.append($('<div>')
231								.addClass('bubble')
232								.append($('<p>').tweet(this)))
233							.append($('<div>')
234								.addClass('author')
235								.append($('<a>')
236									.attr('href', 'http://twitter.com/' + this.from_user + '/status/' + this.id_str)
237									.attr('target', '_blank')
238									.attr('title', 'Tweet anzeigen')
239									.append($('<img>')
240										.addClass('avatar')
241										.attr('src', this.profile_image_url))
242									.append($('<span>')
243										.text(this.from_user))));
244						return element;
245					},
246					showTweet: function(index, limit) {
247						this.fadeTo('normal', opacity(index, limit, 2, .4, 1));
248					},					
249					updated: function(params, status) {
250						var nextUpdate = new Date();
251						nextUpdate = new Date(params.timeout + nextUpdate.getTime());
252						
253						var hours = prefixWithZero(nextUpdate.getHours());
254						var minutes = prefixWithZero(nextUpdate.getMinutes());
255						var seconds = prefixWithZero(nextUpdate.getSeconds());
256						$('#next-update')
257							.attr('class', status)
258							.text("Nächste Aktualisierung: " + hours + ":" + minutes + ":" + seconds);
259					}
260				});
261		});
262	
263	var currentPreview = 0;
264	$(document).everyTime("10s", function() {
265		var nextPreview = $(".bubble img")
266			.filter(function() {
267				return $(this).closest(".tweet").data("id") < currentPreview;
268			}).first();
269		
270		if (!nextPreview.length)
271		{
272			nextPreview = $(".bubble img:first");
273		}
274		
275		currentPreview = nextPreview.first().closest(".tweet").data("id");
276		nextPreview.zoomer({ parent: $("#wrapper") }); 
277	});
278});