partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

PartParameterSearch.js (7144B)


      1 Ext.define("PartKeepr.Components.Widgets.PartParameterSearch", {
      2     extend: "Ext.panel.Panel",
      3 
      4     layout: {
      5         type: 'vbox',
      6         align: 'stretch',
      7         pack: 'start'
      8     },
      9 
     10     shrinkWrap: 2,
     11     minHeight: 110,
     12     minWidth: 380,
     13     items: [
     14         {
     15             xtype: 'PartParameterComboBox',
     16             fieldLabel: i18n("Parameter"),
     17             itemId: 'parameter'
     18         },
     19         {
     20             xtype: 'fieldcontainer',
     21             fieldLabel: i18n("Operator"),
     22             layout: 'hbox',
     23             items: [
     24                 {
     25                     itemId: "operator",
     26                     xtype: 'OperatorComboBox',
     27                     flex: 1,
     28                     disabled: true
     29                 }
     30             ]
     31         }, {
     32             xtype: 'fieldcontainer',
     33             fieldLabel: i18n("Value"),
     34             layout: 'card',
     35             flex: 1,
     36             itemId: 'valueCards',
     37             items: [
     38                 {
     39                     itemId: 'stringValue',
     40                     layout: 'hbox',
     41                     border: false,
     42                     items: [
     43                         {
     44                             disabled: true,
     45                             itemId: "textValueField",
     46                             xtype: 'combobox',
     47                             displayField: 'value',
     48                             valueField: 'value',
     49                             flex: 1
     50                         }
     51                     ]
     52                 }, {
     53                     itemId: 'value',
     54                     layout: 'hbox',
     55                     border: false,
     56                     items: [
     57                         {
     58                             disabled: true,
     59                             xtype: 'SiUnitField',
     60                             siUnitItemId: 'siPrefix',
     61                             itemId: 'valueField',
     62                             siFieldName: 'siPrefix',
     63                             flex: 1
     64                         },
     65                         {
     66                             xtype: 'displayfield',
     67                             itemId: 'unitDisplay',
     68                             name: 'unitName',
     69                             value: ''
     70                         }
     71                     ]
     72                 },
     73             ]
     74         },
     75     ],
     76 
     77     bbar: [
     78         {
     79             xtype: 'button',
     80             itemId: 'apply',
     81             disabled: true,
     82             text: i18n("Apply")
     83         }
     84     ],
     85 
     86     valueType: "",
     87     operatorType: null,
     88     filter: null,
     89     unit: null,
     90 
     91     initComponent: function ()
     92     {
     93         this.callParent();
     94 
     95         this.unitFilter = Ext.create("PartKeepr.util.Filter", {
     96             property: "@id",
     97             operator: "in",
     98             value: []
     99         });
    100 
    101 
    102         this.down("#operator").on("change", this.onOperatorChange, this);
    103         this.down("#parameter").on("select", this.onParameterSelect, this);
    104         this.down("#apply").on("click", this.onApplyClick, this);
    105 
    106         this.partParameterValueStore = Ext.create("Ext.data.Store", {
    107             fields: [{name: 'value'}],
    108             autoLoad: false,
    109             proxy: {
    110                 extraParams: {
    111                     name: "",
    112                     value: ""
    113                 },
    114                 type: 'ajax',
    115                 url: PartKeepr.getBasePath() + "/api/parts/getPartParameterValues",
    116                 reader: {
    117                     type: 'json'
    118                 }
    119             }
    120         });
    121 
    122         this.down("#textValueField").setStore(this.partParameterValueStore);
    123     },
    124     onApplyClick: function ()
    125     {
    126         var operator;
    127 
    128         if (this.down("#operator").getValue() instanceof Ext.data.Model) {
    129             operator = this.down("#operator").getValue().get("operator");
    130         }
    131 
    132         var j = Ext.create("PartKeepr.PartBundle.Entity.MetaPartParameterCriteria");
    133         j.set("partParameterName", this.down("#parameter").getValue());
    134         j.set("valueType", this.valueType);
    135         j.setSiPrefix(this.down("#siPrefix").getValue());
    136         j.setUnit(this.unit);
    137         j.set("stringValue", this.down("#textValueField").getValue());
    138         j.set("operator", operator);
    139         j.set("value", this.down("#valueField").getValue());
    140 
    141         this.fireEvent("apply", j);
    142     },
    143     onParameterSelect: function (combo, value)
    144     {
    145         var prefixes, j, unitFilter = [];
    146 
    147         this.valueType = value.get("valueType");
    148         this.down("#operator").getStore().clearFilter();
    149 
    150         if (value.get("valueType") === "string") {
    151             this.down("#operator").getStore().filter("string", true);
    152             this.down("#operator").getStore().filter("type", "scalar");
    153         } else {
    154             this.down("#operator").getStore().filter("numeric", true);
    155             this.down("#operator").getStore().filter("type", "scalar");
    156         }
    157 
    158         this.down("#siPrefix").getStore().removeFilter(this.unitFilter);
    159 
    160         var unitStore = PartKeepr.getApplication().getUnitStore();
    161         var unit = unitStore.findRecord("name", value.get("unitName"), 0, false, true, true);
    162         if (unit instanceof PartKeepr.UnitBundle.Entity.Unit) {
    163             this.unit = unit;
    164             prefixes = unit.prefixes().getData();
    165 
    166             for (j = 0; j < prefixes.getCount(); j++) {
    167                 unitFilter.push(prefixes.getAt(j).get("@id"));
    168             }
    169 
    170             this.down("#unitDisplay").setValue(unit.get("name"));
    171         } else {
    172             this.unit = null;
    173         }
    174 
    175         this.unitFilter.setValue(unitFilter);
    176 
    177         this.down("#siPrefix").getStore().addFilter(this.unitFilter);
    178 
    179         this.down("#operator").enable();
    180 
    181 
    182         this.partParameterValueStore.getProxy().setExtraParams({
    183             name: value.get("name"),
    184             valueType: value.get("valueType")
    185         });
    186         this.partParameterValueStore.load();
    187         this.switchValueCard();
    188     },
    189     onOperatorChange: function (combo, value)
    190     {
    191         if (value === null) {
    192             this.operatorType = null;
    193             return;
    194         }
    195 
    196         this.operatorType = value.get("type");
    197         this.switchValueCard();
    198     },
    199     switchValueCard: function ()
    200     {
    201         switch (this.operatorType) {
    202             case "scalar":
    203                 if (this.valueType === "string") {
    204                     this.down("#valueCards").setActiveItem(this.down("#stringValue"));
    205                     this.down("#textValueField").enable();
    206                 } else {
    207                     this.down("#valueCards").setActiveItem(this.down("#value"));
    208                     this.down("#siPrefix").enable();
    209                     this.down("#valueField").enable();
    210                 }
    211                 break;
    212             default:
    213                 this.down("#valueCards").setActiveItem(this.down("#value"));
    214                 this.down("#siPrefix").disable();
    215                 this.down("#valueField").disable();
    216         }
    217 
    218         this.validateApplyButton();
    219     },
    220     validateApplyButton: function ()
    221     {
    222         var applyButton = this.down("#apply");
    223 
    224         if (this.down("#operator").getValue() === null) {
    225             applyButton.setDisabled(true);
    226             return;
    227         }
    228 
    229         applyButton.enable();
    230     }
    231 });