this.values[field] = value;
}
- // TODO: handle circ_lib, owning_lib changes specially
+ if (field === 'owning_lib') {
+ return this.owningLibChanged(value);
+ }
this.context.copyList().forEach(copy => {
if (copy[field] && copy[field]() !== value) {
});
}
+ owningLibChanged(orgId: number) {
+ if (!orgId) { return; }
+
+ let promise = Promise.resolve();
+
+ // Map existing vol IDs to their replacments.
+ const newVols: any = {};
+
+ this.context.copyList().forEach(copy => {
+
+ // Change the copy circ lib to match the new owning lib
+ // if configured to do so.
+ if (this.volcopy.defaults.values.circ_lib_mod_with_owning_lib) {
+ if (copy.circ_lib() !== orgId) {
+ copy.circ_lib(orgId);
+ copy.ischanged(true);
+
+ this.batchAttrs
+ .filter(ba => ba.name === 'circ_lib')
+ .forEach(attr => attr.hasChanged = true);
+ }
+ }
+
+ const vol = copy.call_number();
+
+ if (vol.owning_lib() === orgId) { return; } // No change needed
+
+ let newVol;
+ if (newVols[vol.id()]) {
+ newVol = newVols[vol.id()];
+
+ } else {
+
+ // The open-ils.cat.asset.volume.fleshed.batch.update API
+ // will use the existing volume when trying to create a
+ // new volume with the same parameters as an existing volume.
+ newVol = this.idl.clone(vol);
+ newVol.owning_lib(orgId);
+ newVol.id(this.volcopy.autoId--);
+ newVol.isnew(true);
+ newVols[vol.id()] = newVol;
+ }
+
+ copy.call_number(newVol);
+ copy.ischanged();
+
+ this.context.removeCopyNode(copy.id());
+ this.context.findOrCreateCopyNode(copy);
+ });
+
+ // If any of the above actions results in an empty volume
+ // remove it from the tree. Note this does not delete the
+ // volume at the server, since other items could be attached
+ // of which this instance of the editor is not aware.
+ Object.keys(newVols).forEach(volId => {
+ const volNode = this.context.volNodes().filter(
+ volNode => volNode.target.id() === +volId)[0];
+
+ if (volNode && volNode.children.length === 0) {
+ this.context.removeVolNode(+volId);
+ }
+ });
+ }
+
// Create or modify a stat cat entry for each copy that does not
// already match the new value.
- statCatChanged(catId: number) {
+ statCatChanged(catId: number, clear?: boolean) {
catId = Number(catId);
const entryId = this.statCatValues[catId];
+
this.context.copyList().forEach(copy => {
let entry = copy.stat_cat_entries()
.filter(e => e.stat_cat() === catId)[0];
+ console.log('0', entry);
if (entry) {
if (entry.id() === entryId) {
entry = this.idl.create('asce');
entry.stat_cat(catId);
copy.stat_cat_entries().push(entry);
+ console.log('1', entry);
}
entry.id(entryId);
this.store.setLocalItem('cat.copy.last_template', entry.id);
- // TODO: handle owning_lib and statcats differently, location
+ // TODO: handle owning_lib
const template = this.volcopy.templates[entry.id];
if (value === null || value === undefined) { return; }
+ if (field === 'statcats') {
+ Object.keys(value).forEach(catId => {
+ this.statCatValues[+catId] = value[+catId];
+ this.statCatChanged(+catId);
+ });
+ return;
+ }
+
// In some cases, we may have to fetch the data since
// the local code assumes copy field is fleshed.
let promise = Promise.resolve(value);
- // TODO: promises in loops are dangerous becuase they
- // can lead to blasts of duplicate requests for non-local
- // data. Consider an alternative approach.
-
if (field === 'location') {
// May be a 'remote' location. Fetch as needed.
promise = this.volcopy.getLocation(value);
const copy = this.context.copyList()[0];
this.batchAttrs.forEach(comp => {
- if (comp.hasChanged) {
- const value = copy[comp.name]();
- if (value === null) {
- delete template[comp.name];
+ if (!comp.hasChanged) { return; }
- } else {
- // some values are fleshed.
- // this assumes fleshed objects have an 'id' value,
- // which is true so far.
- template[comp.name] =
- typeof value === 'object' ? value.id() : value;
- }
+ const value = copy[comp.name]();
+ const name = comp.name;
+
+ if (value === null) {
+ delete template[name];
+ return;
+ }
+
+ if (name.match(/stat_cat_/)) {
+ const statId = name.match(/stat_cat_(\d+)/)[1];
+ if (!template.statcats) { template.statcats = {}; }
+
+ template.statcats[statId] = value;
+
+ } else {
+
+ // some values are fleshed.
+ // this assumes fleshed objects have an 'id' value,
+ // which is true so far.
+ template[name] =
+ typeof value === 'object' ? value.id() : value;
}
});