Skip to content

Commit

Permalink
moved various util methods for GridData to gridUtil.js in order to be…
Browse files Browse the repository at this point in the history
… able to use them without instantiating GridData (performance), #451
  • Loading branch information
klues committed Nov 20, 2024
1 parent e930ba5 commit a550636
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 69 deletions.
73 changes: 4 additions & 69 deletions src/js/model/GridData.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { modelUtil } from '../util/modelUtil';
import { GridElement } from './GridElement';
import { AdditionalGridFile } from './AdditionalGridFile';
import { GridActionARE } from './GridActionARE';
import { constants } from '../util/constants';
import { Model } from '../externals/objectmodel';
import { Webradio } from './Webradio';
import { gridUtil } from '../util/gridUtil';
import { localStorageService } from '../service/data/localStorageService';
import { encryptionService } from '../service/data/encryptionService';

class GridData extends Model({
id: String,
Expand Down Expand Up @@ -43,48 +41,20 @@ class GridData extends Model({
return this.gridElements.every((elm) => elm.hasSetPosition());
}

getHash() {
let string = '';
this.gridElements.forEach((e) => {
string += JSON.stringify(e.label) + e.x + e.y;
if (e.image && (e.image.data || e.image.url)) {
let temp = e.image.data || e.image.url;
string += temp.substring(temp.length > 30 ? temp.length - 30 : 0);
}
});
return encryptionService.getStringHash(string);
}

hasOutdatedThumbnail() {
return !this.thumbnail || !this.thumbnail.data || this.thumbnail.hash !== this.getHash();
}

getWidth() {
if (this.gridElements.length === 0) {
return 0;
}
return Math.max.apply(
null,
this.gridElements.map((el) => el.x + el.width)
);
return gridUtil.getWidth(this);
}

getHeight() {
if (this.gridElements.length === 0) {
return 0;
}
return Math.max.apply(
null,
this.gridElements.map((el) => el.y + el.height)
);
return gridUtil.getHeight(this);
}

getWidthWithBounds() {
return Math.max(this.getWidth(), this.minColumnCount);
return gridUtil.getWidthWithBounds(this);
}

getHeightWithBounds() {
return Math.max(this.getHeight(), this.rowCount);
return gridUtil.getHeightWithBounds(this);
}

isFull() {
Expand Down Expand Up @@ -198,24 +168,6 @@ class GridData extends Model({
return filteredFiles.length > 0 ? filteredFiles[0] : null;
}

/**
* returns any ARE model (=AdditionalGridFile) that is used in any areAction of this grid. If there is no
* areAction in any gridElement, null is returned.
* @return {*}
*/
getAREModel() {
let areAction = this.getAREFirstAction();
if (areAction) {
let filteredFiles = this.additionalFiles.filter((f) => f.fileName === areAction.areModelGridFileName);
return filteredFiles[0];
}
return null;
}

hasAREModel() {
return !!this.getAREModel();
}

hasPredictionElements() {
for (let element of this.gridElements) {
if (element.type === GridElement.ELEMENT_TYPE_PREDICTION) {
Expand All @@ -225,23 +177,6 @@ class GridData extends Model({
return false;
}

/**
* returns the first GridActionARE that is found in any gridElement of this grid
* @return {*}
*/
getAREFirstAction() {
let allActions = [];
this.gridElements.forEach((element) => {
allActions = allActions.concat(element.actions);
});
return allActions.filter((a) => a.modelName === GridActionARE.getModelName())[0];
}

getAREURL() {
let areAction = this.getAREFirstAction();
return areAction ? areAction.areURL : null;
}

clone() {
let newGrid = new GridData(this);
for (let element of newGrid.gridElements) {
Expand Down
73 changes: 73 additions & 0 deletions src/js/util/gridUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { GridActionCollectElement } from '../model/GridActionCollectElement';
import { GridData } from '../model/GridData';
import { GridElementCollect } from '../model/GridElementCollect.js';
import {constants} from "./constants.js";
import { GridActionARE } from '../model/GridActionARE';
import { encryptionService } from '../service/data/encryptionService';

let gridUtil = {};

Expand Down Expand Up @@ -480,6 +482,77 @@ gridUtil.mergeGrids = function(grid, globalGrid, options = {}) {
}
return grid;
}

gridUtil.getAREFirstAction = function(gridData) {
let allActions = [];
gridData.gridElements.forEach((element) => {
allActions = allActions.concat(element.actions);
});
return allActions.filter((a) => a.modelName === GridActionARE.getModelName())[0];
};

gridUtil.getAREModel = function(gridData) {
let areAction = gridUtil.getAREFirstAction(gridData);
if (areAction) {
let filteredFiles = gridData.additionalFiles.filter((f) => f.fileName === areAction.areModelGridFileName);
return filteredFiles[0];
}
return null;
};

gridUtil.getAREURL = function(gridData) {
let areAction = gridUtil.getAREFirstAction(gridData);
return areAction ? areAction.areURL : null;
};

gridUtil.hasAREModel = function(gridData) {
return !!gridUtil.getAREModel(gridData);
};

gridUtil.hasOutdatedThumbnail = function(gridData) {
return !gridData.thumbnail || !gridData.thumbnail.data || gridData.thumbnail.hash !== gridUtil.getHash(gridData);
};

gridUtil.getHash = function(gridData) {
let string = '';
gridData.gridElements.forEach((e) => {
string += JSON.stringify(e.label) + e.x + e.y;
if (e.image && (e.image.data || e.image.url)) {
let temp = e.image.data || e.image.url;
string += temp.substring(temp.length > 30 ? temp.length - 30 : 0);
}
});
return encryptionService.getStringHash(string);
};

gridUtil.getWidth = function(gridData) {
if (gridData.gridElements.length === 0) {
return 0;
}
return Math.max.apply(
null,
gridData.gridElements.map((el) => el.x + el.width)
);
};

gridUtil.getHeight = function(gridData) {
if (gridData.gridElements.length === 0) {
return 0;
}
return Math.max.apply(
null,
gridData.gridElements.map((el) => el.y + el.height)
);
};

gridUtil.getWidthWithBounds = function(gridData) {
return Math.max(gridUtil.getWidth(gridData), gridData.minColumnCount);
}

gridUtil.getHeightWithBounds = function(gridData) {
return Math.max(gridUtil.getHeight(gridData), gridData.rowCount);
}

/**
* ensure that all defaults are set within the given GridData object
* and all contained GridElement objects
Expand Down

0 comments on commit a550636

Please sign in to comment.