Refactor: Light pass of didInsertElement calls of views

This commit is contained in:
Robin Ward 2013-06-07 12:13:46 -04:00
parent f23807af8f
commit 7c715e76e8
14 changed files with 101 additions and 113 deletions

View File

@ -34,9 +34,9 @@ Discourse.AdminCustomizeView = Discourse.View.extend({
}, },
didInsertElement: function() { didInsertElement: function() {
var _this = this; var controller = this.get('controller');
return Mousetrap.bindGlobal(['meta+s', 'ctrl+s'], function() { return Mousetrap.bindGlobal(['meta+s', 'ctrl+s'], function() {
_this.get('controller').save(); controller.save();
return false; return false;
}); });
}, },

View File

@ -39,14 +39,21 @@ Discourse = Ember.Application.createWithMixins({
even though our path formats are slightly different than what ember prefers. even though our path formats are slightly different than what ember prefers.
*/ */
resolver: Ember.DefaultResolver.extend({ resolver: Ember.DefaultResolver.extend({
resolveTemplate: function(parsedName) { resolveTemplate: function(parsedName) {
var resolvedTemplate = this._super(parsedName); var resolvedTemplate = this._super(parsedName);
if (resolvedTemplate) { return resolvedTemplate; } if (resolvedTemplate) { return resolvedTemplate; }
var decamelized = parsedName.fullNameWithoutType.decamelize();
// See if we can find it with slashes instead of underscores
var slashed = decamelized.replace("_", "/");
resolvedTemplate = Ember.TEMPLATES[slashed];
if (resolvedTemplate) { return resolvedTemplate; }
// If we can't find a template, check to see if it's similar to how discourse // If we can't find a template, check to see if it's similar to how discourse
// lays out templates like: adminEmail => admin/templates/email // lays out templates like: adminEmail => admin/templates/email
if (parsedName.fullNameWithoutType.indexOf('admin') === 0) { if (parsedName.fullNameWithoutType.indexOf('admin') === 0) {
var decamelized = parsedName.fullNameWithoutType.decamelize();
decamelized = decamelized.replace(/^admin\_/, 'admin/templates/'); decamelized = decamelized.replace(/^admin\_/, 'admin/templates/');
decamelized = decamelized.replace(/^admin\./, 'admin/templates/'); decamelized = decamelized.replace(/^admin\./, 'admin/templates/');
decamelized = decamelized.replace(/\./, '_'); decamelized = decamelized.replace(/\./, '_');

View File

@ -14,7 +14,7 @@ Discourse.DropdownButtonView = Discourse.View.extend({
// If there's a click handler, call it // If there's a click handler, call it
if (this.clicked) { if (this.clicked) {
var dropDownButtonView = this; var dropDownButtonView = this;
this.$('ul li').on('click', function(e) { this.$('ul li').on('click.dropdown-button', function(e) {
e.preventDefault(); e.preventDefault();
dropDownButtonView.clicked($(e.currentTarget).data('id')); dropDownButtonView.clicked($(e.currentTarget).data('id'));
return false; return false;
@ -23,7 +23,7 @@ Discourse.DropdownButtonView = Discourse.View.extend({
}, },
willDestroyElement: function(e) { willDestroyElement: function(e) {
this.$('ul li').off('click'); this.$('ul li').off('click.dropdown-button');
}, },
textChanged: function() { textChanged: function() {

View File

@ -12,28 +12,31 @@ Discourse.ComboboxView = Discourse.View.extend({
valueAttribute: 'id', valueAttribute: 'id',
render: function(buffer) { render: function(buffer) {
var _ref,
_this = this;
// Add none option if required // Add none option if required
if (this.get('none')) { if (this.get('none')) {
buffer.push("<option value=\"\">" + (Ember.String.i18n(this.get('none'))) + "</option>"); buffer.push("<option value=\"\">" + (Ember.String.i18n(this.get('none'))) + "</option>");
} }
var selected = (_ref = this.get('value')) ? _ref.toString() : void 0; var selected = this.get('value');
if (selected) { selected = selected.toString(); }
if (this.get('content')) { if (this.get('content')) {
var comboboxView = this;
return this.get('content').each(function(o) { return this.get('content').each(function(o) {
var data, selectedText, val, _ref1; var val = o[comboboxView.get('valueAttribute')];
val = (_ref1 = o[_this.get('valueAttribute')]) ? _ref1.toString() : void 0; if (val) { val = val.toString(); }
selectedText = val === selected ? "selected" : "";
data = ""; var selectedText = (val === selected) ? "selected" : "";
if (_this.dataAttributes) {
_this.dataAttributes.forEach(function(a) { var data = "";
if (comboboxView.dataAttributes) {
comboboxView.dataAttributes.forEach(function(a) {
data += "data-" + a + "=\"" + (o.get(a)) + "\" "; data += "data-" + a + "=\"" + (o.get(a)) + "\" ";
}); });
} }
return buffer.push("<option " + selectedText + " value=\"" + val + "\" " + data + ">" + o.name + "</option>"); buffer.push("<option " + selectedText + " value=\"" + val + "\" " + data + ">" + o.name + "</option>");
}); });
} }
}, },
@ -42,7 +45,7 @@ Discourse.ComboboxView = Discourse.View.extend({
var $combo = this.$(); var $combo = this.$();
var val = this.get('value'); var val = this.get('value');
if (val) { if (val) {
$combo.val(this.get('value').toString()); $combo.val(val.toString());
} else { } else {
$combo.val(null); $combo.val(null);
} }
@ -50,9 +53,9 @@ Discourse.ComboboxView = Discourse.View.extend({
}.observes('value'), }.observes('value'),
didInsertElement: function() { didInsertElement: function() {
var $elem, var $elem = this.$();
_this = this; var comboboxView = this;
$elem = this.$();
$elem.chosen({ template: this.template, disable_search_threshold: 5 }); $elem.chosen({ template: this.template, disable_search_threshold: 5 });
if (this.overrideWidths) { if (this.overrideWidths) {
// The Chosen plugin hard-codes the widths in style attrs. :< // The Chosen plugin hard-codes the widths in style attrs. :<
@ -69,7 +72,7 @@ Discourse.ComboboxView = Discourse.View.extend({
} }
$elem.change(function(e) { $elem.change(function(e) {
_this.set('value', $(e.target).val()); comboboxView.set('value', $(e.target).val());
}); });
} }

View File

@ -12,7 +12,7 @@ Discourse.EmbeddedPostView = Discourse.View.extend({
didInsertElement: function() { didInsertElement: function() {
var postView = this.get('postView') || this.get('parentView.postView'); var postView = this.get('postView') || this.get('parentView.postView');
return postView.get('screenTrack').track(this.get('elementId'), this.get('post.post_number')); postView.get('screenTrack').track(this.get('elementId'), this.get('post.post_number'));
} }
}); });

View File

@ -108,38 +108,41 @@ Discourse.HeaderView = Discourse.View.extend({
willDestroyElement: function() { willDestroyElement: function() {
$(window).unbind('scroll.discourse-dock'); $(window).unbind('scroll.discourse-dock');
return $(document).unbind('touchmove.discourse-dock'); $(document).unbind('touchmove.discourse-dock');
this.$('a.unread-private-messages, a.unread-notifications, a[data-notifications]').off('click.notifications');
this.$('a[data-dropdown]').off('click.dropdown');
}, },
didInsertElement: function() { didInsertElement: function() {
var _this = this;
this.$('a[data-dropdown]').on('click', function(e) { var headerView = this;
return _this.showDropdown($(e.currentTarget)); this.$('a[data-dropdown]').on('click.dropdown', function(e) {
return headerView.showDropdown($(e.currentTarget));
}); });
this.$('a.unread-private-messages, a.unread-notifications, a[data-notifications]').on('click', function(e) { this.$('a.unread-private-messages, a.unread-notifications, a[data-notifications]').on('click.notifications', function(e) {
return _this.showNotifications(e); return headerView.showNotifications(e);
}); });
$(window).bind('scroll.discourse-dock', function() { $(window).bind('scroll.discourse-dock', function() {
return _this.examineDockHeader(); headerView.examineDockHeader();
}); });
$(document).bind('touchmove.discourse-dock', function() { $(document).bind('touchmove.discourse-dock', function() {
return _this.examineDockHeader(); headerView.examineDockHeader();
}); });
this.examineDockHeader(); this.examineDockHeader();
// Delegate ESC to the composer // Delegate ESC to the composer
return $('body').on('keydown.header', function(e) { $('body').on('keydown.header', function(e) {
// Hide dropdowns // Hide dropdowns
if (e.which === 27) { if (e.which === 27) {
_this.$('li').removeClass('active'); headerView.$('li').removeClass('active');
_this.$('.d-dropdown').fadeOut('fast'); headerView.$('.d-dropdown').fadeOut('fast');
} }
if (_this.get('editingTopic')) { if (headerView.get('editingTopic')) {
if (e.which === 13) { if (e.which === 13) {
_this.finishedEdit(); headerView.finishedEdit();
} }
if (e.which === 27) { if (e.which === 27) {
return _this.cancelEdit(); return headerView.cancelEdit();
} }
} }
}); });

View File

@ -1,19 +0,0 @@
/**
This view handles the rendering of a category list
@class ListCategoriesView
@extends Discourse.View
@namespace Discourse
@module Discourse
**/
Discourse.ListCategoriesView = Discourse.View.extend({
templateName: 'list/categories',
didInsertElement: function() {
return Discourse.set('title', Em.String.i18n("category.list"));
}
});

View File

@ -14,6 +14,7 @@ Discourse.ListTopicsView = Discourse.View.extend(Discourse.Scrolling, {
listBinding: 'controller.model', listBinding: 'controller.model',
loadedMore: false, loadedMore: false,
currentTopicId: null, currentTopicId: null,
topicTrackingState: function() { topicTrackingState: function() {
return Discourse.TopicTrackingState.current(); return Discourse.TopicTrackingState.current();
}.property(), }.property(),

View File

@ -12,9 +12,9 @@ Discourse.TopicListItemView = Discourse.View.extend({
classNameBindings: ['content.archived', ':topic-list-item', 'content.hasExcerpt:has-excerpt'], classNameBindings: ['content.archived', ':topic-list-item', 'content.hasExcerpt:has-excerpt'],
attributeBindings: ['data-topic-id'], attributeBindings: ['data-topic-id'],
'data-topic-id': (function() { 'data-topic-id': function() {
return this.get('content.id'); return this.get('content.id');
}).property('content.id'), }.property('content.id'),
init: function() { init: function() {
this._super(); this._super();
@ -22,10 +22,9 @@ Discourse.TopicListItemView = Discourse.View.extend({
}, },
highlight: function() { highlight: function() {
var $topic, originalCol; var $topic = this.$();
$topic = this.$(); var originalCol = $topic.css('backgroundColor');
originalCol = $topic.css('backgroundColor'); $topic.css({
return $topic.css({
backgroundColor: "#ffffcc" backgroundColor: "#ffffcc"
}).animate({ }).animate({
backgroundColor: originalCol backgroundColor: originalCol

View File

@ -26,7 +26,7 @@ Discourse.PagedownEditor = Discourse.ContainerView.extend({
didInsertElement: function() { didInsertElement: function() {
$('#wmd-input').data('init', true); $('#wmd-input').data('init', true);
this.set('editor', Discourse.Markdown.createEditor()); this.set('editor', Discourse.Markdown.createEditor());
return this.get('editor').run(); this.get('editor').run();
}, },
observeValue: function() { observeValue: function() {

View File

@ -243,10 +243,9 @@ Discourse.PostView = Discourse.View.extend({
Discourse.Lightbox.apply($post); Discourse.Lightbox.apply($post);
// If we're scrolling upwards, adjust the scroll position accordingly // If we're scrolling upwards, adjust the scroll position accordingly
var scrollTo; var scrollTo = this.get('post.scrollTo');
if (scrollTo = this.get('post.scrollTo')) { if (scrollTo) {
var newSize = ($(document).height() - scrollTo.height) + scrollTo.top; $('body').scrollTop(($(document).height() - scrollTo.height) + scrollTo.top);
$('body').scrollTop(newSize);
$('section.divider').addClass('fade'); $('section.divider').addClass('fade');
} }

View File

@ -18,9 +18,7 @@ Discourse.QuoteButtonView = Discourse.View.extend({
@property visible @property visible
**/ **/
visible: function() { visible: Em.computed.notEmpty('controller.buffer'),
return this.present('controller.buffer');
}.property('controller.buffer'),
/** /**
Renders the pop-up quote button. Renders the pop-up quote button.
@ -45,23 +43,23 @@ Discourse.QuoteButtonView = Discourse.View.extend({
view = this; view = this;
$(document) $(document)
.on("mousedown.quote-button", function(e) { .on("mousedown.quote-button", function(e) {
view.set('isMouseDown', true); view.set('isMouseDown', true);
if ($(e.target).hasClass('quote-button') || $(e.target).hasClass('create')) return; if ($(e.target).hasClass('quote-button') || $(e.target).hasClass('create')) return;
// deselects only when the user left-click // deselects only when the user left-click
// this also allow anyone to `extend` their selection using a shift+click // this also allow anyone to `extend` their selection using a shift+click
if (e.which === 1 && !e.shiftKey) controller.deselectText(); if (e.which === 1 && !e.shiftKey) controller.deselectText();
}) })
.on('mouseup.quote-button', function(e) { .on('mouseup.quote-button', function(e) {
view.selectText(e.target, controller); view.selectText(e.target, controller);
view.set('isMouseDown', false); view.set('isMouseDown', false);
}) })
.on('selectionchange', function() { .on('selectionchange', function() {
// there is no need to handle this event when the mouse is down // there is no need to handle this event when the mouse is down
if (view.get('isMouseDown')) return; if (view.get('isMouseDown')) return;
// `selection.anchorNode` is used as a target // `selection.anchorNode` is used as a target
view.selectText(window.getSelection().anchorNode, controller); view.selectText(window.getSelection().anchorNode, controller);
}); });
}, },
/** /**
@ -86,9 +84,9 @@ Discourse.QuoteButtonView = Discourse.View.extend({
**/ **/
willDestroyElement: function() { willDestroyElement: function() {
$(document) $(document)
.off("mousedown.quote-button") .off("mousedown.quote-button")
.off("mouseup.quote-button") .off("mouseup.quote-button")
.off("selectionchange"); .off("selectionchange");
}, },
/** /**

View File

@ -11,17 +11,17 @@ Discourse.ShareView = Discourse.View.extend({
elementId: 'share-link', elementId: 'share-link',
classNameBindings: ['hasLink'], classNameBindings: ['hasLink'],
title: (function() { title: function() {
if (this.get('controller.type') === 'topic') return Em.String.i18n('share.topic'); if (this.get('controller.type') === 'topic') return Em.String.i18n('share.topic');
return Em.String.i18n('share.post'); return Em.String.i18n('share.post');
}).property('controller.type'), }.property('controller.type'),
hasLink: (function() { hasLink: function() {
if (this.present('controller.link')) return 'visible'; if (this.present('controller.link')) return 'visible';
return null; return null;
}).property('controller.link'), }.property('controller.link'),
linkChanged: (function() { linkChanged: function() {
if (this.present('controller.link')) { if (this.present('controller.link')) {
var $linkInput = $('#share-link input'); var $linkInput = $('#share-link input');
$linkInput.val(this.get('controller.link')); $linkInput.val(this.get('controller.link'));
@ -31,36 +31,35 @@ Discourse.ShareView = Discourse.View.extend({
$linkInput.select().focus(); $linkInput.select().focus();
}, 160); }, 160);
} }
}).observes('controller.link'), }.observes('controller.link'),
didInsertElement: function() { didInsertElement: function() {
var _this = this;
var shareView = this;
$('html').on('mousedown.outside-share-link', function(e) { $('html').on('mousedown.outside-share-link', function(e) {
// Use mousedown instead of click so this event is handled before routing occurs when a // Use mousedown instead of click so this event is handled before routing occurs when a
// link is clicked (which is a click event) while the share dialog is showing. // link is clicked (which is a click event) while the share dialog is showing.
if (_this.$().has(e.target).length !== 0) { if (shareView.$().has(e.target).length !== 0) { return; }
return; shareView.get('controller').close();
}
_this.get('controller').close();
return true; return true;
}); });
$('html').on('click.discoure-share-link', '[data-share-url]', function(e) { $('html').on('click.discoure-share-link', '[data-share-url]', function(e) {
var $currentTarget, url;
e.preventDefault(); e.preventDefault();
$currentTarget = $(e.currentTarget); var $currentTarget = $(e.currentTarget);
url = $currentTarget.data('share-url'); var url = $currentTarget.data('share-url');
/* Relative urls // Relative urls
*/
if (url.indexOf("/") === 0) { if (url.indexOf("/") === 0) {
url = window.location.protocol + "//" + window.location.host + url; url = window.location.protocol + "//" + window.location.host + url;
} }
_this.get('controller').shareLink(e, url); shareView.get('controller').shareLink(e, url);
return false; return false;
}); });
$('html').on('keydown.share-view', function(e){ $('html').on('keydown.share-view', function(e){
if (e.keyCode === 27) { if (e.keyCode === 27) {
_this.get('controller').close(); shareView.get('controller').close();
} }
}); });
}, },

View File

@ -1,22 +1,21 @@
Discourse.UserSelector = Discourse.TextField.extend({ Discourse.UserSelector = Discourse.TextField.extend({
didInsertElement: function(){ didInsertElement: function(){
var _this = this; var userSelectorView = this;
var selected = []; var selected = [];
var transformTemplate = Handlebars.compile("{{avatar this imageSize=\"tiny\"}} {{this.username}}"); var transformTemplate = Handlebars.compile("{{avatar this imageSize=\"tiny\"}} {{this.username}}");
var template = Discourse.UserSelector.templateFunction();
$(this.get('element')).val(this.get('usernames')).autocomplete({ $(this.get('element')).val(this.get('usernames')).autocomplete({
template: template, template: Discourse.UserSelector.templateFunction(),
dataSource: function(term) { dataSource: function(term) {
var exclude = selected; var exclude = selected;
if (_this.get('excludeCurrentUser')){ if (userSelectorView.get('excludeCurrentUser')){
exclude = exclude.concat([Discourse.User.current('username')]); exclude = exclude.concat([Discourse.User.current('username')]);
} }
return Discourse.UserSearch.search({ return Discourse.UserSearch.search({
term: term, term: term,
topicId: _this.get('topicId'), topicId: userSelectorView.get('topicId'),
exclude: exclude exclude: exclude
}); });
}, },
@ -29,7 +28,7 @@ Discourse.UserSelector = Discourse.TextField.extend({
return i; return i;
} }
}); });
_this.set('usernames', items.join(",")); userSelectorView.set('usernames', items.join(","));
selected = items; selected = items;
}, },
@ -40,7 +39,6 @@ Discourse.UserSelector = Discourse.TextField.extend({
} }
}); });
} }
}); });