Passed
Push — master ( c7474b...dcc88d )
by Rafael
01:47
created

button.js ➔ ???   B

Complexity

Conditions 2
Paths 4

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
nc 4
dl 0
loc 39
rs 8.8571
cc 2
nop 1
1
import { SassCompiler } from '../../style/js/sass-compiler';
2
import { Generate } from './generate.js';
3
4
export class Button {
5
6
	constructor( options ) {
7
		options = options || {};
8
9
		this.files = [
10
			'color-palette-scss/buttons/buttons.scss',
11
			'color-palette-scss/buttons/_options.scss',
12
			'color-palette-scss/buttons/types/_3d.scss',
13
			'color-palette-scss/buttons/types/_border.scss',
14
			'color-palette-scss/buttons/types/_borderless.scss',
15
			'color-palette-scss/buttons/types/_dropdown.scss',
16
			'color-palette-scss/buttons/types/_glow.scss',
17
			'color-palette-scss/buttons/types/_groups.scss',
18
			'color-palette-scss/buttons/types/_longshadow.scss',
19
			'color-palette-scss/buttons/types/_raised.scss',
20
			'color-palette-scss/buttons/types/_shapes.scss',
21
			'color-palette-scss/buttons/types/_sizes.scss',
22
			'color-palette-scss/buttons/types/_wrapper.scss',
23
			'color-palette-scss/buttons/_layout.scss',
24
			'color-palette-scss/buttons/_base.scss',
25
			'color-palette-scss/buttons/_mixins.scss'
26
		];
27
28
		this.namespace = '.btn';
29
30
		/**
31
		 * Default colors to add to the compiled list of button colors.
32
		 * @type {Array}
33
		 */
34
		this._defaultColors = [
35
			{ 'name': 'color-dark', val: '#252525' },
36
			{ 'name': 'color-light', val: '#eff0f1' }
37
		];
38
39
		if ( ! options.sassCompiler ) {
40
			this.sassCompiler = new SassCompiler();
41
		} else {
42
			this.sassCompiler = options.sassCompiler;
43
		}
44
	}
45
46
	init() {
47
		return this.preload();
48
	}
49
50
	preload() {
51
		return this.sassCompiler.preload( this.files );
52
	}
53
54
	/**
55
	 * Given a color palette state, create a list of colors to be passed into the sass.
56
	 *
57
	 * @since 1.0.0
58
	 *
59
	 * @param  {object} state Color palette control state of modifications.
60
	 * @return {array}        Array of colors config objects.
61
	 */
62
	convertColorState( state ) {
63
		let config = [],
64
			neutral = state.palettes['palette-primary']['neutral-color'];
65
66
		for ( let [ index, color ] of state.palettes['palette-primary'].colors.entries() ) {
67
			config.push( {
68
				name: 'color-' + ( index + 1 ),
69
				val: color
70
			} );
71
		}
72
73
		for ( let color of this._defaultColors ) {
74
			config.push( color );
75
		}
76
77
		if ( neutral ) {
78
			config.push( {
79
				name: 'color-neutral',
80
				val: neutral
81
			} );
82
		}
83
84
		return config;
85
	}
86
87
	/**
88
	 * Given a list of colors, create a SASS variable to pass on to library.
89
	 *
90
	 * @since 1.0.0
91
	 *
92
	 * @param  {array} colors Colors config array.
93
	 * @return {string}       Formartted sass definition.
94
	 */
95
	formatColorSass( colors ) {
96
		let contrast,
97
			paletteGenerate = new Generate(),
98
			variablesString = '';
99
100
		colors = colors || [];
101
102
		for ( let color of colors ) {
103
			contrast = paletteGenerate.getContrast( color.val );
104
			variablesString += '(\'' + color.name + '\' ' + color.val + ' ' + contrast + ')';
105
		}
106
107
		if ( variablesString ) {
108
			variablesString = '$ubtn-colors: ' + variablesString + ';';
109
		}
110
111
		return variablesString;
112
	}
113
114
	/**
115
	 * Return the scss string needed to compile the button library.
116
	 *
117
	 * @since. 1.0.0
118
	 *
119
	 * @param  {object} colorsState State of the color compiler.
120
	 * @return {string}             SCSS.
121
	 */
122
	getCompileString( colorsState ) {
123
		let variablesString = this.formatColorSass( this.convertColorState( colorsState ) ),
124
			namespaceString = '$ubtn-namespace: \'' + this.namespace + '\';',
125
			compileString = namespaceString + variablesString + '@import "color-palette-scss/buttons/buttons.scss";';
126
127
		return compileString;
128
	}
129
130
	/**
131
	 * Compile the colors lib.
132
	 *
133
	 * @since 1.0.0
134
	 *
135
	 * @param  {object}   options Configurations for the compile operation.
136
	 * @param  {Function} cb      Callback for the complete process.
137
	 */
138
	compile( options, cb ) {
139
		this.sassCompiler.compiler.compile( compileString, ( result ) => {
0 ignored issues
show
Bug introduced by
The variable compileString seems to be never declared. If this is a global, consider adding a /** global: compileString */ 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...
140
			cb( result );
141
		} );
142
	}
143
144
}
145
146
export { Button as default };
147