Skip to content
Snippets Groups Projects
Select Git revision
  • 70864752fd7dc1b667b70a0917c091df2cad0ea4
  • master default protected
  • 1.31
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
  • 4.30.1
  • 4.30.0
  • 4.29.1
  • 4.29.0
23 results

Monster.Constraints.IsArray.html

Blame
  • util.mjs 1.30 KiB
    /**
     * Copyright 2023 schukai GmbH
     * SPDX-License-Identifier: AGPL-3.0
     */
    
    import {diff} from "../../data/diff.mjs";
    import { Pathfinder } from "../../data/pathfinder.mjs";
    import { isObject } from "../../types/is.mjs";
    import { Observer } from "../../types/observer.mjs";
    
    export { handleDataSourceChanges, datasourceLinkedElementSymbol };
    
    /**
     * @private
     * @type {symbol}
     */
    const datasourceLinkedElementSymbol = Symbol("datasourceLinkedElement");
    
    /**
     * @private
     */
    function handleDataSourceChanges() {
    
    	if (!this[datasourceLinkedElementSymbol]) {
    		return;
    	} 
    
    	let data = this[datasourceLinkedElementSymbol].data;
    
    	const actualData = JSON.stringify(this.getOption("data"));
    	const actualDataAsObj = JSON.parse(actualData);
    
    	const path = this.getOption("mapping.data");
    	if (path) {
    		data = new Pathfinder(data).getVia(path);
    	}
    
    	if (isObject(data)) {
    		const tmp = [];
    		const keys = Object.keys(data);
    		for (const key of keys) {
    			tmp.push(data[key]);
    		}
    		data = tmp;
    	}
    
    	const index = this.getOption("mapping.index");
    	if (index !== undefined && index !== null) {
    		data = data?.[index];
    	}
    
    	if (data === undefined|| data === null) {
    		data = [];
    	}
    	
    	const result = diff(data,actualDataAsObj);
    	if (result.length === 0) {
    		return;
    	}
    	
    	setTimeout(() => {
    		this.setOption("data", data);
    	}, 0);
    }