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