diff --git a/public/javascripts/diaspora.js b/public/javascripts/diaspora.js index c1e9348ec..822fe386c 100644 --- a/public/javascripts/diaspora.js +++ b/public/javascripts/diaspora.js @@ -10,57 +10,61 @@ var Diaspora = { }; - Diaspora.WidgetCollection = function() { - this.initialized = false; - this.collection = { }; - this.eventsContainer = $({}); - }; + Diaspora.EventBroker = { + extend: function(obj) { + obj.eventsContainer = $({}); - Diaspora.WidgetCollection.prototype.add = function(widgetId, Widget) { - $.extend(Widget.prototype, Diaspora.BaseWidget); + obj.subscribe = Diaspora.EventBroker.subscribe; + obj.publish = Diaspora.EventBroker.publish; - this[widgetId] = this.collection[widgetId] = new Widget(); - if(this.initialized) { - this.collection[widgetId].start(); + obj.publish = $.proxy(function(eventId, args) { + this.eventsContainer.trigger(eventId, args); + }, obj); + + obj.subscribe = $.proxy(function(eventIds, callback, context) { + var eventIds = eventIds.split(" "); + + for(var eventId in eventIds) { + this.eventsContainer.bind(eventIds[eventId], $.proxy(callback, context)); + } + }, obj); + + return obj; } }; - Diaspora.WidgetCollection.prototype.remove = function(widgetId) { - delete this.collection[widgetId]; - }; + Diaspora.widgets = { + initialize: false, + collection: {}, + constructors: {}, - Diaspora.WidgetCollection.prototype.init = function() { - this.initialized = true; + initialize: function() { + this.initialized = true; + Diaspora.EventBroker.extend(this); - for(var widgetId in this.collection) { - if(typeof this.collection[widgetId].start !== "undefined") { - this.collection[widgetId].start(); + for(var widgetId in this.collection) { + this.collection[widgetId].publish("widget/ready"); } - }; - - }; + }, - Diaspora.WidgetCollection.prototype.subscribe = function(id, callback, context) { - var ids = id.split(" "); - for(var id in ids) { - this.eventsContainer.bind(ids[id], $.proxy(callback, context)); + add: function(widgetId, Widget) { + $.extend(Widget.prototype, Diaspora.EventBroker.extend({})); + + this[widgetId] = this.collection[widgetId] = new Widget(); + if(this.initialized) { + this.collection[widgetId].publish("widget/ready"); + } + }, + + remove: function(widgetId) { + delete this.collection[widgetId]; } }; - Diaspora.WidgetCollection.prototype.publish = function(id, args) { - this.eventsContainer.trigger(id, args); - }; - - Diaspora.BaseWidget = { - require: function(widgetName) { - this[widgetName] = Diaspora.widgets[widgetName]; - } - }; - - Diaspora.widgets = new Diaspora.WidgetCollection(); - window.Diaspora = Diaspora; })(); -$(document).ready(function() { Diaspora.widgets.init(); }); +$(function() { + Diaspora.widgets.initialize(); +}); diff --git a/public/javascripts/widgets/alert.js b/public/javascripts/widgets/alert.js index 9dcf8ed4c..30a7e1715 100644 --- a/public/javascripts/widgets/alert.js +++ b/public/javascripts/widgets/alert.js @@ -1,34 +1,37 @@ -Diaspora.widgets.add("alert", function() { - this.start = function() { - $(document).bind("close.facebox", function() { - if ($("#diaspora_alert").length) { - $("#diaspora_alert").detach(); - } - }); +(function() { + var Alert = function() { + var self = this; + + this.faceboxTemplate = '
' + + '
' + + '
' + + '

' + + '{{title}}' + + '

' + + '
' + + '{{content}}' + + '
' + + '
'; + + + this.subscribe("widget/ready", function() { + $(document).bind("close.facebox", function() { + $("#facebox, #diaspora_alert").remove(); + }); + }); + + + this.alert = function(title, content) { + $($.mustache(self.faceboxTemplate, { + title: title, + content: content + })).appendTo(document.body); + + $.facebox({ + div: "#diaspora_alert" + }, "diaspora_alert"); + } }; - this.faceboxTemplate = '
' + - '
' + - '
' + - '

' + - '{{title}}' + - '

' + - '
' + - '{{content}}' + - '
' + - '
'; - - - this.alert = function(title, content) { - var template = $.mustache(this.faceboxTemplate, { - title: title, - content: content - }); - - $(template).appendTo(document.body); - - $.facebox({ - div: "#diaspora_alert" - }, 'diaspora_alert'); - } -}); + Diaspora.widgets.add("alert", Alert); +})(); diff --git a/public/javascripts/widgets/directionDetector.js b/public/javascripts/widgets/directionDetector.js index 257b5cee7..d5e5b9610 100644 --- a/public/javascripts/widgets/directionDetector.js +++ b/public/javascripts/widgets/directionDetector.js @@ -3,66 +3,84 @@ * the COPYRIGHT file. */ /* Modified version of https://gitorious.org/statusnet/mainline/blobs/master/plugins/DirectionDetector/jquery.DirectionDetector.js */ +(function() { + var DirectionDetector = function() { + var self = this; + this.binds = []; + this.cleaner = new RegExp("@[^ ]+|^RT[: ]{1}| RT | RT: |[♺♻:]+", "g"); -Diaspora.widgets.add("directionDetector", function() { - - this.start = function() { - Diaspora.widgets.directionDetector.updateBinds(); + this.subscribe("widget/ready", function() { + self.updateBinds(); - Diaspora.widgets.subscribe("stream/scrolled", function() { - Diaspora.widgets.directionDetector.updateBinds(); + Diaspora.widgets.subscribe("stream/scrolled", function() { + self.updateBinds(); + }); }); - }; - this.isRTL = function(str) { - if(typeof str != typeof "" || str.length<1) + this.isRTL = function(str) { + if(typeof str !== "string" || str.length < 1) { + return false; + } + + var charCode = str.charCodeAt(0); + if(charCode >= 1536 && charCode <= 1791) // Sarabic, Persian, ... + return true; + + else if(charCode >= 65136 && charCode <= 65279) // Arabic present 1 + return true; + + else if(charCode >= 64336 && charCode <= 65023) // Arabic present 2 + return true; + + else if(charCode>=1424 && charCode<=1535) // Hebrew + return true; + + else if(charCode>=64256 && charCode<=64335) // Hebrew present + return true; + + else if(charCode>=1792 && charCode<=1871) // Syriac + return true; + + else if(charCode>=1920 && charCode<=1983) // Thaana + return true; + + else if(charCode>=1984 && charCode<=2047) // NKo + return true; + + else if(charCode>=11568 && charCode<=11647) // Tifinagh + return true; + return false; - var cc = str.charCodeAt(0); - if(cc>=1536 && cc<=1791) // arabic, persian, ... - return true; - if(cc>=65136 && cc<=65279) // arabic peresent 2 - return true; - if(cc>=64336 && cc<=65023) // arabic peresent 1 - return true; - if(cc>=1424 && cc<=1535) // hebrew - return true; - if(cc>=64256 && cc<=64335) // hebrew peresent - return true; - if(cc>=1792 && cc<=1871) // Syriac - return true; - if(cc>=1920 && cc<=1983) // Thaana - return true; - if(cc>=1984 && cc<=2047) // NKo - return true; - if(cc>=11568 && cc<=11647) // Tifinagh - return true; - return false; }; - this.cleaner = new RegExp('@[^ ]+|^RT[: ]{1}| RT | RT: |[♺♻:]+', 'g'); + this.updateBinds = function() { + $.each(self.binds, function(index, bind) { + bind.unbind("keyup", self.updateDirection); + }); - this.binds = []; + self.binds = []; - this.updateBinds = function() { - $.each(Diaspora.widgets.directionDetector.binds, function(i, v) {v.unbind('keyup', Diaspora.widgets.directionDetector.updateDirection);}); - Diaspora.widgets.directionDetector.binds = []; + $("textarea, input[type='text'], input[type='search']").each(self.bind); + }; - $("textarea").each(Diaspora.widgets.directionDetector.bind); - $("input[type='text']").each(Diaspora.widgets.directionDetector.bind); - $("input[type='search']").each(Diaspora.widgets.directionDetector.bind); + this.bind = function() { + self.binds.push( + $(this).bind("keyup", self.updateDirection) + ); + }; + + this.updateDirection = function() { + var textArea = $(this), + cleaned = textArea.val().replace(self.cleaner, "").replace(/^[ ]+/, ""); + + if(self.isRTL(cleaned)) { + textArea.css("direction", "rtl"); + } + else { + textArea.css("direction", "ltr"); + } + }; }; - this.bind = function() { - $(this).bind('keyup', Diaspora.widgets.directionDetector.updateDirection); - Diaspora.widgets.directionDetector.binds.push($(this)); - }; - - this.updateDirection = function() { - tArea = $(this); - var cleaned = tArea.val().replace(Diaspora.widgets.directionDetector.cleaner, '').replace(/^[ ]+/, ''); - if(Diaspora.widgets.directionDetector.isRTL(cleaned)) - tArea.css('direction', 'rtl'); - else - tArea.css('direction', 'ltr'); - }; -}); + Diaspora.widgets.add("directionDetector", DirectionDetector); +})(); diff --git a/public/javascripts/widgets/embedder.js b/public/javascripts/widgets/embedder.js index 1f2e4da50..97eebb9a4 100644 --- a/public/javascripts/widgets/embedder.js +++ b/public/javascripts/widgets/embedder.js @@ -5,81 +5,93 @@ (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); - }; + var Embedder = function() { + var self = this; + this.services = {}; - Embedder.prototype.embed = function($this) { - var service = $this.data("host"), - container = document.createElement("div"), - $container = $(container).attr("class", "video-container"), - $videoContainer = $this.closest('.content').children(".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() - .insertAfter($this.parent()) - .slideDown('fast'); - - - $this.click(function() { - $container.slideUp('fast', function() { - $(this).detach(); + self.subscribe("widget/ready", function() { + $.extend(self, { + stream: $("#main_stream") }); + + self.ensureDOMStructure(); + + self.stream.delegate("a.video-link", "click", self.onVideoLinkClicked); + self.registerServices(); }); - }; + + this.ensureDOMStructure = function() { + var post = self.stream.children(".stream_element:first"), + content = post.children(".sm_body").children(".content").children("p"); - Embedder.prototype.start = function() { - $("#main_stream a.video-link").live("click", this.onVideoLinkClicked); - this.registerServices(); + self.canEmbed = !!content.length; + }; + - var $post = $("#main_stream").children(".stream_element:first"), - $contentParagraph = $post.children(".sm_body").children('.content').children("p"); + this.register = function(service, template) { + self.services[service] = template; + }; - this.canEmbed = $contentParagraph.length; - }; + this.render = function(service, views) { + var template = (typeof self.services[service] === "string") + ? self.services[service] + : self.services.undefined; + + return $.mustache(template, views); + }; - Embedder.prototype.registerServices = function() { - var watchVideoOn = Diaspora.widgets.i18n.t("videos.watch"); + this.embed = function(videoLink) { + var host = videoLink.data("host"), + container = $("
", { "class": "video-container" }), + videoContainer = videoLink.closest(".content").children(".video-container"); - this.register("youtube.com", + 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( + self.render(service, videoLink.data()) + ); + + container.hide() + .insertAfter(videoLink.parent()) + .slideDown("fast"); + + videoLink.click(function() { + videoContainer.slideUp("fast", function() { + $(this).detach(); + }); + }); + }; + + this.onVideoLinkClicked = function(evt) { + if(self.canEmbed) { + evt.preventDefault(); + self.embed($(this)); + } + }; + + this.registerServices = function() { + var watchVideoOn = Diaspora.widgets.i18n.t("videos.watch"); + + self.register("youtube.com", '' + $.mustache(watchVideoOn, { provider: "YouTube" }) + '
' + ''); - this.register("vimeo.com", - '' + $.mustache(watchVideoOn, { provider: "Vimeo" }) + '
' + - ''); + self.register("vimeo.com", + '' + $.mustache(watchVideoOn, { provider: "Vimeo" }) + '
' + + ''); - this.register("undefined", '

' + Diaspora.widgets.i18n.t("videos.unknown") + ' - {{host}}

'); - }; - - Embedder.prototype.onVideoLinkClicked = function(evt) { - if(Diaspora.widgets.embedder.canEmbed) { - evt.preventDefault(); - Diaspora.widgets.embedder.embed($(this)); - } + self.register("undefined", '

' + Diaspora.widgets.i18n.t("videos.unknown") + ' - {{host}}

'); + }; }; Diaspora.widgets.add("embedder", Embedder); diff --git a/public/javascripts/widgets/flashes.js b/public/javascripts/widgets/flashes.js index 00232cd6a..ad052f6b8 100644 --- a/public/javascripts/widgets/flashes.js +++ b/public/javascripts/widgets/flashes.js @@ -1,27 +1,30 @@ (function() { var Flashes = function() { - this.start = function() { - this.animateMessages(); - }; + var self = this; + + this.subscribe("widget/ready", function() { + self.animateMessages(); + }); this.animateMessages = function() { - var $this = $("#flash_notice, #flash_error, #flash_alert"); - $this.animate({ + var flashMessages = $("#flash_notice, #flash_error, #flash_alert"); + flashMessages.animate({ top: 0 }).delay(2000).animate({ top: -100 - }, $this.remove); + }, flashMessages.remove); }; this.render = function(result) { - $("
") - .attr("id", (result.success) ? "flash_notice" : "flash_error") - .prependTo(document.body) - .html(result.notice); + $("
", { + id: (result.success) ? "flash_notice" : "flash_error" + }) + .prependTo(document.body) + .html(result.notice); - this.animateMessages(); + self.animateMessages(); }; }; Diaspora.widgets.add("flashes", Flashes); -})(); \ No newline at end of file +})(); diff --git a/public/javascripts/widgets/hovercard.js b/public/javascripts/widgets/hovercard.js index f75b0f753..21664aa99 100644 --- a/public/javascripts/widgets/hovercard.js +++ b/public/javascripts/widgets/hovercard.js @@ -4,9 +4,9 @@ self.jXHRs = []; - this.start = function() { - self.personCache = new this.Cache(); - self.dropdownCache = new this.Cache(); + self.subscribe("widget/ready", function() { + self.personCache = new self.Cache(); + self.dropdownCache = new self.Cache(); var card = $("#hovercard"); self.hoverCard = { @@ -28,7 +28,7 @@ Diaspora.widgets.subscribe("aspectDropdown/updated aspectDropdown/blurred", function(evt, personId, dropdownHtml) { self.dropdownCache.cache["/people/" + personId + "/aspect_membership_button"] = $(dropdownHtml).removeClass("active").get(0).outerHTML; }); - }; + }); this.handleHoverEvent = function(evt) { self.target = $(evt.target); diff --git a/public/javascripts/widgets/i18n.js b/public/javascripts/widgets/i18n.js index b3a259db5..6335d0007 100644 --- a/public/javascripts/widgets/i18n.js +++ b/public/javascripts/widgets/i18n.js @@ -2,31 +2,34 @@ * licensed under the Affero General Public License version 3 or later. See * the COPYRIGHT file. */ - -Diaspora.widgets.add("i18n", function() { - this.language = "en"; - this.locale = { }; - - this.loadLocale = function(locale, language) { - this.language = language; - this.locale = locale; - }; - - this.t = function(item, views) { - var ret, - _item = item.split("."); +(function() { + var I18n = function() { + var self = this; + this.locale = { }; + this.language = "en"; - while(part = _item.shift()) { - ret = (ret) ? ret[part] : this.locale[part]; - if(typeof ret === "undefined") { - return ""; + this.loadLocale = function(locale, language) { + this.locale = locale; + this.language = language; + }; + + this.t = function(item, views) { + var translatedMessage, + items = item.split("."); + + while(nextNamespace = items.shift()) { + translatedMessage = (translatedMessage) + ? translatedMessage[nextNamespace] + : self.locale[nextNamespace]; + + if(typeof translatedMessage === "undefined") { + return ""; + } } - } - - if(typeof views === "object") { - return $.mustache(ret, views || {}); - } - - return ret; + + return $.mustache(translatedMessage, views || { }); + }; }; -}); + + Diaspora.widgets.add("i18n", I18n); +})(); diff --git a/public/javascripts/widgets/infinite-scroll.js b/public/javascripts/widgets/infinite-scroll.js index d7c461048..82bedbfff 100644 --- a/public/javascripts/widgets/infinite-scroll.js +++ b/public/javascripts/widgets/infinite-scroll.js @@ -4,9 +4,9 @@ */ (function() { - var InfiniteScroll = function() { }; - InfiniteScroll.prototype.options = - { + var InfiniteScroll = function() { + var self = this; + this.options = { navSelector : "#pagination", nextSelector : ".paginate", itemSelector : ".stream_element", @@ -22,38 +22,34 @@ loadingImg: '/images/ajax-loader.gif' }; - InfiniteScroll.prototype.reInitialize = function(){ - this.clear(); - this.initialize(); - }; + this.subscribe("widget/ready", function() { + Diaspora.widgets.subscribe("stream/reloaded", self.reInitialize, this); + self.initialize(); + }); - InfiniteScroll.prototype.initialize = function(){ - if($('#main_stream').length !== 0){ - $('#main_stream').infinitescroll(this.options, function() { - Diaspora.widgets.publish("stream/scrolled"); - }); + this.reInitialize = function() { + self.clear(); + self.initialize(); + }; - } else if($('#people_stream.contacts').length !== 0){ - $("#people_stream.contacts").infinitescroll({ - navSelector : ".pagination", - nextSelector : ".next_page", - itemSelector : ".stream_element", - loadingText: "", - loadingImg: '/images/ajax-loader.gif', - bufferPx: 400 - }, function(){ - Diaspora.widgets.publish("stream/scrolled"); - }); - } - }; + this.initialize = function() { + if($('#main_stream').length !== 0){ + $('#main_stream').infinitescroll(this.options, function() { + Diaspora.widgets.publish("stream/scrolled"); + }); + } else if($('#people_stream.contacts').length !== 0){ + $("#people_stream.contacts").infinitescroll($.extend(self.options, { + navSelector : ".pagination", + nextSelector : ".next_page", + }), function() { + Diaspora.widgets.publish("stream/scrolled"); + }); + } + }; - InfiniteScroll.prototype.start = function() { - Diaspora.widgets.subscribe("stream/reloaded", this.reInitialize, this); - this.initialize(); - }; - - InfiniteScroll.prototype.clear = function() { - $('#main_stream').infinitescroll('destroy'); + this.clear = function() { + $("#main_stream").infinitescroll("destroy"); + }; }; Diaspora.widgets.add("infinitescroll", InfiniteScroll); diff --git a/public/javascripts/widgets/lightbox.js b/public/javascripts/widgets/lightbox.js index aa41b4c6f..8b1e7fd6a 100644 --- a/public/javascripts/widgets/lightbox.js +++ b/public/javascripts/widgets/lightbox.js @@ -20,7 +20,7 @@ jQuery.fn.center = (function() { var Lightbox = function() { var self = this; - this.start = function() { + this.subscribe("widget/ready", function() { $.extend(self, { lightbox: $("#lightbox"), imageset: $("#lightbox-imageset"), @@ -62,7 +62,7 @@ jQuery.fn.center = (function() { break; } }); - }; + }); this.nextImage = function(thumb){ var next = thumb.next(); diff --git a/public/javascripts/widgets/notifications-badge.js b/public/javascripts/widgets/notifications-badge.js index e8dfd95d0..54a69a31a 100644 --- a/public/javascripts/widgets/notifications-badge.js +++ b/public/javascripts/widgets/notifications-badge.js @@ -2,16 +2,16 @@ var NotificationDropdown = function() { var self = this; - this.start = function() { - this.badge = $("#notification_badge"); - this.badgeLink = this.badge.find("a"); - this.documentBody = $(document.body); - this.dropdown = $("#notification_dropdown"); - this.dropdownNotifications = this.dropdown.find(".notifications"); - this.ajaxLoader = this.dropdown.find(".ajax_loader"); + this.subscribe("widget/ready",function() { + self.badge = $("#notification_badge"); + self.badgeLink = self.badge.find("a"); + self.documentBody = $(document.body); + self.dropdown = $("#notification_dropdown"); + self.dropdownNotifications = self.dropdown.find(".notifications"); + self.ajaxLoader = self.dropdown.find(".ajax_loader"); - this.badgeLink.toggle(function(evt) { - evt.preventDefault(); + self.badgeLink.toggle(function(evt) { + evt.preventDefault(); evt.stopPropagation(); self.ajaxLoader.show(); @@ -29,16 +29,16 @@ self.dropdown.css("display", "none"); }); - this.dropdown.click(function(evt) { + self.dropdown.click(function(evt) { evt.stopPropagation(); }); - this.documentBody.click(function(evt) { + self.documentBody.click(function(evt) { if(self.dropdownShowing()) { self.badgeLink.click(); } }); - }; + }); this.dropdownShowing = function() { diff --git a/public/javascripts/widgets/notifications.js b/public/javascripts/widgets/notifications.js index 98fef3bb9..3b63dde72 100644 --- a/public/javascripts/widgets/notifications.js +++ b/public/javascripts/widgets/notifications.js @@ -5,13 +5,14 @@ (function() { var Notifications = function() { - this.start = function() { - var self = this; - this.badge = $("#notification_badge .badge_count") - this.index_badge = $(".notification_count"); - this.on_index_page = this.index_badge.length > 0 - this.notificationArea = $("#notifications"); - this.count = parseInt(this.badge.html()) || 0; + var self = this; + + this.subscribe("widget/ready", function() { + self.badge = $("#notification_badge .badge_count") + self.indexBadge = $(".notification_count"); + self.onIndexPage = self.indexBadge.length > 0; + self.notificationArea = $("#notifications"); + self.count = parseInt(self.badge.html()) || 0; $(".stream_element.unread").live("mousedown", function() { self.decrementCount(); @@ -31,47 +32,47 @@ .next(".hidden") .removeClass("hidden"); }); + }); + + this.showNotification = function(notification) { + $(notification.html).prependTo(this.notificationArea) + .fadeIn(200) + .delay(8000) + .fadeOut(200, function() { + $(this).detach(); + }); + + if(typeof notification.incrementCount === "undefined" || notification.incrementCount) { + this.incrementCount(); + } }; - }; - Notifications.prototype.showNotification = function(notification) { - $(notification.html).prependTo(this.notificationArea) - .fadeIn(200) - .delay(8000) - .fadeOut(200, function() { - $(this).detach(); - }); + this.changeNotificationCount = function(change) { + this.count += change; - if(typeof notification.incrementCount === "undefined" || notification.incrementCount) { - this.incrementCount(); - } - }; + if(this.badge.text() !== "") { + this.badge.text(this.count); + if(this.on_index_page) + this.index_badge.text(this.count + " "); - Notifications.prototype.changeNotificationCount = function(change) { - this.count += change; - - if(this.badge.text() !== "") { - this.badge.text(this.count); - if(this.on_index_page) - this.index_badge.text(this.count + " "); - - if(this.count === 0) { - this.badge.addClass("hidden"); - if(this.on_index_page) - this.index_badge.removeClass('unread'); + if(this.count === 0) { + this.badge.addClass("hidden"); + if(this.on_index_page) + this.index_badge.removeClass('unread'); + } + else if(this.count === 1) { + this.badge.removeClass("hidden"); + } } - else if(this.count === 1) { - this.badge.removeClass("hidden"); - } - } - }; + }; - Notifications.prototype.decrementCount = function() { - this.changeNotificationCount(-1); - }; + this.decrementCount = function() { + self.changeNotificationCount(-1); + }; - Notifications.prototype.incrementCount = function() { - this.changeNotificationCount(1); + this.incrementCount = function() { + self.changeNotificationCount(1); + }; }; Diaspora.widgets.add("notifications", Notifications); diff --git a/public/javascripts/widgets/post.js b/public/javascripts/widgets/post.js index 5239aee09..d8ca300b8 100644 --- a/public/javascripts/widgets/post.js +++ b/public/javascripts/widgets/post.js @@ -8,7 +8,7 @@ var self = this; //timeago //set up ikes //comments //audio video links //embedder // - this.start = function() { + this.subscribe("widget/ready", function() { $.extend(self, { likes: { actions: $(".like_it, .dislike_it"), @@ -16,7 +16,7 @@ } }); self.setUpLikes(); - }, + }); this.setUpLikes = function() { self.likes.expanders.live("click", self.expandLikes); diff --git a/public/javascripts/widgets/timeago.js b/public/javascripts/widgets/timeago.js index c46f872ab..b16079b0d 100644 --- a/public/javascripts/widgets/timeago.js +++ b/public/javascripts/widgets/timeago.js @@ -2,29 +2,29 @@ * licensed under the Affero General Public License version 3 or later. See * the COPYRIGHT file. */ +(function() { + var TimeAgo = function() { + var self = this; + this.selector = "abbr.timeago"; + this.subscribe("widget/ready", function() { + Diaspora.widgets.subscribe("stream/scrolled stream/reloaded", self.updateTimeAgo, this); -Diaspora.widgets.add("timeago", function() { - this.selector = "abbr.timeago"; - this.start = function() { - Diaspora.widgets.subscribe("stream/scrolled", this.updateTimeAgo, this); - Diaspora.widgets.subscribe("stream/reloaded", this.updateTimeAgo, this); + self.updateTimeAgo(); - if(this.timeAgoElement().length) { - this.updateTimeAgo(); - } + if(Diaspora.widgets.i18n.language !== "en") { + $.each($.timeago.settings.strings, function(index) { + $.timeago.settings.strings[index] = Diaspora.widgets.i18n.t("timeago." + index); + }); + } + }); - if(Diaspora.widgets.i18n.language !== "en") { - $.each($.timeago.settings.strings, function(index) { - $.timeago.settings.strings[index] = Diaspora.widgets.i18n.t("timeago." + index); - }); - } + this.timeAgoElement = function(selector) { + return $((typeof selector === "string") ? selector : this.selector); + }; + + this.updateTimeAgo = function() { + self.timeAgoElement().timeago(); + }; }; - - this.timeAgoElement = function(selector) { - return $((typeof selector === "string") ? selector : this.selector); - }; - - this.updateTimeAgo = function() { - this.timeAgoElement().timeago(); - }; -}); + Diaspora.widgets.add("timeago", TimeAgo); +})(); diff --git a/spec/javascripts/diaspora-spec.js b/spec/javascripts/diaspora-spec.js index ffbf289eb..c28aa882b 100644 --- a/spec/javascripts/diaspora-spec.js +++ b/spec/javascripts/diaspora-spec.js @@ -4,85 +4,101 @@ */ describe("Diaspora", function() { - describe("WidgetCollection", function() { - describe("prototype", function() { - var widgets; + describe("widgets", function() { + describe("add", function() { + it("adds a widget to the collection", function() { + expect(Diaspora.widgets.collection["nameOfWidget"]).not.toBeDefined(); + Diaspora.widgets.add("nameOfWidget", function() { }); + expect(Diaspora.widgets.collection["nameOfWidget"]).toBeDefined(); + }); + + it("sets a shortcut by referencing the object on Diaspora.widgetCollection", function() { + expect(Diaspora.widgets.sup).toBeFalsy(); + Diaspora.widgets.add("sup", function() { }); + expect(Diaspora.widgets.sup).toEqual(Diaspora.widgets.collection.sup); + }); + }); + + describe("remove", function() { + it("removes a widget from the collection", function() { + Diaspora.widgets.add("nameOfWidget", function() { }); + expect(Diaspora.widgets.collection["nameOfWidget"]).toBeDefined(); + Diaspora.widgets.remove("nameOfWidget"); + expect(Diaspora.widgets.collection["nameOfWidget"]).not.toBeDefined(); + }); + }); + + describe("init", function() { + it("publishes the widget/ready event on all of the present widgets", function() { + Diaspora.widgets.add("nameOfWidget", function() { + var self = this; + this.subscribe("widget/ready", function() { + self.called = true; + }); + }); + + Diaspora.widgets.initialize(); + expect(Diaspora.widgets.collection.nameOfWidget.called).toBeTruthy(); + }); + + it("changes the initialized property to true", function() { + Diaspora.widgets.initialized = false; + Diaspora.widgets.initialize(); + expect(Diaspora.widgets.initialized).toBeTruthy(); + }); + }); + }); + describe("EventBroker", function() { + describe("extend", function() { + var obj; beforeEach(function() { - widgets = new Diaspora.WidgetCollection(); + obj = {}; }); - describe("add", function() { - it("adds a widget to the collection", function() { - expect(widgets.collection["nameOfWidget"]).not.toBeDefined(); - widgets.add("nameOfWidget", function() { }); - expect(widgets.collection["nameOfWidget"]).toBeDefined(); - }); - - it("sets a shortcut by referencing the object on Diaspora.widgetCollection", function() { - expect(widgets.sup).toBeFalsy(); - widgets.add("sup", function() { }); - expect(widgets.sup).toEqual(widgets.collection.sup); - }); + it("adds an events container to an object", function() { + expect(typeof Diaspora.EventBroker.extend(obj).eventsContainer).toEqual("object"); }); - describe("remove", function() { - it("removes a widget from the collection", function() { - widgets.add("nameOfWidget", function() { }); - expect(widgets.collection["nameOfWidget"]).toBeDefined(); - widgets.remove("nameOfWidget"); - expect(widgets.collection["nameOfWidget"]).not.toBeDefined(); - }); + it("adds a publish method to an object", function() { + expect(typeof Diaspora.EventBroker.extend(obj).publish).toEqual("function"); }); - describe("init", function() { - it("calls the start method on all of the widgets present", function() { - widgets.add("nameOfWidget", function() { - this.start = function() { } - }); + it("adds a subscribe method to an object", function() { + expect(typeof Diaspora.EventBroker.extend(obj).subscribe).toEqual("function"); + }); + }); - spyOn(widgets.collection["nameOfWidget"], "start"); - widgets.init(); - expect(widgets.collection["nameOfWidget"].start).toHaveBeenCalled(); - }); - - it("changes the initialized property to true", function() { - expect(widgets.initialized).toBeFalsy(); - widgets.init(); - expect(widgets.initialized).toBeTruthy(); - }); + describe("subscribe", function() { + it("subscribes to an event specified by an id", function() { + expect(Diaspora.widgets.eventsContainer.data("events")).not.toBeDefined(); + Diaspora.widgets.subscribe("testing/event", function() { }); + expect(Diaspora.widgets.eventsContainer.data("events")["testing/event"]).toBeDefined(); }); - describe("subscribe", function() { - it("subscribes to an event specified by an id", function() { - expect(widgets.eventsContainer.data("events")).not.toBeDefined(); - widgets.subscribe("testing/event", function() { }); - expect(widgets.eventsContainer.data("events")["testing/event"]).toBeDefined(); - }); + it("accepts a context in which the function will always be called", function() { + var foo = "bar"; - it("accepts a context in which the function will always be called", function() { - var foo = "bar"; - widgets.subscribe("testing/context", function() { foo = this.foo; }); - widgets.publish("testing/context"); - expect(foo).toEqual(undefined); + Diaspora.widgets.subscribe("testing/context", function() { foo = this.foo; }); + Diaspora.widgets.publish("testing/context"); + expect(foo).toEqual(undefined); - widgets.subscribe("testing/context_", function() { foo = this.foo; }, { foo: "hello" }); - widgets.publish("testing/context_"); - expect(foo).toEqual("hello"); - }); + Diaspora.widgets.subscribe("testing/context_", function() { foo = this.foo; }, { foo: "hello" }); + Diaspora.widgets.publish("testing/context_"); + expect(foo).toEqual("hello"); }); + }); - describe("publish", function() { - it("triggers events that are related to the specified id", function() { - var called = false; + describe("publish", function() { + it("triggers events that are related to the specified id", function() { + var called = false; - widgets.subscribe("testing/event", function() { - called = true; - }); - - widgets.publish("testing/event"); - - expect(called).toBeTruthy(); + Diaspora.widgets.subscribe("testing/event", function() { + called = true; }); + + Diaspora.widgets.publish("testing/event"); + + expect(called).toBeTruthy(); }); }); }); diff --git a/spec/javascripts/widgets/alert-spec.js b/spec/javascripts/widgets/alert-spec.js new file mode 100644 index 000000000..fdc99698d --- /dev/null +++ b/spec/javascripts/widgets/alert-spec.js @@ -0,0 +1,26 @@ +describe("Diaspora", function() { + describe("widgets", function() { + describe("alert", function() { + beforeEach(function() { + $(document).trigger("close.facebox"); + }); + + describe("on widget ready", function() { + it("should attach an event which will close detach the element from the DOM to close.facebox", function() { + Diaspora.widgets.alert.alert("YEAH", "YEAHH"); + expect($("#diaspora_alert").length).toEqual(1); + $(document).trigger("close.facebox"); + expect($("#diaspora_alert").length).toEqual(0); + }); + + }); + describe("alert", function() { + it("should render a mustache template and append it the body", function() { + Diaspora.widgets.alert.alert("YO", "YEAH"); + expect($("#diaspora_alert").length).toEqual(1); + $(document).trigger("close.facebox"); + }); + }); + }); + }); +}); diff --git a/spec/javascripts/widgets/aspect-nav.js b/spec/javascripts/widgets/aspect-nav.js new file mode 100644 index 000000000..6ea91bc72 --- /dev/null +++ b/spec/javascripts/widgets/aspect-nav.js @@ -0,0 +1,9 @@ +describe("Diaspora", function() { + describe("widgets", function() { + describe("aspectNav", function() { + describe("start", function() { + + }); + }); + }); +}); diff --git a/spec/javascripts/widgets/embedder-spec.js b/spec/javascripts/widgets/embedder-spec.js new file mode 100644 index 000000000..762e7a9c4 --- /dev/null +++ b/spec/javascripts/widgets/embedder-spec.js @@ -0,0 +1,65 @@ +/* Copyright (c) 2010, Diaspora Inc. This file is + * licensed under the Affero General Public License version 3 or later. See + * the COPYRIGHT file. + */ + +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() { + beforeEach(function(){ + Diaspora.widgets.embedder.registerServices(); + }); + 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( + '' + ); + }); + + it("attaches onVideoLinkClicked to a.video-link'", function() { + spyOn(Diaspora.widgets.embedder, "onVideoLinkClicked"); + Diaspora.widgets.embedder.publish("widget/ready"); + $("a.video-link:first").click(); + expect(Diaspora.widgets.embedder.onVideoLinkClicked).toHaveBeenCalled(); + }); + }); + + + it("has to have a certain DOM structure", function() { + spec.loadFixture("aspects_index_with_posts"); + + var $post = $("#main_stream").children(".stream_element:first"), + $contentParagraph = $post.children(".sm_body").children('.content').children("p"); + + expect($contentParagraph.length).toEqual(1); + }); + }); + }); +}); diff --git a/spec/javascripts/widgets/flashes-spec.js b/spec/javascripts/widgets/flashes-spec.js index 1ac7061e9..4778533fa 100644 --- a/spec/javascripts/widgets/flashes-spec.js +++ b/spec/javascripts/widgets/flashes-spec.js @@ -12,7 +12,7 @@ describe("Diaspora", function() { it("is called when the DOM is ready", function() { spyOn(Diaspora.widgets.flashes, "animateMessages").andCallThrough(); - Diaspora.widgets.flashes.start(); + Diaspora.widgets.flashes.publish("widget/ready"); expect(Diaspora.widgets.flashes.animateMessages).toHaveBeenCalled(); }); }); @@ -24,9 +24,10 @@ describe("Diaspora", function() { success: true, message: "success!" }); + expect($("#flash_notice").length).toEqual(1); expect(Diaspora.widgets.flashes.animateMessages).toHaveBeenCalled(); }); }); }); }); -}); \ No newline at end of file +}); diff --git a/spec/javascripts/widgets/notifications-spec.js b/spec/javascripts/widgets/notifications-spec.js index c68854205..1008a6297 100644 --- a/spec/javascripts/widgets/notifications-spec.js +++ b/spec/javascripts/widgets/notifications-spec.js @@ -8,9 +8,9 @@ describe("Diaspora", function() { var changeNotificationCountSpy; beforeEach(function() { - changeNotificationCountSpy = spyOn(Diaspora.widgets.notifications, "changeNotificationCount").andCallThrough(); + changeNotificationCountSpy = spyOn(Diaspora.widgets.notifications, "changeNotificationCount").andCallThrough(); $("#jasmine_content").html("
"); - Diaspora.widgets.notifications.start(); + Diaspora.widgets.notifications.publish("widget/ready"); changeNotificationCountSpy.reset(); }); @@ -65,4 +65,4 @@ describe("Diaspora", function() { }); }); }); -}); \ No newline at end of file +}); diff --git a/spec/javascripts/widgets/post-spec.js b/spec/javascripts/widgets/post-spec.js index 5c570317f..545cb1d77 100644 --- a/spec/javascripts/widgets/post-spec.js +++ b/spec/javascripts/widgets/post-spec.js @@ -9,7 +9,7 @@ describe("Diaspora", function() { describe("start", function() { it("should set up like on initialize", function() { spyOn(Diaspora.widgets.post, "setUpLikes"); - Diaspora.widgets.post.start(); + Diaspora.widgets.post.publish("widget/ready"); expect(Diaspora.widgets.post.setUpLikes).toHaveBeenCalled(); }); });