1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {FormatService} from '@eg/core/format.service';
4 import {AdminPageComponent} from '@eg/staff/share/admin-page/admin-page.component';
5 import {ActivatedRoute} from '@angular/router';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {PermService} from '@eg/core/perm.service';
11 import {AuthService} from '@eg/core/auth.service';
12 import {NetService} from '@eg/core/net.service';
13 import {GridCellTextGenerator} from '@eg/share/grid/grid';
14 import {StringComponent} from '@eg/share/string/string.component';
15 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
18 templateUrl: './admin-carousel.component.html'
21 export class AdminCarouselComponent extends AdminPageComponent implements OnInit {
26 refreshSelected: (idlThings: IdlObject[]) => void;
27 createNew: () => void;
28 deleteSelected: (idlThings: IdlObject[]) => void;
29 cellTextGenerator: GridCellTextGenerator;
31 @ViewChild('refreshString', { static: true }) refreshString: StringComponent;
32 @ViewChild('refreshErrString', { static: true }) refreshErrString: StringComponent;
35 route: ActivatedRoute,
37 format: FormatService,
44 private net: NetService
46 super(route, ngLocation, format, idl, org, auth, pcrud, perm, toast);
52 this.classLabel = this.idlClassDef.label;
53 this.includeOrgDescendants = true;
54 this.cellTextGenerator = {
55 bucket: row => row.bucket().name()
58 this.createNew = () => {
62 this.deleteSelected = (idlThings: IdlObject[]) => {
63 super.deleteSelected(idlThings);
66 this.refreshSelected = (idlThings: IdlObject[]) => {
67 idlThings.forEach(cc => {
68 if (cc.type().automatic() === 't') {
71 'open-ils.actor.carousel.refresh',
72 this.auth.token(), cc.id()
73 ).toPromise(); // fire and forget, as this could take a couple minutes
74 this.refreshString.current({ name: cc.name() }).then(str => this.toast.success(str));
76 this.refreshErrString.current({ name: cc.name() }).then(str => this.toast.warning(str));
81 this.editSelected = (carouselFields: IdlObject[]) => {
82 // Edit each IDL thing one at a time
83 const editOneThing = (carousel: IdlObject) => {
84 if (!carousel) { return; }
86 this.showEditDialog(carousel).then(
87 () => editOneThing(carouselFields.shift()));
90 editOneThing(carouselFields.shift());
94 mungeCarousel(editMode: string, rec: IdlObject) {
95 if (editMode === 'create') {
96 rec.creator(this.auth.user().id());
98 rec.editor(this.auth.user().id());
101 // convert empty string to nulls as needed
103 if (rec.owning_lib_filter() === '') {
104 rec.owning_lib_filter(null);
106 if (rec.copy_location_filter() === '') {
107 rec.copy_location_filter(null);
111 postSave(rec: IdlObject) {
112 if (rec._isfieldmapper) {
113 // if we got an actual IdlObject back, the
114 // record had just been created, not just
115 // edited. therefore, we probably need
116 if (rec.bucket() == null) {
117 const bucket = this.idl.create('cbreb');
118 bucket.owner(this.auth.user().id());
119 bucket.name('System-generated bucket for carousel: ' + rec.id()); // FIXME I18N
120 bucket.btype('carousel');
122 bucket.owning_lib(rec.owner());
126 'open-ils.actor.container.create',
127 this.auth.token(), 'biblio', bucket
130 const ccou = this.idl.create('ccou');
131 ccou.carousel(rec.id());
132 ccou.org_unit(rec.owner());
134 rec.bucket(newBucket);
135 this.pcrud.create(ccou).subscribe(
137 this.pcrud.update(rec).subscribe(
138 ok2 => console.debug('updated'),
139 err => console.error(err),
140 () => { this.grid.reload(); }
143 err => console.error(err),
144 () => { this.grid.reload(); }