-
Notifications
You must be signed in to change notification settings - Fork 31
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
Use optgroup to distinguish between campaigns and topics #1053
Changes from all commits
efb44a4
fd79583
8cc1ce9
f15d0e1
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 |
---|---|---|
|
@@ -107,6 +107,11 @@ export class ActionApp { | |
|
||
addToState(key, value) { | ||
this.persistentState.set(key, value); | ||
if (key == 'campaign') { | ||
this.persistentState.delete('topic'); | ||
} else if (key == 'topic') { | ||
this.persistentState.delete('campaign'); | ||
} | ||
this.serializeStateToURL(); | ||
} | ||
|
||
|
@@ -424,6 +429,9 @@ export class ActionApp { | |
this.campaignSelect = $('#selected-campaign'); | ||
fetchJSON(this.config.urls.campaignList) | ||
.then(data => { | ||
let campaignOptGroup = document.createElement('optgroup'); | ||
campaignOptGroup.label = 'Campaigns'; | ||
this.campaignSelect.appendChild(campaignOptGroup); | ||
data.objects.forEach(campaign => { | ||
let o = document.createElement('option'); | ||
o.value = campaign.id; | ||
|
@@ -436,7 +444,7 @@ export class ActionApp { | |
} | ||
); | ||
|
||
this.campaignSelect.appendChild(o); | ||
campaignOptGroup.appendChild(o); | ||
}); | ||
}) | ||
.then(() => { | ||
|
@@ -448,12 +456,18 @@ export class ActionApp { | |
}); | ||
|
||
this.campaignSelect.addEventListener('change', () => { | ||
this.addToState('campaign', this.campaignSelect.value); | ||
let campaignOrTopic = this.getSelectedOptionType(); | ||
this.addToState(campaignOrTopic, this.campaignSelect.value); | ||
this.updateAssetList(); | ||
}); | ||
|
||
fetchJSON(this.config.urls.topicList) | ||
.then(data => { | ||
let topicOptGroup = document.createElement('optgroup'); | ||
topicOptGroup.label = 'Topics'; | ||
topicOptGroup.classList.add('topic-optgroup'); | ||
this.campaignSelect.appendChild(topicOptGroup); | ||
|
||
data.objects.forEach(topic => { | ||
let o = document.createElement('option'); | ||
o.value = topic.id; | ||
|
@@ -466,7 +480,7 @@ export class ActionApp { | |
} | ||
); | ||
|
||
this.campaignSelect.appendChild(o); | ||
topicOptGroup.appendChild(o); | ||
}); | ||
}) | ||
.then(() => { | ||
|
@@ -792,6 +806,29 @@ export class ActionApp { | |
return sortBy(assetList, keyFromAsset); | ||
} | ||
|
||
getSelectedOptionType() { | ||
let campaignOrTopic = 'campaign'; | ||
|
||
if ( | ||
this.campaignSelect.options[ | ||
this.campaignSelect.selectedIndex | ||
].parentElement.classList.contains('topic-optgroup') | ||
) { | ||
campaignOrTopic = 'topic'; | ||
} | ||
|
||
return campaignOrTopic; | ||
} | ||
|
||
assetHasTopic(asset, topicId) { | ||
for (let idx in asset.topics) { | ||
if (asset.topics[idx].id === topicId) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
getVisibleAssets() { | ||
// We allow passing a list of asset IDs which should always be included | ||
// to avoid jarring UI transitions (the display code is responsible for | ||
|
@@ -807,10 +844,22 @@ export class ActionApp { | |
alwaysIncludedAssetIDs.add(this.persistentState.get('asset')); | ||
} | ||
|
||
let currentCampaignId = this.campaignSelect.value; | ||
if (currentCampaignId) { | ||
let currentCampaignSelectValue = this.campaignSelect.value; | ||
let currentCampaignId; | ||
let currentTopicId; | ||
if (currentCampaignSelectValue) { | ||
let campaignOrTopic = this.getSelectedOptionType(); | ||
currentCampaignSelectValue = parseInt( | ||
currentCampaignSelectValue, | ||
10 | ||
); | ||
|
||
// The values specified in API responses are integers, not DOM strings: | ||
currentCampaignId = parseInt(currentCampaignId, 10); | ||
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. We could remove this block by converting 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. I can de-dupe the 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. The de-dupe is what I was thinking about. Leaving the variables is a little busy but the ways to refactor that would either take repeated extra string comparisons against |
||
if (campaignOrTopic == 'campaign') { | ||
currentCampaignId = currentCampaignSelectValue; | ||
} else { | ||
currentTopicId = currentCampaignSelectValue; | ||
} | ||
} | ||
|
||
let currentStatuses; | ||
|
@@ -839,6 +888,13 @@ export class ActionApp { | |
continue; | ||
} | ||
|
||
if ( | ||
currentTopicId && | ||
!this.assetHasTopic(asset, currentTopicId) | ||
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. I'm wondering whether this should simply be 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. Only if I flatten 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. Ah, right – I hadn't gotten that far in the code yet when I left that comment. It might be nice to avoid a check-at-a-distance but I'm not sure that's worth changing |
||
) { | ||
continue; | ||
} | ||
|
||
if (!currentStatuses.includes(asset.status)) { | ||
continue; | ||
} | ||
|
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.
I'd probably favor a class name which doesn't describe the tag (e.g.
topics
) but I'm not sure it's that big a deal.