sp-attachment-library
Version:
This is library is used ro do attachments realted actions on a list item in Sharepoint Angular SPA Addins.
2 lines • 17.3 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@pnp/sp"),require("@fortawesome/free-solid-svg-icons"),require("sweetalert2"),require("@angular/core"),require("@angular/forms"),require("@angular/platform-browser"),require("@sweetalert2/ngx-sweetalert2"),require("@fortawesome/angular-fontawesome")):"function"==typeof define&&define.amd?define("sp-attachment-library",["exports","@pnp/sp","@fortawesome/free-solid-svg-icons","sweetalert2","@angular/core","@angular/forms","@angular/platform-browser","@sweetalert2/ngx-sweetalert2","@fortawesome/angular-fontawesome"],e):e(t["sp-attachment-library"]={},t.sp,t.fa,t.Swal,t.ng.core,t.ng.forms,t.ng.platformBrowser,t.ngxSweetalert2,t.angularFontawesome)}(this,function(t,o,e,n,a,i,r,s,l){"use strict";function u(a,i){var r,o,s,t,l={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return t={next:e(0),"throw":e(1),"return":e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function n(t){if(r)throw new TypeError("Generator is already executing.");for(;l;)try{if(r=1,o&&(s=2&t[0]?o["return"]:t[0]?o["throw"]||((s=o["return"])&&s.call(o),0):o.next)&&!(s=s.call(o,t[1])).done)return s;switch(o=0,s&&(t=[2&t[0],s.value]),t[0]){case 0:case 1:s=t;break;case 4:return l.label++,{value:t[1],done:!1};case 5:l.label++,o=t[1],t=[0];continue;case 7:t=l.ops.pop(),l.trys.pop();continue;default:if(!(s=0<(s=l.trys).length&&s[s.length-1])&&(6===t[0]||2===t[0])){l=0;continue}if(3===t[0]&&(!s||t[1]>s[0]&&t[1]<s[3])){l.label=t[1];break}if(6===t[0]&&l.label<s[1]){l.label=s[1],s=t;break}if(s&&l.label<s[2]){l.label=s[2],l.ops.push(t);break}s[2]&&l.ops.pop(),l.trys.pop();continue}t=i.call(a,l)}catch(e){t=[6,e],o=0}finally{r=s=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([e,t])}}}function m(t){var e="function"==typeof Symbol&&t[Symbol.iterator],n=0;return e?e.call(t):{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}}}function c(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var a,i,r=n.call(t),o=[];try{for(;(void 0===e||0<e--)&&!(a=r.next()).done;)o.push(a.value)}catch(s){i={error:s}}finally{try{a&&!a.done&&(n=r["return"])&&n.call(r)}finally{if(i)throw i.error}}return o}function d(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(c(arguments[e]));return t}n=n&&n.hasOwnProperty("default")?n["default"]:n;function f(t){var e=t.content;return g.dataURLToBlob(e)}var h=new a.InjectionToken("TestLibConfig"),p=function(){function t(t){this.config=t,o.sp.setup({sp:{headers:{Accept:"application/json;odata=verbose"},baseUrl:this.config.baseUrl}})}return t.prototype.getAttachmentsById=function(t,a){return new Promise(function(e,n){o.sp.web.lists.getByTitle(t).items.getById(a).attachmentFiles.get().then(function(t){e({result:t})})["catch"](function(t){n({result:!1,message:t.message})})})},t.prototype.addAttachment=function(t,a,i){return new Promise(function(e,n){o.sp.web.lists.getByTitle(t).items.getById(a).attachmentFiles.add(i.name,i.content).then(function(t){e({result:t})})["catch"](function(t){n({result:!1,message:t.message})})})},t.prototype.addAttachments=function(t,a,i){return new Promise(function(e,n){o.sp.web.lists.getByTitle(t).items.getById(a).attachmentFiles.addMultiple(i).then(function(t){e({result:t})})["catch"](function(t){n({result:!1,message:t.message})})})},t.prototype.deleteAttachment=function(t,a,i){return new Promise(function(e,n){o.sp.web.lists.getByTitle(t).items.getById(a).attachmentFiles.getByName(i)["delete"]().then(function(t){e({result:t})})["catch"](function(t){n({result:!1,message:t.message})})})},t.prototype.deleteAttachments=function(a,i,r){return new Promise(function(e,n){var t;(t=o.sp.web.lists.getByTitle(a).items.getById(i).attachmentFiles).deleteMultiple.apply(t,d(r)).then(function(t){e({result:t})})["catch"](function(t){n({result:!1,message:t.message})})})},t.prototype.getAttachmentContent=function(t,a,i){return new Promise(function(e,n){o.sp.web.lists.getByTitle(t).items.getById(a).attachmentFiles.getByName(i).getBlob().then(function(t){e({result:t})})["catch"](function(t){n({result:!1,message:t.message})})})},t.prototype.updateAttachmentContent=function(t,a,i,r){return new Promise(function(e,n){o.sp.web.lists.getByTitle(t).items.getById(a).attachmentFiles.getByName(i).setContent(r).then(function(t){e({result:t})})["catch"](function(t){n({result:!1,message:t.message})})})},t.decorators=[{type:a.Injectable,args:[{providedIn:"root"}]}],t.ctorParameters=function(){return[{type:undefined,decorators:[{type:a.Inject,args:[h]}]}]},t.ngInjectableDef=a.defineInjectable({factory:function(){return new t(a.inject(h))},token:t,providedIn:"root"}),t}(),g=require("blob-util"),b=function(){function t(t){this.spAttachmentLibraryService=t,this.attachments=[],this.unUpLoadedItems=0,this.fa=e,this.deleteSwalOptions={title:"Do you really want to delete this attachment?",type:"warning",confirmButtonText:"Yes, delete it!",cancelButtonText:"No, keep it",showCancelButton:"true",customClass:{confirmButton:"btn btn-success",cancelButton:"btn btn-danger ml-sm-2"}},this.deleteAllSwalOptions={title:"Do you really want to delete all selected attachments?",message:"All selected attachments added to this list item will be deleted.",type:"warning",confirmButtonText:"Yes, delete it!",cancelButtonText:"No, keep it",showCancelButton:"true",customClass:{confirmButton:"btn btn-danger",cancelButton:"btn btn-success ml-sm-2"}}}return Object.defineProperty(t.prototype,"listName",{set:function(t){this._listName=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"itemId",{set:function(t){this._itemId=t},enumerable:!0,configurable:!0}),t.prototype.ngOnInit=function(){this._listName&&this._itemId&&this.loadExistingFiles()},t.prototype.loadExistingFiles=function(){var n=this,a=this.attachments.filter(function(t){return!t.isSelected&&null===t.serverRelativeUrl});this.spAttachmentLibraryService.getAttachmentsById(this._listName,this._itemId).then(function(t){var e;n.attachments=[],t.result.forEach(function(t){n.attachments.push({fileName:t.FileName,content:null,extension:t.FileName.substring(t.FileName.lastIndexOf(".")+1),serverRelativeUrl:t.ServerRelativeUrl,isUploaded:!1,isContentChanged:!1,isLoading:!1,isSelected:!1})}),(e=n.attachments).push.apply(e,d(a))})},t.prototype.onAttachmentAdded=function(t){function e(e){r.attachments.findIndex(function(t){return t.fileName===e.name})<0?r.getBase64(e).then(function(t){i.attachments.push({fileName:e.name,content:t,extension:e.name.substring(e.name.lastIndexOf(".")+1),serverRelativeUrl:null,isUploaded:!1,isContentChanged:!1,isLoading:!1,isSelected:!1})}):r.promptReplace(e)}var n,a,i=this,r=this;try{for(var o=m(t.target.files),s=o.next();!s.done;s=o.next()){e(s.value)}}catch(l){n={error:l}}finally{try{s&&!s.done&&(a=o["return"])&&a.call(o)}finally{if(n)throw n.error}}},t.prototype.promptReplace=function(a){var i=this;n.mixin({customClass:{confirmButton:"btn btn-success",cancelButton:"btn btn-danger ml-sm-2"},buttonsStyling:!1}).fire({title:"File already exists. Do you want to replace it?",text:"The file content will be replaced",type:"warning",showCancelButton:!0,confirmButtonText:"Yes, replace it!",cancelButtonText:"No, keep it"}).then(function(t){t.value?i.getBase64(a).then(function(t){var e=i.attachments.findIndex(function(t){return t.fileName===a.name}),n=i.attachments.find(function(t){return t.fileName===a.name});n.content=t,n.extension=a.name.substring(a.name.lastIndexOf(".")+1),n.isContentChanged=!0,n.isUploaded=!1,i.attachments.splice(e,1,n)}):t.dismiss===n.DismissReason.cancel&&n.fire("Cancelled")})},t.prototype.resetFiles=function(t){t.target.value=null},t.prototype.removeAttachment=function(n){var a=this;if(n.isLoading=!0,n.serverRelativeUrl)n.isLoading=!0,this.spAttachmentLibraryService.deleteAttachment(this._listName,this._itemId,n.fileName).then(function(t){var e=a.attachments.findIndex(function(t){return t.fileName===n.fileName});n.isLoading=!1,a.attachments.splice(e,1)})["catch"](function(t){console.log(t)});else{var t=this.attachments.findIndex(function(t){return t.fileName===n.fileName});n.isLoading=!1,this.attachments.splice(t,1)}},t.prototype.uploadAttachment=function(n){var a=this;n.isLoading=!0,this._selectedUploadAttachment={content:f(n),name:n.fileName},this.spAttachmentLibraryService.addAttachment(this._listName,this._itemId,this._selectedUploadAttachment).then(function(t){n.isLoading=!1;var e=a.attachments.findIndex(function(t){return t.fileName===n.fileName});a.attachments[e].serverRelativeUrl=t.result.data.ServerRelativeUrl,a.attachments[e].isContentChanged=!1})["catch"](function(t){console.log(t)})},t.prototype.hasNewUnUploadedAndSelcted=function(){return 0<this.attachments.filter(function(t){return null===t.serverRelativeUrl&&t.isSelected}).length},t.prototype.getUnUploadedCount=function(){return this.attachments.filter(function(t){return null===t.serverRelativeUrl&&t.isSelected}).length},t.prototype.hasSelected=function(){return 0<this.attachments.filter(function(t){return t.isSelected}).length},t.prototype.allSelected=function(){return this.attachments.every(function(t){return t.isSelected})},t.prototype.getSelectedCount=function(){return this.attachments.filter(function(t){return t.isSelected}).length},t.prototype.markOrUnMarkAll=function(){this.attachments.forEach(function(t){t.isSelected=!t.isSelected})},t.prototype.uploadNewAttachments=function(){var t,e,a=this,i=Array(),r=this.attachments.filter(function(t){return null===t.serverRelativeUrl&&t.isSelected});r.forEach(function(t){t.isLoading=!0});var n=[];try{for(var o=m(r),s=o.next();!s.done;s=o.next()){var l=s.value;n.push(f(l))}}catch(c){t={error:c}}finally{try{s&&!s.done&&(e=o["return"])&&e.call(o)}finally{if(t)throw t.error}}Promise.all(n).then(function(t){t.forEach(function(e,n){return function t(e,o,s,l){return new(s||(s=Promise))(function(t,n){function a(t){try{r(l.next(t))}catch(e){n(e)}}function i(t){try{r(l["throw"](t))}catch(e){n(e)}}function r(e){e.done?t(e.value):new s(function(t){t(e.value)}).then(a,i)}r((l=l.apply(e,o||[])).next())})}(a,void 0,void 0,function(){return u(this,function(t){switch(t.label){case 0:return[4,i.push({name:r[n].fileName,content:e})];case 1:return t.sent(),[2]}})})}),a.spAttachmentLibraryService.addAttachments(a._listName,a._itemId,i).then(function(t){a.loadExistingFiles()})["catch"](function(t){console.log(t)})})["catch"](function(t){console.log(t)})},t.prototype.replaceAttachment=function(e){var n=this;e.isLoading=!0,this.spAttachmentLibraryService.deleteAttachment(this._listName,this._itemId,e.fileName).then(function(t){e.isLoading=!1,n.uploadAttachment(e)})["catch"](function(t){console.log(t)})},t.prototype.deleteSelected=function(){var n=this,t=this.attachments.filter(function(t){return t.isSelected&&null===t.serverRelativeUrl});this.attachments.forEach(function(t){t.isLoading=!!t.isSelected}),t.forEach(function(e){var t=n.attachments.findIndex(function(t){return t.fileName===e.fileName});n.attachments.splice(t,1)});var e=this.attachments.filter(function(t){return null!==t.serverRelativeUrl&&t.isSelected});this.attachments.forEach(function(t){t.isLoading=!!t.isSelected});var a=e.map(function(t){return t.fileName});this.spAttachmentLibraryService.deleteAttachments(this._listName,this._itemId,a).then(function(t){n.loadExistingFiles()})["catch"](function(t){console.log(t)})},t.prototype.getBase64=function(a){return new Promise(function(t,e){var n=new FileReader;n.readAsDataURL(a),n.onload=function(){return t(n.result)},n.onerror=function(t){return e(t)}})},t.decorators=[{type:a.Component,args:[{selector:"sp-attachment",template:'<div class="container">\r\n <h4>Attachments</h4>\r\n <div class="row">\r\n <div class="col-md-12">\r\n <div class="form-group">\r\n <label class="control-label">Attachments</label>\r\n <input type="file" multiple size="50" class="custom-file-input" id="customFile" name="filename"\r\n base-sixty-four-input (click)="resetFiles($event)" (input)="onAttachmentAdded($event)">\r\n <label class="custom-file-label" for="customFile">choose your attachments</label>\r\n <ul class="list-group">\r\n <li class="list-group-item">\r\n <div class="row">\r\n <div class="col-md-8 pull-left">Files</div>\r\n <div class="col-md-4 pull-right">Actions</div>\r\n </div>\r\n </li>\r\n </ul>\r\n <ul class="list-group" *ngFor="let attachment of attachments">\r\n <li *ngIf="!attachment.isLoading" class="list-group-item">\r\n <div class="row">\r\n <div class="col-md-8 pull-left">\r\n <div class="form-check">\r\n <input type="checkbox" [(ngModel)]="attachment.isSelected" class="form-check-input" id="{{ attachment.fileName }}">\r\n </div>\r\n <a class="ml-sm-3" href="{{ attachment.serverRelativeUrl }}" target="_blank">\r\n {{attachment.fileName}}\r\n </a>\r\n </div>\r\n <div class="col-md-4 pull-right">\r\n <button class="btn btn-danger btn-xs" [swal]="deleteSwalOptions"\r\n (confirm)="removeAttachment(attachment)">\r\n <fa-icon [icon]="fa.faTrash"></fa-icon>\r\n </button>\r\n <button *ngIf="!attachment.serverRelativeUrl" class="btn btn-primary btn-xs ml-sm-2"\r\n (click)="uploadAttachment(attachment)">\r\n <fa-icon [icon]="fa.faUpload"></fa-icon>\r\n </button>\r\n <button *ngIf="attachment.isContentChanged" class="btn btn-primary btn-xs ml-sm-2"\r\n (click)="replaceAttachment(attachment)">\r\n <fa-icon [icon]="fa.faEdit"></fa-icon>\r\n </button>\r\n </div>\r\n </div>\r\n </li>\r\n <li *ngIf="attachment.isLoading" class="list-group-item animated-background">\r\n </li>\r\n </ul>\r\n </div>\r\n </div>\r\n </div>\r\n <div class="row">\r\n <div class="col-md-12" *ngIf="attachments.length > 0">\r\n <button type="button" class="btn btn-info btn-sm ml-sm-2"\r\n (click)="markOrUnMarkAll()">\r\n {{ allSelected() ? \'Unmark All\' : \'Mark All\'}}\r\n </button>\r\n <button *ngIf="hasNewUnUploadedAndSelcted()" type="button" class="btn btn-success btn-sm ml-sm-2"\r\n (click)="uploadNewAttachments()">\r\n Upload Selected({{ getUnUploadedCount() }})\r\n </button>\r\n <button *ngIf="hasSelected()" type="button" class="btn btn-danger btn-sm ml-sm-2" [swal]="deleteAllSwalOptions"\r\n (confirm)="deleteSelected()">Delete Selected({{ getSelectedCount() }})\r\n </button>\r\n </div>\r\n </div>\r\n</div>',styles:[".btn-group-xs>.btn,.btn-xs{padding:.25rem .4rem;font-size:.875rem;line-height:.5;border-radius:.2rem}.loader-container{margin:0 auto;width:400px;max-width:90%;display:flex;flex-direction:column;justify-content:center;align-items:center}@-webkit-keyframes placeholder{0%{background-position:-600px 0}100%{background-position:600px 0}}@keyframes placeholder{0%{background-position:-600px 0}100%{background-position:600px 0}}.animated-background{-webkit-animation-duration:1s;animation-duration:1s;-webkit-animation-fill-mode:forwards;animation-fill-mode:forwards;-webkit-animation-iteration-count:infinite;animation-iteration-count:infinite;-webkit-animation-name:placeholder;animation-name:placeholder;-webkit-animation-timing-function:linear;animation-timing-function:linear;background:#eee;background:linear-gradient(to right,#eee 8%,#ddd 18%,#eee 33%);background-size:1200px 100px;min-height:30px;width:100%;margin:5px 0;border-radius:3px}"]}]}],t.ctorParameters=function(){return[{type:p}]},t.propDecorators={listName:[{type:a.Input}],itemId:[{type:a.Input}]},t}(),y=function(){function e(){}return e.forRoot=function(t){return{ngModule:e,providers:[p,{provide:h,useValue:t}]}},e.decorators=[{type:a.NgModule,args:[{declarations:[b],imports:[r.BrowserModule,i.FormsModule,s.SweetAlert2Module.forRoot({buttonsStyling:!1,customClass:"modal-content",confirmButtonClass:"btn btn-primary",cancelButtonClass:"btn"}),l.FontAwesomeModule],exports:[b]}]}],e}();t.SpAttachmentLibraryService=p,t.SpAttachmentLibraryComponent=b,t.SpAttachmentLibraryModule=y,t.SpAttachmentConfigService=h,t.ɵa=h,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=sp-attachment-library.umd.min.js.map