Commit 91243f64 authored by JC Brand's avatar JC Brand

Replace lodash methods with native ones.

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