diff --git a/.gitignore b/.gitignore index 30bc1627986aa5d1e6aebc1718de71eaee6021c5..671a70285149afb85c64c031d38b612959fe9d1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/node_modules \ No newline at end of file +/node_modules +/test \ No newline at end of file diff --git a/modele/Cellule.js b/modele/Cellule.js new file mode 100644 index 0000000000000000000000000000000000000000..3cea68836eeaecfa1b97ba02ba72ee555f61d544 --- /dev/null +++ b/modele/Cellule.js @@ -0,0 +1,32 @@ +const Component = require("./Component"); +const Nombre = require("./Nombre"); + +class Cellule extends Component{ + + #numero; + #expression; + + constructor(numero, expression = null) { + super(); + this.#numero = numero; + this.#expression = expression; + } + + setExpression(expression){ + this.#expression = expression; + } + + toJSON(){ + return { + numero: this.#numero, + expression: this.#expression.toJSON() //voir si on garde expression ou si on met le type d'expression (ex nombre: 12) + } + } + + readJSON(json){ + //TODO: gerer les autres expressions par la suite + this.#expression = new Nombre(json.expression); + } +} + +module.exports = Cellule; \ No newline at end of file diff --git a/modele/Component.js b/modele/Component.js new file mode 100644 index 0000000000000000000000000000000000000000..790945a5e89131584ec056db711b66203b156d7a --- /dev/null +++ b/modele/Component.js @@ -0,0 +1,17 @@ +class Component{ + #nom; + + constructor(name = ''){ + this.#nom = name; + } + + getNom(){ + return this.#nom; + } + + setNom(name){ + this.#nom = name; + } +} + +module.exports = Component; \ No newline at end of file diff --git a/modele/Document.js b/modele/Document.js new file mode 100644 index 0000000000000000000000000000000000000000..9505b2140873f9fdbba74d7ee819607d3490f690 --- /dev/null +++ b/modele/Document.js @@ -0,0 +1,87 @@ +const Component = require("./Component"); +const Feuille = require("./Feuille"); + +class Document extends Component{ + + #feuilles; + #auteur; //id de compte utilisateur + #id; + #dateCreation; + #dateModification; + + constructor(id, user){ + super("Sans titre"); + this.#id = id; + this.#auteur = user; + this.#feuilles = [new Feuille()]; + this.#dateCreation = Date.now(); + this.#dateModification = Date.now(); + //TODO: historique des commandes + } + + //ajoute une feuille à la liste des feuilles + addFeuille(feuille){ + this.#feuilles.push(feuille); + } + + //enlève la feuille à l'index id + removeFeuille(id){ + this.#feuilles.splice(id, 1); + } + + //nettoie la liste des feuilles + clearFeuilles(){ + this.#feuilles = []; + } + + //set la date de creation + setDateCreation(date){ + this.#dateCreation = date; + } + + //set la date de modification + setDateModification(date){ + this.#dateModification = date; + } + + get nom(){ + return super.getNom(); + } + + get auteur(){ + return this.#auteur; + } + + toJSON(){ + + let feuillesJSON = []; + this.#feuilles.forEach(feuille => feuillesJSON.push(feuille.toJSON())); + + return { + id: this.#id, + nom: super.getNom(), + auteur: this.#auteur, + feuilles: feuillesJSON, + dateCreation: this.#dateCreation, + dateModification: this.#dateModification + } + } + + readJSON(json){ + super.setNom(json.nom); + this.#auteur = json.auteur; + this.#id = json.id; + this.#dateCreation = json.dateCreation; + this.#dateModification = json.dateModification; + + this.clearFeuilles(); + json.feuilles.forEach(feuille => { + let f = new Feuille(this); + f.readJSON(feuille); + this.addFeuille(f); + }); + } + +} + +module.exports = Document; \ No newline at end of file diff --git a/modele/Expression.js b/modele/Expression.js new file mode 100644 index 0000000000000000000000000000000000000000..740826dae3dfa9add7fca1cc0315df6e88773903 --- /dev/null +++ b/modele/Expression.js @@ -0,0 +1,25 @@ +class Expression{ + #contenu; + + constructor(content = ""){ + this.#contenu = content; + } + + afficherContenu(){ + return this.#contenu; + } + + isNombre(){ + return false; + } + + isTexte(){ + return false; + } + + toJSON(){ + return this.#contenu; + } +} + +module.exports = Expression; \ No newline at end of file diff --git a/modele/Feuille.js b/modele/Feuille.js new file mode 100644 index 0000000000000000000000000000000000000000..62a0ba0eed42ad800ee8772b50a194f61fbdc17e --- /dev/null +++ b/modele/Feuille.js @@ -0,0 +1,59 @@ +const Component = require("./Component"); +const Cellule = require("./Cellule"); + +class Feuille extends Component{ + + #cells; + #parent; //objet Document + + constructor(parent = null){ + super("Sans titre"); + this.#cells = [[],[]]; //TODO: définir la taille de la feuille + } + + addCell(cell,row){ + this.#cells[row].push(cell); + } + + removeCell(id){ + this.#cells.splice(id, 1); + } + + clearCells(){ + this.#cells = [[]]; + } + + toJSON(){ + let cellsJSON = []; + let tmp = []; + + for (let i = 0; i < this.#cells.length; i++){ + this.#cells[i].forEach(cell => tmp.push(cell.toJSON())); + cellsJSON.push(tmp); + tmp = []; + } + + return { + nom: super.getNom(), + cells: cellsJSON + } + } + + readJSON(json){ + super.setNom(json.nom); + let tmp = []; + this.#cells = []; //reset du tableau + + for (let i = 0; i < json.cells.length; i++){ + json.cells[i].forEach(cell => { + let cellule = new Cellule(cell.numero); + cellule.readJSON(cell); + tmp.push(cellule); + }); + this.#cells.push(tmp); + tmp = []; + } + } +} + +module.exports = Feuille; \ No newline at end of file diff --git a/modele/Nombre.js b/modele/Nombre.js new file mode 100644 index 0000000000000000000000000000000000000000..72dffbbbe92a2d51b2167c519957c9c65c9a4fcd --- /dev/null +++ b/modele/Nombre.js @@ -0,0 +1,18 @@ +const Expression = require("./Expression"); + +class Nombre extends Expression{ + + constructor(content){ + super(content); + } + + afficherContenu(){ + return super.afficherContenu(); + } + + isNombre(){ + return true; + } +} + +module.exports = Nombre; \ No newline at end of file diff --git a/modele/Texte.js b/modele/Texte.js new file mode 100644 index 0000000000000000000000000000000000000000..fc74922722739317f54e894eca6ecdf82fcf69f7 --- /dev/null +++ b/modele/Texte.js @@ -0,0 +1,18 @@ +const Expression = require("./Expression"); + +class Texte extends Expression{ + + constructor(content){ + super(content); + } + + afficherContenu(){ + return super.afficherContenu(); + } + + isTexte(){ + return true; + } +} + +module.exports = Texte; \ No newline at end of file diff --git a/modele/fileHandler.js b/modele/fileHandler.js new file mode 100644 index 0000000000000000000000000000000000000000..37efbf7a0a7f18a4995c276e065676d7e7441a39 --- /dev/null +++ b/modele/fileHandler.js @@ -0,0 +1,21 @@ +const fs = require('fs'); + +function writeFile(document){ + const data = JSON.stringify(document.toJSON()); + + const directoryPath = `storage/${document.auteur}`; + + + if (!fs.existsSync(directoryPath)) { + fs.mkdirSync(directoryPath, { recursive: true }); + } + + fs.writeFile(`${directoryPath}/${document.nom}.json`,data, (err) => { + if (err){ + console.log(err); + throw err; + } + }); +} + +module.exports = { writeFile }; \ No newline at end of file diff --git a/package.json b/package.json index fde06764348585c80db28cd6f84a174632f02969..84178d400e070e5b8aaaceb35d0dca873f73a936 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "collabsheet", - "version": "0.0.1", + "version": "0.2.1", "description": "Tableau collaboratif", "main": "index.js", "scripts": {