Skip to content

Commit

Permalink
Merge pull request #3 from J9rem/master
Browse files Browse the repository at this point in the history
refactor(js) + fix fieldName detection
  • Loading branch information
mrflos authored Aug 6, 2024
2 parents 5a1b144 + 79f293b commit 1fabcf4
Show file tree
Hide file tree
Showing 32 changed files with 1,509 additions and 740 deletions.
52 changes: 41 additions & 11 deletions fields/EnumLevel2CommonsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public function __construct(array $values, ContainerInterface $services)
? $matches[1]
: "";


$this->associatingFieldId = $explodedParam[2] ?? '';
$this->associatingFieldId = (
!empty($this->associatingFieldId) &&
!empty($this->associatingFormId) &&
substr($internalValues[parent::FIELD_TYPE],-5) == 'fiche' // current is form
!empty($this->associatingFieldId) &&
!empty($this->associatingFormId) &&
substr($internalValues[parent::FIELD_TYPE], -5) == 'fiche' // current is form
) ? strval($this->associatingFieldId) : '';

// construct with parent
Expand All @@ -93,7 +93,7 @@ public function __construct(array $values, ContainerInterface $services)
public function renderInputIfPermitted($entry)
{
return $this->render("@bazar/inputs/enum-level2.twig", [])
.parent::renderInputIfPermitted($entry);
. parent::renderInputIfPermitted($entry);
}

// Format input values before save
Expand All @@ -109,7 +109,7 @@ public function formatValuesBeforeSaveIfEditable($entry, bool $isCreation = fals
&& (strval($assocFormId) !== strval(intval($assocFormId)) || $assocFormId !== $parentField->getLinkedObjectName())) {
$formId = $this->formatFormId($this->getLinkedObjectName());
$associatingFormId = $this->formatFormId($assocFormId);
if (!empty($formId) && $formId == $associatingFormId){
if (!empty($formId) && $formId == $associatingFormId) {
$availableValues = $this->getAvailableChildrenValuesReverseFromForm($parentValues, $parentField->getLinkedObjectName());
} else {
$availableValues = $this->getAvailableChildrenValuesFromLists($parentValues, $parentField->getLinkedObjectName());
Expand Down Expand Up @@ -142,7 +142,31 @@ protected function getParentField($entry): ?EnumField
}
$formId = $this->formatFormId($entry['id_typeannonce'] ?? '');
$formManager = $this->getService(FormManager::class);
return empty($formId) ? null : $formManager->findFieldFromNameOrPropertyName($parentFieldName, $formId);
if (empty($formId)) {
return null;
}
$form = $formManager->getOne($formId);
if (empty($form['prepared']) || !is_array($form['prepared'])) {
return null;
}
foreach ($form['prepared'] as $field) {
if (
$field instanceof EnumField
&& (
$field->getName() === $parentFieldName
|| in_array(
$field->getPropertyName(),
[
$parentFieldName,
$field->getType() . $field->getLinkedObjectName() . $parentFieldName
]
)
)
) {
return $field;
}
}
return null;
}

protected function formatFormId($formId): string
Expand Down Expand Up @@ -244,7 +268,7 @@ protected function getAvailableChildrenValuesFromLists(array $parentValues, stri
$parentValuesInEntry = $this->getFieldValues($parentField, $entry);
if (count(array_filter($parentValues, function ($v) use ($parentValuesInEntry) {
return in_array($v, $parentValuesInEntry);
}))>0) {
})) > 0) {
$childrenValuesInEntry = $this->getFieldValues($childField, $entry);
foreach ($childrenValuesInEntry as $value) {
if (!in_array($value, $availableValues)) {
Expand All @@ -271,7 +295,13 @@ protected function getAvailableChildrenValuesReverseFromForm(array $parentValues
$fields = [];
foreach ($form['prepared'] as $field) {
if ($field instanceof EnumField && $field->getLinkedObjectName() == $parentLinkedObjectName && $field->getPropertyName() !== "") {
if (!empty($this->associatingFieldId) && $this->associatingFieldId == $field->getName()){
if (
!empty($this->associatingFieldId)
&& (
$this->associatingFieldId == $field->getName()
|| $field->getName() === ($field->getType() . $field->getLinkedObjectName() . $this->associatingFieldId)
)
) {
$fields = [$field];
break;
}
Expand All @@ -288,9 +318,9 @@ protected function getAvailableChildrenValuesReverseFromForm(array $parentValues
]
]);
if (!empty($entries)) {
$availableValues= array_map(function($entry){
$availableValues = array_map(function ($entry) {
return $entry['id_fiche'];
},$entries);
}, $entries);
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions javascripts/allEntriesLoader.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of the YesWiki Extension twolevels.
*
* Authors : see README.md file that was distributed with this source code.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import WikiDataLoader from './wikiDataLoader.prototype.js'

/** data */
const allEntriesCache = {}

/** methods */
/**
* @param {Object} responseDecoded
*/
const allEntriesTestFunction = (responseDecoded) => {
return (typeof responseDecoded == "object"
|| Array.isArray(responseDecoded))
}


/**
* @param {string} id
*/
const load = async (id) => {
WikiDataLoader.assertIsRegularFormId(id)
if (id in allEntriesCache){
return allEntriesCache[id]
} else {
return await (new WikiDataLoader(
wiki.url(`?api/forms/${id}/entries`),
id,
'getAllEntries',
'loadingAllEntries',
allEntriesTestFunction
)).load()
.then((entries)=>{
const entriesToSave =
Object.fromEntries(
(typeof entries == "object" ? Object.values(entries) : entries)
.filter((e)=>{
return typeof e.id_fiche === "string" &&
typeof e.id_typeannonce === "string" &&
typeof e.bf_titre === "string"
}).map((e)=>[e.id_fiche,e])
)
allEntriesCache[id] = entriesToSave
return entriesToSave
})
.catch((e)=>{
throw new Error(
`error when getting all Entries for form '${id}'`,
{cause:e}
)
})
}
}

export default {
load
}
51 changes: 51 additions & 0 deletions javascripts/bazar/FilterLoadRootComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* This file is part of the YesWiki Extension twolevels.
*
* Authors : see README.md file that was distributed with this source code.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import utils from './RootVueJsComponentUtil.js'

export default {
props: ['root'],
data: function(){
return {
actions: {
filteredEntries: 'updateFilteredEntries',
params: 'processParams'
},
unwatcher: {}
};
},
methods: {
processParams: function(){
this.unwatcher.params();
if (utils.canShowAnd(this.root)){
this.registerWatcher('filteredEntries');
}
},
registerWatcher: function(varname){
if (varname in this.actions && typeof this[this.actions[varname]] == "function"){
this.unwatcher[varname] = this.root.$watch(varname,()=>(this[this.actions[varname]])())
}
},
updateFilteredEntries: function(){
if (Object.keys(this.root.computedFilters).length > 0){
const results = utils.filterEntriesSync(this.root.searchedEntries,this.root)
this.unwatcher.filteredEntries();
this.root.filteredEntries = results
this.registerWatcher('filteredEntries');
this.root.paginateEntries();
}
}
},
mounted(){
this.registerWatcher('params');
},
template: `
<span v-show="false"></span>
`
}
107 changes: 107 additions & 0 deletions javascripts/bazar/RootVueJsComponentUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* This file is part of the YesWiki Extension twolevels.
*
* Authors : see README.md file that was distributed with this source code.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import filtersService from './filters.service.js'
import optionRegistry from './options.registry.js'

const canShowAnd = (root) => {
return (root.params.intrafiltersmode === 'and')
}

const filterEntriesSync = (entries,root) => {
if (!canShowAnd(root)){
return entries
}
let results = entries
for(const filterId in root.computedFilters) {
results = results.filter(entry => {
if (!(filterId in entry) || typeof entry[filterId] != "string"){
return false
}
return root.computedFilters[filterId].every((value)=>entry[filterId].split(',').includes(value));
})
}
return results
}


let isSubLevelCache = null
const isSubLevel = (root) => {
if (isSubLevelCache === null){
if (Object.keys(root.formFields).length === 0){
return false
}
isSubLevelCache = Object.values(root.formFields).some((field)=>('parentFieldName' in field && 'associatingFormId' in field))
}
return isSubLevelCache
}

const getUuid = (root) => root?._uid ?? 'unknown'

const getOptionData = (root) => {
const uuid = getUuid(root)
return optionRegistry.getAndInitIfNeeded(uuid)
}

const searchFieldByName = (fieldName,fields) => {
for (const key in fields) {
if (Object.hasOwnProperty.call(fields, key)) {
const field = fields[key];
if ('name' in field && (
field.name === fieldName
|| field.name === ('' + field.type + field.linkedObjectName + fieldName)
)
){
return field
}
}
}
return null
}

const getFieldFromRoot = (root,fieldName) => {
if ('formFields' in root
&& typeof root.formFields === 'object'
&& Object.keys(root.formFields).length > 0){
if (fieldName in root.formFields){
return root.formFields[fieldName]
}
return searchFieldByName(fieldName, root.formFields)
}
return null
}

const pushIfNotPresent = (value,data) => {
if (!data.a.includes(value)){
data.a.push(value)
}
}

const getChekedFilters = (root) => {
let filters = [];
for(let fieldid in root.filters) {
const filter = root.filters[fieldid]
for (let option of (filter?.list ?? filter.nodes)) {
if (option.checked) {
pushIfNotPresent(filtersService.getFieldName(filter,option),{a:filters})
}
}
}
return filters
}

export default {
canShowAnd,
filterEntriesSync,
getChekedFilters,
getFieldFromRoot,
getOptionData,
isSubLevel,
pushIfNotPresent
}
Loading

0 comments on commit 1fabcf4

Please sign in to comment.