Completed
Push — master ( af2b22...774afd )
by LA
11:45
created

dist/coverimage.js   B

Complexity

Total Complexity 43
Complexity/F 1.95

Size

Lines of Code 215
Function Count 22

Duplication

Duplicated Lines 25
Ratio 11.63 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 25
loc 215
rs 8.3157
wmc 43
mnd 2
bc 36
fnc 22
bpm 1.6363
cpm 1.9545
noi 8

16 Functions

Rating   Name   Duplication   Size   Complexity  
A coverimage.js ➔ resizeImage 0 12 2
A _createClass.constructor 0 1 1
A coverimage.js ➔ coverDimensions 0 8 1
B coverimage.js ➔ draw 25 25 4
A coverimage.js ➔ max 0 3 2
A coverimage.js ➔ containDimensions 0 8 1
A coverimage.js ➔ getTransform 0 3 1
C coverimage.js ➔ CoverImage 0 80 9
A coverimage.js ➔ defineProperties 0 1 3
B CoverImage.constructor 25 203 1
A coverimage.js ➔ min 0 3 2
A coverimage.js ➔ _createClass 0 1 3
A coverimage.js ➔ _classCallCheck 0 1 2
A coverimage.js ➔ setImageSize 0 18 1
A coverimage.js ➔ getElementForSizing 0 13 3
A elems.forEach 0 3 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like dist/coverimage.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
3
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
4
5
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7
var CoverImage = function () {
8
	function CoverImage($el, cb) {
9
		_classCallCheck(this, CoverImage);
10
11
		var _this = this;
12
13
		_this.$el = $el ? jQuery($el) : jQuery(window);
14
		_this.$img = _this.getElementForSizing();
15
		_this.disableOnMobile = _this.$el.data('cover-image-mobile') === false;
16
		_this.cb = cb || function () {
17
			//DEBUG console.log("Default callback");
18
		};
19
		_this.positioning = {
20
			x: 0.5,
21
			y: 0.5
22
		};
23
		_this.options = {
24
			parallax: _this.$el.data('coverImageParallax') === ''
25
		};
26
27
		if (!_this.$img) {
28
			console.log('Error:', 'no image found', _this.$img);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
29
			return;
30
		}
31
32
		_this.imageWidth = _this.$img.attr('width');
33
		_this.imageHeight = _this.$img.attr('height');
34
35
		// If the image doesn't have harcoded width|height
36
		// attributes then load the image to calculate
37
		// the dimensions
38
		if (!_this.imageWidth || !_this.imageHeight) {
39
			console.log('No dimensions found. Generating image');
40
			_this.img = new Image();
1 ignored issue
show
Bug introduced by
The variable Image seems to be never declared. If this is a global, consider adding a /** global: Image */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
			_this.img.src = _this.$img.attr('src');
42
43
			_this.imageWidth = _this.img.width;
44
			_this.imageHeight = _this.img.height;
45
		}
46
47
		if (_this.disableOnMobile && window.innerWidth < 480) {
48
			return;
49
		}
50
51
		_this.elementDimensions = {
52
			height: _this.$el.outerHeight(),
53
			width: _this.$el.outerWidth()
54
		};
55
56
		_this.$el.css({
57
			overflow: 'hidden',
58
			position: 'relative'
59
		});
60
61
		if (!_this.$img.length) {
62
63
			// TODO: Implement load
64
			setTimeout(function () {
65
				new CoverImage(_this.$el);
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new CoverImage(_this.$el) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
66
			}, 1000);
67
		} else {
68
			_this.resizeImage();
69
		}
70
71
		_this.$img.one('load', function () {
72
			console.log('Image loaded:', 'resize');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
73
			_this.resizeImage();
74
		});
75
76
		$(window).on('resize', function () {
77
			_this.resizeImage();
78
		});
79
80
		$(window).on('ci.resize', function () {
81
			_this.resizeImage();
82
		});
83
84
		if (_this.options.parallax) {
85
			_this.draw();
86
		}
87
	}
88
89
	/**
90
  * Parallax FX
91
  *
92
  */
93
94
95
	_createClass(CoverImage, [{
96
		key: 'draw',
97 View Code Duplication
		value: function draw() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
98
			var _this = this;
99
			var friction = 0.5;
100
			var imageOffsetX = document.body.scrollTop * friction;
101
			var imageOffsetY = document.body.scrollTop * friction;
102
			var maximumMovementY = (_this.imageDimensions.height - _this.elementDimensions.height) * _this.positioning.y;
103
			var maximumMovementX = (_this.imageDimensions.width - _this.elementDimensions.width) * _this.positioning.x;
104
105
			if (maximumMovementX > 0) {
106
				if (imageOffsetX < maximumMovementX) {
107
					// console.log('New position:', maximumMovementX - imageOffsetX);
108
					_this.$img.css({
109
						'transform': 'translateX(' + (maximumMovementX - imageOffsetX) + 'px)'
110
					});
111
				}
112
			} else {
113
				if (imageOffsetY < maximumMovementY) {
114
					_this.$img.css('transform', 'translateY(' + (maximumMovementY - imageOffsetY) + 'px)');
115
				}
116
			}
117
118
			window.requestAnimationFrame(function () {
119
				_this.draw();
120
			});
121
		}
122
	}, {
123
		key: 'getElementForSizing',
124
		value: function getElementForSizing() {
125
			var _this = this;
126
			var selector = _this.$el.data('coverImageEl');
127
128
			if (selector) {
129
130
				console.log('Element selector Present', _this.$el.find(selector));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
131
132
				return _this.$el.find(selector) ? _this.$el.find(selector) : _this.$el.find('img');
133
			}
134
135
			return _this.$el.find('img').first();
136
		}
137
	}, {
138
		key: 'resizeImage',
139
		value: function resizeImage() {
140
			var _this = this;
141
			var dimensions = _this.coverDimensions(_this.imageWidth, _this.imageHeight, _this.$el.outerWidth(), _this.$el.outerHeight());
142
143
			_this.imageDimensions = dimensions;
144
145
			if (isNaN(dimensions.width)) {
146
				console.log('Failed to calculate image sizes.');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
147
			}
148
149
			_this.setImageSize();
150
		}
151
	}, {
152
		key: 'setImageSize',
153
		value: function setImageSize() {
154
			var _this = this;
155
156
			_this.$img.attr({
157
				'width': _this.imageDimensions.width,
158
				'height': _this.imageDimensions.height
159
			}).css({
160
				'position': 'absolute',
161
				'width': _this.imageDimensions.width,
162
				'height': _this.imageDimensions.height,
163
				'transform': _this.getTransform((_this.$el.width() - _this.imageDimensions.width) * _this.positioning.y, (_this.$el.outerHeight() - _this.imageDimensions.height) * _this.positioning.x),
164
				'max-width': 'none'
165
			}).data('resrc-width', _this.imageDimensions.width);
166
167
			_this.$img.addClass('ci--sized');
168
169
			_this.cb();
170
		}
171
	}, {
172
		key: 'getTransform',
173
		value: function getTransform(x, y) {
174
			return 'translate3d(' + x + 'px,' + y + 'px,0)';
175
		}
176
	}, {
177
		key: 'coverDimensions',
178
		value: function coverDimensions(child_w, child_h, container_w, container_h) {
179
			var scale_factor = this.max(container_w / child_w, container_h / child_h);
180
181
			return {
182
				width: Math.ceil(child_w * scale_factor),
183
				height: Math.ceil(child_h * scale_factor)
184
			};
185
		}
186
	}, {
187
		key: 'containDimensions',
188
		value: function containDimensions(child_w, child_h, container_w, container_h) {
189
			var scale_factor = this.min(container_w / child_w, container_h / child_h);
190
191
			return {
192
				width: child_w * scale_factor,
193
				height: child_h * scale_factor
194
			};
195
		}
196
	}, {
197
		key: 'min',
198
		value: function min(a, b) {
199
			return a > b ? b : a;
200
		}
201
	}, {
202
		key: 'max',
203
		value: function max(a, b) {
204
			return a > b ? a : b;
205
		}
206
	}]);
207
208
	return CoverImage;
209
}();
210
211
var elems = document.body.querySelectorAll('[data-cover-image]');
212
213
elems.forEach(function (el) {
214
	new CoverImage(jQuery(el));
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new CoverImage(jQuery(el)) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
215
});