diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index f71d9d104..558a06710 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -208,6 +208,17 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { } }, + /** + Add an initializer hook for after the Discourse Application starts up. + + @method addInitializer + @param {Function} init the initializer to add. + **/ + addInitializer: function(init) { + Discourse.initializers = Discourse.initializers || []; + Discourse.initializers.push(init); + }, + /** Start up the Discourse application. @@ -223,6 +234,12 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { // Developer specific functions Discourse.Development.observeLiveChanges(); Discourse.subscribeUserToNotifications(); + + if (Discourse.initializers) { + Discourse.initializers.forEach(function (init) { + init.call(this); + }); + } } }); diff --git a/app/assets/javascripts/discourse/views/composer/composer_view.js b/app/assets/javascripts/discourse/views/composer/composer_view.js index 49bc80801..19d055374 100644 --- a/app/assets/javascripts/discourse/views/composer/composer_view.js +++ b/app/assets/javascripts/discourse/views/composer/composer_view.js @@ -8,7 +8,7 @@ @namespace Discourse @module Discourse **/ -Discourse.ComposerView = Discourse.View.extend({ +Discourse.ComposerView = Discourse.View.extend(Ember.Evented, { templateName: 'composer', elementId: 'reply-control', classNameBindings: ['model.creatingPrivateMessage:private-message', @@ -49,18 +49,17 @@ Discourse.ComposerView = Discourse.View.extend({ }.property('model.createdPost'), observeReplyChanges: function() { - var composerView = this; + var self = this; if (this.get('model.hidePreview')) return; - Ember.run.next(null, function() { - var $wmdPreview, caretPosition; - if (composerView.editor) { - composerView.editor.refreshPreview(); + Ember.run.next(function() { + if (self.editor) { + self.editor.refreshPreview(); // if the caret is on the last line ensure preview scrolled to bottom - caretPosition = Discourse.Utilities.caretPosition(composerView.wmdInput[0]); - if (!composerView.wmdInput.val().substring(caretPosition).match(/\n/)) { - $wmdPreview = $('#wmd-preview'); + var caretPosition = Discourse.Utilities.caretPosition(self.wmdInput[0]); + if (!self.wmdInput.val().substring(caretPosition).match(/\n/)) { + var $wmdPreview = $('#wmd-preview'); if ($wmdPreview.is(':visible')) { - return $wmdPreview.scrollTop($wmdPreview[0].scrollHeight); + $wmdPreview.scrollTop($wmdPreview[0].scrollHeight); } } } @@ -129,8 +128,8 @@ Discourse.ComposerView = Discourse.View.extend({ Discourse.SyntaxHighlighting.apply($wmdPreview); - var post = this.get('model.post'); - var refresh = false; + var post = this.get('model.post'), + refresh = false; // If we are editing a post, we'll refresh its contents once. This is a feature that // allows a user to refresh its contents once. @@ -146,6 +145,8 @@ Discourse.ComposerView = Discourse.View.extend({ $('span.mention', $wmdPreview).each(function(i, e) { Discourse.Mention.load(e, refresh); }); + + this.trigger('previewRefreshed', $wmdPreview); }, 100), initEditor: function() { diff --git a/app/assets/javascripts/discourse/views/post_view.js b/app/assets/javascripts/discourse/views/post_view.js index db155843e..57c3f5e54 100644 --- a/app/assets/javascripts/discourse/views/post_view.js +++ b/app/assets/javascripts/discourse/views/post_view.js @@ -6,7 +6,7 @@ @namespace Discourse @module Discourse **/ -Discourse.PostView = Discourse.GroupedView.extend({ +Discourse.PostView = Discourse.GroupedView.extend(Ember.Evented, { classNames: ['topic-post', 'clearfix'], templateName: 'post', classNameBindings: ['postTypeClass', @@ -193,8 +193,9 @@ Discourse.PostView = Discourse.GroupedView.extend({ }, didInsertElement: function() { - var $post = this.$(); - var post = this.get('post'); + var $post = this.$(), + post = this.get('post'); + this.showLinkCounts(); // Track this post @@ -204,6 +205,8 @@ Discourse.PostView = Discourse.GroupedView.extend({ Discourse.SyntaxHighlighting.apply($post); Discourse.Lightbox.apply($post); + this.trigger('postViewInserted', $post); + // Find all the quotes this.insertQuoteControls(); diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index ca74b9c65..b73716e22 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -102,7 +102,7 @@ class Plugin::Instance def automatic_assets css = "" - js = "(function(){" + js = "" css = @styles.join("\n") if @styles js = @javascripts.join("\n") if @javascripts @@ -127,10 +127,14 @@ class Plugin::Instance end end - js << "})();" + # Generate an IIFE for the JS + js = "(function(){#{js}})();" if js.present? - # TODO don't serve blank assets - [[css,"css"],[js,"js"]].map do |asset, extension| + result = [] + result << [css, 'css'] if css.present? + result << [js, 'js'] if js.present? + + result.map do |asset, extension| hash = Digest::SHA1.hexdigest asset ["#{auto_generated_path}/plugin_#{hash}.#{extension}", asset] end