1
0

storeObjectArray.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { StoreObject } from './storeObject';
  2. export class StoreObjectArray extends StoreObject {
  3. static get WRONG_LINE_NUMBER () {
  4. return 1;
  5. }
  6. static get WRONG_TYPE () {
  7. return 2;
  8. }
  9. static get WRONG_COLUMN_NUMBER () {
  10. return 3;
  11. }
  12. constructor (type, lines, columns, value = null, readOnly = false) {
  13. super(type, value, readOnly);
  14. this._lines = lines;
  15. this._columns = columns;
  16. }
  17. get lines () {
  18. return this._lines;
  19. }
  20. get columns () {
  21. return this._columns;
  22. }
  23. isCompatible (another) {
  24. if(another instanceof StoreObject) {
  25. if(((this.lines === -1 && another.lines > 0) ||
  26. (this.lines === another.lines))) {
  27. if ((this.columns === -1 && another.columns > 0) ||
  28. (this.columns === another.columns)) {
  29. return super.isCompatible(another);
  30. }
  31. }
  32. }
  33. return false;
  34. }
  35. get isVector () {
  36. return this.type.dimensions === 1;
  37. }
  38. get isValid () {
  39. if (this.value !== null) {
  40. if( this.isVector) {
  41. if(this.value.length !== this.lines) {
  42. return [StoreObjectArray.WRONG_LINE_NUMBER, this.value.length];;
  43. }
  44. const mustBeNull = this.value.find(v => !this.type.canAccept(v.type) );
  45. if(!!mustBeNull) {
  46. return [StoreObjectArray.WRONG_TYPE, this.value.indexOf(mustBeNull)];;
  47. }
  48. }
  49. return [];
  50. } else {
  51. if(this.lines !== this.value.length) {
  52. return [StoreObjectArray.WRONG_LINE_NUMBER, this.value.length];
  53. }
  54. for (let i = 0; i < this.lines; i++) {
  55. for (let j = 0; j < this.columns; j++) {
  56. const arr = this.value[i];
  57. if(arr.length !== this.columns) {
  58. return [StoreObjectArray.WRONG_COLUMN_NUMBER, arr.length];
  59. }
  60. const mustBeNull = arr.find(v => !this.type.canAccept(v.type) );
  61. if(!!mustBeNull) {
  62. return [StoreObjectArray.WRONG_TYPE, i, arr.indexOf(mustBeNull)];
  63. }
  64. }
  65. }
  66. return [];
  67. }
  68. }
  69. }