-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from helsingborg-stad/feat/osm-v2
feat!: osm v2
- Loading branch information
Showing
42 changed files
with
1,486 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : [] | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.