{"version":3,"file":"upload.min.js","sources":["upload.ts"],"sourcesContent":["/// \r\n\r\nnamespace eXpress.core {\r\n\texport type UploadSettings = {\r\n\t\tafterInit: () => void;\r\n\t\tbeforeUploadStart: (totalFiles: number) => void;\r\n\t\tfileUploaded: (uploadedFiles: number) => void;\r\n\t\timportFinished: (folderId: string, importedFileIds: string[] | null, nrOfFailedUploads: number, failedFileNames: string[] | null) => void;\r\n\t\tuploadButtonId: string;\r\n\t\tcreateUploadSessionAsync?: (runtime: string) => JQueryPromise\r\n\t};\r\n\texport class Upload {\r\n\r\n\t\tprivate remainingFiles = 0;\r\n\t\tprivate folderId: string;\r\n\t\tprivate uploadErrorTimer: number | undefined;\r\n\t\tprivate uploadErrors = 0;\r\n\t\tprivate pollSessionCounter = 0;\r\n\t\tprivate uploading: boolean;\r\n\t\tprivate failedFileNames: string[] = [];\r\n\r\n\t\tprivate flow: string;\r\n\t\tprivate sessionId: string;\r\n\r\n\t\tprivate uploadService: eXpress.services.UploadService;\r\n\t\tprivate context: IAppContext;\r\n\r\n\t\tprivate settings: UploadSettings;\r\n\r\n\t\tprivate plUploader: plupload.Uploader;\r\n\r\n\t\tpublic constructor(settings: UploadSettings, context: eXpress.core.IAppContext, uploadService: eXpress.services.UploadService, flow: string) {\r\n\t\t\tthis.flow = flow;\r\n\t\t\tthis.uploadService = uploadService;\r\n\t\t\tthis.context = context;\r\n\t\t\tthis.settings = settings;\r\n\t\t}\r\n\r\n\t\tpublic setFolderId(folderId: string) {\r\n\t\t\tthis.folderId = folderId;\r\n\t\t}\r\n\r\n\t\tpublic init() {\r\n\t\t\tthis.plUploader = new plupload.Uploader({\r\n\t\t\t\truntimes: 'html5, silverlight, html4',\r\n\t\t\t\tbrowse_button: this.settings.uploadButtonId,\r\n\t\t\t\turl: \"http://trunk.localhost\",\r\n\t\t\t\tflash_swf_url: '/plupload/Moxie.swf',\r\n\t\t\t\tsilverlight_xap_url: '/plupload/Moxie.xap',\r\n\t\t\t\tfilters: {\r\n\t\t\t\t\tmax_file_size: '100mb',\r\n\t\t\t\t\tmime_types: [\r\n\t\t\t\t\t\t{ title: \"Jpeg files\", extensions: \"jpe,jpeg,jpg\" },\r\n\t\t\t\t\t\t{ title: \"Png files\", extensions: \"png\" },\r\n\t\t\t\t\t\t{ title: \"HEIF files\", extensions: \"heic,heif\" }\r\n\t\t\t\t\t]\r\n\t\t\t\t},\r\n\t\t\t\tinit: {\r\n\t\t\t\t\tInit: (uploader) => {\r\n\t\t\t\t\t\t//if (SupportsTouchEvents() && isChrome) {\r\n\t\t\t\t\t\t//\tsetTimeout(function () { $('input').attr('accept', 'image/*') }, 300);\r\n\t\t\t\t\t\t//}\r\n\r\n\t\t\t\t\t\tthis.remainingFiles = 0;\r\n\t\t\t\t\t\tthis.uploading = false;\r\n\r\n\t\t\t\t\t\tthis.settings.afterInit && this.settings.afterInit();\r\n\t\t\t\t\t},\r\n\t\t\t\t\tFilesAdded: (uploader, files) => {\r\n\t\t\t\t\t\tif (this.folderId && this.folderId !== core.emptyGuid) {\r\n\t\t\t\t\t\t\tthis.createUploadSessionForFolderAsync()\r\n\t\t\t\t\t\t\t\t.then((uploadSession) => this.createUploadSessionComplete(files, uploader, uploadSession));\r\n\t\t\t\t\t\t} else if (this.settings.createUploadSessionAsync) {\r\n\t\t\t\t\t\t\tthis.settings.createUploadSessionAsync(uploader.runtime)\r\n\t\t\t\t\t\t\t\t.then((uploadSession) => this.createUploadSessionComplete(files, uploader, uploadSession));\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t},\r\n\t\t\t\t\tUploadComplete: (uploader, files) => {\r\n\t\t\t\t\t\tthis.uploading = false;\r\n\t\t\t\t\t\tthis.startSessionPolling(files.length);\r\n\t\t\t\t\t\tuploader.splice();\r\n\t\t\t\t\t},\r\n\t\t\t\t\tFileUploaded: (uploader, file, response) => {\r\n\t\t\t\t\t\tthis.remainingFiles--;\r\n\t\t\t\t\t\tthis.settings.fileUploaded(uploader.files.length - this.remainingFiles);\r\n\t\t\t\t\t},\r\n\t\t\t\t\tQueueChanged: (uploader) => {\r\n\t\t\t\t\t\tthis.remainingFiles = uploader.files.length;\r\n\t\t\t\t\t\twindow.clearTimeout(this.uploadErrorTimer);\r\n\t\t\t\t\t},\r\n\t\t\t\t\tError: (uploader, error) => {\r\n\t\t\t\t\t\tif (error.code !== -200) {\r\n\t\t\t\t\t\t\tthis.failedFileNames.push(error.file.name);\r\n\r\n\t\t\t\t\t\t\tthis.uploadErrors++;\r\n\r\n\t\t\t\t\t\t\t// HandleMultiUploadErrors is only called if at least 1 file was succesfully uploaded\r\n\t\t\t\t\t\t\t// if all files are rejected we still need HandleMultiUploadErrors to be called\r\n\t\t\t\t\t\t\t// once we added a file to the queue, we can kill this timer\r\n\t\t\t\t\t\t\tif (uploader.files.length === 0) {\r\n\t\t\t\t\t\t\t\tif (this.uploadErrorTimer !== null) {\r\n\t\t\t\t\t\t\t\t\t// wait for all errors\r\n\t\t\t\t\t\t\t\t\twindow.clearTimeout(this.uploadErrorTimer);\r\n\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t\tthis.uploadErrorTimer = window.setTimeout(() => {\r\n\t\t\t\t\t\t\t\t\tthis.settings.importFinished(this.folderId, null, this.uploadErrors, this.failedFileNames);\r\n\t\t\t\t\t\t\t\t\tthis.resetUploadErrors();\r\n\t\t\t\t\t\t\t\t}, 1000);\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t});\r\n\r\n\t\t\tthis.plUploader.init();\r\n\t\t}\r\n\r\n\t\tpublic isUploading(): boolean {\r\n\t\t\treturn this.uploading;\r\n\t\t}\r\n\r\n\t\tpublic browse() {\r\n\t\t\t$(\"#\" + this.settings.uploadButtonId).trigger(\"click\");\r\n\t\t}\r\n\r\n\t\tpublic destroy() {\r\n\t\t\tif (this.plUploader)\r\n\t\t\t\tthis.plUploader.destroy();\r\n\t\t}\r\n\r\n\t\tpublic refresh() {\r\n\t\t\tif (this.plUploader)\r\n\t\t\t\tthis.plUploader.refresh();\r\n\t\t}\r\n\r\n\t\tprivate startSessionPolling(expectedFiles: number) {\r\n\t\t\tthis.pollSessionCounter = 0;\r\n\t\t\twindow.setTimeout(() => this.pollSession(expectedFiles), 1000);\r\n\t\t}\r\n\r\n\t\tprivate pollSession(expectedFiles: number) {\r\n\t\t\tthis.pollSessionCounter++;\r\n\t\t\tthis.uploadService.checkImportStatusForSessionAsync(GetCookie(\"accid\"), this.sessionId)\r\n\t\t\t\t.then(imports => {\r\n\t\t\t\t\tconst finishedImports = imports.filter(importItem => importItem.Status !== services.ImportStatus.Waiting);\r\n\t\t\t\t\tif (finishedImports.length === expectedFiles) {\r\n\t\t\t\t\t\tconst importedFiles = imports.filter(importItem => importItem.Status === services.ImportStatus.Imported);\r\n\t\t\t\t\t\tconst failedFiles = imports.filter(i => i.Status === services.ImportStatus.Failed);\r\n\t\t\t\t\t\tthis.failedFileNames.push(...failedFiles.map(m => m.OriginalFileName));\r\n\t\t\t\t\t\tthis.uploadErrors += failedFiles.length;\r\n\t\t\t\t\t\tthis.settings.importFinished(this.folderId, importedFiles.map(importedFile => importedFile.FileId), this.uploadErrors, this.failedFileNames);\r\n\t\t\t\t\t\tthis.resetUploadErrors();\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse {\r\n\t\t\t\t\t\twindow.setTimeout(() => this.pollSession(expectedFiles), 1000 + this.pollSessionCounter * 1000);\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\t\t}\r\n\r\n\t\tprivate createUploadSessionForFolderAsync(): JQueryPromise {\r\n\t\t\treturn this.uploadService.createUploadSessionAsync(this.context.pointOfSaleId, GetCookie(\"accid\"), this.folderId, this.flow, this.plUploader.runtime);\r\n\t\t}\r\n\r\n\t\tprivate createUploadSessionComplete(files: any, uploader: plupload.Uploader, uploadSession: services.ICreateUploadSessionResult) {\r\n\t\t\tthis.uploading = true;\r\n\t\t\tthis.settings.beforeUploadStart(files.length);\r\n\t\t\tthis.sessionId = uploadSession.Id;\r\n\t\t\tuploader.settings.url = uploadSession.UploadUrl + '?uploadId=' + uploadSession.Id;\r\n\t\t\tuploader.start();\r\n\t\t}\r\n\r\n\t\tpublic resetUploadErrors() {\r\n\t\t\tthis.uploadErrors = 0;\r\n\t\t\tthis.uploadErrorTimer = undefined;\r\n\t\t\tthis.failedFileNames = [];\r\n\t\t}\r\n\r\n\t\tpublic getUploadErrorPopupAsync(contentManager: core.ContentManager, numberOfErrors: number, failedFileNames: string[]): JQueryPromise {\r\n\t\t\tconst stringToHash = failedFileNames.join(\"\");\r\n\t\t\tconst hashCode = this.convertToHash(stringToHash);\r\n\r\n\t\t\treturn ContentPopup.createDialogAsync(\r\n\t\t\t\tcontentManager,\r\n\t\t\t\t{\r\n\t\t\t\t\tlabelContentKey: \"Label.plupload.error.multi\",\r\n\t\t\t\t\tdialogId: \"importFailed-\" + hashCode,\r\n\t\t\t\t\tokContentKey: \"Label.CloseButtonText\",\r\n\t\t\t\t\tcssClass: \"notification\",\r\n\t\t\t\t\treplacements: [numberOfErrors.toString(), failedFileNames.join(\", \")]\r\n\t\t\t\t}\r\n\t\t\t);\r\n\t\t}\r\n\r\n\t\tpublic convertToHash(str): string {\r\n\t\t\tlet results = \"\";\r\n\t\t\tconst fixedNumber = 100;\r\n\t\t\tfor (let i = 0; i < str.length; i++) {\r\n\t\t\t\tresults += (fixedNumber + str[i].charCodeAt(0)).toString();\r\n\t\t\t}\r\n\r\n\t\t\treturn results.replace('.', '').replace('+', '').toString();\r\n\t\t}\r\n\t}\r\n}\r\n"],"names":["eXpress","core","Upload","settings","context","uploadService","flow","this","remainingFiles","uploadErrors","pollSessionCounter","failedFileNames","prototype","setFolderId","folderId","init","_this","plUploader","plupload","Uploader","runtimes","browse_button","uploadButtonId","url","flash_swf_url","silverlight_xap_url","filters","max_file_size","mime_types","title","extensions","Init","uploader","uploading","afterInit","FilesAdded","files","emptyGuid","createUploadSessionForFolderAsync","then","uploadSession","createUploadSessionComplete","createUploadSessionAsync","runtime","UploadComplete","startSessionPolling","length","splice","FileUploaded","file","response","fileUploaded","QueueChanged","window","clearTimeout","uploadErrorTimer","Error","error","code","push","name","setTimeout","importFinished","resetUploadErrors","isUploading","browse","$","trigger","destroy","refresh","expectedFiles","pollSession","checkImportStatusForSessionAsync","GetCookie","sessionId","imports","filter","importItem","Status","services","ImportStatus","Waiting","importedFiles","Imported","failedFiles","i","Failed","_a","apply","map","m","OriginalFileName","importedFile","FileId","pointOfSaleId","beforeUploadStart","Id","UploadUrl","start","undefined","getUploadErrorPopupAsync","contentManager","numberOfErrors","stringToHash","join","hashCode","convertToHash","ContentPopup","createDialogAsync","labelContentKey","dialogId","okContentKey","cssClass","replacements","toString","str","results","charCodeAt","replace"],"mappings":"AAEA,IAAUA,SAAV,SAAUA,SAAQ,IAAAC,KASjBC,OATiBD,KAAAD,QAAIC,OAAJD,aAyMjB,CAAA,GAhMAE,OAAA,WAoBC,SAAAA,OAAmBC,SAA0BC,QAAmCC,cAA+CC,MAlBvHC,KAAcC,eAAG,EAGjBD,KAAYE,aAAG,EACfF,KAAkBG,mBAAG,EAErBH,KAAeI,gBAAa,GAanCJ,KAAKD,KAAOA,KACZC,KAAKF,cAAgBA,cACrBE,KAAKH,QAAUA,QACfG,KAAKJ,SAAWA,QAChB,CAsKF,OApKQD,OAAWU,UAAAC,YAAlB,SAAmBC,UAClBP,KAAKO,SAAWA,UAGVZ,OAAAU,UAAAG,KAAP,WAAA,IAyECC,MAAAT,KAxEAA,KAAKU,WAAa,IAAIC,SAASC,SAAS,CACvCC,SAAU,4BACVC,cAAed,KAAKJ,SAASmB,eAC7BC,IAAK,yBACLC,cAAe,sBACfC,oBAAqB,sBACrBC,QAAS,CACRC,cAAe,QACfC,WAAY,CACX,CAAEC,MAAO,aAAcC,WAAY,gBACnC,CAAED,MAAO,YAAaC,WAAY,OAClC,CAAED,MAAO,aAAcC,WAAY,eAGrCf,KAAM,CACLgB,KAAM,SAACC,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