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() {
var _this = this;
var controller = this.get('controller');
return Mousetrap.bindGlobal(['meta+s', 'ctrl+s'], function() {
_this.get('controller').save();
controller.save();
return false;
});
},

View file

@ -39,14 +39,21 @@ Discourse = Ember.Application.createWithMixins({
even though our path formats are slightly different than what ember prefers.
*/
resolver: Ember.DefaultResolver.extend({
resolveTemplate: function(parsedName) {
var resolvedTemplate = this._super(parsedName);
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
// lays out templates like: adminEmail => admin/templates/email
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(/\./, '_');

View file

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

View file

@ -12,28 +12,31 @@ Discourse.ComboboxView = Discourse.View.extend({
valueAttribute: 'id',
render: function(buffer) {
var _ref,
_this = this;
// Add none option if required
if (this.get('none')) {
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')) {
var comboboxView = this;
return this.get('content').each(function(o) {
var data, selectedText, val, _ref1;
val = (_ref1 = o[_this.get('valueAttribute')]) ? _ref1.toString() : void 0;
selectedText = val === selected ? "selected" : "";
data = "";
if (_this.dataAttributes) {
_this.dataAttributes.forEach(function(a) {
var val = o[comboboxView.get('valueAttribute')];
if (val) { val = val.toString(); }
var selectedText = (val === selected) ? "selected" : "";
var data = "";
if (comboboxView.dataAttributes) {
comboboxView.dataAttributes.forEach(function(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 val = this.get('value');
if (val) {
$combo.val(this.get('value').toString());
$combo.val(val.toString());
} else {
$combo.val(null);
}
@ -50,9 +53,9 @@ Discourse.ComboboxView = Discourse.View.extend({
}.observes('value'),
didInsertElement: function() {
var $elem,
_this = this;
$elem = this.$();
var $elem = this.$();
var comboboxView = this;
$elem.chosen({ template: this.template, disable_search_threshold: 5 });
if (this.overrideWidths) {
// The Chosen plugin hard-codes the widths in style attrs. :<
@ -69,7 +72,7 @@ Discourse.ComboboxView = Discourse.View.extend({
}
$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() {
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() {
$(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() {
var _this = this;
this.$('a[data-dropdown]').on('click', function(e) {
return _this.showDropdown($(e.currentTarget));
var headerView = this;
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) {
return _this.showNotifications(e);
this.$('a.unread-private-messages, a.unread-notifications, a[data-notifications]').on('click.notifications', function(e) {
return headerView.showNotifications(e);
});
$(window).bind('scroll.discourse-dock', function() {
return _this.examineDockHeader();
headerView.examineDockHeader();
});
$(document).bind('touchmove.discourse-dock', function() {
return _this.examineDockHeader();
headerView.examineDockHeader();
});
this.examineDockHeader();
// Delegate ESC to the composer
return $('body').on('keydown.header', function(e) {
$('body').on('keydown.header', function(e) {
// Hide dropdowns
if (e.which === 27) {
_this.$('li').removeClass('active');
_this.$('.d-dropdown').fadeOut('fast');
headerView.$('li').removeClass('active');
headerView.$('.d-dropdown').fadeOut('fast');
}
if (_this.get('editingTopic')) {
if (headerView.get('editingTopic')) {
if (e.which === 13) {
_this.finishedEdit();
headerView.finishedEdit();
}
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',
loadedMore: false,
currentTopicId: null,
topicTrackingState: function() {
return Discourse.TopicTrackingState.current();
}.property(),

View file

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

View file

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

View file

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

View file

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

View file

@ -11,17 +11,17 @@ Discourse.ShareView = Discourse.View.extend({
elementId: 'share-link',
classNameBindings: ['hasLink'],
title: (function() {
title: function() {
if (this.get('controller.type') === 'topic') return Em.String.i18n('share.topic');
return Em.String.i18n('share.post');
}).property('controller.type'),
}.property('controller.type'),
hasLink: (function() {
hasLink: function() {
if (this.present('controller.link')) return 'visible';
return null;
}).property('controller.link'),
}.property('controller.link'),
linkChanged: (function() {
linkChanged: function() {
if (this.present('controller.link')) {
var $linkInput = $('#share-link input');
$linkInput.val(this.get('controller.link'));
@ -31,36 +31,35 @@ Discourse.ShareView = Discourse.View.extend({
$linkInput.select().focus();
}, 160);
}
}).observes('controller.link'),
}.observes('controller.link'),
didInsertElement: function() {
var _this = this;
var shareView = this;
$('html').on('mousedown.outside-share-link', function(e) {
// 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.
if (_this.$().has(e.target).length !== 0) {
return;
}
_this.get('controller').close();
if (shareView.$().has(e.target).length !== 0) { return; }
shareView.get('controller').close();
return true;
});
$('html').on('click.discoure-share-link', '[data-share-url]', function(e) {
var $currentTarget, url;
e.preventDefault();
$currentTarget = $(e.currentTarget);
url = $currentTarget.data('share-url');
/* Relative urls
*/
var $currentTarget = $(e.currentTarget);
var url = $currentTarget.data('share-url');
// Relative urls
if (url.indexOf("/") === 0) {
url = window.location.protocol + "//" + window.location.host + url;
}
_this.get('controller').shareLink(e, url);
shareView.get('controller').shareLink(e, url);
return false;
});
$('html').on('keydown.share-view', function(e){
if (e.keyCode === 27) {
_this.get('controller').close();
shareView.get('controller').close();
}
});
},

View file

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