Skip to content

Commit

Permalink
Merge pull request #41 from helsingborg-stad/feat/osm-v2
Browse files Browse the repository at this point in the history
feat!: osm v2
  • Loading branch information
sebastianthulin authored Jul 31, 2024
2 parents 01a5a21 + 560d7d8 commit 4bd2476
Show file tree
Hide file tree
Showing 42 changed files with 1,486 additions and 382 deletions.
1 change: 1 addition & 0 deletions Public.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
if (!function_exists('open_street_map_render_blade_view')) {
function open_street_map_render_blade_view($view, $data = [], $compress = true)
{
$markup = '';
$componentLibrary = new ComponentLibraryInit([]);
$bladeEngine = $componentLibrary->getEngine();
$data = array_merge($data, array('errorMessage' => false));
Expand Down
5 changes: 4 additions & 1 deletion modularity-open-street-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Modularity Open Street Map
* Plugin URI: https://github.com/NiclasNorin/modularity-open-street-map
* Description: A map based of OSM.
* Version: 2.1.3
* Version: 2.1.3
* Author: Niclas Norin
* Author URI: https://github.com/NiclasNorin
* License: MIT
Expand All @@ -24,6 +24,9 @@
define('MODULARITYOPENSTREETMAP_VIEW_PATH', MODULARITYOPENSTREETMAP_PATH . 'views/');
define('MODULARITYOPENSTREETMAP_MODULE_VIEW_PATH', MODULARITYOPENSTREETMAP_PATH . 'source/php/Module/views');

// Endpoint address
define('OSM_ENDPOINT', 'osm/v1/');

require_once MODULARITYOPENSTREETMAP_PATH . 'Public.php';

// Register the autoloader
Expand Down
118 changes: 118 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"postcss-loader": "^6.2.1",
"sass": "^1.49.7",
"sass-loader": "^12.4.0",
"ts-loader": "^9.5.1",
"typescript": "^5.5.3",
"webpack": "^5.68.0",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-cli": "^4.9.2",
Expand All @@ -58,4 +60,4 @@
"leaflet": "^1.9.3",
"leaflet.markercluster": "^1.5.3"
}
}
}
47 changes: 47 additions & 0 deletions source/js/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { StorageInterface } from "./storageInterface";
import TaxonomySelectUpdater from "./taxonomySelectUpdater";

class Main {
constructor(
private acf: any,
private postTypeSelectFieldKey: string,
private filterSelectFieldKey: string,
private storageInstance: StorageInterface,
private taxonomySelectUpdaterInstance: TaxonomySelectUpdater
) {
const postTypeSelect = document.querySelector(`[data-key="${this.postTypeSelectFieldKey}"] select`) as HTMLSelectElement|undefined;

if (postTypeSelect) {
if (postTypeSelect.value) {
this.taxonomySelectUpdaterInstance.updateExistsingRows(postTypeSelect.value);
}

this.listenForPostTypeChanges(postTypeSelect);
this.listenForFilterRowsAdded(postTypeSelect);
}
}

private listenForPostTypeChanges(postTypeSelect: HTMLSelectElement): void {
postTypeSelect.addEventListener('change', (event) => {
const selectedValue = (event.target as HTMLSelectElement).value;
this.taxonomySelectUpdaterInstance.updateExistsingRows(selectedValue);
});
}

private listenForFilterRowsAdded(postTypeSelect: HTMLSelectElement): void
{
this.acf.addAction('append', ( jqueryEl: any ) => {
if (!jqueryEl.hasOwnProperty(0)) {
return;
}

const selectField = jqueryEl[0].querySelector(`[data-key="${this.filterSelectFieldKey}"] select`);

if (selectField) {
this.taxonomySelectUpdaterInstance.addSelectFieldOptions(selectField, 0, this.storageInstance.getTaxonomies(postTypeSelect.value), []);
}
});
}
}

export default Main;
31 changes: 31 additions & 0 deletions source/js/modularity-open-street-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import TaxonomySelectUpdater from "./taxonomySelectUpdater";
import Storage from "./storage";
import Main from "./main";

document.addEventListener('DOMContentLoaded', function() {
const filterSelectFieldKey = 'field_668d1cc80d853';
const postTypeSelectFieldKey = 'field_642a8818a908c';

if (typeof acf !== 'undefined') {
const [taxonomies, selected] = getOsm();
const storageInstance = new Storage(taxonomies, selected);
const taxonomySelectUpdaterInstance = new TaxonomySelectUpdater(filterSelectFieldKey, storageInstance);
new Main(acf, postTypeSelectFieldKey, filterSelectFieldKey, storageInstance, taxonomySelectUpdaterInstance);
}
});

function getOsm() {
let parsedData = {};
try {
parsedData = JSON.parse(osm);
} catch (error) {
console.error('Error parsing OSM data', error);
}

console.log(parsedData);

return [
parsedData.hasOwnProperty('taxonomies') ? parsedData.taxonomies : [],
parsedData.hasOwnProperty('selected') ? parsedData.selected : []
];
}
25 changes: 25 additions & 0 deletions source/js/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { StorageInterface } from "./storageInterface";
class Storage implements StorageInterface {
constructor(private taxonomies: { [key: string]: string[] }, private selected: { [key: string]: string[] }) {
console.log(this.selected);
}

public getSelected(postType: string): string[] {
console.log(this.selected);
if (this.selected.hasOwnProperty(postType)) {
return this.selected[postType];
}

return [];
}

public getTaxonomies(postType: string): string[] {
if (this.taxonomies.hasOwnProperty(postType)) {
return this.taxonomies[postType];
}

return [];
}
}

export default Storage;
4 changes: 4 additions & 0 deletions source/js/storageInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface StorageInterface {
getSelected(postType: string): string[];
getTaxonomies(postType: string): string[];
}
36 changes: 36 additions & 0 deletions source/js/taxonomySelectUpdater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { StorageInterface } from "./storageInterface";
class TaxonomySelectUpdater {

constructor(
private selectFieldKey: string,
private storage: StorageInterface

) {}

public updateExistsingRows(postType: string): void {
const taxonomies = this.storage.getTaxonomies(postType);
const selectedValues = this.storage.getSelected(postType);

[...document.querySelectorAll(`tr.acf-row:not(.acf-clone) [data-key="${this.selectFieldKey}"] select`)].forEach((selectField, index) => {
selectField.innerHTML = "";
this.addSelectFieldOptions(selectField, index, taxonomies, selectedValues);
});
}

public addSelectFieldOptions(
selectField: Element,
index: number,
taxonomies: string[],
selectedValues: string[],
): void {
for (const key in taxonomies) {
const option = document.createElement('option');
option.selected = selectedValues[index] && selectedValues[index] === key ? true : false;
option.value = key;
option.text = taxonomies[key];
selectField.appendChild(option);
}
}
}

export default TaxonomySelectUpdater;
Loading

0 comments on commit 4bd2476

Please sign in to comment.