Skip to content

Commit de78ee3

Browse files
committed
- Extract Browser capabilities from Discourse namespace into a Singleton.
- Change Scrolling slack based on Android / Touch / Desktop
1 parent 8b4c030 commit de78ee3

8 files changed

Lines changed: 64 additions & 37 deletions

File tree

app/assets/javascripts/discourse.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,6 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
7575
@method bindDOMEvents
7676
**/
7777
bindDOMEvents: function() {
78-
var $html, hasTouch;
79-
80-
$html = $('html');
81-
hasTouch = false;
82-
83-
if ($html.hasClass('touch')) {
84-
hasTouch = true;
85-
}
86-
87-
if (Modernizr.prefixed("MaxTouchPoints", navigator) > 1) {
88-
hasTouch = true;
89-
}
90-
91-
if (hasTouch) {
92-
$html.addClass('discourse-touch');
93-
this.touch = true;
94-
this.hasTouch = true;
95-
} else {
96-
$html.addClass('discourse-no-touch');
97-
this.touch = false;
98-
}
99-
100-
$('#main').on('click.discourse', '[data-not-implemented=true]', function(e) {
101-
e.preventDefault();
102-
alert(I18n.t('not_implemented'));
103-
return false;
104-
});
105-
10678
$('#main').on('click.discourse', 'a', function(e) {
10779
if (e.isDefaultPrevented() || e.shiftKey || e.metaKey || e.ctrlKey) { return; }
10880

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*global Modernizr:true*/
2+
3+
/**
4+
Initializes the `Discourse.Capabilities` singleton by sniffing out the browser
5+
capabilities.
6+
**/
7+
Discourse.addInitializer(function() {
8+
var $html = $('html'),
9+
touch = $html.hasClass('touch') || (Modernizr.prefixed("MaxTouchPoints", navigator) > 1),
10+
caps = Discourse.Capabilities.current();
11+
12+
// Store the touch ability in our capabilities object
13+
caps.set('touch', touch);
14+
$html.addClass(touch ? 'discourse-touch' : 'discourse-no-touch');
15+
16+
// Detect Android
17+
if (navigator) {
18+
var ua = navigator.userAgent;
19+
caps.set('android', ua && ua.indexOf('Android') !== -1);
20+
}
21+
22+
// We consider high res a device with 1280 horizontal pixels. High DPI tablets like
23+
// iPads should report as 1024.
24+
caps.set('highRes', Modernizr.mq("only screen and (min-width: 1280px)"));
25+
26+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
Singleton to store the application's capabilities
3+
4+
@class Capabilities
5+
@namespace Discourse
6+
@module Discourse
7+
**/
8+
Discourse.Capabilities = Ember.Object.extend({
9+
10+
/**
11+
How much slack we should allow with infinite scrolling.
12+
13+
@property slackRatio
14+
**/
15+
slackRatio: function() {
16+
// Android is slow, so we use a really small slack
17+
if (this.get('android')) { return 0.5; }
18+
19+
// Touch devices get more slack due to inertia
20+
if (this.get('touch')) { return 1.5; }
21+
22+
// Higher resolution devices (likely laptops/desktops) should get more slack because they
23+
// can handle the perf.
24+
return this.get('highRes') ? 2.0 : 0.75;
25+
26+
}.property('android', 'touch', 'highRes')
27+
28+
});
29+
30+
Discourse.Capabilities.reopenClass(Discourse.Singleton);

app/assets/javascripts/discourse/views/cloaked_collection_view.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ Discourse.CloakedCollectionView = Ember.CollectionView.extend(Discourse.Scrollin
1414
var cloakView = this.get('cloakView'),
1515
idProperty = this.get('idProperty') || 'id';
1616

17-
// Give ourselves more slack on touch devices
18-
this.set('slackRatio', Discourse.touch ? 1.5 : 0.75);
19-
17+
this.set('slackRatio', Discourse.Capabilities.currentProp('slackRatio'));
2018
this.set('itemViewClass', Discourse.CloakedView.extend({
2119
classNames: [cloakView + '-cloak'],
2220
cloaks: Em.String.classify(cloakView) + 'View',

app/assets/javascripts/main_include.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//= require jquery.sortable.js
2828
//= require lodash.js
2929
//= require md5.js
30-
//= require modernizr.custom.95264.js
30+
//= require modernizr.custom.00874.js
3131
//= require mousetrap.js
3232
//= require rsvp.js
3333
//= require show-html.js
@@ -70,3 +70,4 @@
7070
//= require_tree ./discourse/helpers
7171
//= require_tree ./discourse/templates
7272
//= require_tree ./discourse/routes
73+
//= require_tree ./discourse/initializers

test/javascripts/test_helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
//= require jquery.tagsinput.js
4848
//= require lodash.js
4949
//= require md5.js
50-
//= require modernizr.custom.95264.js
50+
//= require modernizr.custom.00874.js
5151
//= require mousetrap.js
5252
//= require rsvp.js
5353
//= require show-html.js

vendor/assets/javascripts/modernizr.custom.00874.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)