-
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 #47 from helsingborg-stad/feat/gutenberg-fields-ha…
…ndling feat: gutenberg field handling
- Loading branch information
Showing
6 changed files
with
146 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import TaxonomySelectUpdater from "./taxonomySelectUpdater"; | ||
import Storage from "./storage"; | ||
import Main from "./main"; | ||
|
||
class Gutenberg { | ||
initializedOsmBlocks = []; | ||
|
||
constructor() { | ||
this.editor = wp.data.select('core/block-editor'); | ||
|
||
wp.data.subscribe(() => { | ||
const osmBlockIds = this.editor.getBlocksByName('acf/open-street-map'); | ||
if (osmBlockIds.length > 0) { | ||
osmBlockIds.forEach((osmBlockId) => { | ||
if (!this.initializedOsmBlocks.includes(osmBlockId)) { | ||
this.tryInitializeBlock(osmBlockId); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
initializeBlock(block, osmBlockId) { | ||
const filterSelectFieldKey = 'field_668d1cc80d853'; | ||
const postTypeSelectFieldKey = 'field_642a8818a908c'; | ||
|
||
const [taxonomies, selected] = this.getOsm(osmBlockId); | ||
const storageInstance = new Storage(taxonomies, selected); | ||
const taxonomySelectUpdaterInstance = new TaxonomySelectUpdater(filterSelectFieldKey, storageInstance); | ||
|
||
const intervalId = setInterval(() => { | ||
if (block.querySelector('[data-name="mod_osm_post_type"]')) { | ||
new Main( | ||
block, | ||
acf, | ||
postTypeSelectFieldKey, | ||
filterSelectFieldKey, | ||
storageInstance, | ||
taxonomySelectUpdaterInstance | ||
); | ||
|
||
clearInterval(intervalId); | ||
} | ||
}, 1000); | ||
} | ||
|
||
tryInitializeBlock(osmBlockId) { | ||
const block = document.querySelector('#block-' + osmBlockId); | ||
if (block) { | ||
this.initializedOsmBlocks.push(osmBlockId); | ||
this.initializeBlock(block, osmBlockId); | ||
} | ||
} | ||
|
||
getOsm(osmBlockId) { | ||
let parsedData = {}; | ||
|
||
try { | ||
parsedData = JSON.parse(osm); | ||
} catch (error) { | ||
console.error('Error parsing OSM data', error); | ||
} | ||
|
||
return [ | ||
parsedData.hasOwnProperty('taxonomies') ? parsedData.taxonomies : {}, | ||
this.getSelected(osmBlockId) | ||
]; | ||
} | ||
|
||
getSelected(osmBlockId) { | ||
const blockData = this.editor.getBlockAttributes(osmBlockId); | ||
if (!blockData?.data?.mod_osm_post_type) { | ||
return {}; | ||
} | ||
|
||
let selected = {}; | ||
selected[blockData.data.mod_osm_post_type] = []; | ||
|
||
if (blockData?.data?.mod_osm_filters > 0) { | ||
let i = 0; | ||
for (i; i < blockData.data.mod_osm_filters; i++) { | ||
if (blockData.data[`mod_osm_filters_${i}_mod_osm_filter_taxonomy`]) { | ||
selected[blockData.data.mod_osm_post_type].push(blockData.data[`mod_osm_filters_${i}_mod_osm_filter_taxonomy`]); | ||
} | ||
} | ||
} | ||
|
||
return selected; | ||
} | ||
} | ||
|
||
export default Gutenberg; |
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 |
---|---|---|
@@ -1,31 +1,12 @@ | ||
import TaxonomySelectUpdater from "./taxonomySelectUpdater"; | ||
import Storage from "./storage"; | ||
import Main from "./main"; | ||
import Module from "./module"; | ||
import Gutenberg from "./gutenberg"; | ||
|
||
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); | ||
if (typeof wp !== 'undefined' && typeof acf !== 'undefined') { | ||
if (document.body.classList.contains('block-editor-page')) { | ||
new Gutenberg(); | ||
} else { | ||
new Module(); | ||
} | ||
} | ||
|
||
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,35 @@ | ||
import TaxonomySelectUpdater from "./taxonomySelectUpdater"; | ||
import Storage from "./storage"; | ||
import Main from "./main"; | ||
|
||
class Module { | ||
constructor() { | ||
const filterSelectFieldKey = 'field_668d1cc80d853'; | ||
const postTypeSelectFieldKey = 'field_642a8818a908c'; | ||
const container = document.querySelector('#acf-group_64219abb0caec'); | ||
|
||
if (container) { | ||
const [taxonomies, selected] = this.getOsm(); | ||
const storageInstance = new Storage(taxonomies, selected); | ||
const taxonomySelectUpdaterInstance = new TaxonomySelectUpdater(filterSelectFieldKey, storageInstance); | ||
new Main(container, acf, postTypeSelectFieldKey, filterSelectFieldKey, storageInstance, taxonomySelectUpdaterInstance); | ||
} | ||
} | ||
|
||
getOsm() { | ||
let parsedData = {}; | ||
|
||
try { | ||
parsedData = JSON.parse(osm); | ||
} catch (error) { | ||
console.error('Error parsing OSM data', error); | ||
} | ||
|
||
return [ | ||
parsedData.hasOwnProperty('taxonomies') ? parsedData.taxonomies : {}, | ||
parsedData.hasOwnProperty('selected') ? parsedData.selected : {} | ||
]; | ||
} | ||
} | ||
|
||
export default Module; |
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