12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.25/esri/copyright.txt for details.
- */
- import{_ as t}from"../../chunks/tslib.es6.js";import e from"../../request.js";import r from"../../core/Collection.js";import o from"../../core/Error.js";import{clone as s}from"../../core/lang.js";import i from"../../core/Loadable.js";import{unwrap as a}from"../../core/maybe.js";import{property as l}from"../../core/accessorSupport/decorators/property.js";import"../../core/accessorSupport/ensureType.js";import{subclass as n}from"../../core/accessorSupport/decorators/subclass.js";import{formatDate as p,convertDateFormatToIntlOptions as c}from"../../intl/date.js";import{getLanguage as m}from"../../intl/locale.js";import u from"../../portal/Portal.js";import{execute as f}from"../../rest/geoprocessor/execute.js";import"../../rest/geoprocessor/GPOptions.js";import"../../core/has.js";import"../../geometry/support/normalizeUtils.js";import"../../layers/support/Field.js";import"../../layers/support/MapImage.js";import"../../config.js";import"../../kernel.js";import"../../core/urlUtils.js";import"../../rest/support/DataFile.js";import"../../rest/support/FeatureSet.js";import"../../rest/support/LinearUnit.js";import"../../rest/support/ParameterValue.js";import"../../rest/support/RasterData.js";import"../../rest/support/JobInfo.js";import{execute as d}from"../../rest/print.js";import{fromJSON as h}from"../../rest/support/fileFormat.js";import{fromJSON as y}from"../../rest/support/layoutTemplate.js";import v from"../../rest/support/PrintParameters.js";import T from"../../rest/support/PrintTemplate.js";import w from"./CustomTemplate.js";const j=6e4,x=r.ofType(w);function g(t){t.layoutOptions||(t.layoutOptions={}),t.layoutOptions.customTextElements||(t.layoutOptions.customTextElements=[]);const e="date";if(!t.layoutOptions.customTextElements.find((t=>e in t))){const{customTextElements:e}=t.layoutOptions;let r=p(Date.now(),c("short-date"));"ar"===m()&&(r=""+r),e.push({date:r})}}let S=class extends i{constructor(t){super(t),this.allowedFormats="all",this.allowedLayouts="all",this.defaultTemplates=new x,this.extraParameters=null,this.includeDefaultTemplates=!0,this.error=null,this.portal=u.getDefault(),this.printServiceUrl=null,this.templatesInfo=null,this.updateDelay=1e3,this.view=null,this.templateCustomTextElements=null,this.print=this.print.bind(this)}destroy(){this.view=null}get effectivePrintServiceUrl(){return this.printServiceUrl??null}get effectiveTemplateCustomTextElements(){if(!this._serviceTemplateCustomTextElements)return{};const t=s(this._serviceTemplateCustomTextElements);return this.templateCustomTextElements&&Object.keys(this.templateCustomTextElements).forEach((e=>{const r=t[e];if(r){const t=this.templateCustomTextElements[e];r.forEach((e=>{const[r]=Object.entries(e)[0];t.find((t=>{const[o,s]=Object.entries(t)[0];r===o&&(e[r]=s)}))}))}})),Object.freeze(t)}get state(){return"loading"===this.loadStatus?"initializing":this.error||"failed"===this.loadStatus?"error":this.get("view.ready")&&"loaded"===this.loadStatus?"ready":"disabled"}async load(t){return this.addResolvingPromise(this._loadResources(t).catch((t=>this.error=t))),this}async print(t){const{view:e,extraParameters:r,updateDelay:s}=this;if(!e)throw new o("print:view-required","view is not set");g(t);const i=new v({view:e,template:t,extraParameters:r,updateDelay:s});try{return await d(this.effectivePrintServiceUrl,i)}catch(a){throw new o("print:export-error","An error occurred while exporting the web map.",{error:a})}}toPrintTemplate({attributionEnabled:t,author:e,copyright:r,customTextElements:o,dpi:s,forceFeatureAttributes:i,format:a,height:l,layout:n,legendEnabled:p,northArrowEnabled:c,scale:m,scaleEnabled:u,title:f,width:d}){const h=new T({attributionVisible:t,forceFeatureAttributes:i,format:a,layout:n,layoutOptions:{authorText:e||"",copyrightText:r||"",customTextElements:o,titleText:f||""},outScale:m,scalePreserved:u});d&&(h.exportOptions.width=d),l&&(h.exportOptions.height=l),s&&(h.exportOptions.dpi=s),p||(h.layoutOptions.legendLayers=[]);const y=this.templateToNorthArrowInfo[n];if(y){y.visible===c||(h.layoutOptions.elementOverrides={[y.name]:{visible:c}})}return h}async _loadResources(t){let e=[];const{printServiceUrl:r}=this;if(!r){if(this.destroyed)return;const{portal:r}=this;try{await r.load(t)}catch(s){throw new o("print:could-not-load-portal","Cannot load print resource information from portal",{url:this.effectivePrintServiceUrl})}const i=r.helperServices?.printTask;i&&(this._set("effectivePrintServiceUrl",i.url),e=(i?.templates??[]).map((t=>w.fromJSON(t))))}e.length>0&&this.defaultTemplates.addMany(e);if(-1===this.effectivePrintServiceUrl.toLowerCase().split("/").indexOf("gpserver"))throw new o("print:invalid-print-service-url","Can't fetch print templates information from provided URL",{url:this.effectivePrintServiceUrl});this._processLayoutTemplateInfos(await this._getLayoutTemplatesInfo(t)),await this._loadServiceDescription(t)}async _loadServiceDescription(t){const e=await this._getPrintTemplatesFromService(t);this._set("templatesInfo",e)}async _getLayoutTemplatesInfo(t){let e=[];const r=async e=>{const r=this.effectivePrintServiceUrl.replace(/(\/GPServer\/).+/i,`$1${encodeURI(e)}`);return(await f(r,null,null,a(t))).results[0].value};try{e=await r("Get Layout Templates Info Task")}catch(o){}if(!e||e.length<1)try{e=await r("Get Layout Templates Info")}catch(o){}return e}_processLayoutTemplateInfos(t){const e={},r={};t.forEach((({layoutTemplate:t,layoutOptions:{customTextElements:o,mapSurroundInfos:s}})=>{const i=y(t);e[i]=o,s&&(r[i]=s.find((t=>"CIMMarkerNorthArrow"===t.type)))})),this.templateCustomTextElements=Object.freeze(e),this.templateToNorthArrowInfo=r}async _getPrintTemplatesFromService(t){return e(this.effectivePrintServiceUrl,{...t,query:{f:"json"},timeout:j}).then((t=>{const e=t&&t.data,r=e&&e.parameters;let o=null,s=null;r.forEach((t=>{let e,r=t.choiceList&&t.choiceList.slice();r&&r.length&&t.defaultValue&&(e=r.indexOf(t.defaultValue)),e>-1&&(r.splice(e,1),r.unshift(t.defaultValue));const i=(t,e)=>{const r="all"===e?t:t.filter((t=>e.includes(t)));return 0===r.length?t:r};if("Format"===t.name){const e=i(r.map(h),this.allowedFormats),s=h(t.defaultValue);o={defaultValue:e.includes(s)?s:e[0],choiceList:e}}else if("Layout_Template"===t.name){let e,o;r=r.filter((t=>"map_only"!==t.toLowerCase())),r.some(((t,r)=>{const o=t.toLowerCase();return o.includes("letter")&&o.includes("landscape")?(e=r,!0):!(!o.includes("a4")||!o.includes("landscape"))&&(e=r,!1)})),e&&(o=r[e],r.splice(e,1),r.unshift(o));const a=i(r.map(y),this.allowedLayouts),l=y(t.defaultValue);s={defaultValue:a.includes(l)?l:a[0],choiceList:a}}})),this.error=null;return{format:o,layout:s}})).catch((t=>{throw new o("print:unavailable-service-info","Can't fetch templates info from service",{error:t})}))}};t([l()],S.prototype,"_serviceTemplateCustomTextElements",void 0),t([l()],S.prototype,"allowedFormats",void 0),t([l()],S.prototype,"allowedLayouts",void 0),t([l({type:x})],S.prototype,"defaultTemplates",void 0),t([l()],S.prototype,"extraParameters",void 0),t([l()],S.prototype,"includeDefaultTemplates",void 0),t([l({readOnly:!0})],S.prototype,"effectivePrintServiceUrl",null),t([l()],S.prototype,"effectiveTemplateCustomTextElements",null),t([l()],S.prototype,"error",void 0),t([l({type:u})],S.prototype,"portal",void 0),t([l()],S.prototype,"printServiceUrl",void 0),t([l({readOnly:!0})],S.prototype,"state",null),t([l({readOnly:!0})],S.prototype,"templatesInfo",void 0),t([l()],S.prototype,"updateDelay",void 0),t([l()],S.prototype,"view",void 0),t([l()],S.prototype,"templateCustomTextElements",void 0),t([l()],S.prototype,"templateToNorthArrowInfo",void 0),S=t([n("esri.widgets.Print.PrintViewModel")],S);const E=S;export{E as default};
|