Skip to content

Commit

Permalink
Style location widget for bootstrap5. Inlcude dark mode variant
Browse files Browse the repository at this point in the history
  • Loading branch information
lenadax committed Oct 14, 2024
1 parent 3c002eb commit 79c0e6c
Show file tree
Hide file tree
Showing 14 changed files with 832 additions and 11 deletions.
39 changes: 38 additions & 1 deletion js/rollup.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cleanup from 'rollup-plugin-cleanup';
import {terser} from 'rollup-plugin-terser';

const out_dir = 'src/yafowil/widget/location/resources';
const out_dir_bs5 = 'src/yafowil/widget/location/resources/bootstrap5';

const outro = `
window.yafowil = window.yafowil || {};
Expand Down Expand Up @@ -43,5 +44,41 @@ export default args => {
interop: 'default'
});
}
return conf;

// Bootstrap 5
let conf_2 = {
input: 'js/src/bootstrap5/bundle.js',
plugins: [
cleanup()
],
output: [{
name: 'yafowil_location',
file: `${out_dir_bs5}/widget.js`,
format: 'iife',
outro: outro,
globals: {
jquery: 'jQuery'
},
interop: 'default'
}],
external: [
'jquery'
]
};
if (args.configDebug !== true) {
conf_2.output.push({
name: 'yafowil_location',
file: `${out_dir_bs5}/widget.min.js`,
format: 'iife',
plugins: [
terser()
],
outro: outro,
globals: {
jquery: 'jQuery'
},
interop: 'default'
});
}
return [conf, conf_2];
};
17 changes: 17 additions & 0 deletions js/src/bootstrap5/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import $ from 'jquery';

import {BS5LocationWidget} from './widget.js';
import {register_array_subscribers} from './widget.js';

export * from './widget.js';

$(function() {
if (window.ts !== undefined) {
ts.ajax.register(BS5LocationWidget.initialize, true);
} else if (window.bdajax !== undefined) {
bdajax.register(BS5LocationWidget.initialize, true);
} else {
BS5LocationWidget.initialize();
}
register_array_subscribers();
});
66 changes: 66 additions & 0 deletions js/src/bootstrap5/widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {LocationWidgetMarkerPopup} from '../widget.js';
import {LocationWidget} from '../widget.js';

const locationIcon = L.divIcon({
html: '<i class="bi bi-geo-alt-fill custom-marker"></i>',
className: 'leaflet-div-icon',
iconSize: [32, 32],
iconAnchor: [16, 32],
popupAnchor: [0, -32]
});

export class BS5LocationWidgetMarker {

constructor(widget, lat, lon) {
this.widget = widget;
let marker = new L.Marker([lat, lon], {icon: locationIcon}, {draggable: true});
marker.addTo(widget.markers);
new LocationWidgetMarkerPopup(widget, marker);
marker.on('dragend', this.dragend_handle.bind(this));
}

dragend_handle(evt) {
let latlng = evt.target._latlng,
widget = this.widget;
widget.lat = latlng.lat;
widget.lon = latlng.lng;
widget.zoom = widget.map.getZoom();
}
}

export class BS5LocationWidget extends LocationWidget {

static initialize(context) {
$('div.location-map', context).each(function() {
if (window.yafowil_array !== undefined &&
window.yafowil_array.inside_template($(this))) {
return;
}
new BS5LocationWidget($(this));
});
}

constructor(elem) {
super(elem);
}

create_marker(lat, lon) {
console.log('XXXX')
return new BS5LocationWidgetMarker(this, lat, lon);
}
}

//////////////////////////////////////////////////////////////////////////////
// yafowil.widget.array integration
//////////////////////////////////////////////////////////////////////////////

function location_on_array_add(inst, context) {
BS5LocationWidget.initialize(context);
}

export function register_array_subscribers() {
if (window.yafowil_array === undefined) {
return;
}
window.yafowil_array.on_array_event('on_add', location_on_array_add);
}
12 changes: 8 additions & 4 deletions js/src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class LocationWidgetSearch {
lon = location.x,
widget = this.widget;
widget.markers.clearLayers();
new LocationWidgetMarker(widget, lat, lon);
this.widget.create_marker(lat, lon);
widget.lat = lat;
widget.lon = lon;
}
Expand Down Expand Up @@ -159,6 +159,10 @@ export class LocationWidget {
this._input_zoom.val(val);
}

create_marker(lat, lon) {
return new LocationWidgetMarker(this, lat, lon);
}

create_map() {
// create map
let map = this.map = new L.Map(this.id);
Expand All @@ -174,7 +178,7 @@ export class LocationWidget {
map.addLayer(markers);
// add marker if value given
if (this.value) {
new LocationWidgetMarker(this, this.lat, this.lon);
this.create_marker(this.lat, this.lon);
}
// add or move marker on map click
map.on('click', this.click_handle.bind(this));
Expand All @@ -184,7 +188,7 @@ export class LocationWidget {
// XXX: confirmation dialog
this.markers.clearLayers();
let latlng = evt.latlng;
new LocationWidgetMarker(this, latlng.lat, latlng.lng);
this.create_marker(latlng.lat, latlng.lng);
this.lat = latlng.lat;
this.lon = latlng.lng;
this.zoom = this.map.getZoom();
Expand All @@ -198,7 +202,7 @@ export class LocationWidget {
}
this[name] = val;
this.markers.clearLayers();
new LocationWidgetMarker(this, this.lat, this.lon);
this.create_marker(this.lat, this.lon);
this.map.setView([this.lat, this.lon], this.zoom);
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"qunit": "^2.19.3",
"rollup": "^2.79.1",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-terser": "^7.0.2"
"rollup-plugin-terser": "^7.0.2",
"sass": "^1.58.3"
}
}
7 changes: 7 additions & 0 deletions scripts/styles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

SASS_BIN="./node_modules/sass/sass.js"
SASS_DIR="./scss"
TARGET_DIR="./src/yafowil/widget/location/resources"

$SASS_BIN $SASS_DIR/bootstrap5.scss --no-source-map $TARGET_DIR/bootstrap5/widget.css
178 changes: 178 additions & 0 deletions scss/bootstrap5.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
.location-wrapper {
// bootstrap 5 input-group styles

.leaflet-control-geosearch {
.results {
border-top-left-radius: 0;
border-top-right-radius: 0;
border-bottom-left-radius: 0.25rem;
border-bottom-right-radius: 0.25rem;
border: 1px solid var(--bs-border-color);
color: var(--bs-body-color);
background-color: var(--bs-body-bg);

&:not(.active) {
border: 0;
}

> * {
padding: 0.375rem 0.75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
border: none;

&:hover, &.active {
background-color: var(--bs-secondary-bg);
border: none;
}
}
}

> form {
border: 0;
background: none;

&.open input {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}

input {
display: block;
width: 100%;
height: 36px;
padding: 0.375rem 0.75rem;
font-size: var(--bs-body-font-size);
font-weight: var(--bs-body-font-weight);
line-height: var(--bs-body-line-height);
color: var(--bs-body-color);
background-clip: padding-box;
border: 1px solid var(--bs-border-color);
appearance: none;
border-radius: var(--bs-border-radius);
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
background-color: var(--bs-body-bg);

&:focus {
color: var(--bs-body-color);
background-color: var(--bs-body-bg);
border-color: var(--bs-border-color);
outline: 0;
box-shadow: 0 0 0 0.25rem rgba(13,110,253,.25);
}

&::placeholder {
color: var(--bs-placeholder-color);
opacity: 1;
}
}
}
a.reset {
background: none;
line-height: 36px;
height: 36px;
font-size: 0;

> * {
display: none;
}

&::before {
content: "\F659";
font-family: 'bootstrap-icons';
font-size: 1rem;
color: var(--bs-body-color);
}
}
}
.leaflet-geosearch-bar form {
padding: 0;
}

.leaflet-control-zoom {
font-size: var(--bs-body-font-size);
font-weight: var(--bs-body-font-weight);
line-height: var(--bs-body-line-height);
color: var(--bs-body-color);
border: 1px solid var(--bs-border-color);
background-color: var(--bs-body-bg);

.leaflet-control-zoom-in {
font-size: 0;
border-top-left-radius: var(--bs-border-radius)!important;
border-top-right-radius: var(--bs-border-radius)!important;

&::before {
content: "\F64D";
font-family: 'bootstrap-icons';
font-size: 1rem;
color: currentColor;
}
}
.leaflet-control-zoom-out {
font-size: 0;
border-bottom-left-radius: var(--bs-border-radius)!important;
border-bottom-right-radius: var(--bs-border-radius)!important;

&::before {
content: "\F63B";
font-family: 'bootstrap-icons';
font-size: 1rem;
color: currentColor;
}
}
}

.leaflet-div-icon {
background: transparent;
border: none;
}
.custom-marker {
font-size: 2rem;
color: var(--bs-primary);
}

div.location-map {
height: 360px;
width: 100%;
}

.leaflet-popup-content-wrapper, .leaflet-popup-tip {
background: var(--bs-secondary-bg);
box-shadow: 0 3px 14px rgba(0, 0, 0, 0.4);

a {
color: var(--bs-body-color);
font-size: var(--bs-body-font-size);
}
}

.leaflet-popup-close-button {
font-size: 0!important;

&::before {
content: "\F659";
font-family: 'bootstrap-icons';
font-size: .7rem;
color: var(--bs-body-color);
}
}
}

[data-bs-theme="dark"] {
.leaflet-container {
background: #000;
}

.leaflet-popup-content-wrapper, .leaflet-popup-tip {
box-shadow: 0 3px 14px rgba(0, 0, 0, 1);
}

.leaflet-layer,
.leaflet-control-zoom-in,
.leaflet-control-zoom-out,
.leaflet-control-attribution {
filter: invert(100%) hue-rotate(355deg) brightness(95%) contrast(90%);
}
}
Loading

0 comments on commit 79c0e6c

Please sign in to comment.