diff --git a/config/assets.yml b/config/assets.yml index acbdc1443..bc04d24e7 100644 --- a/config/assets.yml +++ b/config/assets.yml @@ -17,10 +17,10 @@ javascripts: - public/javascripts/vendor/Mustache.js - public/javascripts/view.js - public/javascripts/stream.js - - public/javascripts/embedder.js - public/javascripts/application.js - public/javascripts/diaspora.js - public/javascripts/widgets/alert.js + - public/javascripts/widgets/embedder.js mobile: - public/javascripts/vendor/jquery144.min.js diff --git a/public/javascripts/embedder.js b/public/javascripts/embedder.js deleted file mode 100644 index 441ea672b..000000000 --- a/public/javascripts/embedder.js +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright (c) 2010, Diaspora Inc. This file is - * licensed under the Affero General Public License version 3 or later. See - * the COPYRIGHT file. - */ -var Embedder = { - services: {}, - register: function(service, template) { - Embedder.services[service] = template; - }, - render: function(service, views) { - var template = (typeof Embedder.services[service] === "string") - ? Embedder.services[service] - : Embedder.services.undefined; - - return $.mustache(template, views); - }, - embed: function($this) { - var service = $this.data("host"), - container = document.createElement("div"), - $container = $(container).attr("class", "video-container"), - $videoContainer = $this.parent().siblings("div.video-container"); - - if($videoContainer.length) { - $videoContainer.slideUp("fast", function() { $(this).detach(); }); - return; - } - - if ($("div.video-container").length) { - $("div.video-container").slideUp("fast", function() { $(this).detach(); }); - } - - $container.html(Embedder.render(service, $this.data())); - - $container.hide() - .insertAfter($this.parent()) - .slideDown('fast'); - - $this.click(function() { - $container.slideUp('fast', function() { - $(this).detach(); - }); - }); - }, - initialize: function() { - $(".stream").delegate("a.video-link", "click", Embedder.onVideoLinkClick); - }, - onVideoLinkClick: function(evt) { - evt.preventDefault(); - Embedder.embed($(this)); - } -}; - -Embedder.register("youtube.com", - 'Watch this video on Youtube
' + - ''); - -Embedder.register("vimeo.com", - 'Watch this video on Vimeo
' + - ''); - -Embedder.register("undefined", '

Unknown video type - {{host}}

'); - -$(document).ready(function() { - Embedder.initialize(); -}); \ No newline at end of file diff --git a/public/javascripts/widgets/embedder.js b/public/javascripts/widgets/embedder.js new file mode 100644 index 000000000..44dc1d1b7 --- /dev/null +++ b/public/javascripts/widgets/embedder.js @@ -0,0 +1,76 @@ +/* Copyright (c) 2010, Diaspora Inc. This file is + * licensed under the Affero General Public License version 3 or later. See + * the COPYRIGHT file. + */ + + +(function() { + var Embedder = function() { }; + Embedder.prototype.services = {}; + Embedder.prototype.register = function(service, template) { + this.services[service] = template; + }; + + Embedder.prototype.render = function(service, views) { + var template = (typeof this.services[service] === "string") + ? this.services[service] + : this.services.undefined; + + return $.mustache(template, views); + }; + + + Embedder.prototype.embed = function($this) { + var service = $this.data("host"), + container = document.createElement("div"), + $container = $(container).attr("class", "video-container"), + $videoContainer = $this.siblings(".video-container"); + + if($videoContainer.length) { + $videoContainer.slideUp("fast", function() { $(this).detach(); }); + return; + } + + if ($("div.video-container").length) { + $("div.video-container").slideUp("fast", function() { $(this).detach(); }); + } + + $container.html( + this.render(service, $this.data()) + ); + + $container.hide() + .insertBefore($this.siblings(".info")) + .slideDown('fast'); + + $this.click(function() { + $container.slideUp('fast', function() { + $(this).detach(); + }); + }); + }; + + Embedder.prototype.start = function() { + $(".stream").delegate("a.video-link", "click", this.onVideoLinkClicked); + this.registerServices(); + }; + + Embedder.prototype.registerServices = function() { + this.register("youtube.com", + 'Watch this video on Youtube
' + + ''); + + this.register("vimeo.com", + 'Watch this video on Vimeo
' + + ''); + + this.register("undefined", '

Unknown video type - {{host}}

'); + }; + + Embedder.prototype.onVideoLinkClicked = function(evt) { + evt.preventDefault(); + Diaspora.widgets.embedder.embed($(this)); + }; + + Diaspora.widgets.add("embedder", Embedder); +})(); \ No newline at end of file diff --git a/spec/javascripts/embedder-spec.js b/spec/javascripts/embedder-spec.js index 203f7bec9..a1f92eb34 100644 --- a/spec/javascripts/embedder-spec.js +++ b/spec/javascripts/embedder-spec.js @@ -2,45 +2,49 @@ * licensed under the Affero General Public License version 3 or later. See * the COPYRIGHT file. */ -describe("Embedder", function() { - describe("services", function() { - it("is an object containing all the supported services", function() { - expect(typeof Embedder.services === "object").toBeTruthy(); - }); - }); - describe("register", function() { - it("adds a service and it's template to Embedder.services", function() { - expect(typeof Embedder.services["ohaibbq"] === "undefined").toBeTruthy(); - Embedder.register("ohaibbq", "sup guys"); - expect(typeof Embedder.services["ohaibbq"] === "undefined").toBeFalsy(); - }); - }); - describe("render", function() { - it("renders the specified mustache template", function() { - var template = Embedder.render("youtube.com", {"video-id": "asdf"}); - expect(template.length > 0).toBeTruthy(); - expect(typeof template === "string").toBeTruthy(); - }); - it("renders the 'undefined' template if the service is not found", function() { - var template = Embedder.render("yoimmafakeservice", {host: "yo"}); - expect(template).toEqual(Embedder.render("undefined", {host: "yo"})); - }); - }); - describe("embed", function() { - beforeEach(function() { - $("#jasmine_content").html( - '
' + - '' + - 'spec video' + - '' + - '
' - ); - }); +describe("Diaspora", function() { + describe("widgets", function() { + describe("embedder", function() { + describe("services", function() { + it("is an object containing all the supported services", function() { + expect(typeof Diaspora.widgets.embedder.services === "object").toBeTruthy(); + }); + }); + describe("register", function() { + it("adds a service and it's template to Diaspora.widgets.embedder.services", function() { + expect(typeof Diaspora.widgets.embedder.services["ohaibbq"] === "undefined").toBeTruthy(); + Diaspora.widgets.embedder.register("ohaibbq", "sup guys"); + expect(typeof Diaspora.widgets.embedder.services["ohaibbq"] === "undefined").toBeFalsy(); + }); + }); + describe("render", function() { + it("renders the specified mustache template", function() { + var template = Diaspora.widgets.embedder.render("youtube.com", {"video-id": "asdf"}); + expect(template.length > 0).toBeTruthy(); + expect(typeof template === "string").toBeTruthy(); + }); + it("renders the 'undefined' template if the service is not found", function() { + var template = Diaspora.widgets.embedder.render("yoimmafakeservice", {host: "yo"}); + expect(template).toEqual(Diaspora.widgets.embedder.render("undefined", {host: "yo"})); + }); + }); + describe("embed", function() { + beforeEach(function() { + $("#jasmine_content").html( + '
' + + '' + + 'spec video' + + '' + + '
' + ); + }); - it("delegates '.stream a.video-link'", function() { - spyOn($.fn, "delegate"); - Embedder.initialize(); - expect($.fn.delegate).toHaveBeenCalledWith("a.video-link", "click", Embedder.onVideoLinkClick); + it("delegates '.stream a.video-link'", function() { + spyOn($.fn, "delegate"); + Diaspora.widgets.embedder.start(); + expect($.fn.delegate).toHaveBeenCalledWith("a.video-link", "click", Diaspora.widgets.embedder.onVideoLinkClicked); + }); + }); }); }); }); \ No newline at end of file diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index d4a518e1d..eb5b3b689 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -21,11 +21,11 @@ src_files: - public/javascripts/vendor/facebox.js - public/javascripts/diaspora.js - public/javascripts/widgets/alert.js + - public/javascripts/widgets/embedder.js - public/javascripts/mobile.js - public/javascripts/aspect-edit.js - public/javascripts/aspect-contacts.js - public/javascripts/web-socket-receiver.js - - public/javascripts/embedder.js - public/javascripts/view.js - public/javascripts/stream.js - public/javascripts/validation.js