Passed
Push — master ( f084fb...8e6af2 )
by Rafael
01:27
created

renderer.js ➔ ???   A

Complexity

Conditions 1
Paths 4

Size

Total Lines 20

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 20
rs 9.4285
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
		// Create a new compiler.
22
		this.sassCompiler = new SassCompiler( this.configs.sass || {} );
23
24
		this.buttonColors = new ButtonColors( {
25
			sassCompiler: this.sassCompiler
26
		} );
27
28
		this.buttonColors.init();
29
30
		this.sassCompiler.preload( this.configs.sassFiles );
31
	}
32
33
	setPaletteSettings( settings ) {
34
		this.palettes = this._createPaletteSetting( settings );
35
		this.formatConfig();
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, settings ) {
88
		let $control, html;
89
90
		this.setPaletteSettings( settings );
91
92
		html = this._createHtml();
93
		$control = $( html );
94
95
		$target.append( html );
96
97
		window.BOLDGRID.COLOR_PALETTE.Modify.init( $control, {
98
			renderer: this
99
		} );
100
101
		this.$control = $control;
102
103
		return $control;
104
	}
105
106
	/**
107
	 * Get the markup needed for the control.
108
	 *
109
	 * @since 1.0.0
110
	 *
111
	 * @param  {object} colorPalettes Color palettes configuration.
112
	 * @return {string}               Control Markup.
113
	 */
114
	_createHtml( 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...
115
		let file = require( '../template.html' );
116
		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...
117
	}
118
119
	/**
120
	 * Given requested settings, create configs that we can use.
121
	 *
122
	 * @since 1.0.0
123
	 *
124
	 * @param  {object} setting Configurations.
125
	 * @return {object}         A full set of configs for the control.
126
	 */
127
	_createPaletteSetting( setting ) {
128
		let colorConfig = new Config();
129
130
		if ( ! setting ) {
131
			setting = colorConfig.createSimpleConfig();
132
		}
133
134
		return $.extend( true, {}, setting );
135
	}
136
}
137
138
export { Renderer as default };
139