Skip to content

Commit

Permalink
Fixes #718 by moving the comparators into ApiComparators
Browse files Browse the repository at this point in the history
Fixes #725 by eliminating one of the inits that was basically useless
  • Loading branch information
dickschoeller committed Jun 15, 2018
1 parent 379d1f0 commit 0da02c1
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {Component, OnInit, Input} from '@angular/core';
import {MenuItem, SelectItem} from 'primeng/api';
import { Component, OnInit, Input } from '@angular/core';
import { MenuItem, SelectItem } from 'primeng/api';

import {SourceCreator} from '../../bases';
import {HasAttributeList} from '../../interfaces';
import {ApiAttribute, ApiSource, LinkSourceDialogData, LinkSourceItem} from '../../models';
import {NewSourceLinkService, SourceService} from '../../services';
import {NewSourceHelper, UrlBuilder} from '../../utils';
import { SourceCreator } from '../../bases';
import { HasAttributeList } from '../../interfaces';
import { ApiAttribute, ApiSource, LinkSourceDialogData, LinkSourceItem } from '../../models';
import { NewSourceLinkService, SourceService } from '../../services';
import { ApiComparators, NewSourceHelper, UrlBuilder } from '../../utils';

import {LinkSourceDialogComponent} from '../link-source-dialog';
import {NewSourceDialogComponent} from '../new-source-dialog';
import { LinkSourceDialogComponent } from '../link-source-dialog';
import { NewSourceDialogComponent } from '../new-source-dialog';

@Component({
selector: 'app-attribute-list-item-sources',
Expand Down Expand Up @@ -42,6 +42,8 @@ export class AttributeListItemSourcesComponent extends SourceCreator implements
},
];

comparator: ApiComparators = new ApiComparators();

constructor(
public sourceService: SourceService,
public newSourceLinkService: NewSourceLinkService,
Expand Down Expand Up @@ -104,7 +106,7 @@ export class AttributeListItemSourcesComponent extends SourceCreator implements
this.sourceService.getAll(data.dataset).subscribe(
(value: ApiSource[]) => {
data.sources = value;
data.sources.sort(data.compare);
data.sources.sort(this.comparator.compareSources);
data._data = new LinkSourceDialogData();
let index = 0;
for (const source of data.sources) {
Expand Down Expand Up @@ -143,7 +145,7 @@ export class AttributeListItemSourcesComponent extends SourceCreator implements
this.sourceService.getAll(data.dataset).subscribe(
(value: ApiSource[]) => {
data.sources = value;
data.sources.sort(data.compare);
data.sources.sort(this.comparator.compareSources);
data._data = new LinkSourceDialogData();
let index = 0;
for (const attribute of this.attribute.attributes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Component, OnInit, Input, EventEmitter, Output, OnDestroy, OnChanges} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import { Component, OnInit, Input, EventEmitter, Output, OnDestroy, OnChanges } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import {BaseDialog} from '../../bases';
import {ApiPerson, LinkPersonItem, LinkPersonDialogData} from '../../models';
import { BaseDialog } from '../../bases';
import { ApiPerson, LinkPersonItem, LinkPersonDialogData } from '../../models';

@Component({
selector: 'app-link-person-dialog',
Expand All @@ -14,7 +14,6 @@ export class LinkPersonDialogComponent
implements OnInit, OnChanges {
@Input() titleString: string;
@Input() multi: boolean;
dataset: string;
persons: Array<ApiPerson>;

_data: LinkPersonDialogData = new LinkPersonDialogData();
Expand All @@ -37,21 +36,6 @@ export class LinkPersonDialogComponent
}

private init(): void {
this.route.params.subscribe((params) => {
this.dataset = params['dataset'];
});
}

/**
* Comparison function to sort the persons returned.
* Index name is the first level sort.
* If those are the same, then by ID.
*/
compare = function(a: ApiPerson, b: ApiPerson) {
const val = a.indexName.localeCompare(b.indexName);
if (val !== 0) {
return val;
}
return a.string.localeCompare(b.string);
};
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Component, OnInit, Input, EventEmitter, Output, OnDestroy, OnChanges} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import { Component, OnInit, Input, EventEmitter, Output, OnDestroy, OnChanges } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import {BaseDialog} from '../../bases';
import {ApiSource, LinkSourceItem, LinkSourceDialogData} from '../../models';
import { BaseDialog } from '../../bases';
import { ApiSource, LinkSourceItem, LinkSourceDialogData } from '../../models';

@Component({
selector: 'app-link-source-dialog',
Expand Down Expand Up @@ -40,17 +40,4 @@ export class LinkSourceDialogComponent
this.dataset = params['dataset'];
});
}

/**
* Comparison function to sort the persons returned.
* Index name is the first level sort.
* If those are the same, then by ID.
*/
compare = function(a: ApiSource, b: ApiSource) {
const val = a.title.localeCompare(b.title);
if (val !== 0) {
return val;
}
return a.string.localeCompare(b.string);
};
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Component, OnInit, OnChanges} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import { Component, OnInit, OnChanges } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import {NewPersonHelper} from '../../utils';
import {ApiPerson} from '../../models';
import {PersonService} from '../../services';
import { ApiPerson } from '../../models';
import { PersonService } from '../../services';
import { ApiComparators, NewPersonHelper } from '../../utils';

@Component({
selector: 'app-person-list-page',
Expand All @@ -14,6 +14,8 @@ export class PersonListPageComponent implements OnInit, OnChanges {
dataset: string;
persons: ApiPerson[];

comparator: ApiComparators = new ApiComparators();

constructor(private route: ActivatedRoute,
private personService: PersonService,
private router: Router) { }
Expand All @@ -33,24 +35,11 @@ export class PersonListPageComponent implements OnInit, OnChanges {
this.route.data.subscribe(
(data: {dataset: string, persons: ApiPerson[]}) => {
this.persons = data.persons;
this.persons.sort(this.compare);
this.persons.sort(this.comparator.comparePersons);
}
);
}

/**
* Comparison function to sort the persons returned.
* Index name is the first level sort.
* If those are the same, then by ID.
*/
compare = function(a: ApiPerson, b: ApiPerson) {
const val = a.indexName.localeCompare(b.indexName);
if (val !== 0) {
return val;
}
return a.string.localeCompare(b.string);
};

refreshPerson(): void {
this.router.routeReuseStrategy.shouldReuseRoute = function() {
return false;
Expand All @@ -60,7 +49,7 @@ export class PersonListPageComponent implements OnInit, OnChanges {

this.personService.getAll(this.dataset).subscribe(
(persons: Array<ApiPerson>) => {
this.persons = persons.sort(this.compare);
this.persons = persons.sort(this.comparator.comparePersons);
}
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Component, OnInit, OnChanges} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import { Component, OnInit, OnChanges } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import {ApiSource} from '../../models';
import {SourceService} from '../../services';
import { ApiSource } from '../../models';
import { SourceService } from '../../services';
import { ApiComparators } from '../../utils';

@Component({
selector: 'app-source-list-page',
Expand All @@ -13,6 +14,8 @@ export class SourceListPageComponent implements OnInit, OnChanges {
dataset: string;
sources: Array<ApiSource>;

comparator: ApiComparators = new ApiComparators();

constructor(private route: ActivatedRoute,
private sourceService: SourceService,
private router: Router) { }
Expand All @@ -32,24 +35,11 @@ export class SourceListPageComponent implements OnInit, OnChanges {
this.route.data.subscribe(
(data: {dataset: string, sources: ApiSource[]}) => {
this.sources = data.sources;
this.sources.sort(this.compare);
this.sources.sort(this.comparator.compareSources);
}
);
}

/**
* Comparison function to sort the persons returned.
* Index name is the first level sort.
* If those are the same, then by ID.
*/
compare = function(a: ApiSource, b: ApiSource) {
const val = a.title.localeCompare(b.title);
if (val !== 0) {
return val;
}
return a.string.localeCompare(b.string);
};

refreshSource(): void {
this.router.routeReuseStrategy.shouldReuseRoute = function() {
return false;
Expand All @@ -59,7 +49,7 @@ export class SourceListPageComponent implements OnInit, OnChanges {

this.sourceService.getAll(this.dataset).subscribe(
(sources: Array<ApiSource>) => {
this.sources = sources.sort(this.compare);
this.sources = sources.sort(this.comparator.compareSources);
}
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Component, OnInit, OnChanges} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import { Component, OnInit, OnChanges } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

import {ApiSubmitter} from '../../models';
import {SubmitterService} from '../../services';
import { ApiSubmitter } from '../../models';
import { SubmitterService } from '../../services';
import { ApiComparators } from '../../utils';

@Component({
selector: 'app-submitter-list-page',
Expand All @@ -13,6 +14,8 @@ export class SubmitterListPageComponent implements OnInit, OnChanges {
dataset: string;
submitters: Array<ApiSubmitter>;

comparator: ApiComparators = new ApiComparators();

constructor(private route: ActivatedRoute,
private submitterService: SubmitterService,
private router: Router
Expand All @@ -33,24 +36,11 @@ export class SubmitterListPageComponent implements OnInit, OnChanges {
this.route.data.subscribe(
(data: {submitters: ApiSubmitter[]}) => {
this.submitters = data.submitters;
this.submitters.sort(this.compare);
this.submitters.sort(this.comparator.compareSubmitters);
}
);
}

/**
* Comparison function to sort the persons returned.
* Index name is the first level sort.
* If those are the same, then by ID.
*/
compare = function(a: ApiSubmitter, b: ApiSubmitter) {
const val = a.name.localeCompare(b.name);
if (val !== 0) {
return val;
}
return a.string.localeCompare(b.string);
};

refreshSubmitter(): void {
this.router.routeReuseStrategy.shouldReuseRoute = function() {
return false;
Expand All @@ -60,7 +50,7 @@ export class SubmitterListPageComponent implements OnInit, OnChanges {

this.submitterService.getAll(this.dataset).subscribe(
(submitters: Array<ApiSubmitter>) => {
this.submitters = submitters.sort(this.compare);
this.submitters = submitters.sort(this.comparator.compareSubmitters);
alert('there are ' + this.submitters.length + ' submitters');
}
);
Expand Down
23 changes: 23 additions & 0 deletions gedbrowserng-frontend/src/app/utils/api-comparators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ApiSource, ApiPerson, ApiSubmitter } from '../models';

export class ApiComparators {
comparePersons = function(a: ApiPerson, b: ApiPerson) {
return this.twoPartCompare(a.indexName, a. string, b.indexName, b.string);
};

compareSources = function(a: ApiSource, b: ApiSource) {
return this.twoPartCompare(a.title, a. string, b.title, b.string);
};

compareSubmitters = function(a: ApiSubmitter, b: ApiSubmitter) {
return this.twoPartCompare(a.name, a.string, b.name, b.string);
};

private twoPartCompare(a1: string, a2: string, b1: string, b2: string): number {
const val = a1.localeCompare(b1);
if (val !== 0) {
return val;
}
return a2.localeCompare(b2);
}
}
1 change: 1 addition & 0 deletions gedbrowserng-frontend/src/app/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './api-comparators';
export * from './image-util';
export * from './lifespan-util';
export * from './string-util';
Expand Down
6 changes: 5 additions & 1 deletion gedbrowserng-frontend/src/app/utils/link-person-helper.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { LinkCheck } from '../interfaces';
import { ApiPerson, LinkPersonDialogData } from '../models';
import { PersonService } from '../services';

import { ApiComparators } from './api-comparators';
import { LifespanUtil } from './lifespan-util';

export class LinkPersonHelper {
comparator: ApiComparators = new ApiComparators();

constructor(private personService: PersonService) {}

onLinkChildDialogOpen(dialogComponent: any, component: LinkCheck) {
this.personService.getAll(dialogComponent.dataset).subscribe(
(value: ApiPerson[]) => {
dialogComponent.persons = value;
dialogComponent.persons.sort(dialogComponent.compare);
dialogComponent.persons.sort(this.comparator.comparePersons);
dialogComponent._data = new LinkPersonDialogData();
for (const person of dialogComponent.persons) {
if (this.alreadyLinked(person, component)) {
Expand Down

0 comments on commit 0da02c1

Please sign in to comment.