refactor the logic of multi select

This commit is contained in:
joey 2020-03-09 11:01:16 -04:00
parent c12968fac2
commit 1f6b60c07c

View file

@ -344,9 +344,8 @@ const Logic = {
return this._currentSelectedIdentities; return this._currentSelectedIdentities;
}, },
addSelectedIdentiry(identity) { addSelectedIdentity(identity) {
const index = this._currentSelectedIdentities.indexOf(identity); if (!this._currentSelectedIdentities.includes(identity)) {
if (index === -1) {
this._currentSelectedIdentities.push(identity); this._currentSelectedIdentities.push(identity);
} }
}, },
@ -988,8 +987,8 @@ Logic.registerPanel(P_CONTAINER_INFO, {
Logic.registerPanel(P_CONTAINERS_EDIT, { Logic.registerPanel(P_CONTAINERS_EDIT, {
panelSelector: "#edit-containers-panel", panelSelector: "#edit-containers-panel",
selectedHistory: [], lastSelected: null,
switchOn: 0, shiftOn: 0,
// This method is called when the object is registered. // This method is called when the object is registered.
initialize() { initialize() {
@ -1009,13 +1008,13 @@ Logic.registerPanel(P_CONTAINERS_EDIT, {
document.addEventListener("keydown", e => { document.addEventListener("keydown", e => {
if (e.keyCode === 16) { if (e.keyCode === 16) {
this.switchOn = 1; this.shiftOn = 1;
} }
}); });
document.addEventListener("keyup", e => { document.addEventListener("keyup", e => {
if (e.keyCode === 16) { if (e.keyCode === 16) {
this.switchOn = 0; this.shiftOn = 0;
} }
}); });
}, },
@ -1059,30 +1058,35 @@ Logic.registerPanel(P_CONTAINERS_EDIT, {
} else if (e.target.matches(".delete-container-icon") || e.target.parentNode.matches(".delete-container-icon")) { } else if (e.target.matches(".delete-container-icon") || e.target.parentNode.matches(".delete-container-icon")) {
Logic.showPanel(P_CONTAINER_DELETE, identity); Logic.showPanel(P_CONTAINER_DELETE, identity);
} else if (e.target.matches(".select-container") || e.target.parentNode.matches(".select-container")) { } else if (e.target.matches(".select-container") || e.target.parentNode.matches(".select-container")) {
console.log(this.switchOn); console.log(this.shiftOn);
const index = this.selectedHistory.indexOf(identity); const currentSelectedIdentity = Logic.currentSelectedIdentities();
const index = currentSelectedIdentity.indexOf(identity);
if (this.switchOn === 1) { if (this.shiftOn === 0) {
const identities = Logic.identities(); this.lastSelected = identity;
let start = identities.indexOf(this.selectedHistory[this.selectedHistory.length-1]);
let end = identities.indexOf(identity);
if (start > end) {
const tmp = start;
end = start;
start = tmp;
}
for (let i = start; i <= end; i++) {
Logic.addSelectedIdentiry(identities[i]);
}
} else if (index === -1) {
this.selectedHistory.push(identity);
Logic.addSelectedIdentiry(identity);
} else if (this.switchOn === 0){
this.selectedHistory.splice(index, 1);
Logic.removeSelectedIdentity(identity);
} }
const identities = Logic.identities();
let start = identities.indexOf(this.lastSelected);
let end = Logic.identities().indexOf(identity);
if (start > end) {
const tmp = start;
start = end;
end = tmp;
}
if (index === -1) {
for (let i = start; i <= end; i++) {
Logic.addSelectedIdentity(identities[i]);
}
} else {
for (let i = start; i <= end; i++) {
Logic.removeSelectedIdentity(identities[i]);
}
}
this.lastSelected = identity;
console.log(Logic.currentSelectedIdentities()); console.log(Logic.currentSelectedIdentities());
} }
}); });