Replace lodash methods with native ones.

This commit is contained in:
JC Brand 2020-03-06 14:38:32 +01:00
parent b6b00887e4
commit 91243f64af

View File

@ -4,7 +4,7 @@
* @description This is the DOM/HTML utilities module.
*/
import URI from "urijs";
import _ from "../headless/lodash.noconflict";
import { get, isFunction } from "lodash";
import log from '@converse/headless/log';
import sizzle from "sizzle";
import tpl_audio from "../templates/audio.js";
@ -233,10 +233,7 @@ u.calculateElementHeight = function (el) {
/* Return the height of the passed in DOM element,
* based on the heights of its children.
*/
return _.reduce(
el.children,
(result, child) => result + child.offsetHeight, 0
);
return Array.from(el.children).reduce((result, child) => result + child.offsetHeight, 0);
}
u.getNextElement = function (el, selector='*') {
@ -307,10 +304,10 @@ u.removeElement = function (el) {
return el;
}
u.showElement = _.flow(
_.partial(u.removeClass, 'collapsed'),
_.partial(u.removeClass, 'hidden')
)
u.showElement = el => {
u.removeClass('collapsed', el);
u.removeClass('hidden', el);
}
u.hideElement = function (el) {
(el instanceof Element) && el.classList.add('hidden');
@ -410,16 +407,11 @@ u.addHyperlinks = function (text) {
u.slideInAllElements = function (elements, duration=300) {
return Promise.all(
_.map(
elements,
_.partial(u.slideIn, _, duration)
));
return Promise.all(Array.from(elements).map(e => u.slideIn(e, duration)));
};
u.slideToggleElement = function (el, duration) {
if (_.includes(el.classList, 'collapsed') ||
_.includes(el.classList, 'hidden')) {
if (u.hasClass('collapsed', el) || u.hasClass('hidden', el)) {
return u.slideOut(el, duration);
} else {
return u.slideIn(el, duration);
@ -499,7 +491,7 @@ u.slideIn = function (el, duration=200) {
const err = "An element needs to be passed in to slideIn";
log.warn(err);
return reject(new Error(err));
} else if (_.includes(el.classList, 'collapsed')) {
} else if (u.hasClass('collapsed', el)) {
return resolve(el);
} else if (window.converse_disable_effects) { // Effects are disabled (for tests)
el.classList.add('collapsed');
@ -541,7 +533,7 @@ u.slideIn = function (el, duration=200) {
function afterAnimationEnds (el, callback) {
el.classList.remove('visible');
if (_.isFunction(callback)) {
if (isFunction(callback)) {
callback();
}
}
@ -570,12 +562,12 @@ u.fadeIn = function (el, callback) {
el.classList.remove('hidden');
return afterAnimationEnds(el, callback);
}
if (_.includes(el.classList, 'hidden')) {
if (u.hasClass('hidden', el)) {
el.classList.add('visible');
el.classList.remove('hidden');
el.addEventListener("webkitAnimationEnd", _.partial(afterAnimationEnds, el, callback));
el.addEventListener("animationend", _.partial(afterAnimationEnds, el, callback));
el.addEventListener("oanimationend", _.partial(afterAnimationEnds, el, callback));
el.addEventListener("webkitAnimationEnd", () => afterAnimationEnds(el, callback));
el.addEventListener("animationend", () => afterAnimationEnds(el, callback));
el.addEventListener("oanimationend", () => afterAnimationEnds(el, callback));
} else {
afterAnimationEnds(el, callback);
}
@ -594,22 +586,16 @@ u.xForm2webForm = function (field, stanza, options) {
if (field.getAttribute('type') === 'list-single' ||
field.getAttribute('type') === 'list-multi') {
const values = _.map(
u.queryChildren(field, 'value'),
_.partial(_.get, _, 'textContent')
);
const options = _.map(
u.queryChildren(field, 'option'),
function (option) {
const value = _.get(option.querySelector('value'), 'textContent');
return tpl_select_option({
'value': value,
'label': option.getAttribute('label'),
'selected': _.includes(values, value),
'required': !!field.querySelector('required')
})
}
);
const values = u.queryChildren(field, 'value').map(el => get(el, 'textContent'));
const options = u.queryChildren(field, 'option').map(option => {
const value = get(option.querySelector('value'), 'textContent');
return tpl_select_option({
'value': value,
'label': option.getAttribute('label'),
'selected': values.includes(value),
'required': !!field.querySelector('required')
});
});
return tpl_form_select({
'id': u.getUniqueId(),
'name': field.getAttribute('var'),
@ -619,13 +605,13 @@ u.xForm2webForm = function (field, stanza, options) {
'required': !!field.querySelector('required')
});
} else if (field.getAttribute('type') === 'fixed') {
const text = _.get(field.querySelector('value'), 'textContent');
const text = get(field.querySelector('value'), 'textContent');
return '<p class="form-help">'+text+'</p>';
} else if (field.getAttribute('type') === 'jid-multi') {
return tpl_form_textarea({
'name': field.getAttribute('var'),
'label': field.getAttribute('label') || '',
'value': _.get(field.querySelector('value'), 'textContent'),
'value': get(field.querySelector('value'), 'textContent'),
'required': !!field.querySelector('required')
});
} else if (field.getAttribute('type') === 'boolean') {
@ -633,13 +619,13 @@ u.xForm2webForm = function (field, stanza, options) {
'id': u.getUniqueId(),
'name': field.getAttribute('var'),
'label': field.getAttribute('label') || '',
'checked': _.get(field.querySelector('value'), 'textContent') === "1" && 'checked="1"' || '',
'checked': get(field.querySelector('value'), 'textContent') === "1" && 'checked="1"' || '',
'required': !!field.querySelector('required')
});
} else if (field.getAttribute('var') === 'url') {
return tpl_form_url({
'label': field.getAttribute('label') || '',
'value': _.get(field.querySelector('value'), 'textContent')
'value': get(field.querySelector('value'), 'textContent')
});
} else if (field.getAttribute('var') === 'username') {
return tpl_form_username({
@ -647,7 +633,7 @@ u.xForm2webForm = function (field, stanza, options) {
'name': field.getAttribute('var'),
'type': XFORM_TYPE_MAP[field.getAttribute('type')],
'label': field.getAttribute('label') || '',
'value': _.get(field.querySelector('value'), 'textContent'),
'value': get(field.querySelector('value'), 'textContent'),
'required': !!field.querySelector('required')
});
} else if (field.getAttribute('var') === 'ocr') { // Captcha
@ -656,7 +642,7 @@ u.xForm2webForm = function (field, stanza, options) {
return tpl_form_captcha({
'label': field.getAttribute('label'),
'name': field.getAttribute('var'),
'data': _.get(el, 'textContent'),
'data': get(el, 'textContent'),
'type': uri.getAttribute('type'),
'required': !!field.querySelector('required')
});
@ -671,7 +657,7 @@ u.xForm2webForm = function (field, stanza, options) {
'placeholder': null,
'required': !!field.querySelector('required'),
'type': XFORM_TYPE_MAP[field.getAttribute('type')],
'value': _.get(field.querySelector('value'), 'textContent')
'value': get(field.querySelector('value'), 'textContent')
});
}
}