Add spotify to medamessage.js
[KiwiIRC.git] / client / src / views / mediamessage.js
index bd5b5726d7e406143c05c12caf6e46c34ddc89ba..93907b1fa0336d39861dcd124d093a8c74245ee9 100644 (file)
@@ -132,35 +132,44 @@ _kiwi.view.MediaMessage = Backbone.View.extend({
             });
 
             return $('<div>' + _kiwi.global.i18n.translate('client_views_mediamessage_load_gist').fetch() + '...</div>');
-        }
-    }
-    }, {
+        },
 
-    /**
-     * 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});
-    },
+        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);
+        },
 
 
+    }
+    }, {
+
     // 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>';
@@ -196,6 +205,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;
     }
 });