Passed
Push — master ( b15b71...ab24e8 )
by Paul
04:46
created

+/scripts/public/excerpts.js   A

Complexity

Total Complexity 6
Complexity/F 1.2

Size

Lines of Code 51
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 2
dl 0
loc 51
rs 10
wmc 6
mnd 1
bc 6
fnc 5
bpm 1.2
cpm 1.2
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A Excerpts.createLinks_ 0 11 1
A Excerpts.init_ 0 6 2
A Excerpts.onClick_ 0 10 1
A excerpts.js ➔ Excerpts 0 3 1
1
/** global: GLSR */
2
;(function() {
3
4
	'use strict';
5
6
	var Excerpts = function( el ) { // HTMLElement
7
		this.init_( el || document );
8
	};
9
10
	Excerpts.prototype = {
11
		config: {
12
			hiddenClass: 'glsr-hidden',
13
			hiddenTextSelector: '.glsr-hidden-text',
14
			readMoreClass: 'glsr-read-more',
15
			visibleClass: 'glsr-visible',
16
		},
17
18
		/** @return void */
19
		createLinks_: function( el ) { // HTMLElement
20
			var readMoreSpan = document.createElement( 'span' );
21
			var readmoreLink = document.createElement( 'a' );
22
			readmoreLink.setAttribute( 'href', '#' );
23
			readmoreLink.setAttribute( 'data-text', el.getAttribute( 'data-show-less' ));
24
			readmoreLink.innerHTML = el.getAttribute( 'data-show-more' );
25
			readmoreLink.addEventListener( 'click', this.onClick_.bind( this ));
26
			readMoreSpan.setAttribute( 'class', this.config.readMoreClass );
27
			readMoreSpan.appendChild( readmoreLink );
28
			el.parentNode.insertBefore( readMoreSpan, el.nextSibling );
29
		},
30
31
		/** @return void */
32
		onClick_: function( ev ) { // MouseEvent
33
			ev.preventDefault();
34
			var el = ev.target;
35
			var hiddenNode = el.parentNode.previousSibling;
36
			var text = el.getAttribute( 'data-text' );
37
			hiddenNode.classList.toggle( this.config.hiddenClass );
38
			hiddenNode.classList.toggle( this.config.visibleClass );
39
			el.setAttribute( 'data-text', el.innerText );
40
			el.innerText = text;
41
		},
42
43
		init_: function( el ) { // HTMLElement
44
			var excerpts = el.querySelectorAll( this.config.hiddenTextSelector );
45
			for( var i = 0; i < excerpts.length; i++ ) {
46
				this.createLinks_( excerpts[i] );
47
			}
48
		},
49
	};
50
51
	GLSR.Excerpts = Excerpts;
52
})();
53