Skip to content

Instantly share code, notes, and snippets.

View bartwttewaall's full-sized avatar

Bart Wttewaall bartwttewaall

View GitHub Profile
@bartwttewaall
bartwttewaall / traverse-2d-array.js
Created March 20, 2024 13:04
Traverse a 2-dimensional array in a single for-loop
function traverse (values, numCols) {
var numRows = values.length / numCols;
for (var i = 0, length = values.length; i < length; i++) {
var row = (i % length / numCols) << 0; // = Math.floor
var col = i % numCols;
console.log(row, col);
}
}
<?php
namespace modules\wirelab;
use Craft;
use craft\elements\Entry;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class WirelabTwigExtension extends AbstractExtension
@bartwttewaall
bartwttewaall / twig-rebug.twig
Created January 30, 2024 10:47
Debugging twig templates
Some usefull tips to debug in templates:
```twig
{{ dump(_context) }}
{{ dump(_context|keys) }}
{{ dump(myProductQuery.rawSQL()) }}
```
If you ever lose the overview of what template is used at what url, you can use this snippet in each template to show the template name as a regular html comment.
@bartwttewaall
bartwttewaall / toggle-mouseover.ts
Created January 16, 2024 08:59
Toggle submenu on mouseover with debounced close on mouseout
import { debounce } from 'ts-debounce';
export function initLinksWithSubmenu(el: HTMLElement) {
// find all links with children
const links = el.querySelectorAll<HTMLElement>("li.has-children > a");
let mouseover = false;
const debouncedClose = debounce(async (el: HTMLElement) => toggle(el, false), 1500);
const toggle = (el?: HTMLElement, value?: boolean) => {
@bartwttewaall
bartwttewaall / threejs-coords.js
Created January 8, 2024 09:29
Math functions for mouse position and camera bounds
export const getScreenPoint = (clientX, clientY, el) => {
const boundingRect = el.getBoundingClientRect();
return new THREE.Vector2(
(clientX - boundingRect.left) * (el.offsetWidth / boundingRect.width),
(clientY - boundingRect.top) * (el.offsetHeight / boundingRect.height)
);
};
export const screenToLatLng = (view, vector) => {
const direction = view.screenToWorld(vector);
<?php
namespace craft\contentmigrations;
use Craft;
use craft\db\Migration;
use craft\elements\User;
/**
* m240105_091858_disablePermissionUtilityFindReplace migration.
@bartwttewaall
bartwttewaall / _hrefLang.twig
Created January 4, 2024 15:54
Alternate content links
{# Include in the head of the base page template #}
{% set currentElement = craft.app.urlManager.matchedElement %}
{% set sites = craft.app.getSites().getGroupById(currentSite.groupId).getSites()|filter(s => s.baseUrl is not empty) %}
{% set altLinks = [] %}
{% for site in sites %}
{% set title = craft.app.i18n.getLocaleById(site.language).displayName %}
{% set url = site.getBaseUrl() %}
@bartwttewaall
bartwttewaall / _sites.twig
Last active January 4, 2024 15:52
Generates a list of site links with icon
{# Generates a list of site links with icon
Basic Example:
{% embed 'partials/_sites.twig' %}{% endembed %}
Advanced Example:
{% embed 'partials/_sites.twig' %}
{% block sites %}
{% if languages is defined and languages is not empty %}
<ul class="rounded-2xl border divide-y divide-gray-200 py-2">
export function initNavigation() {
const navigation = document.querySelector<HTMLElement>("[data-navigation]");
if (navigation) initSticky(navigation);
}
function initStickyBehaviour(root: HTMLElement) {
// hide the top navigation on scroll
const topNav = root.querySelector<HTMLElement>("[data-topnav]");
if (!topNav) {
@bartwttewaall
bartwttewaall / random.ts
Created June 23, 2022 09:52
Random functions
// Random integer from <low, high> interval
export function randInt(low: number, high: number) {
return low + Math.floor(Math.random() * (high - low + 1));
}
// Random float from <low, high> interval
export function randFloat(low: number, high: number) {
return low + Math.random() * (high - low);
}