MultiplotRequestObject.js 5.15 KB
/**
 * Project      : AMDA-NG
 * Name         : MultiplotRequestObject.js
 * @class   amdaPlotObj.MutiplotRequestObject
 * @extends amdaModel.AmdaTimeObject
 * @brief   Multi-Plot Request Business Object Definition 
 * @author  Benjamin Renard
 * @version $Id: MultiplotRequestObject.js benjamin $
 ******************************************************************************
 *    FT Id     :   Date   : Name - Description
 ******************************************************************************
 *	:           :28/02/2020: BRE  - file creation
 */
 
 		
Ext.define('amdaPlotObj.MultiplotRequestObject', {
    extend: 'amdaModel.AmdaTimeObject',
    idProperty: 'id',

    requires: [
        'amdaModel.PlotNode',
        'amdaPlotObj.PlotRequestObject'
    ],
	
    hasMany: {
        model : 'amdaModel.PlotNode',
        name  : 'plots',
        associationKey:'plots'
    },
    
    constructor: function(){
    	var me = this;
    	me.callParent(arguments);
    },

    isDirty: function() {
        var dirty = false;
        this.plots().each(function(plotNode) {
            var plotObject = plotNode.get('object');
            if (plotObject && (plotObject.get('id') != '') && plotObject.dirty) {
                dirty = true;
            }
        });
        return dirty;
    },
    
    createNewPlot: function() {
        var plotObject = Ext.create('amdaPlotObj.PlotRequestObject');
        return this.createNewPlotFromObject(plotObject);
    },

    duplicatePlot: function(plotNode) {
        var me = this;
        var newPlotNode = null;
        this.plots().each(function(node, index) {
            if (node == plotNode) {
                newPlotNode = Ext.create('amdaModel.PlotNode', {
                    leaf : true
                });
		var data = plotNode.get('object').getJsonValues();
                newPlotNode.set('object', Ext.create('amdaPlotObj.PlotRequestObject', data));
		plotNode.get('object').reject();
                me.plots().remove(plotNode);
                me.plots().insert(index, [newPlotNode]);
            }
        });
        return newPlotNode;
    },

    overwritePlot: function(plotNode, targetPlotNode) {
        var targetIndex = -1;
        this.plots().each(function(node, index) {
            if (node == targetPlotNode) {
                targetIndex = index;
            }
        });
        if (targetIndex < 0) {
            //Target not exists in multiplot node => add it
            this.plots().add(targetPlotNode);
            targetIndex = this.plots().count() - 1;
        }
        var data = plotNode.get('object').getJsonValues();
        data.id = targetPlotNode.get('id');
        targetPlotNode.set('object', Ext.create('amdaPlotObj.PlotRequestObject', data));
        plotNode.get('object').reject();
        return targetPlotNode;
    },

    createNewPlotFromObject: function(plotObject) {
        var plotNode = Ext.create('amdaModel.PlotNode', {
            leaf : true
        });
        plotNode.set('object',plotObject);
        this.plots().add(plotNode);
        return this.plots().getAt(this.plots().count()-1);
    },

    removePlotByInternalId: function(internalId) {
        var plotNode = this.plots().data.getByKey(internalId);
        if (plotNode)
            this.plots().remove(plotNode);
    },

    getJsonValues : function()
    {
        var requestValues  = new Object();

        requestValues['nodeType'] = 'multiplot';
        requestValues['leaf'] = true;

        requestValues['timesrc'] =  this.get('timesrc');
        // if there's at least one timeTable name into 'timeTables' collection
        if (this.get('timesrc') == amdaModel.AmdaTimeObject.inputTimeSrc[0] && this.get('timeTables') && this.get('timeTables').length){
            // get complete timeTables collection
            var timeTables = this.get('timeTables');
            // init an empty array for timeTables
            requestValues['timeTables'] = [];
            // for each interval record
            Ext.Array.each(timeTables, function(item, index, all){
                if (!item.$className) {
                    requestValues['timeTables'][index] = {timeTableName : item.timeTableName, id : item.id};
                }
                // get Json simplified value
                else {
                    requestValues['timeTables'][index] = item.getJsonValues();
                }
            });
        } else {
            requestValues['startDate'] =  Ext.Date.format(this.get('startDate'), 'Y-m-d\\TH:i:s.u');	
            requestValues['stopDate'] =  Ext.Date.format(this.get('stopDate'), 'Y-m-d\\TH:i:s.u');	
            requestValues['durationDay'] = this.get('durationDay');
            requestValues['durationHour'] = this.get('durationHour');
            requestValues['durationMin'] = this.get('durationMin');
            requestValues['durationSec'] = this.get('durationSec');
            requestValues['durationMs'] = this.get('durationMs');
        }

        requestValues['plots'] = [];
        this.plots().each(function(plotNode) {
            if (plotNode.get('object').get('multi-selected')) {
                requestValues['plots'].push(plotNode.get('object').getJsonValues());
            }
        });

        return requestValues;
    }
});