InsertToTTCatlog.js 7.13 KB
Ext.define('amdaPlotComp.intervalSelection.InsertToTTCatlog', {
    extend: 'Ext.form.FieldSet',

    collapsible: false,
    layout: LAYOUT_STYLE,
    title: 'Add in Time Table or Catalog',
    name: 'tt-insertion-fieldset',

    fieldTypeTTCat: 'ttcat-type',
    fieldTypeName: 'ttcat-name',
    ttModuleId: 'timetab-win',
    catModuleId: 'catalog-win',
    linkedTTCatNode: null,

    parent: null,

    initComponent: function () {
        const self = this;

        const insertTypeStore = Ext.create('Ext.data.Store', {
            fields: ['key', 'name'],
            data: [
                { "key": "timeTable", "name": "TimeTable" },
                { "key": "catalog", "name": "Catalog" }
            ]
        });

        Ext.apply(self, {
            items: [{
                xtype: 'combo',
                fieldLabel: 'Insert In',
                store: insertTypeStore,
                queryMode: 'local',
                displayField: 'name',
                valueField: 'key',
                editable: false,
                value: 'timeTable',
                name: self.fieldTypeTTCat,
                itemId: self.fieldTypeTTCat,
            },
            {
                xtype: 'textfield',
                fieldLabel: 'Name',
                name: self.fieldTypeName,
                itemId: self.fieldTypeName,
                listeners:
                {
                    render: function (o, op) {
                        var field = this;
                        var el = this.el;
                        Ext.create('Ext.dd.DropTarget', el, {
                            ddGroup: 'explorerTree',
                            notifyOver: function (ddSource, e, data) {
                                var TTCatType = self._getFieldTypeTTCat().getValue();
                                if (data.records[0].data.leaf && (data.records[0].data.nodeType == TTCatType)) {
                                    this.valid = true;
                                    return this.dropAllowed;
                                }
                                this.valid = false;
                                return this.dropNotAllowed;
                            },
                            notifyDrop: function (ddSource, e, data) {
                                if (!this.valid)
                                    return false;
                                field.setValue(data.records[0].get('text'));
                                return true;
                            }
                        });
                    }
                }
            },
            {
                xtype: 'button',
                width: width,
                text: 'Insert Interval',
                scope: this,
                handler: function () {
                    var me = this;

                    var TTCatType = self._getFieldTypeTTCat().getValue();
                    var TTCatName = self._getFieldNameTTCat().getValue();

                    var isCatalog = (TTCatType == 'catalog');
                    myDesktopApp.getLoadedModule(isCatalog ? this.catModuleId : this.ttModuleId, true, function (module) {
                        var targetModuleUI = module.getUiContent();
                        if (me.linkedTTCatNode && (me.linkedTTCatNode.get('text') == TTCatName) && (me.linkedTTCatNode.get('nodeType') == TTCatType)) {
                            if (targetModuleUI)
                                me.insertInterval();
                            else {
                                me.linkedTTCatNode.editLeaf(function () {
                                    me.insertInterval();
                                });
                            }
                        }
                        else {
                            var explorerTree = Ext.getCmp(amdaUI.ExplorerUI.RESRC_TAB.TREE_ID);
                            var ttCatRootNode = explorerTree.getRootNode().findChild('id', isCatalog ? 'catalog-treeRootNode' : 'timeTable-treeRootNode', true);
                            amdaModel.InteractiveNode.preloadNodes(ttCatRootNode, function () {
                                var nodeWithSameName = null;

                                if (TTCatName != '')
                                    nodeWithSameName = ttCatRootNode.findChild('text', TTCatName, true);

                                if (nodeWithSameName !== null)
                                    me.linkedTTCatNode = nodeWithSameName;
                                else {
                                    module.createLinkedNode();
                                    module.getLinkedNode().set('text', TTCatName);
                                    me.linkedTTCatNode = module.getLinkedNode();
                                    var obj = {
                                        name: TTCatName,
                                        fromPlugin: true
                                    };
                                    if (isCatalog) {
                                        Ext.Msg.prompt('Define Parameters', 'Please enter parameters number for the new catalog:', function (btn, text) {
                                            if (btn == 'ok') {
                                                obj.nbParameters = parseInt(text, 10);
                                                if (isNaN(obj.nbParameters)) {
                                                    obj.nbParameters = 1;
                                                }
                                                module.createObject(obj);
                                                me.linkedTTCatNode.editLeaf(function () {
                                                    me.insertInterval();
                                                });
                                            }
                                        });
                                        return;
                                    }
                                    else {
                                        module.createObject(obj);
                                    }
                                }

                                me.linkedTTCatNode.editLeaf(function () {
                                    me.insertInterval();
                                });
                            });
                        }
                    });
                }
            }
            ]
        });

        self.callParent(arguments);
    },


    _getFieldTypeTTCat: function () {
        return this.down('#' + this.fieldTypeTTCat);
    },

    _getFieldNameTTCat: function () {
        return this.down('#' + this.fieldTypeName);
    },

    /**
     * add Interval to Time table or Catalog
     */
    insertInterval: function () {
        const start = this.parent.getField1Value();
        const stop = this.parent.getField2Value();

        const TTCatType = this._getFieldTypeTTCat().getValue();
        const isCatalog = (TTCatType == 'catalog');

        myDesktopApp.getLoadedModule(isCatalog ? this.catModuleId : this.ttModuleId, true, function (module) {
            var targetModuleUI = module.getUiContent();
            if (targetModuleUI)
                targetModuleUI.addInterval(start, stop);
        });
    }
});