FIX: Better `metaKey` support for menu panels

This commit is contained in:
Robin Ward 2015-09-03 11:47:18 -04:00
parent e53d9f0e8b
commit 12e0225c51
4 changed files with 43 additions and 42 deletions

View File

@ -1,9 +1,10 @@
import computed from 'ember-addons/ember-computed-decorators';
import { iconHTML } from 'discourse/helpers/fa-icon';
import DiscourseURL from 'discourse/lib/url';
import interceptClick from 'discourse/lib/intercept-click';
export default Ember.Component.extend({
tagName: 'a',
classNames: ['d-link'],
attributeBindings: ['translatedTitle:title', 'translatedTitle:aria-title', 'href'],
@computed('path')
@ -27,18 +28,14 @@ export default Ember.Component.extend({
if (text) return I18n.t(text);
},
click() {
click(e) {
const action = this.get('action');
if (action) {
this.sendAction('action');
return false;
}
const href = this.get('href');
if (href) {
DiscourseURL.routeTo(href);
return false;
}
return false;
return interceptClick(e);
},
render(buffer) {

View File

@ -156,7 +156,8 @@ export default Ember.Component.extend({
@on('didInsertElement')
_bindEvents() {
this.$().on('click.discourse-menu-panel', 'a', (e) => {
this.$().on('click.discourse-menu-panel', 'a', e => {
if (e.metaKey) { return; }
if ($(e.target).data('ember-action')) { return; }
this.hide();
});
@ -164,7 +165,7 @@ export default Ember.Component.extend({
this.appEvents.on('dropdowns:closeAll', this, this.hide);
this.appEvents.on('dom:clean', this, this.hide);
$('body').on('keydown.discourse-menu-panel', (e) => {
$('body').on('keydown.discourse-menu-panel', e => {
if (e.which === 27) {
this.hide();
}

View File

@ -1,37 +1,8 @@
import DiscourseURL from 'discourse/lib/url';
import interceptClick from 'discourse/lib/intercept-click';
/**
Discourse does some server side rendering of HTML, such as the `cooked` contents of
posts. The downside of this in an Ember app is the links will not go through the router.
This jQuery code intercepts clicks on those links and routes them properly.
**/
export default {
name: "click-interceptor",
initialize: function() {
$('#main').on('click.discourse', 'a', function(e) {
if (e.isDefaultPrevented() || e.shiftKey || e.metaKey || e.ctrlKey) { return; }
var $currentTarget = $(e.currentTarget),
href = $currentTarget.attr('href');
if (!href ||
href === '#' ||
$currentTarget.attr('target') ||
$currentTarget.data('ember-action') ||
$currentTarget.data('auto-route') ||
$currentTarget.data('share-url') ||
$currentTarget.data('user-card') ||
$currentTarget.hasClass('mention') ||
$currentTarget.hasClass('ember-view') ||
$currentTarget.hasClass('lightbox') ||
href.indexOf("mailto:") === 0 ||
(href.match(/^http[s]?:\/\//i) && !href.match(new RegExp("^http:\\/\\/" + window.location.hostname, "i")))) {
return;
}
e.preventDefault();
DiscourseURL.routeTo(href);
return false;
});
initialize() {
$('#main').on('click.discourse', 'a', interceptClick);
}
};

View File

@ -0,0 +1,32 @@
import DiscourseURL from 'discourse/lib/url';
/**
Discourse does some server side rendering of HTML, such as the `cooked` contents of
posts. The downside of this in an Ember app is the links will not go through the router.
This jQuery code intercepts clicks on those links and routes them properly.
**/
export default function interceptClick(e) {
if (e.isDefaultPrevented() || e.shiftKey || e.metaKey || e.ctrlKey) { return; }
const $currentTarget = $(e.currentTarget),
href = $currentTarget.attr('href');
if (!href ||
href === '#' ||
$currentTarget.attr('target') ||
$currentTarget.data('ember-action') ||
$currentTarget.data('auto-route') ||
$currentTarget.data('share-url') ||
$currentTarget.data('user-card') ||
$currentTarget.hasClass('mention') ||
(!$currentTarget.hasClass('d-link') && $currentTarget.hasClass('ember-view')) ||
$currentTarget.hasClass('lightbox') ||
href.indexOf("mailto:") === 0 ||
(href.match(/^http[s]?:\/\//i) && !href.match(new RegExp("^http:\\/\\/" + window.location.hostname, "i")))) {
return;
}
e.preventDefault();
DiscourseURL.routeTo(href);
return false;
}