Skip to content

Commit

Permalink
Remove requirement for full lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
Sian-Lee-SA committed Apr 7, 2023
1 parent e1b5d00 commit a87658b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 49 deletions.
27 changes: 8 additions & 19 deletions honeycomb-menu.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/event-manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const isArray = require('lodash/isEmpty');

class EventManager {
};

Expand Down Expand Up @@ -30,7 +32,7 @@ EventManager.prototype.addEventListener = function( _event, _func, _options )

EventManager.prototype.removeEventListeners = function(_event)
{
if( _.isArray(_event) )
if( isArray(_event) )
return _event.forEach( e => this.removeEventListeners(e) );

this.getEvents(_event).forEach( o => {
Expand Down
18 changes: 11 additions & 7 deletions src/honeycomb-menu-item.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { LitElement, html, css } from 'lit';
import { objectEvalTemplate, getTemplateOrValue, provideHass, createCard } from "./helpers.js";

const _ = require('lodash');
// const _ = require('lodash');
const merge = require('lodash/merge');
const assign = require('lodash/assign');
const isEmpty = require('lodash/isEmpty');
const isString = require('lodash/isString');

class HoneycombMenuItem extends LitElement
{
Expand Down Expand Up @@ -66,25 +70,25 @@ class HoneycombMenuItem extends LitElement

set config( config )
{
if( config.type == 'break' || _.isEmpty(config) || config.disabled )
if( config.type == 'break' || isEmpty(config) || config.disabled )
{
this.disabled = true;
return;
}

// Assign Defaults
this._config = _.assign({
this._config = assign({
autoclose: true,
audio: true,
active: false,
variables: {},
}, config);

if( _.isString( this._config.tap_action ) )
if( isString( this._config.tap_action ) )
this._config.tap_action = {'action': this._config.tap_action};
if( _.isString( this._config.hold_action ) )
if( isString( this._config.hold_action ) )
this._config.hold_action = {'action': this._config.hold_action};
if( _.isString( this._config.double_tap_action ) )
if( isString( this._config.double_tap_action ) )
this._config.double_tap_action = {'action': this._config.double_tap_action};

if( ! this._config.active )
Expand Down Expand Up @@ -206,7 +210,7 @@ class HoneycombMenuItem extends LitElement

_createLovelaceCard()
{
var card = createCard(_.merge({}, {
var card = createCard(merge({}, {
type: 'custom:button-card',
size: '30px',
show_name: false
Expand Down
55 changes: 35 additions & 20 deletions src/honeycomb-menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _ = require('lodash');
// const _ = require('lodash');

import { LitElement, html, css } from 'lit';
import "./honeycomb-menu-item.js";
Expand All @@ -7,7 +7,16 @@ import { objectEvalTemplate, getTemplateOrValue, fireEvent, lovelace_view, provi

const honeycomb_templates = lovelace_config().honeycomb_menu_templates || null;
const hass = document.querySelector('home-assistant').hass;
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;

const merge = require('lodash/merge');
const omit = require('lodash/omit');
const split = require('lodash/split');
const clamp = require('lodash/clamp');
const _template = require('lodash/template');
const isEmpty = require('lodash/isEmpty');
const _defaults = require('lodash/defaults');

// _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;

const manager = new function() {
this.honeycomb = null;
Expand Down Expand Up @@ -66,7 +75,7 @@ function traverseConfigs( _config, _buttons )

// Allow non extensible to be a new object that can be extended. Using
// merge will also affect sub properties
_config = _.merge({}, _config );
_config = merge({}, _config );


if( ! _config.template || ! honeycomb_templates || ! honeycomb_templates[_config.template] )
Expand Down Expand Up @@ -240,7 +249,6 @@ class HoneycombMenu extends LitElement
animation-name: zoomOut;
}
:host([closing]) honeycomb-menu-item[selected] {
animation-delay: 800ms !important;
animation-duration: 0.75s;
animation-name: bounceOut;
}
Expand Down Expand Up @@ -269,7 +277,6 @@ class HoneycombMenu extends LitElement
{
return html`
<div id="shade" class="shade" @click=${this._handleShadeClick}></div>
<audio id="audio"></audio>
${(this.config.xy_pad) ? html`
<xy-pad
Expand All @@ -288,7 +295,6 @@ class HoneycombMenu extends LitElement
${this.buttons.map((v, i) => html`
<honeycomb-menu-item
style="animation-delay: ${this._computeAnimateDelay(i)};"
class="animated"
.hass=${this.hass}
.config=${this._computeItemConfig(v)}
@action=${this._handleItemAction}>
Expand All @@ -300,7 +306,7 @@ class HoneycombMenu extends LitElement
{
provideHass(this);

_.defaults(config, {
_defaults(config, {
action: 'hold',
entity: null,
active: false,
Expand Down Expand Up @@ -345,14 +351,20 @@ class HoneycombMenu extends LitElement

this.closing = true;

if( _item ) _item.setAttribute('selected', '');
let ele = _item || this.shadowRoot.querySelectorAll('honeycomb-menu-item')[5];
if( _item )
{
_item.setAttribute('selected', '');
_item.setAttribute('style', `animation-delay: ${this._computeAnimateDelay(3)};`);
}

fireEvent(this, 'closing', { item: _item });
// Remove shade div earlier to allow clicking of other lovelace elements while the animation continues
this.shadowRoot.querySelector('#shade').addEventListener('animationend', function(e) {
this.remove();
});
this.shadowRoot.querySelectorAll('honeycomb-menu-item')[5].addEventListener('animationend', e => {

ele.addEventListener('animationend', e => {
this.remove();
fireEvent(this, 'closed', { item: _item });
});
Expand Down Expand Up @@ -385,7 +397,7 @@ class HoneycombMenu extends LitElement
button = {};

// Clone to allow writable object from button-card
this.buttons[i] = _.merge({}, button);
this.buttons[i] = merge({}, button);
}
}

Expand All @@ -408,8 +420,8 @@ class HoneycombMenu extends LitElement
}

let rect = this.view.getBoundingClientRect();
_x = _.clamp( _x - rect.left, bounds.min.x, bounds.max.x - 5 );
_y = _.clamp( _y - rect.top, bounds.min.y, bounds.max.y - 5 );
_x = clamp( _x - rect.left, bounds.min.x, bounds.max.x - 5 );
_y = clamp( _y - rect.top, bounds.min.y, bounds.max.y - 5 );

this.style.left = `${_x - container.w}px`;
this.style.top = `${_y - container.h}px`;
Expand Down Expand Up @@ -456,12 +468,15 @@ class HoneycombMenu extends LitElement
if( ! _item.config.audio )
return;

const audio_ele = this.shadowRoot.querySelector('#audio');
if( audio_ele )
let audio_ele = document.querySelector('#honeycomb-audio');
if( ! audio_ele )
{
audio_ele.src = _item.config.audio;
audio_ele.play();
audio_ele = document.createElement('audio');
audio_ele.id = 'honeycomb-audio';
document.querySelector("home-assistant").append(audio_ele);
}
audio_ele.src = _item.config.audio;
audio_ele.play();
}

_handleXYPad(e)
Expand All @@ -477,7 +492,7 @@ class HoneycombMenu extends LitElement
return;

this._service[axis] = true;
let service = _.split( config.service, '.', 2);
let service = split( config.service, '.', 2);
this.hass
.callService( service[0], service[1], this.__renderServiceData(e.detail, config.service_data) )
.then(e => this._service[axis] = false);
Expand All @@ -493,7 +508,7 @@ class HoneycombMenu extends LitElement
return objectEvalTemplate( this.hass, this.hass.states[this.config.entity], {}, data, (val) => {
if( val == 'entity' )
return this.config.entity;
return _.template(val)(vars);
return _template(val, {interpolate: /{{([\s\S]+?)}}/g})(vars);
});
}

Expand All @@ -514,9 +529,9 @@ class HoneycombMenu extends LitElement

_computeItemConfig( item )
{
if( _.isEmpty(item) )
if( isEmpty(item) )
return item;
return _.omit( _.merge( {}, this.config, item ), ['buttons', 'size', 'action', 'xy_pad', 'spacing'] );
return omit( merge( {}, this.config, item ), ['buttons', 'size', 'action', 'xy_pad', 'spacing'] );
}

_computeAnimateDelay( i )
Expand Down
6 changes: 4 additions & 2 deletions src/xy-pad.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LitElement, html, css } from 'lit';

const clamp = require('lodash/clamp');

class XYPad extends LitElement
{
static get is()
Expand Down Expand Up @@ -142,8 +144,8 @@ class XYPad extends LitElement
{
_x = _x - this.joystick.offsetLeft - (this.size / 2);
_y = _y - this.joystick.offsetTop - (this.size / 2);
this._current.x = _.clamp(_x, -this.clampX, this.clampX);
this._current.y = _.clamp(_y, -this.clampY, this.clampY);
this._current.x = clamp(_x, -this.clampX, this.clampX);
this._current.y = clamp(_y, -this.clampY, this.clampY);

this.joystick.style.transform = `translate3d(${this._current.x}px, ${this._current.y}px, 0)`;

Expand Down

0 comments on commit a87658b

Please sign in to comment.