-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRibsCore.js
168 lines (141 loc) · 4.34 KB
/
RibsCore.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
class RibsCore {
/**
* function that return height of an element that is no displayed
* @param element
* @returns {Number}
*/
static getHeight(element) {
//if max height get value and delete if for a moment
const maxHeight = window.getComputedStyle(element).getPropertyValue('max-height');
if (maxHeight !== "none") {
element.style.maxHeight = 'inherit';
}
element.style.display = 'block';
let height = parseInt(window.getComputedStyle(element).getPropertyValue('height'));
element.style.display = '';
if (maxHeight !== "none") {
element.style.maxHeight = maxHeight;
}
return height;
}
/**
* function that return width of an element that is not displayed
* @param element
* @returns {number}
*/
static getWidth(element) {
element.style.display = 'block';
const width = parseInt(window.getComputedStyle(element).getPropertyValue('width'));
element.style.display = '';
return width;
}
/**
* this method do animation on height when displaying an element
* if max height = none it show the element else this function hide it
* @param element
* @param duration
*/
static toggleSlide(element, duration) {
let maxHeight = 0;
const maxHeightDiv = window.getComputedStyle(element).getPropertyValue('max-height');
if (maxHeightDiv === 'none' || maxHeightDiv === '0px') {
maxHeight = this.getHeight(element);
}
element.style.transition = `max-height ${duration/1000}s ease-in-out, padding ${duration/1000}s ease-in-out`;
element.style.maxHeight = 0;
element.style.display = 'block';
element.style.overflow = 'hidden';
if (maxHeight === 0) {
element.style.padding = '0px';
} else {
element.style.removeProperty('padding');
}
setTimeout(function () {
element.style.maxHeight = `${maxHeight}px`;
}, 10);
}
/**
* add an element arround a specified element. This function is like .wrap() in jQuery
* @param element
* @param newElement
*/
static wrap(element, newElement, className = null) {
const parentElement = element.parentNode;
const wrapper = document.createElement(newElement);
wrapper.className = className;
parentElement.insertBefore(wrapper, element);
parentElement.removeChild(element);
wrapper.appendChild(element);
}
/**
* method to get a wanted parent in parentsNodes of an element
* @param element
* @param wanted
* @returns {*}
*/
static parents(element, wanted) {
for ( ; element && element !== document; element = element.parentNode) {
if (this.checkWanted(wanted) === 'class' && element.classList.contains(wanted.split('.')[1])) {
return element;
} else if (this.checkWanted(wanted) === 'id' && element.id === wanted.split('#')[1]) {
return element;
}
}
return null;
}
/**
* method to test if a wanter element is a class or an id
* @param wanted
* @returns {*}
*/
static checkWanted(wanted) {
if (wanted.indexOf('.') !== -1) {
return 'class';
} else if (wanted.indexOf('#') !== -1) {
return 'id';
}
return null;
}
/**
* method to validate a mail address
* @param mail
* @returns {boolean}
*/
static validateMail(mail) {
const regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regex.test(mail);
}
/**
* method to test strength of a given password
* @param password
* @param strength
* @param length
* @returns {boolean}
*/
static testPasswordStrength(password, strength = 4, length = 8) {
let strengthValue = 0;
const strengths = [
/(?=.*[a-z])/,
/(?=.*[A-Z])/,
/(?=.*[0-9])/,
/(?=.[!@#$%\^&*])/,
];
for (const strengthReg of strengths) {
if (strengthReg.test(password)) {
strengthValue += 100/strengths.length;
}
}
this.passwordStrength = strengthValue;
if (password.length < length) {
this.passwordError = `You password must contain at least ${length} caracters`;
return false;
}
if (strengthValue >= (strength*(100/strengths.length))) {
return true;
} else {
this.passwordError = `You password is not enough secure`;
return false;
}
}
}
export default (RibsCore);