Conditions | 9 |
Paths | 44 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 11 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | class CoverImage { |
||
3 | constructor($el, cb) { |
||
4 | |||
5 | const _this = this; |
||
6 | |||
7 | _this.$el = $el ? jQuery($el) : jQuery(window); |
||
8 | _this.$img = _this.getElementForSizing(); |
||
9 | _this.disableOnMobile = _this.$el.data('cover-image-mobile') === false; |
||
10 | _this.cb = cb || (() => { |
||
11 | //DEBUG console.log("Default callback"); |
||
12 | }); |
||
13 | _this.positioning = { |
||
14 | x : 0.5, |
||
15 | y : 0.5 |
||
16 | }; |
||
17 | _this.options = { |
||
18 | parallax : _this.$el.data('coverImageParallax') === '' |
||
19 | }; |
||
20 | |||
21 | if (!_this.$img) { |
||
22 | console.log('Error:', 'no image found', _this.$img ); |
||
|
|||
23 | return; |
||
24 | } |
||
25 | |||
26 | _this.imageWidth = _this.$img.attr('width'); |
||
27 | _this.imageHeight = _this.$img.attr('height'); |
||
28 | |||
29 | // If the image doesn't have harcoded width|height |
||
30 | // attributes then load the image to calculate |
||
31 | // the dimensions |
||
32 | if (!_this.imageWidth || !_this.imageHeight) { |
||
33 | console.log('No dimensions found. Generating image') |
||
34 | _this.img = new Image(); |
||
35 | _this.img.src = _this.$img.attr('src'); |
||
36 | |||
37 | _this.imageWidth = _this.img.width; |
||
38 | _this.imageHeight = _this.img.height; |
||
39 | } |
||
40 | |||
41 | if ( _this.disableOnMobile && window.innerWidth < 480 ) { |
||
42 | return; |
||
43 | } |
||
44 | |||
45 | _this.elementDimensions = { |
||
46 | height : _this.$el.outerHeight(), |
||
47 | width : _this.$el.outerWidth() |
||
48 | }; |
||
49 | |||
50 | |||
51 | _this.$el.css({ |
||
52 | overflow : 'hidden', |
||
53 | position : 'relative' |
||
54 | }); |
||
55 | |||
56 | if (!_this.$img.length) { |
||
57 | |||
58 | // TODO: Implement load |
||
59 | setTimeout( () => { |
||
60 | new CoverImage( _this.$el ); |
||
61 | }, 1000); |
||
62 | |||
63 | } else { |
||
64 | _this.resizeImage(); |
||
65 | } |
||
66 | |||
67 | _this.$img.one('load', () => { |
||
68 | console.log('Image loaded:', 'resize'); |
||
69 | _this.resizeImage(); |
||
70 | }); |
||
71 | |||
72 | $(window).on('resize', () => { |
||
73 | _this.resizeImage(); |
||
74 | }); |
||
75 | |||
76 | $(window).on('ci.resize', () => { |
||
77 | _this.resizeImage(); |
||
78 | }); |
||
79 | |||
80 | if (_this.options.parallax) { |
||
81 | _this.draw(); |
||
82 | } |
||
83 | } |
||
84 | |||
202 |