Skip to content

Commit 588db0e

Browse files
authored
Merge pull request #385 from PillouMatou/import/export_user
Import/export user
2 parents 896fb40 + f1aaeef commit 588db0e

15 files changed

Lines changed: 353 additions & 11 deletions

src/app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ import { DialogAddGridComponent } from './components/dialog-add-grid/dialog-add-
8585
import { ChooseYourGridComponent } from './components/choose-your-grid/choose-your-grid.component';
8686
import { DeleteGridUserComponent } from './components/delete-grid-user/delete-grid-user.component';
8787
import { DialogDeleteGridUserComponent } from './components/dialog-delete-grid-user/dialog-delete-grid-user.component';
88+
import { ExportSaveUserDialogComponent } from './components/export-save-user-dialog/export-save-user-dialog.component';
89+
import { ImportUserComponent } from './components/import-user/import-user.component';
8890

8991
@NgModule({
9092
declarations: [
@@ -152,7 +154,9 @@ import { DialogDeleteGridUserComponent } from './components/dialog-delete-grid-u
152154
DialogAddGridComponent,
153155
ChooseYourGridComponent,
154156
DeleteGridUserComponent,
155-
DialogDeleteGridUserComponent
157+
DialogDeleteGridUserComponent,
158+
ExportSaveUserDialogComponent,
159+
ImportUserComponent
156160
],
157161
imports: [
158162
BrowserModule,

src/app/components/dialog-add-grid/dialog-add-grid.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ export class DialogAddGridComponent implements OnInit {
4747
return this.userPageService.currentUser.id === user.id
4848
});
4949
this.userPageService.usersList[indexUser] = this.userPageService.currentUser;
50+
this.boardService.gridChosen = newGrid.value['nameGrid'];
5051
setTimeout(() => {
5152
this.indexeddbaccessService.addGrid();
5253
this.boardService.updateElementList();
54+
5355
},200);
5456
}
5557

src/app/components/export-save-user-dialog/export-save-user-dialog.component.css

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="dialogContent">
2+
<div style="display: flex; flex-wrap: nowrap; align-items: center; color: lightgray;">
3+
<input [(ngModel)]="this.name" maxlength="32" placeholder="{{this.multilinguism.translate('EnterNameOfTheSave')}}" style="width: 100%"
4+
type="textfield">.useraugcom
5+
</div>
6+
<input (click)="this.exportSave()" type="button" value="{{this.multilinguism.translate('validate')}}">
7+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { ExportSaveUserDialogComponent } from './export-save-user-dialog.component';
4+
import {Ng2ImgMaxModule} from "ng2-img-max";
5+
import {RouterTestingModule} from "@angular/router/testing";
6+
7+
describe('XportSaveUserDialogComponent', () => {
8+
let component: ExportSaveUserDialogComponent;
9+
let fixture: ComponentFixture<ExportSaveUserDialogComponent>;
10+
11+
beforeEach(async(() => {
12+
TestBed.configureTestingModule({
13+
declarations: [ ExportSaveUserDialogComponent ],
14+
imports: [Ng2ImgMaxModule, RouterTestingModule]
15+
})
16+
.compileComponents();
17+
}));
18+
19+
beforeEach(() => {
20+
fixture = TestBed.createComponent(ExportSaveUserDialogComponent);
21+
component = fixture.componentInstance;
22+
fixture.detectChanges();
23+
});
24+
25+
it('should create', () => {
26+
expect(component).toBeTruthy();
27+
});
28+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {Component, OnInit} from '@angular/core';
2+
import {ExportManagerService} from "../../services/export-manager.service";
3+
import JSZip from "jszip";
4+
import {saveAs as importedSaveAs} from 'file-saver';
5+
import {MultilinguismService} from "../../services/multilinguism.service";
6+
import {BoardService} from "../../services/board.service";
7+
8+
@Component({
9+
selector: 'app-xport-save-user-dialog',
10+
templateUrl: './export-save-user-dialog.component.html',
11+
styleUrls: ['./export-save-user-dialog.component.css']
12+
})
13+
export class ExportSaveUserDialogComponent implements OnInit {
14+
15+
name: String = "save";
16+
17+
constructor(public exportManagerService: ExportManagerService,
18+
public multilinguism: MultilinguismService,
19+
public boardservice: BoardService) {
20+
}
21+
22+
ngOnInit(): void {
23+
}
24+
25+
exportSave() {
26+
let tempName;
27+
if (this.name.endsWith(".json")) {
28+
tempName = this.name.slice(0, this.name.length - 5);
29+
} else if (this.name.endsWith(".useraugcom")) {
30+
tempName = this.name.slice(0, this.name.length - 7);
31+
} else {
32+
tempName = this.name;
33+
}
34+
if (tempName.length == 0) {
35+
tempName = "save";
36+
}
37+
38+
const jszip = new JSZip();
39+
jszip.file(tempName + '.opgf', this.exportManagerService.data);
40+
41+
jszip.generateAsync({type: 'blob'}).then(function (content) {
42+
// see FileSaver.js
43+
importedSaveAs(content, tempName + '.useraugcom');
44+
});
45+
46+
}
47+
48+
}

src/app/components/import-user/import-user.component.css

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<input #file type="file" accept=".useraugcom" (change)="importUser(file.files)" onclick="this.value=null">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { ImportUserComponent } from './import-user.component';
4+
import {Ng2ImgMaxModule} from "ng2-img-max";
5+
import {RouterTestingModule} from "@angular/router/testing";
6+
7+
describe('ImportUserComponent', () => {
8+
let component: ImportUserComponent;
9+
let fixture: ComponentFixture<ImportUserComponent>;
10+
11+
beforeEach(async(() => {
12+
TestBed.configureTestingModule({
13+
declarations: [ ImportUserComponent ],
14+
imports: [Ng2ImgMaxModule, RouterTestingModule]
15+
})
16+
.compileComponents();
17+
}));
18+
19+
beforeEach(() => {
20+
fixture = TestBed.createComponent(ImportUserComponent);
21+
component = fixture.componentInstance;
22+
fixture.detectChanges();
23+
});
24+
25+
it('should create', () => {
26+
expect(component).toBeTruthy();
27+
});
28+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import * as JSZip from 'jszip';
3+
import {zip} from "rxjs";
4+
import {BoardService} from "../../services/board.service";
5+
import {LayoutService} from "../../services/layout.service";
6+
import {Router} from "@angular/router";
7+
import {IndexeddbaccessService} from "../../services/indexeddbaccess.service";
8+
import {GridElement} from "../../types";
9+
import {JsonValidatorService} from "../../services/json-validator.service";
10+
11+
@Component({
12+
selector: 'app-import-user',
13+
templateUrl: './import-user.component.html',
14+
styleUrls: ['./import-user.component.css']
15+
})
16+
export class ImportUserComponent implements OnInit {
17+
18+
constructor(public boardService: BoardService,
19+
public layoutService: LayoutService,
20+
public router: Router,
21+
public indexedDBacess:IndexeddbaccessService,
22+
public jsonValidator: JsonValidatorService) { }
23+
24+
ngOnInit(): void {
25+
}
26+
27+
importUser(files: FileList) {
28+
const zipFolder: JSZip = new JSZip();
29+
//let tempBoard;
30+
zipFolder.loadAsync(files[0]).then((zipFiles) => {
31+
zipFiles.forEach((fileName) => {
32+
zipFolder
33+
.file(fileName)
34+
.async('base64')
35+
.then((content) => {
36+
this.useUserAugcomZip(content);
37+
}
38+
);
39+
});
40+
});
41+
}
42+
43+
useUserAugcomZip(contentZip:any){
44+
let userToBeImported;
45+
userToBeImported = JSON.parse(this.b64DecodeUnicode(contentZip));
46+
console.log('userToBeImported : ',userToBeImported);
47+
/*
48+
tempBoard.ElementList.forEach(element => {
49+
this.checkAndUpdateElementDefaultForm(element);
50+
});
51+
this.boardService.board = this.jsonValidator.getCheckedGrid(tempBoard);
52+
this.layoutService.refreshAll(this.boardService.board.NumberOfCols, this.boardService.board.NumberOfRows, this.boardService.board.GapSize);
53+
this.boardService.updateElementList();
54+
this.boardService.backHome();
55+
console.log(this.boardService.board);
56+
this.indexedDBacess.update();
57+
*/
58+
this.indexedDBacess.importUserInDatabase(userToBeImported);
59+
this.router.navigate(['logging']);
60+
}
61+
62+
b64DecodeUnicode(str) {
63+
return decodeURIComponent(atob(str).split('').map(function(c) {
64+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
65+
}).join(''));
66+
}
67+
68+
checkAndUpdateElementDefaultForm(element: GridElement) {
69+
const defaultForm = element.ElementFormsList.find(form => {
70+
const newForm = form.LexicInfos.find(info => {
71+
return (info.default != null && info.default);
72+
});
73+
return newForm != null;
74+
});
75+
if (defaultForm === null) {
76+
if (element.ElementFormsList[0] !== null && element.ElementFormsList[0] !== undefined) {
77+
element.ElementFormsList.push({
78+
DisplayedText: element.ElementFormsList[0].DisplayedText,
79+
VoiceText: element.ElementFormsList[0].VoiceText,
80+
LexicInfos: [{default: true}],
81+
ImageID: element.ElementFormsList[0].ImageID
82+
});
83+
} else {
84+
console.log('DEFAULT FORM NOT FOUND FOR ' + element.ID);
85+
element.ElementFormsList.push({
86+
DisplayedText: element.ID,
87+
VoiceText: element.ID,
88+
LexicInfos: [{default: true}],
89+
ImageID: element.ID
90+
});
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)