diff options
author | Dan Hansen <mokker1234@gmail.com> | 2011-07-14 09:23:31 +0400 |
---|---|---|
committer | Dan Hansen <mokker1234@gmail.com> | 2011-07-14 09:23:31 +0400 |
commit | ca2413ff6bf7347dac9a2cfb076af4d2099f378d (patch) | |
tree | 34f15c5d22eb9933feed7407fea0bef892dc8b53 /public | |
parent | abe10b3889ee0d1e87f5193173353635e4e74ae9 (diff) |
Widgets now subscribe to a widget/ready event. Apply change to all widgets, added some tests & lots of cleanup.
Diffstat (limited to 'public')
-rw-r--r-- | public/javascripts/diaspora.js | 76 | ||||
-rw-r--r-- | public/javascripts/widgets/alert.js | 59 | ||||
-rw-r--r-- | public/javascripts/widgets/directionDetector.js | 118 | ||||
-rw-r--r-- | public/javascripts/widgets/embedder.js | 146 | ||||
-rw-r--r-- | public/javascripts/widgets/flashes.js | 27 | ||||
-rw-r--r-- | public/javascripts/widgets/hovercard.js | 8 | ||||
-rw-r--r-- | public/javascripts/widgets/i18n.js | 51 | ||||
-rw-r--r-- | public/javascripts/widgets/infinite-scroll.js | 60 | ||||
-rw-r--r-- | public/javascripts/widgets/lightbox.js | 4 | ||||
-rw-r--r-- | public/javascripts/widgets/notifications-badge.js | 26 | ||||
-rw-r--r-- | public/javascripts/widgets/notifications.js | 83 | ||||
-rw-r--r-- | public/javascripts/widgets/post.js | 4 | ||||
-rw-r--r-- | public/javascripts/widgets/timeago.js | 42 |
13 files changed, 372 insertions, 332 deletions
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 = $({}); + + obj.subscribe = Diaspora.EventBroker.subscribe; + obj.publish = Diaspora.EventBroker.publish; + + obj.publish = $.proxy(function(eventId, args) { + this.eventsContainer.trigger(eventId, args); + }, obj); - Diaspora.WidgetCollection.prototype.add = function(widgetId, Widget) { - $.extend(Widget.prototype, Diaspora.BaseWidget); + 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); - this[widgetId] = this.collection[widgetId] = new Widget(); - if(this.initialized) { - this.collection[widgetId].start(); + 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({})); - Diaspora.WidgetCollection.prototype.publish = function(id, args) { - this.eventsContainer.trigger(id, args); - }; + this[widgetId] = this.collection[widgetId] = new Widget(); + if(this.initialized) { + this.collection[widgetId].publish("widget/ready"); + } + }, - Diaspora.BaseWidget = { - require: function(widgetName) { - this[widgetName] = Diaspora.widgets[widgetName]; + remove: function(widgetId) { + delete this.collection[widgetId]; } }; - 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 = '<div id="diaspora_alert" class="facebox_content">' + - '<div class="span-12 last">' + - '<div id="facebox_header">' + - '<h4>' + - '{{title}}' + - '</h4>' + + this.faceboxTemplate = '<div id="diaspora_alert" class="facebox_content">' + + '<div class="span-12 last">' + + '<div id="facebox_header">' + + '<h4>' + + '{{title}}' + + '</h4>' + + '</div>' + + '{{content}}' + '</div>' + - '{{content}}' + - '</div>' + - '</div>'; + '</div>'; + + this.subscribe("widget/ready", function() { + $(document).bind("close.facebox", function() { + $("#facebox, #diaspora_alert").remove(); + }); + }); - this.alert = function(title, content) { - var template = $.mustache(this.faceboxTemplate, { - title: title, - content: content - }); - $(template).appendTo(document.body); + this.alert = function(title, content) { + $($.mustache(self.faceboxTemplate, { + title: title, + content: content + })).appendTo(document.body); + + $.facebox({ + div: "#diaspora_alert" + }, "diaspora_alert"); + } + }; - $.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.bind = function() { - $(this).bind('keyup', Diaspora.widgets.directionDetector.updateDirection); - Diaspora.widgets.directionDetector.binds.push($(this)); + 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.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); - }; - - - 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'); + var Embedder = function() { + var self = this; + this.services = {}; - $this.click(function() { - $container.slideUp('fast', function() { - $(this).detach(); + self.subscribe("widget/ready", function() { + $.extend(self, { + stream: $("#main_stream") }); - }); - }; - - Embedder.prototype.start = function() { - $("#main_stream a.video-link").live("click", this.onVideoLinkClicked); - this.registerServices(); - - var $post = $("#main_stream").children(".stream_element:first"), - $contentParagraph = $post.children(".sm_body").children('.content').children("p"); - - this.canEmbed = $contentParagraph.length; - }; - - Embedder.prototype.registerServices = function() { - var watchVideoOn = Diaspora.widgets.i18n.t("videos.watch"); + + self.ensureDOMStructure(); - this.register("youtube.com", + 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"); + + self.canEmbed = !!content.length; + }; + + + this.register = function(service, template) { + self.services[service] = template; + }; + + this.render = function(service, views) { + var template = (typeof self.services[service] === "string") + ? self.services[service] + : self.services.undefined; + + return $.mustache(template, views); + }; + + this.embed = function(videoLink) { + var host = videoLink.data("host"), + container = $("<div/>", { "class": "video-container" }), + videoContainer = videoLink.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( + 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", '<a href="//www.youtube.com/watch?v={{video-id}}{{anchor}}" target="_blank">' + $.mustache(watchVideoOn, { provider: "YouTube" }) + '</a><br />' + '<iframe class="youtube-player" type="text/html" src="http://www.youtube.com/embed/{{video-id}}?wmode=opaque{{anchor}}"></iframe>'); - this.register("vimeo.com", - '<a href="http://vimeo.com/{{video-id}}">' + $.mustache(watchVideoOn, { provider: "Vimeo" }) + '</a><br />' + - '<iframe class="vimeo-player" src="http://player.vimeo.com/video/{{video-id}}"></iframe>'); - - this.register("undefined", '<p>' + Diaspora.widgets.i18n.t("videos.unknown") + ' - {{host}}</p>'); - }; + self.register("vimeo.com", + '<a href="http://vimeo.com/{{video-id}}">' + $.mustache(watchVideoOn, { provider: "Vimeo" }) + '</a><br />' + + '<iframe class="vimeo-player" src="http://player.vimeo.com/video/{{video-id}}"></iframe>'); - Embedder.prototype.onVideoLinkClicked = function(evt) { - if(Diaspora.widgets.embedder.canEmbed) { - evt.preventDefault(); - Diaspora.widgets.embedder.embed($(this)); - } + self.register("undefined", '<p>' + Diaspora.widgets.i18n.t("videos.unknown") + ' - {{host}}</p>'); + }; }; 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) { - $("<div/>") - .attr("id", (result.success) ? "flash_notice" : "flash_error") - .prependTo(document.body) - .html(result.notice); + $("<div/>", { + 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. */ +(function() { + var I18n = function() { + var self = this; + this.locale = { }; + this.language = "en"; + + this.loadLocale = function(locale, language) { + this.locale = locale; + this.language = language; + }; + + this.t = function(item, views) { + var translatedMessage, + items = item.split("."); -Diaspora.widgets.add("i18n", function() { - this.language = "en"; - this.locale = { }; - - this.loadLocale = function(locale, language) { - this.language = language; - this.locale = locale; - }; + while(nextNamespace = items.shift()) { + translatedMessage = (translatedMessage) + ? translatedMessage[nextNamespace] + : self.locale[nextNamespace]; - this.t = function(item, views) { - var ret, - _item = item.split("."); - - while(part = _item.shift()) { - ret = (ret) ? ret[part] : this.locale[part]; - if(typeof ret === "undefined") { - return ""; + 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(); - }; - - InfiniteScroll.prototype.initialize = function(){ - if($('#main_stream').length !== 0){ - $('#main_stream').infinitescroll(this.options, function() { - Diaspora.widgets.publish("stream/scrolled"); - }); + this.subscribe("widget/ready", function() { + Diaspora.widgets.subscribe("stream/reloaded", self.reInitialize, this); + 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.reInitialize = function() { + self.clear(); + self.initialize(); + }; - InfiniteScroll.prototype.start = function() { - Diaspora.widgets.subscribe("stream/reloaded", this.reInitialize, this); - this.initialize(); - }; + 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.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.badgeLink.toggle(function(evt) { - evt.preventDefault(); + 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"); + + 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"); }); - }; - }; - - Notifications.prototype.showNotification = function(notification) { - $(notification.html).prependTo(this.notificationArea) - .fadeIn(200) - .delay(8000) - .fadeOut(200, function() { - $(this).detach(); - }); + }); + + 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(); - } - }; + if(typeof notification.incrementCount === "undefined" || notification.incrementCount) { + this.incrementCount(); + } + }; - Notifications.prototype.changeNotificationCount = function(change) { - this.count += change; + this.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.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.timeAgoElement = function(selector) { + return $((typeof selector === "string") ? selector : this.selector); + }; - this.updateTimeAgo = function() { - this.timeAgoElement().timeago(); + this.updateTimeAgo = function() { + self.timeAgoElement().timeago(); + }; }; -}); + Diaspora.widgets.add("timeago", TimeAgo); +})(); |