From 8e753cc07329127a5bc5114609e89e891457b403 Mon Sep 17 00:00:00 2001
From: archi <archi@loki.localnet>
Date: Sun, 17 Oct 2010 12:56:26 +0200
Subject: [PATCH] Youtube proof of concept patch/preview - not done yet (see
 bug #332)

---
 app/helpers/status_messages_helper.rb | 7 +++++--
 public/javascripts/view.js            | 9 +++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb
index 4131ccaf08..f44ffe42e5 100644
--- a/app/helpers/status_messages_helper.rb
+++ b/app/helpers/status_messages_helper.rb
@@ -16,8 +16,11 @@ module StatusMessagesHelper
     
     # next line is important due to XSS! (h is rail's make_html_safe-function)
     message = h(message).html_safe
-    message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/, '\1http://\2');
-    return message.gsub(/(http|ftp):\/\/([^ ]+)/, '<a target="_blank" href="\1://\2">\2</a>');
+    message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/, '\1http://\2')
+    message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch.*v=([A-Za-z0-9_]+)[^ ]*/, '\1youtube::\2')
+    message.gsub!(/(http|ftp):\/\/([^ ]+)/, '<a target="_blank" href="\1://\2">\2</a>')
+    message.gsub!(/youtube::([A-Za-z0-9_]+)/, '<a name="\1" onclick="openYoutube(\'\1\', this)" href="#\1">Youtube: \1</a>')
+    return message
   end
 
 end
diff --git a/public/javascripts/view.js b/public/javascripts/view.js
index dec170a07b..3625432436 100644
--- a/public/javascripts/view.js
+++ b/public/javascripts/view.js
@@ -72,3 +72,12 @@ $.fn.clearForm = function() {
   });
 };
 
+function openYoutube(videoid, link) {
+  var container = document.createElement('div');
+  container.innerHTML = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/'+videoid+'?fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+videoid+'?fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object><br><a href="#'+videoid+'" onclick="closeYoutube(this)">Close</a> <a href="http://www.youtube.com/watch?v='+videoid+'" target="_blank">Watch on Youtube</a>';
+  link.parentNode.insertBefore(container, this.nextSibling);
+}
+
+function closeYoutube(link) {
+  link.parentNode.parentNode.removeChild(link.parentNode);
+}
-- 
GitLab