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