Skip to content

Commit e788dfd

Browse files
authored
Simplify implementation by removing concept of 'first' and 'second' on selected attributes and dataset (#83)
1 parent 24ab202 commit e788dfd

File tree

4 files changed

+48
-72
lines changed

4 files changed

+48
-72
lines changed

src/linkPanel/model.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ILinkEditorModel,
99
IAdvLinkCategories,
1010
IAdvLinkDescription,
11-
IDatasets,
1211
ComponentLinkType,
1312
IdentityLinkFunction
1413
} from './types';
@@ -25,11 +24,6 @@ export class LinkEditorModel implements ILinkEditorModel {
2524
this._sharedModel.linksChanged.connect(this.onLinksChanged, this);
2625
this._sharedModel.datasetChanged.connect(this.onDatasetsChanged, this);
2726
this._getAdvancedLinksCategories();
28-
29-
this._currentDatasets = {
30-
first: '',
31-
second: ''
32-
};
3327
}
3428

3529
get sharedModel(): IGlueSessionSharedModel {
@@ -39,26 +33,26 @@ export class LinkEditorModel implements ILinkEditorModel {
3933
/**
4034
* Getter and setter for datasets.
4135
*/
42-
get currentDatasets(): IDatasets {
36+
get currentDatasets(): [string, string] {
4337
return this._currentDatasets;
4438
}
45-
set currentDatasets(datasets: IDatasets) {
39+
set currentDatasets(datasets: [string, string]) {
4640
this._currentDatasets = datasets;
4741
this._datasetsChanged.emit(this._currentDatasets);
4842
}
4943

5044
/**
5145
* Replace one current dataset.
5246
*/
53-
setCurrentDataset(position: keyof IDatasets, value: string): void {
54-
this._currentDatasets[position] = value;
47+
setCurrentDataset(index: number, value: string): void {
48+
this._currentDatasets[index] = value;
5549
this._datasetsChanged.emit(this._currentDatasets);
5650
}
5751

5852
/**
5953
* A signal emits when current datasets changes
6054
*/
61-
get currentDatasetsChanged(): ISignal<this, IDatasets> {
55+
get currentDatasetsChanged(): ISignal<this, [string, string]> {
6256
return this._datasetsChanged;
6357
}
6458

@@ -133,10 +127,10 @@ export class LinkEditorModel implements ILinkEditorModel {
133127
// Reset the current dataset, with empty values if there is less than 2 datasets.
134128
if (this._sharedModel.dataset) {
135129
const datasetsList = Object.keys(this._sharedModel.dataset);
136-
this._currentDatasets = {
137-
first: datasetsList.length > 1 ? datasetsList[0] : '',
138-
second: datasetsList.length > 1 ? datasetsList[1] : ''
139-
};
130+
this._currentDatasets = [
131+
datasetsList.length > 1 ? datasetsList[0] : '',
132+
datasetsList.length > 1 ? datasetsList[1] : ''
133+
];
140134
this._datasetsChanged.emit(this._currentDatasets);
141135
}
142136
}
@@ -164,8 +158,8 @@ export class LinkEditorModel implements ILinkEditorModel {
164158
}
165159

166160
private _sharedModel: IGlueSessionSharedModel;
167-
private _currentDatasets: IDatasets;
168-
private _datasetsChanged = new Signal<this, IDatasets>(this);
161+
private _currentDatasets: [string, string] = ['', ''];
162+
private _datasetsChanged = new Signal<this, [string, string]>(this);
169163
private _advLinksPromise = new PromiseDelegate<IAdvLinkCategories>();
170164
private _advLinkCategories: IAdvLinkCategories = {};
171165
private _identityLinks = new Map<string, ILink>();

src/linkPanel/types.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ export const IdentityLinkUsing = {
1212
function: IdentityLinkFunction
1313
};
1414

15-
export const IDatasetsKeys = ['first', 'second'] as (keyof IDatasets)[];
1615
/**
1716
* The link editor model.
1817
*/
1918
export interface ILinkEditorModel {
20-
currentDatasets: IDatasets;
21-
setCurrentDataset(position: keyof IDatasets, value: string): void;
22-
readonly currentDatasetsChanged: ISignal<this, IDatasets>;
19+
currentDatasets: [string, string];
20+
setCurrentDataset(index: number, value: string): void;
21+
readonly currentDatasetsChanged: ISignal<this, [string, string]>;
2322
readonly identityLinks: Map<string, ILink>;
2423
readonly advancedLinks: Map<string, ILink>;
2524
readonly linksChanged: ISignal<this, void>;
@@ -46,11 +45,3 @@ export interface IAdvLinkDescription {
4645
export interface IAdvLinkCategories {
4746
[category: string]: IAdvLinkDescription[];
4847
}
49-
50-
/**
51-
* Definition of the selected datasets.
52-
*/
53-
export interface IDatasets {
54-
first: string;
55-
second: string;
56-
}

src/linkPanel/widgets/linking.tsx

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import {
1818
// IAdvLinkDescription,
1919
ILink,
2020
ILinkEditorModel,
21-
IDatasets,
22-
IDatasetsKeys,
2321
// IdentityLinkFunction,
2422
IdentityLinkUsing
2523
} from '../types';
@@ -63,11 +61,6 @@ export class Linking extends LinkEditorWidget {
6361
);
6462
}
6563

66-
this._identityAttributes = {
67-
first: undefined,
68-
second: undefined
69-
};
70-
7164
this.updateDatasets();
7265
}
7366

@@ -84,7 +77,10 @@ export class Linking extends LinkEditorWidget {
8477
* @param _sender - the link editor model.
8578
* @param selection - the selected datasets.
8679
*/
87-
onDatasetChange = (_sender: ILinkEditorModel, selection: IDatasets): void => {
80+
onDatasetChange = (
81+
_sender: ILinkEditorModel,
82+
selection: [string, string]
83+
): void => {
8884
// this.updateAttributes();
8985
// this.updateAdvancedLink();
9086
};
@@ -104,7 +100,7 @@ export class Linking extends LinkEditorWidget {
104100
updateDatasets(): void {
105101
const currentDatasets = this._linkEditorModel.currentDatasets;
106102

107-
IDatasetsKeys.forEach((position, index) => {
103+
[0, 1].forEach(index => {
108104
const datasetsList = Object.keys(this._sharedModel.dataset);
109105

110106
// Remove all the existing datasets.
@@ -121,22 +117,22 @@ export class Linking extends LinkEditorWidget {
121117
dataset.classList.add('glue-LinkEditor-linkingItem');
122118
dataset.innerText = value;
123119
dataset.onclick = () => {
124-
this.onDatasetSelected(position, dataset);
120+
this.onDatasetSelected(index, dataset);
125121
};
126122
this._datasetsPanels[index].append(dataset);
127123

128124
// Select the current dataset if exists.
129-
if (currentDatasets[position] === value) {
125+
if (currentDatasets[index] === value) {
130126
selectedDataset = dataset;
131127
}
132128
});
133129

134130
// Select a default dataset.
135131
if (selectedDataset) {
136-
this.onDatasetSelected(position, selectedDataset);
132+
this.onDatasetSelected(index, selectedDataset);
137133
} else if (this._datasetsPanels[index].childNodes[index]) {
138134
this.onDatasetSelected(
139-
position,
135+
index,
140136
this._datasetsPanels[index].childNodes[index] as HTMLDivElement
141137
);
142138
}
@@ -149,7 +145,7 @@ export class Linking extends LinkEditorWidget {
149145
* @param position - 'first' or 'second', the column where the widget belongs.
150146
* @param dataset - Element clicked.
151147
*/
152-
onDatasetSelected(position: keyof IDatasets, dataset: HTMLDivElement): void {
148+
onDatasetSelected(index: number, dataset: HTMLDivElement): void {
153149
// no-op if the dataset is already selected.
154150
if (dataset.classList.contains('selected')) {
155151
return;
@@ -162,8 +158,8 @@ export class Linking extends LinkEditorWidget {
162158

163159
// Select the dataset.
164160
dataset.classList.add('selected');
165-
this.updateAttributes(IDatasetsKeys.indexOf(position), dataset.title);
166-
this._linkEditorModel.setCurrentDataset(position, dataset.title);
161+
this.updateAttributes(index, dataset.title);
162+
this._linkEditorModel.setCurrentDataset(index, dataset.title);
167163
}
168164

169165
/**
@@ -202,7 +198,7 @@ export class Linking extends LinkEditorWidget {
202198
this._attributesPanels[index].append(attribute);
203199
});
204200

205-
this._identityAttributes[IDatasetsKeys[index]] = undefined;
201+
this._identityAttributes[index] = undefined;
206202

207203
// Enable/disable the Glue button if datasets are different and attributes selected.
208204
this._updateGlueButtonStatus();
@@ -225,12 +221,12 @@ export class Linking extends LinkEditorWidget {
225221
// Select the attribute.
226222
if (!isSelected) {
227223
attribute.classList.add('selected');
228-
this._identityAttributes[IDatasetsKeys[index]] = {
224+
this._identityAttributes[index] = {
229225
name: attribute.title,
230226
label: attribute.innerText
231227
};
232228
} else {
233-
this._identityAttributes[IDatasetsKeys[index]] = undefined;
229+
this._identityAttributes[index] = undefined;
234230
}
235231

236232
// Enable/disable the Glue button if datasets are different and attributes selected.
@@ -243,27 +239,27 @@ export class Linking extends LinkEditorWidget {
243239
private _updateGlueButtonStatus(): void {
244240
const currentDatasets = this._linkEditorModel.currentDatasets;
245241
const status =
246-
Object.values(this._identityAttributes).every(value => !!value) &&
247-
currentDatasets.first !== currentDatasets.second;
242+
this._identityAttributes.every(value => !!value) &&
243+
currentDatasets[0] !== currentDatasets[1];
248244
this._reloadGlueButton.emit(!status);
249245
}
250246

251247
/**
252248
* Creates identity link.
253249
*/
254250
glueIdentity = (): void => {
255-
if (!(this._identityAttributes.first && this._identityAttributes.second)) {
251+
if (!(this._identityAttributes[0] && this._identityAttributes[1])) {
256252
return;
257253
}
258254

259255
const link: ILink = {
260256
_type: ComponentLinkType,
261-
cids1: [this._identityAttributes.first.name],
262-
cids2: [this._identityAttributes.second.name],
263-
cids1_labels: [this._identityAttributes.first.label],
264-
cids2_labels: [this._identityAttributes.second.label],
265-
data1: this._linkEditorModel.currentDatasets.first,
266-
data2: this._linkEditorModel.currentDatasets.second,
257+
cids1: [this._identityAttributes[0].name],
258+
cids2: [this._identityAttributes[1].name],
259+
cids1_labels: [this._identityAttributes[0].label],
260+
cids2_labels: [this._identityAttributes[1].label],
261+
data1: this._linkEditorModel.currentDatasets[0],
262+
data2: this._linkEditorModel.currentDatasets[1],
267263
inverse: IdentityLinkUsing,
268264
using: IdentityLinkUsing
269265
};
@@ -452,7 +448,10 @@ export class Linking extends LinkEditorWidget {
452448
return panel;
453449
}
454450

455-
private _identityAttributes: Private.ISelectedIdentityAttributes;
451+
private _identityAttributes: [
452+
Private.IAttribute | undefined,
453+
Private.IAttribute | undefined
454+
] = [undefined, undefined];
456455
// private _selectedAdvLink: Private.IAdvancedLinkSelected;
457456
private _datasetsPanels: [HTMLDivElement, HTMLDivElement];
458457
private _attributesPanels: [HTMLDivElement, HTMLDivElement];
@@ -478,14 +477,6 @@ namespace Private {
478477
name: string;
479478
}
480479

481-
/**
482-
* The selected identity attributes.
483-
*/
484-
export interface ISelectedIdentityAttributes {
485-
first?: IAttribute;
486-
second?: IAttribute;
487-
}
488-
489480
/**
490481
* The IO types names.
491482
*/

src/linkPanel/widgets/summary.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import * as React from 'react';
1515

1616
import { LinkEditorWidget } from '../linkEditorWidget';
17-
import { IDatasets, ILink, ILinkEditorModel } from '../types';
17+
import { ILink } from '../types';
1818

1919
/**
2020
* The widget displaying the links for the selected dataset.
@@ -43,27 +43,27 @@ export class Summary extends LinkEditorWidget {
4343
this._linkEditorModel.linksChanged.connect(this.linksChanged, this);
4444

4545
if (this._linkEditorModel.currentDatasets) {
46-
this.updateLinks(this._linkEditorModel.currentDatasets);
46+
this.updateLinks();
4747
}
4848
}
4949

5050
/**
5151
* Triggered when links has changed.
5252
*/
5353
linksChanged(): void {
54-
this.updateLinks(this._linkEditorModel.currentDatasets);
54+
this.updateLinks();
5555
}
5656
/**
5757
* Callback when the selected datasets change.
5858
*/
59-
onDatasetsChange(_sender: ILinkEditorModel, datasets: IDatasets): void {
60-
this.updateLinks(datasets);
59+
onDatasetsChange(): void {
60+
this.updateLinks();
6161
}
6262

6363
/**
6464
* Updates the list of links when the selected dataset changes.
6565
*/
66-
updateLinks(dataset: IDatasets): void {
66+
updateLinks(): void {
6767
const datasetLinks = new Map<string, Private.ISummaryLink[]>();
6868

6969
// Remove all the existing widgets.

0 commit comments

Comments
 (0)