-
Notifications
You must be signed in to change notification settings - Fork 0
/
jebgradients.js
259 lines (236 loc) · 12.8 KB
/
jebgradients.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* En este objeto se guardan los estilos de los mensajes y los links de los degradados */
const jebgradients_const = {
link: 'https://raw.githubusercontent.com/ghosh/uiGradients/master/gradients.json',
link_j: 'https://raw.githubusercontent.com/jebbarbas/jebgradients/master/gradients.json',
cssSuccess: 'color:lime;font-weight:bold;font-size:1rem;display:block',
cssSuccessText: 'color:lightgreen;display:block',
cssError: 'color:red;font-weight:bold;font-size:1rem;display:block',
cssErrorText: 'color:pink;display:block',
cssWarn: 'color:gold;font-weight:bold;font-size:1rem;display:block',
cssWarnText: 'color:lightyellow;display:block',
};
/* Con esta clase se harán todas las 'operaciones' de degradados */
class Gradient{
constructor(type = '', direction = '', colors = 'transparent,transparent', repeat = false){
this.type = this.validateType(type);
this.direction = this.validateDirection(direction,this.type);
this.colors = this.validateColors(colors);
this.repeat = this.validateRepeat(repeat);
this.prefixes = ['','-moz-','-webkit-','-o-'];
};
/* Se asegura que el tipo solo sea o 'linear' o 'radial' */
validateType = (type = '') => {
type = type.trim();
if(type === 'linear' || type === 'radial' || type === 'preradial'){ return type; }
else { return 'linear' }
};
/* Se asegura se que solo haya direcciones que empiecen con 'to ' o que terminen en 'deg' */
validateDirection = (direction = '', type = '') => {
direction = direction.trim();
if(type === 'linear'){
if(direction.startsWith('to ')){ return direction; }
else if(direction.endsWith('deg')){ return direction; }
else {return `to ${direction}`; }
}
else if(type === 'radial'){
/* Este if verifica si la dirección es la que se agrega por default en la funcion jebgradients_apply(); */
if(direction === 'right'){
return 'circle'
}
else{
return direction;
}
}
else if(type === 'preradial'){
/* Aqui se hace un switch en base al estilo predefinido */
switch(direction){
case 'top-left': return 'at 0% 0%';
case 'top-center': return 'at 50% 0%';
case 'top-right': return 'at 100% 0%';
case 'center-left': return 'at 0% 50%';
case 'center-center': return 'at 50% 50%';
case 'center-right': return 'at 100% 50%';
case 'bottom-left': return 'at 0% 100%';
case 'bottom-center': return 'at 50% 100%';
case 'bottom-right': return 'at 100% 100%';
default: return 'at 0% 0%';
}
}
};
/* Valida los colores */
validateColors = (colors = '') => {
colors = colors.trim();
let arrayColors = colors.split(',');
if(colors.startsWith(',')){
arrayColors.shift();
}
if(colors.endsWith(',')){
arrayColors.pop();
}
arrayColors = arrayColors.map(color => color.trim());
return arrayColors.join();
};
/* Verifica si existe repeat o no */
validateRepeat = (repeat = false) => {
if(repeat){ return 'repeating-'}
else {return ''};
};
/* En base al tipo regresa un array de strings diferente */
gradient = () => {
let gradientArray = [];
if(this.type === 'linear'){
/* Se hace un estilo para cada navegador de acuerdo a this.prefixes */
this.prefixes.forEach(prefix => {
gradientArray.push(`${prefix}${this.repeat}linear-gradient(${this.direction},${this.colors})`);
});
}
else if(this.type === 'radial' || this.type === 'preradial'){
this.prefixes.forEach(prefix => {
gradientArray.push(`${prefix}${this.repeat}radial-gradient(${this.direction},${this.colors})`);
});
}
return gradientArray;
};
/* Funcion para aplicar el degradado a un elemento */
static apply = (element,objGradient = new Gradient()) => {
/* Se crea un array con todos los posibles estilos en base a los prefijos */
const backgroundArray = objGradient.gradient();
/* Para cada prefijo se le asigna al background del elemento
si la propiedad es invalida, no se aplica, BTW: solo se aplica 1 */
backgroundArray.forEach(background => {
element.style.background = background;
});
};
/* Sirve para obtener el background de un elemento en los logs de random */
static getBackground = element => {
return `background:${element.style.background}`;
};
}
/* Funcion que aplica los degradados a cada elemento con la clase jebg
Tambien usa el JSON de ui-gradients para aplicar un degradado de estos */
const jebgradients_apply = () => {
/* Funcion que busca entre un element todos los atributos de color, excepto uno(only)
devuelve true si no existen los demas y false si se encuentran */
let totalErrors = 0;
const isOnlyIn = (only = '',element) => {
if(only == 'jebg-ui'){
if(!element.getAttribute('jebg-colors') && !element.getAttribute('jebg-grad')){
return true;
}
return false;
}
if(only == 'jebg-colors'){
if(!element.getAttribute('jebg-ui') && !element.getAttribute('jebg-grad')){
return true;
}
return false;
}
if(only == 'jebg-grad'){
if(!element.getAttribute('jebg-colors') && !element.getAttribute('jebg-ui')){
return true;
}
return false;
}
return true;
};
const allElementsWithGradients = document.querySelectorAll('*[jebg-colors],*[jebg-grad],*[jebg-ui]');
allElementsWithGradients.forEach(htmlEl => {
/* Verifica que atributo de color tiene */
if(isOnlyIn('jebg-colors',htmlEl)){
/* Se guardan los valores de los atributos del elemento */
const j_type = htmlEl.getAttribute('jebg-type') || 'linear';
const j_dir = htmlEl.getAttribute('jebg-dir') || 'right';
const j_colors = htmlEl.getAttribute('jebg-colors') || 'transparent';
const j_repeat = (htmlEl.getAttribute('jebg-repeat')) ? true : false;
const gradient = new Gradient(j_type,j_dir,j_colors,j_repeat);
Gradient.apply(htmlEl,gradient);
}
else if(isOnlyIn('jebg-grad',htmlEl)){
const j_type = htmlEl.getAttribute('jebg-type') || 'linear';
const j_dir = htmlEl.getAttribute('jebg-dir') || 'right';
const j_jebName = htmlEl.getAttribute('jebg-grad').trim() || 'null';
const j_repeat = (htmlEl.getAttribute('jebg-repeat')) ? true : false;
/* Verifica si se ha especificado un nombre, sino, aplica un degradado random */
if(j_jebName !== '$random$'){
fetch(jebgradients_const.link_j).then(result => result.json()).then(jebGradients => {
let jebGradient = jebGradients.find(gradient => gradient.name == j_jebName);
if(jebGradient){
let jebColors = jebGradient.colors.join();
const gradient = new Gradient(j_type,j_dir,jebColors,j_repeat);
Gradient.apply(htmlEl,gradient);
}
else{
totalErrors++;
console.error(`%cError❌ %cCannot find the jebGradient named: "${j_jebName}" in %c${htmlEl.outerHTML}. %cAre you sure that jebGradient exists and its well written?`,jebgradients_const.cssError,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText);
}
}).catch(err => {
totalErrors++;
console.error(`%cError❌ %c${err}, are you connected to internet and you can connect to ${jebgradients_const.link_j}? %c${htmlEl.outerHTML}`,jebgradients_const.cssError,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText);
});
}
else{
fetch(jebgradients_const.link_j).then(result => result.json()).then(jebGradients => {
let randomNumber = Math.floor(Math.random() * jebGradients.length );
let randomGradient = jebGradients[randomNumber];
let jebColors = randomGradient.colors.join();
const gradient = new Gradient(j_type,j_dir,jebColors,j_repeat);
Gradient.apply(htmlEl,gradient);
console.log(`%c${randomGradient.name}`,'padding:.5rem;'+Gradient.getBackground(htmlEl));
}).catch(err => {
totalErrors++;
console.error(`%cError❌ %c${err}, are you connected to internet and you can connect to ${jebgradients_const.link_j}? %c${htmlEl.outerHTML}`,jebgradients_const.cssError,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText);
});
}
}
else if(isOnlyIn('jebg-ui',htmlEl)){
const j_type = htmlEl.getAttribute('jebg-type') || 'linear';
const j_dir = htmlEl.getAttribute('jebg-dir') || 'right';
const j_uiName = htmlEl.getAttribute('jebg-ui').trim() || 'null';
const j_repeat = (htmlEl.getAttribute('jebg-repeat')) ? true : false;
/* Verifica si se ha especificado un nombre, sino, aplica un degradado random */
if(j_uiName !== '$random$'){
fetch(jebgradients_const.link).then(result => result.json()).then(uiGradients => {
let uiGradient = uiGradients.find(gradient => gradient.name == j_uiName);
if(uiGradient){
let uiColors = uiGradient.colors.join();
const gradient = new Gradient(j_type,j_dir,uiColors,j_repeat);
Gradient.apply(htmlEl,gradient);
}
else{
totalErrors++;
console.error(`%cError❌ %cCannot find the uiGradient named: "${j_uiName}" in %c${htmlEl.outerHTML}. %cAre you sure that uiGradient exists and its well written?`,jebgradients_const.cssError,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText);
}
}).catch(err => {
totalErrors++;
console.error(`%cError❌ %c${err}, are you connected to internet and you can connect to ${jebgradients_const.link}? %c${htmlEl.outerHTML}`,jebgradients_const.cssError,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText);
});
}
else{
fetch(jebgradients_const.link).then(result => result.json()).then(uiGradients => {
let randomNumber = Math.floor(Math.random() * uiGradients.length );
let randomGradient = uiGradients[randomNumber];
let uiColors = randomGradient.colors.join();
const gradient = new Gradient(j_type,j_dir,uiColors,j_repeat);
Gradient.apply(htmlEl,gradient);
console.log(`%c${randomGradient.name}`,'padding:.5rem;'+Gradient.getBackground(htmlEl));
}).catch(err => {
totalErrors++;
console.error(`%cError❌ %c${err}, are you connected to internet and you can connect to ${jebgradients_const.link}? %c${htmlEl.outerHTML}`,jebgradients_const.cssError,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText);
});
}
}
else{
totalErrors++;
console.error(`%cError❌ %cThis element contains two or more of this color-attributes: jebg-colors, jebg-ui or jebg-grad. Please write only one of these in %c${htmlEl.outerHTML}`,jebgradients_const.cssError,jebgradients_const.cssErrorText,jebgradients_const.cssErrorText);
}
});
/* Mensaje para avisar que los degradados han sido aplicados o ha habido errores */
if(totalErrors == 0){
console.log(`%cJebGradients Applied✔️ %cUse jebgradients_apply(); to recharge them. See the repo and documentation in: 'https://github.com/JebBarbas/jebgradients' `,jebgradients_const.cssSuccess,jebgradients_const.cssSuccessText);
}
else{
console.warn(`%cJebGradients Failed⚠️ %cThere are ${totalErrors} error(s), please check and solve them. See the repo and documentation in: 'https://github.com/JebBarbas/jebgradients'`,jebgradients_const.cssWarn,jebgradients_const.cssWarnText);
}
};
/* Realizar la función jebgradients_apply() una vez que el DOM esté cargado */
document.addEventListener('DOMContentLoaded',jebgradients_apply);