Passed
Push — master ( c9127b...c81131 )
by Rafael
01:07
created

renderer.js ➔ ???   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
nc 4
dl 0
loc 25
rs 8.8571
cc 1
nop 1
1
var $ = window.jQuery;
2
3
import '../scss/control.scss';
4
import SampleConfig from './sampleConfig.js';
5
import './control.js';
6
import { Config } from './config.js';
7
import { Button as ButtonColors } from './button.js';
8
import { SassCompiler } from '../../style/js/sass-compiler.js';
9
10
export class Renderer {
11
12
	constructor( configs ) {
13
		this.configs = _.defaults( configs || {}, {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ 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...
14
			sassFiles: [],
15
			defaultSassFiles: [ 'color-palette-scss/classes/color-classes.scss' ]
16
		} );
17
18
		// Merge all sass files.
19
		this.configs.sassFiles = this.configs.sassFiles.concat( this.configs.defaultSassFiles );
20
21
		// Clone object to prevent modification of the original.
22
		this.palettes = this._getPaletteSetting( this.configs.paletteSettings );
23
24
		this.formatConfig();
25
26
		// Create a new compiler.
27
		this.sassCompiler = new SassCompiler( this.configs.sass || {} );
28
29
		this.buttonColors = new ButtonColors( {
30
			sassCompiler: this.sassCompiler
31
		} );
32
33
		this.buttonColors.init();
34
35
		this.sassCompiler.preload( this.configs.sassFiles );
36
	}
37
38
	/**
39
	 * Update the configuration given to set derived values.
40
	 *
41
	 * @since 1.0.0
42
	 */
43
	formatConfig() {
44
		this.palettes.hasNeutralColor = this.palettes.palettes[0]['neutral-color'] ? 1 : 0;
45
		this.palettes.colorPaletteColumns = this.palettes['color-palette-size'] + this.palettes.hasNeutralColor;
46
		this.assignNeutral();
47
	}
48
49
	/**
50
	 * For each palette, move the neutral-color into the array of colors.
51
	 *
52
	 * @since 1.0.0
53
	 */
54
	assignNeutral() {
55
		for ( let palette of this.palettes.palettes ) {
56
			if ( palette['neutral-color'] ) {
57
				palette.colors.push( palette['neutral-color'] );
58
			}
59
		}
60
	}
61
62
	/**
63
	 * Import all specified files.
64
	 *
65
	 * @since 1.0.0
66
	 *
67
	 * @return {string} An import for each given file.
68
	 */
69
	getImportString() {
70
		let string = '';
71
72
		for ( let file of this.configs.sassFiles ) {
73
			string += '@import "' + file + '";';
74
		}
75
76
		return string;
77
	}
78
79
	/**
80
	 * Render the control to a given target.
81
	 *
82
	 * @since 1.0.0
83
	 *
84
	 * @param  {jQuery} $target Location to put the control.
85
	 * @return {jQuery}         Control Element.
86
	 */
87
	render( $target ) {
88
		let html = this.getHtml(),
89
			$control = $( html );
90
91
		$target.append( html );
92
93
		window.BOLDGRID.COLOR_PALETTE.Modify.init( $control, {
94
			renderer: this
95
		} );
96
97
		this.$control = $control;
98
99
		return $control;
100
	}
101
102
	/**
103
	 * Get the markup needed for the control.
104
	 *
105
	 * @since 1.0.0
106
	 *
107
	 * @param  {object} colorPalettes Color palettes configuration.
108
	 * @return {string}               Control Markup.
109
	 */
110
	getHtml( colorPalettes ) {
0 ignored issues
show
Unused Code introduced by
The parameter colorPalettes is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
111
		let file = require( '../template.html' );
112
		return _.template( file )( { config: this.palettes } );
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ 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...
113
	}
114
115
	/**
116
	 * Given requested settings, create configs that we can use.
117
	 *
118
	 * @since 1.0.0
119
	 *
120
	 * @param  {object} setting Configurations.
121
	 * @return {object}         A full set of configs for the control.
122
	 */
123
	_getPaletteSetting( setting ) {
124
		let colorConfig = new Config();
125
126
		if ( ! setting ) {
127
			setting = colorConfig.createSimpleConfig();
128
		}
129
130
		return $.extend( true, {}, setting );
131
	}
132
}
133
134
export { Renderer as default };
135