Skip to content

Commit

Permalink
move scrollbar thickness measurement to service
Browse files Browse the repository at this point in the history
  • Loading branch information
buschtoens committed Mar 22, 2017
1 parent 8e68179 commit 3572c3d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
36 changes: 6 additions & 30 deletions addon/components/ember-scrollable.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const {
computed,
deprecate,
isPresent,
inject: {
service
},
run: {
scheduleOnce,
debounce,
Expand Down Expand Up @@ -110,6 +113,8 @@ export default Ember.Component.extend(InboundActionsMixin, DomMixin, {
*/
verticalScrollbar: null,

scrollbarThickness: service(),

didReceiveAttrs() {
const horizontal = this.get('horizontal');
const vertical = this.get('horizontal');
Expand Down Expand Up @@ -215,35 +220,6 @@ export default Ember.Component.extend(InboundActionsMixin, DomMixin, {
this._contentElement = this.$(`${contentSelector}:first`);
},

/**
* Original function by Jonathan Sharp:
* http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php
*
* Modified to work with newer jQuery version.
*
* @property scrollbarThickness
* @private
* @type Number
*/
scrollbarThickness: computed(() => {
// Append a temporary scrolling element to the DOM, then measure
// the difference between between its outer and inner elements.
let tempEl = $(`
<div class="scrollbar-width-tester" style="width: 50px; position: absolute; top: 0px;">
<div style="overflow: scroll;">
<div class="scrollbar-width-tester__inner"></div>
</div>
</div>
`);
$('body').append(tempEl);
let width = $(tempEl).width();
let widthMinusScrollbars = $('.scrollbar-width-tester__inner', tempEl).width();
tempEl.remove();

return (width - widthMinusScrollbars);
}),


/**
* Used to create/reset scrollbar(s) if they are necessary
*
Expand All @@ -269,7 +245,7 @@ export default Ember.Component.extend(InboundActionsMixin, DomMixin, {
resizeScrollContent() {
const width = this.$().width();
const height = this.$().height();
const scrollbarThickness = this.get('scrollbarThickness');
const scrollbarThickness = this.get('scrollbarThickness.thickness');

const hasHorizontal = this.get('horizontal');
const hasVertical = this.get('vertical');
Expand Down
24 changes: 24 additions & 0 deletions addon/services/scrollbar-thickness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Ember from 'ember';

const {
computed,
Service
} = Ember;

export default Service.extend({
thickness: computed(() => {
const tempEl = $(`
<div class="scrollbar-width-tester" style="width: 50px; position: absolute; left: -100px;">
<div style="overflow: scroll;">
<div class="scrollbar-width-tester__inner"></div>
</div>
</div>
`);
$('body').append(tempEl);
const width = $(tempEl).width();
const widthMinusScrollbars = $('.scrollbar-width-tester__inner', tempEl).width();
tempEl.remove();

return (width - widthMinusScrollbars);
})
});
1 change: 1 addition & 0 deletions app/services/scrollbar-thickness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-scrollable/services/scrollbar-thickness';

0 comments on commit 3572c3d

Please sign in to comment.