Merge branch 'winston' of https://github.com/M2Ys4U/KiwiIRC into winston
[KiwiIRC.git] / client / src / views / mediamessage.js
index 369c1ddaa500b74e6ee13827e71ea8ecebc0537a..f7f09d4c1198f64711c6d251bd031adc021fb59d 100644 (file)
@@ -132,14 +132,65 @@ _kiwi.view.MediaMessage = Backbone.View.extend({
             });
 
             return $('<div>' + _kiwi.global.i18n.translate('client_views_mediamessage_load_gist').fetch() + '...</div>');
-        }
+        },
+
+        spotify: function () {
+            var uri = this.$el.data('uri');
+            var method = this.$el.data('method');
+            var that = this;
+
+            switch (method) {
+                case "track":
+                case "album":
+                     var spot = {
+                         url: 'https://embed.spotify.com/?uri=' + uri,
+                         width: 300,
+                         height: 80 
+                     };
+                     break;
+                case "artist":
+                     var spot = {
+                         url: 'https://embed.spotify.com/follow/1/?uri=' + uri +'&size=detail&theme=dark',
+                         width: 300,
+                         height: 56
+                     };
+                     break;
+            };
+
+            var html = '<iframe src="' + spot.url + '" width="' + spot.width + '" height="' + spot.height + '" frameborder="0" allowtransparency="true"></iframe>';
+
+            return $(html);
+        },
+
+
     }
     }, {
 
+    /**
+     * Add a media message type to append HTML after a matching URL
+     * match() should return true if it wants to handle this URL
+     * buildHtml() should return the HTML string to append after the URL in the message
+     */
+    addType: function(match, buildHtml) {
+        if (typeof match !== 'function' || typeof buildHtml !== 'function')
+            return;
+
+        this.types = this.types || [];
+        this.types.push({match: match, buildHtml: buildHtml});
+    },
+
+
     // Build the closed media HTML from a URL
     buildHtml: function (url) {
         var html = '', matches;
 
+        _.each(this.types || [], function(type) {
+            if (!type.match(url))
+                return;
+
+            html += type.buildHtml(url);
+        });
+
         // Is it an image?
         if (url.match(/(\.jpe?g|\.gif|\.bmp|\.png)\??$/i)) {
             html += '<span class="media image" data-type="image" data-url="' + url + '" title="Open Image"><a class="open"><i class="icon-chevron-right"></i></a></span>';
@@ -175,6 +226,15 @@ _kiwi.view.MediaMessage = Backbone.View.extend({
             html += '<span class="media gist" data-type="gist" data-url="' + url + '" data-gist_id="' + matches[1] + '" title="GitHub Gist"><a class="open"><i class="icon-chevron-right"></i></a></span>';
         }
 
+        // Is this a spotify link?
+        matches = (/http:\/\/(?:play|open\.)?spotify.com\/(album|track|artist)\/([a-zA-Z0-9]+)\/?/i).exec(url);
+        if (matches) {
+            // Make it a Spotify URI! (spotify:<type>:<id>)
+            var method = matches[1],
+                uri = "spotify:" + matches[1] + ":" + matches[2];
+            html += '<span class="media spotify" data-type="spotify" data-uri="' + uri + '" data-method="' + method + '" title="Spotify ' + method + '"><a class="open"><i class="icon-chevron-right"></i></a></span>';
+        }
+
         return html;
     }
 });