type EgOrgNodeOrId = number | EgIdlObject;
+interface OrgFilter {
+ canHaveUsers?: boolean;
+ canHaveVolumes?: boolean;
+ opacVisible?: boolean;
+}
+
@Injectable()
export class EgOrgService {
return this.orgList;
};
+ /**
+ * Returns a list of org units that match the selected criteria.
+ * Unset filter options are ignored.
+ */
+ filterList(filter: OrgFilter, asId: boolean): any[] {
+ let list = [];
+ this.list().forEach(org => {
+
+ let chu = filter.canHaveUsers;
+ if (chu && !this.canHaveUsers(org)) return;
+ if (chu === false && this.canHaveUsers(org)) return;
+
+ let chv = filter.canHaveVolumes;
+ if (chv && !this.canHaveVolumes(org)) return;
+ if (chv === false && this.canHaveVolumes(org)) return;
+
+ let ov = filter.opacVisible;
+ if (ov && !this.opacVisible(org)) return;
+ if (ov === false && this.opacVisible(org)) return;
+
+ // All filter tests passed. Add it to the list
+ list.push(asId ? org.id() : org);
+ });
+
+ return list;
+ }
+
tree(): EgIdlObject {
return this.orgTree;
}
}
// list of org_unit objects or IDs for ancestors + me
- ancestors(nodeOrId: EgOrgNodeOrId, asId: Boolean): EgIdlObject[] {
+ ancestors(nodeOrId: EgOrgNodeOrId, asId?: boolean): any[] {
let node = this.get(nodeOrId);
if (!node) return [];
let nodes = [node];
while( (node = this.get(node.parent_ou())))
nodes.push(node);
- if (asId)
- return nodes.map(function(n){return n.id()});
+ if (asId) return nodes.map(n => n.id());
return nodes;
};
// tests that a node can have users
- canHaveUsers(nodeOrId): Boolean {
+ canHaveUsers(nodeOrId): boolean {
return this
.get(nodeOrId)
.ou_type()
}
// tests that a node can have volumes
- canHaveVolumes(nodeOrId): Boolean {
+ canHaveVolumes(nodeOrId): boolean {
return this
.get(nodeOrId)
.ou_type()
.can_have_vols() == 't';
}
+ opacVisible(nodeOrId): boolean {
+ return this.get(nodeOrId).opac_visible() == 't';
+ }
+
// list of org_unit objects or IDs for me + descendants
- descendants(nodeOrId: EgOrgNodeOrId, asId: Boolean): EgIdlObject[] {
+ descendants(nodeOrId: EgOrgNodeOrId, asId?: boolean): any[] {
let node = this.get(nodeOrId);
if (!node) return [];
let nodes = [];
}
// list of org_unit objects or IDs for ancestors + me + descendants
- fullPath(nodeOrId: EgOrgNodeOrId, asId: Boolean): EgIdlObject[] {
+ fullPath(nodeOrId: EgOrgNodeOrId, asId?: boolean): any[] {
let list = this.ancestors(nodeOrId, false).concat(
this.descendants(nodeOrId, false).slice(1));
if (asId)
<div class="row mt-2">
<div class="col-2">
<eg-org-select
- [onChange]="orgOnChange"
- [shouldHide]="shouldHideOrg"
- [shouldDisable]="shouldDisableOrg"
+ (onChange)="orgOnChange"
+ [hideOrgs]="hideOrgs"
+ [disableOrgs]="disableOrgs"
[initialOrg]="initialOrg"
[placeholder]="'Owner'" >
</eg-org-select>
newOwner: EgIdlObject;
newName: String;
- //initialOrg: EgIdlObject; // XXX testing
-
- // Org selector callbacks
- shouldDisableOrg = (org: EgIdlObject): boolean => {
- // TODO: check register perms too
- return !this.org.canHaveUsers(org);
- }
-
- shouldHideOrg = (org: EgIdlObject): boolean => {
- return org.id() == 1; // XXX TESTING
- }
+ // Org selector options.
+ hideOrgs: number[];
+ disableOrgs: number[];
orgOnChange = (org: EgIdlObject): void => {
this.newOwner = org;
}
this.store.getItem('eg.workstation.all')
.then(res => this.workstations = res);
- // this.initialOrg = this.org.root().children()[0];
+ // TODO: perm limits required here too
+ this.disableOrgs = this.org.filterList({canHaveUsers : true}, true);
}
selected(): Workstation {
<div class="col-9 d-flex flex-row">
<div class="flex-1">
<eg-org-select
- [onChange]="orgOnChange"
+ (onChange)="orgOnChange($event)"
[initialOrg]="searchContext.searchOrg"
[placeholder]="'Library'" >
</eg-org-select>
-import {Component, OnInit, Input} from '@angular/core';
+import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {map, debounceTime} from 'rxjs/operators';
import {EgAuthService} from '@eg/core/auth';
export class EgOrgSelectComponent implements OnInit {
selected: OrgDisplay;
+ startOrg: EgIdlObject;
+ hidden: number[] = [];
+ disabled: number[] = [];
// Read-only properties optionally provided by the calling component.
@Input() placeholder: string;
- @Input() initialOrg: EgIdlObject;
@Input() stickySetting: string;
@Input() displayField: string = 'shortname';
- // Call-backs
- // Note: onChange could be handled via an EventEmitter, but
- // should* functions require real time two-way communication.
- @Input() onChange: (org:EgIdlObject) => void;
- @Input() shouldDisable: (org:EgIdlObject) => boolean;
- @Input() shouldHide: (org:EgIdlObject) => boolean;
+ @Input() set initialOrg(org: EgIdlObject) {
+ if (org) this.startOrg = org;
+ }
+
+ @Input() set hideOrgs(ids: number[]) {
+ if (ids) this.hidden = ids;
+ }
+
+ @Input() set disableOrgs(ids: number[]) {
+ if (ids) this.disabled = ids;
+ }
+
+ /** Emitted when the org unit value is changed via the selector.
+ * Does not fire on initialOrg.
+ */
+ @Output() onChange = new EventEmitter<EgIdlObject>();
constructor(
private auth: EgAuthService,
) {}
ngOnInit() {
- if (this.initialOrg) {
- this.selected = this.formatForDisplay(this.initialOrg);
+ if (this.startOrg) {
+ this.selected = this.formatForDisplay(this.startOrg);
}
}
}
orgChanged(selEvent: NgbTypeaheadSelectItemEvent) {
- if (this.onChange) {
- this.onChange(this.org.get(selEvent.item.id));
- }
+ this.onChange.emit(this.org.get(selEvent.item.id));
}
// Formats the selected value
return org[this.displayField]()
.toLowerCase().indexOf(term.toLowerCase()) > -1
- }).filter(org => {
- // Exclude hidden orgs
- return !this.shouldHide || !this.shouldHide(org)
+ }).filter(org => { // Exclude hidden orgs
+ return this.hidden.filter(
+ id => {return org.id() == id}).length == 0;
- }).map(org => { return this.formatForDisplay(org) })
+ }).map(org => {return this.formatForDisplay(org)})
});
}
}