PopupTemplate.js 8.9 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.24/esri/copyright.txt for details.
  4. */
  5. import{_ as t}from"./chunks/tslib.es6.js";import e from"./core/Collection.js";import{JSONSupport as o}from"./core/JSONSupport.js";import{clone as s}from"./core/lang.js";import i from"./core/Logger.js";import{isPromiseLike as r}from"./core/promiseUtils.js";import{property as n}from"./core/accessorSupport/decorators/property.js";import{cast as p}from"./core/accessorSupport/decorators/cast.js";import{reader as l}from"./core/accessorSupport/decorators/reader.js";import{subclass as a}from"./core/accessorSupport/decorators/subclass.js";import{writer as d}from"./core/accessorSupport/decorators/writer.js";import{ensureOneOfType as c}from"./core/accessorSupport/ensureType.js";import{collectFields as f,collectArcadeFieldNames as m}from"./layers/support/fieldUtils.js";import{persistableTypes as u}from"./popup/content.js";import y from"./popup/ExpressionInfo.js";import h from"./popup/FieldInfo.js";import I from"./popup/LayerOptions.js";import x from"./popup/RelatedRecordsInfo.js";import{types as F}from"./popup/content/support/mediaInfoTypes.js";import A from"./support/actions/ActionBase.js";import _ from"./support/actions/ActionButton.js";import g from"./support/actions/ActionToggle.js";import w from"./popup/content/MediaContent.js";import j from"./popup/content/TextContent.js";import N from"./popup/content/AttachmentsContent.js";import C from"./popup/content/FieldsContent.js";import E from"./popup/content/ExpressionContent.js";import v from"./popup/content/Content.js";import O from"./popup/content/CustomContent.js";var S;const T=e.ofType({key:"type",defaultKeyValue:"button",base:A,typeMap:{button:_,toggle:g}}),b={base:v,key:"type",typeMap:{media:w,custom:O,text:j,attachments:N,fields:C,expression:E}},R="esri.PopupTemplate",L=i.getLogger(R),J=["attachments","fields","media","text","expression"];let M=S=class extends o{constructor(){super(...arguments),this.actions=null,this.content="",this.expressionInfos=null,this.fieldInfos=null,this.layerOptions=null,this.lastEditInfoEnabled=!0,this.outFields=null,this.overwriteActions=!1,this.returnGeometry=!1,this.title="",this.relatedRecordsInfo=null}castContent(t){return Array.isArray(t)?t.map((t=>c(b,t))):"string"==typeof t||"function"==typeof t||t instanceof HTMLElement||r(t)?t:(L.error("content error","unsupported content value",{value:t}),null)}readContent(t,e){const{popupElements:o}=e;return Array.isArray(o)&&o.length>0?this._readPopupInfoElements(e):this._readPopupInfo(e)}writeContent(t,e,o,s){"string"!=typeof t?Array.isArray(t)&&(e.popupElements=t.filter((t=>J.includes(t.type))).map((t=>t&&t.toJSON(s))),e.popupElements.forEach((t=>{"attachments"===t.type?this._writeAttachmentContent(e):"media"===t.type?this._writeMediaContent(t,e):"text"===t.type&&this._writeTextContent(t,e)}))):e.description=t}writeFieldInfos(t,e,o,s){const{content:i}=this,r=Array.isArray(i)?i:null;if(t){const o=r?r.filter((t=>"fields"===t.type)):[],i=o.length&&o.every((t=>t.fieldInfos?.length));e.fieldInfos=t.filter(Boolean).map((t=>{const e=t.toJSON(s);return i&&(e.visible=!1),e}))}if(r)for(const n of r)"fields"===n.type&&this._writeFieldsContent(n,e)}writeLayerOptions(t,e,o,s){e[o]=!t||null===t.showNoDataRecords&&null===t.returnTopmostRaster?null:t.toJSON(s)}writeTitle(t,e){e.title=t||""}clone(){const{actions:t}=this,e=t?s(t.toArray()):[];return new S({actions:e,content:Array.isArray(this.content)?s(this.content):this.content,expressionInfos:Array.isArray(this.expressionInfos)?s(this.expressionInfos):null,fieldInfos:Array.isArray(this.fieldInfos)?s(this.fieldInfos):null,layerOptions:this.layerOptions?s(this.layerOptions):null,lastEditInfoEnabled:this.lastEditInfoEnabled,outFields:Array.isArray(this.outFields)?s(this.outFields):null,overwriteActions:this.overwriteActions,returnGeometry:this.returnGeometry,title:this.title,relatedRecordsInfo:this.relatedRecordsInfo?s(this.relatedRecordsInfo):null})}async collectRequiredFields(t,e){const o=this.expressionInfos||[];await this._collectExpressionInfoFields(t,e,[...o,...this._getContentExpressionInfos(this.content,o)]),f(t,e,[...this.outFields||[],...this._getActionsFields(this.actions),...this._getTitleFields(this.title),...this._getContentFields(this.content)])}async getRequiredFields(t){const e=new Set;return await this.collectRequiredFields(e,t),[...e].sort()}_writeFieldsContent(t,e){if(!Array.isArray(t.fieldInfos)||!t.fieldInfos.length)return;const o=s(t.fieldInfos);Array.isArray(e.fieldInfos)?o.forEach((t=>{const o=e.fieldInfos.find((e=>e.fieldName.toLowerCase()===t.fieldName.toLowerCase()));o?o.visible=!0:e.fieldInfos.push(t)})):e.fieldInfos=o}_writeAttachmentContent(t){t.showAttachments||(t.showAttachments=!0)}_writeTextContent(t,e){!e.description&&t.text&&(e.description=t.text)}_writeMediaContent(t,e){if(!Array.isArray(t.mediaInfos)||!t.mediaInfos.length)return;const o=s(t.mediaInfos);Array.isArray(e.mediaInfos)?e.mediaInfos=[...e.mediaInfos,...o]:e.mediaInfos=o}_readPopupInfoElements({description:t,mediaInfos:e,popupElements:o}){const s={description:!1,mediaInfos:!1};return o.map((o=>"media"===o.type?(o.mediaInfos||!e||s.mediaInfos||(o.mediaInfos=e,s.mediaInfos=!0),w.fromJSON(o)):"text"===o.type?(o.text||!t||s.description||(o.text=t,s.description=!0),j.fromJSON(o)):"attachments"===o.type?N.fromJSON(o):"fields"===o.type?C.fromJSON(o):"expression"===o.type?E.fromJSON(o):void 0)).filter(Boolean)}_readPopupInfo({description:t,mediaInfos:e,showAttachments:o}){const s=[];return t?s.push(new j({text:t})):s.push(new C),Array.isArray(e)&&e.length&&s.push(w.fromJSON({mediaInfos:e})),o&&s.push(N.fromJSON({displayType:"auto"})),s.length?s:t}_getContentElementFields(t){const e=t?.type;if("attachments"===e)return[...this._extractFieldNames(t.title),...this._extractFieldNames(t.description)];if("custom"===e)return t.outFields||[];if("fields"===e)return[...this._extractFieldNames(t.title),...this._extractFieldNames(t.description),...this._getFieldInfoFields(t.fieldInfos||this.fieldInfos)];if("media"===e){const e=t.mediaInfos||[];return[...this._extractFieldNames(t.title),...this._extractFieldNames(t.description),...e.reduce(((t,e)=>[...t,...this._getMediaInfoFields(e)]),[])]}return"text"===e?this._extractFieldNames(t.text):[]}_getMediaInfoFields(t){const{caption:e,title:o,value:s}=t,i=s||{},{fields:r=[],normalizeField:n,tooltipField:p,sourceURL:l,linkURL:a}=i,d=[...this._extractFieldNames(o),...this._extractFieldNames(e),...this._extractFieldNames(l),...this._extractFieldNames(a),...r];return n&&d.push(n),p&&d.push(p),d}_getContentExpressionInfos(t,e){return Array.isArray(t)?t.reduce(((t,e)=>[...t,..."expression"===e.type&&e.expressionInfo?[e.expressionInfo]:[]]),e):[]}_getContentFields(t){return"string"==typeof t?this._extractFieldNames(t):Array.isArray(t)?t.reduce(((t,e)=>[...t,...this._getContentElementFields(e)]),[]):[]}async _collectExpressionInfoFields(t,e,o){o&&await Promise.all(o.map((o=>m(t,e,o.expression))))}_getFieldInfoFields(t){return t?t.filter((t=>void 0===t.visible||!!t.visible)).map((t=>t.fieldName)).filter((t=>!t.includes("relationships/")&&!t.includes("expression/"))):[]}_getActionsFields(t){return t?t.toArray().reduce(((t,e)=>[...t,...this._getActionFields(e)]),[]):[]}_getActionFields(t){const{className:e,title:o,type:s}=t,i="button"===s||"toggle"===s?t.image:"";return[...this._extractFieldNames(o),...this._extractFieldNames(e),...this._extractFieldNames(i)]}_getTitleFields(t){return"string"==typeof t?this._extractFieldNames(t):[]}_extractFieldNames(t){if(!t||"string"!=typeof t)return[];const e=/{[^}]*}/g,o=t.match(e);if(!o)return[];const s=/\{(\w+):.+\}/,i=o.filter((t=>!(0===t.indexOf("{relationships/")||0===t.indexOf("{expression/")))).map((t=>t.replace(s,"{$1}")));return i?i.map((t=>t.slice(1,-1))):[]}};t([n({type:T})],M.prototype,"actions",void 0),t([n()],M.prototype,"content",void 0),t([p("content")],M.prototype,"castContent",null),t([l("content",["description","fieldInfos","popupElements","mediaInfos","showAttachments"])],M.prototype,"readContent",null),t([d("content",{popupElements:{type:e.ofType(u)},showAttachments:{type:Boolean},mediaInfos:{type:e.ofType(F)},description:{type:String}})],M.prototype,"writeContent",null),t([n({type:[y],json:{write:!0}})],M.prototype,"expressionInfos",void 0),t([n({type:[h]})],M.prototype,"fieldInfos",void 0),t([d("fieldInfos")],M.prototype,"writeFieldInfos",null),t([n({type:I})],M.prototype,"layerOptions",void 0),t([d("layerOptions")],M.prototype,"writeLayerOptions",null),t([n({type:Boolean,json:{read:{source:"showLastEditInfo"},write:{target:"showLastEditInfo"},default:!0}})],M.prototype,"lastEditInfoEnabled",void 0),t([n()],M.prototype,"outFields",void 0),t([n()],M.prototype,"overwriteActions",void 0),t([n()],M.prototype,"returnGeometry",void 0),t([n({json:{type:String}})],M.prototype,"title",void 0),t([d("title")],M.prototype,"writeTitle",null),t([n({type:x,json:{write:!0}})],M.prototype,"relatedRecordsInfo",void 0),M=S=t([a(R)],M);const B=M;export{B as default};