Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
list all profiles when none match conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
siikamiika committed Mar 6, 2020
1 parent c55e4ac commit eacae54
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
10 changes: 7 additions & 3 deletions ext/bg/js/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class Backend {
const {profiles, profileCurrent} = this.options;

if (typeof optionsContext.index === 'number') {
yield {index: optionsContext.index, profile: profiles[optionsContext.index]};
yield {index: optionsContext.index, profile: profiles[optionsContext.index], selected: true};
return;
}

Expand All @@ -233,14 +233,18 @@ class Backend {
for (const profile of profiles) {
const conditionGroups = profile.conditionGroups;
if (conditionGroups.length > 0 && Backend.testConditionGroups(conditionGroups, optionsContext)) {
yield {index: profileIndex, profile, selected: !contextHasResults};
contextHasResults = true;
yield {index: profileIndex, profile};
}
++profileIndex;
}

if (!contextHasResults) {
yield {index: profileCurrent, profile: profiles[profileCurrent]};
profileIndex = 0;
for (const profile of profiles) {
yield {index: profileIndex, profile, selected: profileIndex === profileCurrent};
++profileIndex;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/fg/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Frontend extends TextScanner {
this.optionsContext.url = this.popup.url;
const profileSwitcher = new ProfileSwitcher(await apiProfilesGetMatching(this.optionsContext));
this.setOptions(profileSwitcher);
await this.onProfileChanged(0);
await this.onProfileChanged(profileSwitcher.getIndex());
}

async onProfileChanged(profileIndex) {
Expand Down
6 changes: 3 additions & 3 deletions ext/mixed/js/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,19 @@ class DisplayGenerator {
return node;
}

createProfileSelect(profiles, currentProfileIndex) {
createProfileSelect(profiles, selectedIndex) {
const select = document.createElement('select');

let profileIndex = 0;
for (const profile of profiles) {
const option = document.createElement('option');
option.textContent = profile.name;
option.textContent = profile.profile.name;
option.value = profileIndex;
select.appendChild(option);
++profileIndex;
}

select.selectedIndex = currentProfileIndex;
select.selectedIndex = selectedIndex;

return select;
}
Expand Down
13 changes: 9 additions & 4 deletions ext/mixed/js/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ class Display {

async updateOptions() {
this.profileSwitcher = new ProfileSwitcher(await apiProfilesGetMatching(this.optionsContext));
this.onProfileChanged(0);
this.renderProfileSelect(this.profileSwitcher.getProfiles());
this.renderProfileSelect();
this.onProfileChanged(this.profileSwitcher.getIndex());
}

updateDocumentOptions(options) {
Expand Down Expand Up @@ -415,11 +415,16 @@ class Display {
}
}

renderProfileSelect(profiles) {
renderProfileSelect() {
const profiles = this.profileSwitcher.getProfiles();
const index = this.profileSwitcher.getIndex();

const profileSelectContainer = document.querySelector('#profile-select');
profileSelectContainer.textContent = '';

if (profiles.length <= 1) { return; }
const profileSelect = this.displayGenerator.createProfileSelect(profiles, this.profileIndex);

const profileSelect = this.displayGenerator.createProfileSelect(profiles, index);
profileSelect.addEventListener('change', this.onProfileSelect.bind(this));
profileSelectContainer.appendChild(profileSelect);
}
Expand Down
28 changes: 26 additions & 2 deletions ext/mixed/js/profile-switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class ProfileSwitcher {
constructor(profiles) {
this._index = 0;
this._profiles = profiles;
this._profiles = this._processProfiles(profiles);
}

get options() {
Expand All @@ -34,6 +34,10 @@ class ProfileSwitcher {
return this._profiles[this._index].index;
}

getIndex() {
return this._index;
}

setIndex(index) {
if (index >= this._profiles.length) {
throw new Error('Profile index is out of bounds');
Expand All @@ -42,6 +46,26 @@ class ProfileSwitcher {
}

getProfiles() {
return this._profiles.map((profile) => profile.profile);
return this._profiles;
}

_processProfiles(profiles) {
const processedProfiles = [];

let profileSwitcherIndex = 0;
let selectedIndex = 0;

for (const currentProfile of profiles) {
const {profile, index, selected} = currentProfile;
if (selected) {
selectedIndex = profileSwitcherIndex;
}
processedProfiles.push({profile, index});
++profileSwitcherIndex;
}

this._index = selectedIndex;

return processedProfiles;
}
}

0 comments on commit eacae54

Please sign in to comment.