-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up and rename computed variables #10307
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,15 @@ | |
<th> | ||
<select class="form-control" v-model="selectedIdentifier" name="name"> | ||
<option disabled value="">Select one</option> | ||
<template v-if="isEdition"> | ||
<option v-for="entry in popularEditionConfigs" :key="entry.name" :value="entry.name"> | ||
<template v-if="hasPopularIds"> | ||
<option v-for="entry in popularIdDict" :key="entry.name" :value="entry.name"> | ||
{{ entry.label }} | ||
</option> | ||
<option disabled value="">---</option> | ||
<option v-for="entry in secondaryEditionConfigs" :key="entry.name" :value="entry.name"> | ||
{{ entry.label }} | ||
</option> | ||
</template> | ||
<template v-else> | ||
<option v-for="idConfig in identifierConfigsByKey" :key="idConfig.name" :value="idConfig.name"> | ||
{{idConfig.label}} | ||
</option> | ||
</template> | ||
<option v-for="idConfig in identifierConfigsByKey" :key="idConfig.name" :value="idConfig.name"> | ||
{{idConfig.label}} | ||
</option> | ||
</select> | ||
</th> | ||
<th> | ||
|
@@ -60,6 +55,7 @@ const identifierPatterns = { | |
amazon: /^B[0-9A-Za-z]{9}$/, | ||
youtube: /^@[A-Za-z0-9_\-.]{3,30}/, | ||
} | ||
|
||
export default { | ||
// Props are for external options; if a subelement of this is modified, | ||
// the view automatically re-renders | ||
|
@@ -74,13 +70,15 @@ export default { | |
* {"identifiers": [{"label": "ISNI", "name": "isni", "notes": "", "url": "http://www.isni.org/@@@", "website": "http://www.isni.org/"}, ... ]} | ||
*/ | ||
id_config_string: { | ||
type: String | ||
type: String, | ||
required: true | ||
}, | ||
/** see createHiddenInputs function for usage | ||
* #hiddenEditionIdentifiers, #hiddenWorkIdentifiers | ||
*/ | ||
output_selector: { | ||
type: String | ||
type: String, | ||
required: true | ||
}, | ||
input_prefix: { | ||
/* | ||
|
@@ -89,19 +87,20 @@ export default { | |
*/ | ||
type: String | ||
}, | ||
/* Props specifically for Editions */ | ||
/* Editions and works can have multiple identifiers in the form of a list */ | ||
multiple: { | ||
/* Editions can have multiple identifiers in the form of a list */ | ||
type: String, | ||
default: 'false', | ||
}, | ||
admin: { | ||
type: String, | ||
default: 'false', | ||
}, | ||
/* Maintains identifier order in the dropdown list */ | ||
edition_popular: {}, | ||
secondary_identifiers: {} | ||
/** Maintains identifier order in the dropdown list | ||
* Expects a list containing the name field from the | ||
* respective identifiers.yaml file | ||
*/ | ||
popular_ids: {}, | ||
}, | ||
|
||
// Data is for internal stuff. This needs to be a function so that we get | ||
|
@@ -115,40 +114,37 @@ export default { | |
}, | ||
|
||
computed: { | ||
popularEditionConfigs: function() { | ||
if (this.edition_popular) { | ||
const popularConfigs = JSON.parse(decodeURIComponent(this.edition_popular)); | ||
return Object.fromEntries(popularConfigs.map(e => [e.name, e])); | ||
} | ||
return {}; | ||
parsedConfig: function() { | ||
return JSON.parse(decodeURIComponent(this.id_config_string)) | ||
}, | ||
secondaryEditionConfigs: function() { | ||
if (this.secondary_identifiers) { | ||
const secondConfigs = JSON.parse(decodeURIComponent(this.secondary_identifiers)); | ||
return Object.fromEntries(secondConfigs.map(e => [e.name, e])); | ||
popularIdDict: function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, looking great! This can now just be a list, since we never have to do a look-up by identifier name, which makes this code much tighter! popularIds: function() {
const popularIdNames = JSON.parse(decodeURIComponent(this.popular_ids));
return this.parsedConfig
.filter((config) => popularConfig.includes(config.name));
}, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And to keep the naming consistent, let's renaming the property to |
||
if (this.popular_ids) { | ||
let popularConfig = JSON.parse(decodeURIComponent(this.popular_ids)) | ||
popularConfig = this.parsedConfig.filter((config) => { | ||
if (popularConfig.includes(config.name)) { | ||
return true; | ||
} | ||
return false; | ||
}) | ||
return Object.fromEntries(popularConfig.map(e => [e.name, e])) | ||
} | ||
return {}; | ||
}, | ||
identifierConfigsByKey: function(){ | ||
const parsedConfigs = JSON.parse(decodeURIComponent(this.id_config_string)); | ||
if (this.isEdition) { | ||
const betterConfigs = JSON.parse(decodeURIComponent(this.id_config_string)); | ||
return Object.fromEntries(betterConfigs.map(e => [e.name, e])); | ||
} | ||
return Object.fromEntries(parsedConfigs['identifiers'].map(e => [e.name, e])); | ||
identifierConfigsByKey: function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This now also looks un-needed and can be removed ; we can replace where this is used with the |
||
return Object.fromEntries(this.parsedConfig.map(e => [e.name, e])); | ||
}, | ||
isAdmin() { | ||
isAdmin: function() { | ||
return this.admin.toLowerCase() === 'true'; | ||
}, | ||
isEdition() { | ||
return this.multiple.toLowerCase() === 'true' && this.edition_popular; | ||
}, | ||
saveIdentifiersAsList() { | ||
saveIdentifiersAsList: function() { | ||
return this.multiple.toLowerCase() === 'true'; | ||
}, | ||
setButtonEnabled: function(){ | ||
return this.selectedIdentifier !== '' && this.inputValue !== ''; | ||
} | ||
}, | ||
hasPopularIds: function() { | ||
return Object.keys(this.popularIdDict).length !== 0; | ||
}, | ||
}, | ||
|
||
methods: { | ||
|
@@ -218,7 +214,7 @@ export default { | |
}, | ||
selectIdentifierByInputValue: function() { | ||
// Ignore for edition identifiers | ||
if (this.isEdition) { | ||
if (this.saveIdentifiersAsList) { | ||
return; | ||
} | ||
// Selects the dropdown identifier based on the input value when possible | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -447,7 +447,7 @@ | |
$ set1 = [id_dict[name] for name in popular if name in id_dict and name not in exclude] | ||
$ set2 = sorted([id for id in edition_config.identifiers if id.name not in popular and id.name not in exclude], key=lambda id:id.label) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh and |
||
|
||
$:render_component('IdentifiersInput', attrs=dict(id_config_string=edition_config.identifiers, assigned_ids_string=book.get_identifiers().values(), input_prefix='edition--identifiers', output_selector='#hiddenEditionIdentifiers', multiple='true', admin=str(is_privileged_user), edition_popular=set1,secondary_identifiers=set2)) | ||
$:render_component('IdentifiersInput', attrs=dict(id_config_string=edition_config.identifiers, assigned_ids_string=book.get_identifiers().values(), input_prefix='edition--identifiers', output_selector='#hiddenEditionIdentifiers', multiple='true', popular_ids=popular,admin=str(is_privileged_user))) | ||
</div> | ||
</div> | ||
</div> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this can then be renamed to
idConfig
, and we don't specifyparsed