AttachmentsViewModel.js 5.8 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{_ as t}from"../../chunks/tslib.es6.js";import e from"../../Graphic.js";import i from"../../core/Accessor.js";import a from"../../core/Collection.js";import n from"../../core/Error.js";import{isSome as s}from"../../core/maybe.js";import{watch as r,initial as o}from"../../core/reactiveUtils.js";import{property as c}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import{cast as h}from"../../core/accessorSupport/decorators/cast.js";import{subclass as l}from"../../core/accessorSupport/decorators/subclass.js";import d from"../../rest/query/support/AttachmentInfo.js";import m from"../../rest/support/AttachmentQuery.js";import{getSourceLayer as p}from"../Feature/support/featureUtils.js";const f={editing:!1,operations:{add:!0,update:!0,delete:!0}},u=a.ofType(d);let y=class extends i{constructor(t){super(t),this._getAttachmentsPromise=null,this._attachmentLayer=null,this.abilities={...f},this.activeAttachmentInfo=null,this.activeFileInfo=null,this.attachmentInfos=new u,this.fileInfos=new a,this.graphic=null,this.mode="view",this.filesEnabled=!1,this.addHandles(r((()=>this.graphic),(()=>this._graphicChanged()),o))}destroy(){this._attachmentLayer=null,this.graphic=null}castAbilities(t){return{...f,...t}}get state(){return this._getAttachmentsPromise?"loading":this.graphic?"ready":"disabled"}get supportsResizeAttachments(){const{graphic:t}=this;if(!t)return!1;const e=t.layer||t.sourceLayer;return e?.loaded&&"capabilities"in e&&"operations"in e.capabilities&&"supportsResizeAttachments"in e.capabilities.operations&&e.capabilities.operations.supportsResizeAttachments||!1}async getAttachments(){const{_attachmentLayer:t,attachmentInfos:e}=this;if(!t||"function"!=typeof t.queryAttachments)throw new n("invalid-layer","getAttachments(): A valid layer is required.");const i=this._getObjectId(),a=new m({objectIds:[i],returnMetadata:!0}),s=[],r=t.queryAttachments(a).then((t=>t[i]||s)).catch((()=>s));this._getAttachmentsPromise=r,this.notifyChange("state");const o=await r;return e.removeAll(),o.length&&e.addMany(o),this._getAttachmentsPromise=null,this.notifyChange("state"),o}async addAttachment(t,e=this.graphic){const{_attachmentLayer:i,attachmentInfos:a,abilities:s}=this;if(!e)throw new n("invalid-graphic","addAttachment(): A valid graphic is required.",{graphic:e});if(!t)throw new n("invalid-attachment","addAttachment(): An attachment is required.",{attachment:t});if(!s.operations.add)throw new n("invalid-abilities","addAttachment(): add abilities are required.");if(!i||"function"!=typeof i.addAttachment)throw new n("invalid-layer","addAttachment(): A valid layer is required.");const r=i.addAttachment(e,t).then((t=>this._queryAttachment(t.objectId,e))),o=await r;return a.add(o),o}async deleteAttachment(t){const{_attachmentLayer:e,attachmentInfos:i,graphic:a,abilities:s}=this;if(!t)throw new n("invalid-attachment-info","deleteAttachment(): An attachmentInfo is required.",{attachmentInfo:t});if(!s.operations.delete)throw new n("invalid-abilities","deleteAttachment(): delete abilities are required.");if(!e||"function"!=typeof e.deleteAttachments)throw new n("invalid-layer","deleteAttachment(): A valid layer is required.");const r=e.deleteAttachments(a,[t.id]).then((()=>t)),o=await r;return i.remove(o),o}async updateAttachment(t,e=this.activeAttachmentInfo){const{_attachmentLayer:i,attachmentInfos:a,graphic:s,abilities:r}=this;if(!t)throw new n("invalid-attachment","updateAttachment(): An attachment is required.",{attachment:t});if(!e)throw new n("invalid-attachment-info","updateAttachment(): An attachmentInfo is required.",{attachmentInfo:e});if(!r.operations.update)throw new n("invalid-abilities","updateAttachment(): Update abilities are required.");const o=a.findIndex((t=>t===e));if(!i||"function"!=typeof i.updateAttachment)throw new n("invalid-layer","updateAttachment(): A valid layer is required.");const c=i.updateAttachment(s,e.id,t).then((t=>this._queryAttachment(t.objectId))),h=await c;return a.splice(o,1,h),h}async commitFiles(){return await Promise.all(this.fileInfos.items.map((t=>this.addAttachment(t.form)))),this.fileInfos.removeAll(),this.getAttachments()}addFile(t,e){if(!t||!e)return null;const i={file:t,form:e};return this.fileInfos.add(i),i}updateFile(t,e,i=this.activeFileInfo){if(!t||!e||!i)return null;const a=this.fileInfos.findIndex((t=>i===t));return a>-1&&this.fileInfos.splice(a,1,{file:t,form:e}),this.fileInfos.items[a]}deleteFile(t){const e=this.fileInfos.find((e=>e.file===t));return this.fileInfos.remove(e)??null}async _queryAttachment(t,e){if(!t)throw new n("invalid-attachment-id","Could not query attachment.");const{_attachmentLayer:i}=this,a=this._getObjectId(e),s=new m({objectIds:[a],attachmentsWhere:`AttachmentId=${t}`,returnMetadata:!0});return i.queryAttachments(s).then((t=>t[a][0]))}_getObjectId(t=this.graphic){return t?.getObjectId()??null}_graphicChanged(){this.graphic&&(this._setAttachmentLayer(),this.getAttachments().catch((()=>{})))}_setAttachmentLayer(){const{graphic:t}=this,e=p(t);this._attachmentLayer=e?"scene"===e.type&&s(e.associatedLayer)?e.associatedLayer:e:null}};t([c()],y.prototype,"abilities",void 0),t([h("abilities")],y.prototype,"castAbilities",null),t([c()],y.prototype,"activeAttachmentInfo",void 0),t([c()],y.prototype,"activeFileInfo",void 0),t([c({readOnly:!0,type:u})],y.prototype,"attachmentInfos",void 0),t([c()],y.prototype,"fileInfos",void 0),t([c({type:e})],y.prototype,"graphic",void 0),t([c()],y.prototype,"mode",void 0),t([c({readOnly:!0})],y.prototype,"state",null),t([c()],y.prototype,"filesEnabled",void 0),t([c({readOnly:!0})],y.prototype,"supportsResizeAttachments",null),y=t([l("esri.widgets.Attachments.AttachmentsViewModel")],y);const A=y;export{A as default};