|
| 1 | +import { Component, Input } from "@angular/core"; |
| 2 | +import { storiesOf, moduleMetadata } from "@storybook/angular"; |
| 3 | + |
| 4 | +import { action } from "@storybook/addon-actions"; |
| 5 | +import { |
| 6 | + withKnobs, |
| 7 | + boolean, |
| 8 | + text, |
| 9 | + array |
| 10 | +} from "@storybook/addon-knobs"; |
| 11 | + |
| 12 | +import { FileUploaderModule, NotificationModule, ButtonModule } from "../"; |
| 13 | +import { NotificationService } from "../notification/notification.service"; |
| 14 | + |
| 15 | +@Component({ |
| 16 | + selector: "app-file-uploader", |
| 17 | + template: ` |
| 18 | + <ibm-file-uploader |
| 19 | + [title]="title" |
| 20 | + [description]="description" |
| 21 | + [buttonText]="buttonText" |
| 22 | + [accept]="accept" |
| 23 | + [multiple]="multiple" |
| 24 | + [(files)]="files"> |
| 25 | + </ibm-file-uploader> |
| 26 | +
|
| 27 | + <div [id]="notificationId" style="width: 300px; margin-top: 20px"></div> |
| 28 | + <button ibmButton *ngIf="files && files.size > 0" (click)="onUpload()"> |
| 29 | + Upload |
| 30 | + </button> |
| 31 | + ` |
| 32 | +}) |
| 33 | +class FileUploaderStory { |
| 34 | + static notificationCount = 0; |
| 35 | + |
| 36 | + @Input() notificationId = `notification-${FileUploaderStory.notificationCount}`; |
| 37 | + @Input() files: any; |
| 38 | + @Input() title; |
| 39 | + @Input() description; |
| 40 | + @Input() buttonText; |
| 41 | + @Input() accept; |
| 42 | + @Input() multiple; |
| 43 | + |
| 44 | + protected maxSize = 500000; |
| 45 | + |
| 46 | + constructor(protected notificationService: NotificationService) { |
| 47 | + FileUploaderStory.notificationCount++; |
| 48 | + } |
| 49 | + |
| 50 | + onUpload() { |
| 51 | + this.files.forEach(fileItem => { |
| 52 | + if (fileItem.file.size > this.maxSize) { |
| 53 | + this.notificationService.showNotification({ |
| 54 | + type: "error", |
| 55 | + title: `'${fileItem.file.name}' exceeds size limit`, |
| 56 | + message: `500kb max size. Please select a new file and try again`, |
| 57 | + target: `#${this.notificationId}` |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + let filesArray = Array.from<any>(this.files); |
| 63 | + if (filesArray.every(fileItem => fileItem.file.size <= this.maxSize)) { |
| 64 | + this.files.forEach(fileItem => { |
| 65 | + if (!fileItem.uploaded) { |
| 66 | + fileItem.state = "upload"; |
| 67 | + setTimeout(() => { |
| 68 | + fileItem.state = "complete"; |
| 69 | + fileItem.uploaded = true; |
| 70 | + console.log(fileItem); |
| 71 | + }, 1500); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +@Component({ |
| 79 | + selector: "app-ngmodel-file-uploader", |
| 80 | + template: ` |
| 81 | + <ibm-file-uploader |
| 82 | + [title]="title" |
| 83 | + [description]="description" |
| 84 | + [buttonText]="buttonText" |
| 85 | + [accept]="accept" |
| 86 | + [multiple]="multiple" |
| 87 | + [(ngModel)]="model"> |
| 88 | + </ibm-file-uploader> |
| 89 | +
|
| 90 | + <br><div [id]="notificationId" style="width: 300px"></div> |
| 91 | + <button ibmButton *ngIf="model && model.size > 0" (click)="onUpload()"> |
| 92 | + Upload |
| 93 | + </button> |
| 94 | + ` |
| 95 | +}) |
| 96 | +class NgModelFileUploaderStory { |
| 97 | + static notificationCount = 0; |
| 98 | + |
| 99 | + @Input() notificationId = `notification-${FileUploaderStory.notificationCount}`; |
| 100 | + @Input() title; |
| 101 | + @Input() description; |
| 102 | + @Input() buttonText; |
| 103 | + @Input() accept; |
| 104 | + @Input() multiple; |
| 105 | + |
| 106 | + protected model = new Set(); |
| 107 | + protected maxSize = 500000; |
| 108 | + |
| 109 | + constructor(protected notificationService: NotificationService) { |
| 110 | + FileUploaderStory.notificationCount++; |
| 111 | + } |
| 112 | + |
| 113 | + onUpload() { |
| 114 | + this.model.forEach(fileItem => { |
| 115 | + if (fileItem.file.size > this.maxSize) { |
| 116 | + this.notificationService.showNotification({ |
| 117 | + type: "error", |
| 118 | + title: `'${fileItem.file.name}' exceeds size limit`, |
| 119 | + message: `500kb max size. Please select a new file and try again`, |
| 120 | + target: `#${this.notificationId}` |
| 121 | + }); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + let filesArray = Array.from<any>(this.model); |
| 126 | + if (filesArray.every(fileItem => fileItem.file.size <= this.maxSize)) { |
| 127 | + this.model.forEach(fileItem => { |
| 128 | + if (!fileItem.uploaded) { |
| 129 | + fileItem.state = "upload"; |
| 130 | + setTimeout(() => { |
| 131 | + fileItem.state = "complete"; |
| 132 | + fileItem.uploaded = true; |
| 133 | + console.log(fileItem); |
| 134 | + }, 1500); |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +storiesOf("File Uploader", module) |
| 142 | + .addDecorator( |
| 143 | + moduleMetadata({ |
| 144 | + imports: [FileUploaderModule, NotificationModule, ButtonModule], |
| 145 | + declarations: [FileUploaderStory, NgModelFileUploaderStory] |
| 146 | + }) |
| 147 | + ) |
| 148 | + .addDecorator(withKnobs) |
| 149 | + .add("Basic", () => ({ |
| 150 | + template: ` |
| 151 | + <app-file-uploader |
| 152 | + [title]="title" |
| 153 | + [description]="description" |
| 154 | + [buttonText]="buttonText" |
| 155 | + [accept]="accept" |
| 156 | + [multiple]="multiple"> |
| 157 | + </app-file-uploader> |
| 158 | + `, |
| 159 | + props: { |
| 160 | + title: text("The title", "Account Photo"), |
| 161 | + description: text("The description", "only .jpg and .png files. 500kb max file size."), |
| 162 | + buttonText: text("Button text", "Add files"), |
| 163 | + accept: array("Accepted file extensions", [".png", ".jpg"], ","), |
| 164 | + multiple: boolean("Supports multiple files", true) |
| 165 | + } |
| 166 | + })) |
| 167 | + .add("Using ngModel", () => ({ |
| 168 | + template: ` |
| 169 | + <app-ngmodel-file-uploader |
| 170 | + [title]="title" |
| 171 | + [description]="description" |
| 172 | + [buttonText]="buttonText" |
| 173 | + [accept]="accept" |
| 174 | + [multiple]="multiple"> |
| 175 | + </app-ngmodel-file-uploader> |
| 176 | + `, |
| 177 | + props: { |
| 178 | + title: text("The title", "Account Photo"), |
| 179 | + description: text("The description", "only .jpg and .png files. 500kb max file size."), |
| 180 | + buttonText: text("Button text", "Add files"), |
| 181 | + accept: array("Accepted file extensions", [".png", ".jpg"], ","), |
| 182 | + multiple: boolean("Supports multiple files", true) |
| 183 | + } |
| 184 | + })); |
0 commit comments