Skip to content

Commit

Permalink
Merge pull request #1 from Encanto/webcomponent
Browse files Browse the repository at this point in the history
Webcomponent
  • Loading branch information
WilliamRClark authored May 28, 2021
2 parents c07245d + f1a0ca3 commit 4aafd4e
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 71 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# NgxFileSelect

This is a local file browse web component that you can integrate into your website.
<img src="./doc/FileBrowseComponent.png" width="600">

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 10.1.1.

## Development server
Expand All @@ -25,3 +28,19 @@ Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protrac
## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).


## Custom Components

The file-browse custom component is exported from this library. The goal of this component is to select files from your local filesystem, and return the result to anyone who is listening. Anywhere in your code, you can add the following text to listen:

```javascript
window.addEventListener('fselect', (event) => {
alert('File selected: ' + JSON.stringify(event.detail))
})
```
And add the following tag to display the listener:

```html
<file-browse></file-browse>
```
13 changes: 13 additions & 0 deletions build-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fs = require('fs-extra');
const concat = require('concat');

build = async () =>{
const files = [
'./dist/ngx-file-select/runtime.js',
'./dist/ngx-file-select/polyfills.js',
'./dist/ngx-file-select/main.js'
];

await concat(files, 'dist/ngx-file-select.js');
}
build();
Binary file added doc/FileBrowseComponent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 44 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"build": "ng build --prod --output-hashing none && node build-component.js",
"test": "ng test",
"test-headless": "ng test --watch=false --browsers=ChromeHeadless",
"lint": "ng lint",
Expand All @@ -31,10 +31,12 @@
"@angular-devkit/build-angular": "~0.1001.1",
"@angular/cli": "~10.1.1",
"@angular/compiler-cli": "~10.1.1",
"@types/node": "^12.11.1",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"concat": "^1.0.3",
"fs-extra": "^10.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
Expand Down
Empty file removed src/app/app.component.css
Empty file.
3 changes: 0 additions & 3 deletions src/app/app.component.html

This file was deleted.

29 changes: 0 additions & 29 deletions src/app/app.component.spec.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/app/app.component.ts

This file was deleted.

27 changes: 18 additions & 9 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { Injector, NgModule } from '@angular/core';
import { createCustomElement } from '@angular/elements';

import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { FileBrowseComponent } from './file-browse/file-browse.component';

@NgModule({
declarations: [
AppComponent,
FileBrowseComponent
],
imports: [
BrowserModule,
HttpClientModule
HttpClientModule,
],
providers: [],
bootstrap: [AppComponent]
declarations: [FileBrowseComponent]
})
export class AppModule { }
export class AppModule {

constructor(private injector: Injector) {

}

ngDoBootstrap(){
const fbComp = createCustomElement(FileBrowseComponent, { injector: this.injector });
if (!customElements.get('file-browse')) {
customElements.define('file-browse', fbComp);
}
}

}
17 changes: 14 additions & 3 deletions src/app/file-browse/file-browse.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { Component, EventEmitter, Output, Injector, OnInit } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { FileServiceService } from '../services/file-service.service';


@Component({
selector: 'local-file-browse',
templateUrl: './file-browse.component.html',
Expand All @@ -12,7 +14,14 @@ export class FileBrowseComponent implements OnInit {
public selectedFile;
private rootPath;

constructor(private fileService: FileServiceService) {
constructor(injector: Injector, private fileService: FileServiceService) {
// Convert `FileBrowseComponent` to a custom element.
const FileBrowseElement = createCustomElement(FileBrowseComponent, {injector});

if (!customElements.get('file-browse')) {
// Register the custom element with the browser.
customElements.define('file-browse', FileBrowseElement);
}
}

ngOnInit(): void {
Expand Down Expand Up @@ -40,12 +49,14 @@ export class FileBrowseComponent implements OnInit {
// Compute the full path.
if (this.selectedFile) {
console.log('file selected: ' + this.rootPath + this.getCurrentPath() + '/' + this.selectedFile.name);
let event = new CustomEvent("file-select", {
// Dispatch using window custom event so we can listen from anywhere.
let event = new CustomEvent("fselect", {
detail: {
path: this.rootPath + this.getCurrentPath() + '/' + this.selectedFile.name
}
});
window.dispatchEvent(event);

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<app-root></app-root>
<file-browse></file-browse>
</body>
</html>
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
platformBrowserDynamic()
.bootstrapModule(AppModule, { })
.catch(err => console.error(err));

0 comments on commit 4aafd4e

Please sign in to comment.