-
Notifications
You must be signed in to change notification settings - Fork 1
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 #3 from J9rem/master
refactor(js) + fix fieldName detection
- Loading branch information
Showing
32 changed files
with
1,509 additions
and
740 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
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 | ||
} |
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,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> | ||
` | ||
} |
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,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 | ||
} |
Oops, something went wrong.