Completed
Pull Request — master (#1710)
by Aristeides
18:06 queued 08:13
created

controls/js/src/kirki.util.js   A

Complexity

Total Complexity 24
Complexity/F 2

Size

Lines of Code 230
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 48
dl 0
loc 230
rs 10
c 0
b 0
f 0
wmc 24
mnd 1
bc 21
fnc 12
bpm 1.75
cpm 2
noi 14
1
/* global ajaxurl */
2
var kirki = kirki || {};
3
kirki = jQuery.extend( kirki, {
4
	/**
5
	 * A collection of utility methods.
6
	 *
7
	 * @since 3.0.17
8
	 */
9
	util: {
10
11
		/**
12
		 * A collection of utility methods for webfonts.
13
		 *
14
		 * @since 3.0.17
15
		 */
16
		webfonts: {
17
18
			/**
19
			 * Google-fonts related methods.
20
			 *
21
			 * @since 3.0.17
22
			 */
23
			google: {
24
25
				/**
26
				 * An object containing all Google fonts.
27
				 *
28
				 * to set this call this.setFonts();
29
				 *
30
				 * @since 3.0.17
31
				 */
32
				fonts: {},
33
34
				/**
35
				 * Init for google-fonts.
36
				 *
37
				 * @since 3.0.17
38
				 * @returns {null}
39
				 */
40
				initialize: function() {
41
					var self = this;
42
43
					self.setFonts();
44
				},
45
46
				/**
47
				 * Set fonts in this.fonts
48
				 *
49
				 * @since 3.0.17
50
				 * @returns {null}
51
				 */
52
				setFonts: function() {
53
					var self = this;
54
55
					// No need to run if we already have the fonts.
56
					if ( ! _.isEmpty( self.fonts ) ) {
57
						return;
58
					}
59
60
					// Make an AJAX call to set the fonts object (alpha).
61
					jQuery.post( ajaxurl, { 'action': 'kirki_fonts_google_all_get' }, function( response ) {
62
63
						// Get fonts from the JSON array.
64
						self.fonts = JSON.parse( response );
65
					} );
66
				},
67
68
				/**
69
				 * Gets all properties of a font-family.
70
				 *
71
				 * @since 3.0.17
72
				 * @param {string} family - The font-family we're interested in.
73
				 * @returns {Object}
74
				 */
75
				getFont: function( family ) {
76
					var self = this,
77
					    fonts = self.getFonts();
78
79
					if ( 'undefined' === typeof fonts[ family ] ) {
80
						return false;
81
					}
82
					return fonts[ family ];
83
				},
84
85
				/**
86
				 * Gets all properties of a font-family.
87
				 *
88
				 * @since 3.0.17
89
				 * @param {string} order - How to order the fonts (alpha|popularity|trending).
90
				 * @param {int}    number - How many to get. 0 for all.
91
				 * @returns {Object}
92
				 */
93
				getFonts: function( order, number ) {
94
					var self    = this,
95
					    ordered = {},
96
					    partial = [];
97
98
					// Make sure order is correct.
99
					order  = order || 'alpha';
100
					order  = ( 'alpha' !== order && 'popularity' !== order && 'trending' !== order ) ? 'alpha' : order;
101
102
					// Make sure number is correct.
103
					number = number || 0;
104
					number = parseInt( number, 10 );
105
106
					if ( 'alpha' === order || 0 === number ) {
107
						ordered = self.fonts.items;
108
					} else {
109
						partial = _.first( self.fonts.order[ order ], number );
110
						_.each( partial, function( family ) {
111
							ordered[ family ] = self.fonts.items[ family ];
112
						} );
113
					}
114
115
					return ordered;
116
				},
117
118
				/**
119
				 * Gets the variants for a font-family.
120
				 *
121
				 * @since 3.0.17
122
				 * @param {string} family - The font-family we're interested in.
123
				 * @returns {Array}
124
				 */
125
				getVariants: function( family ) {
126
					var self = this,
127
					    font = self.getFont( family );
128
129
					// Early exit if font was not found.
130
					if ( ! font ) {
131
						return false;
132
					}
133
134
					// Early exit if font doesn't have variants.
135
					if ( _.isUndefined( font.variants ) ) {
136
						return false;
137
					}
138
139
					// Return the variants.
140
					return font.variants;
141
				},
142
143
				/**
144
				 * Get the subsets for a font-family.
145
				 *
146
				 * @since 3.0.17
147
				 * @param {string} family - The font-family we're interested in.
148
				 * @returns {Object}
149
				 */
150
				getSubsets: function( family ) {
151
					var self = this,
152
					    font = self.getFont( family );
153
154
					// Early exit if font was not found.
155
					if ( ! font ) {
156
						return false;
157
					}
158
159
					// Early exit if font doesn't have subsets.
160
					if ( _.isUndefined( font.subsets ) ) {
161
						return false;
162
					}
163
164
					// Return the variants.
165
					return font.subsets;
166
				}
167
			},
168
169
			/**
170
			 * Standard fonts related methods.
171
			 *
172
			 * @since 3.0.17
173
			 */
174
			standard: {
175
176
				/**
177
				 * An object containing all Standard fonts.
178
				 *
179
				 * to set this call this.setFonts();
180
				 *
181
				 * @since 3.0.17
182
				 */
183
				fonts: {},
184
185
				/**
186
				 * Init for google-fonts.
187
				 *
188
				 * @since 3.0.17
189
				 * @returns {null}
190
				 */
191
				initialize: function() {
192
					var self = this;
193
194
					self.setFonts();
195
				},
196
197
				/**
198
				 * Set fonts in this.fonts
199
				 *
200
				 * @since 3.0.17
201
				 * @returns {null}
202
				 */
203
				setFonts: function() {
204
					var self = this;
205
206
					// No need to run if we already have the fonts.
207
					if ( ! _.isEmpty( self.fonts ) ) {
208
						return;
209
					}
210
211
					// Make an AJAX call to set the fonts object.
212
					jQuery.post( ajaxurl, { 'action': 'kirki_fonts_standard_all_get' }, function( response ) {
213
214
						// Get fonts from the JSON array.
215
						self.fonts = JSON.parse( response );
216
					} );
217
				},
218
219
				/**
220
				 * Gets the variants for a font-family.
221
				 *
222
				 * @since 3.0.17
223
				 * @returns {Array}
224
				 */
225
				getVariants: function( family ) { // jshint ignore: line
226
					return ['regular', 'italic', '700', '700italic'];
227
				}
228
			},
229
230
			/**
231
			 * Figure out what this font-family is (google/standard)
232
			 *
233
			 * @since 3.0.20
234
			 * @param {string} family - The font-family.
235
			 * @returns {string|false} - Returns string if found (google|standard)
236
			 *                           and false in case the font-family is an arbitrary value
237
			 *                           not found anywhere in our font definitions.
238
			 */
239
			getFontType: function( family ) {
240
				var self = this;
241
242
				// Check for standard fonts first.
243
				if (
244
					'undefined' !== typeof self.standard.fonts[ family ] || (
245
						'undefined' !== typeof self.standard.fonts.stack &&
246
						'undefined' !== typeof self.standard.fonts.stack[ family ]
247
					)
248
				) {
249
					return 'standard';
250
				}
251
252
				// Check in googlefonts.
253
				if ( 'undefined' !== typeof self.google.fonts.items[ family ] ) {
254
					return 'google';
255
				}
256
				return false;
257
			}
258
		}
259
	}
260
} );
261