partkeepr

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

PartEditorWindow.js (6104B)


      1 /**
      2  * @class PartKeepr.PartEditorWindow
      3 
      4  * <p>The PartEditorWindow encapsulates the PartKeepr.PartEditor within a window.</p>
      5  */
      6 Ext.define('PartKeepr.PartEditorWindow', {
      7     extend: 'Ext.window.Window',
      8 
      9     /* Constrain the window to fit the viewport */
     10     constrainHeader: true,
     11 
     12     /* Fit the editor within the window */
     13     layout: 'fit',
     14 
     15     /* Width and height settings */
     16     width: 750,
     17     minWidth: 600,
     18     minHeight: 435,
     19     height: 435,
     20 
     21     saveText: i18n("Save"),
     22     cancelText: i18n("Cancel"),
     23 
     24     /* Default edit mode. If mode = "create", we show additional fields */
     25     partMode: 'edit',
     26     title: i18n("Add Part"),
     27 
     28     saveButtonReenableTask: null,
     29 
     30     /**
     31      * Creates the part editor and put it into the window.
     32      */
     33     initComponent: function ()
     34     {
     35         this.editor = Ext.create("PartKeepr.PartEditor", {
     36             border: false,
     37             partMode: this.partMode,
     38             enableButtons: false
     39         });
     40 
     41         /* If the edit mode is "create", we need to enlarge the window a bit to fit the fields without scrolling */
     42         if (this.partMode && this.partMode == "create") {
     43             this.height = 500;
     44             this.minHeight = 500;
     45         }
     46 
     47         this.items = [this.editor];
     48 
     49         /**
     50          * We need a delay, since if others are listening for "editorClose", the dialog plus the record could be destroyed
     51          * before any following listeners have a chance to receive the record, resulting in strange problems.
     52          */
     53         this.editor.on("editorClose", function ()
     54         {
     55             this.close();
     56         }, this, {delay: 200});
     57 
     58         this.editor.on("_titleChange", function (val)
     59         {
     60             this.setTitle(val);
     61         }, this);
     62         this.editor.on("itemSaved", this.onItemSaved, this);
     63 
     64         this.octoPartButton = Ext.create("Ext.button.Button", {
     65             text: i18n("Octopart…"),
     66             iconCls: 'partkeepr-icon octopart',
     67             handler: Ext.bind(this.onOctoPartClick, this)
     68         });
     69 
     70         this.saveButton = Ext.create("Ext.button.Button", {
     71             text: this.saveText,
     72             iconCls: 'fugue-icon disk',
     73             handler: Ext.bind(this.onItemSave, this)
     74         });
     75 
     76         this.cancelButton = Ext.create("Ext.button.Button", {
     77             text: this.cancelText,
     78             iconCls: 'web-icon cancel',
     79             handler: Ext.bind(this.onCancelEdit, this)
     80         });
     81 
     82         this.bottomToolbar = Ext.create("Ext.toolbar.Toolbar", {
     83             enableOverflow: true,
     84             defaults: {minWidth: 100},
     85             dock: 'bottom',
     86             ui: 'footer',
     87             pack: 'start',
     88             items: [this.saveButton, this.cancelButton, this.octoPartButton]
     89         });
     90 
     91         this.dockedItems = [this.bottomToolbar];
     92 
     93         this.keepOpenCheckbox = Ext.create("Ext.form.field.Checkbox", {
     94             boxLabel: i18n("Create blank item after save")
     95         });
     96 
     97         this.createCopyCheckbox = Ext.create("Ext.form.field.Checkbox", {
     98             boxLabel: i18n("Create Copy after save")
     99         });
    100 
    101         this.copyPartDataCheckbox = Ext.create("Ext.form.field.Checkbox", {
    102             boxLabel: i18n("Takeover all data"),
    103             disabled: true
    104         });
    105 
    106         if (this.partMode == "create") {
    107             this.bottomToolbar.add(this.keepOpenCheckbox);
    108             this.bottomToolbar.add(this.copyPartDataCheckbox);
    109         } else {
    110             this.bottomToolbar.add(this.createCopyCheckbox);
    111         }
    112 
    113         this.keepOpenCheckbox.on("change", this.onKeepOpenCheckboxClick, this);
    114 
    115         this.editor.keepOpenCheckbox = this.keepOpenCheckbox;
    116         this.editor.copyPartDataCheckbox = this.copyPartDataCheckbox;
    117         this.editor.createCopyCheckbox = this.createCopyCheckbox;
    118 
    119         this.callParent();
    120     },
    121     onCancelEdit: function ()
    122     {
    123         this.editor.onCancelEdit();
    124     },
    125     /**
    126      * Listens to the keepOpenCheckbox clicks and enables/disables the copyPartDataCheckbox
    127      * @param value
    128      */
    129     onKeepOpenCheckboxClick: function (field, value)
    130     {
    131         if (value) {
    132             this.copyPartDataCheckbox.enable();
    133         } else {
    134             this.copyPartDataCheckbox.disable();
    135         }
    136     },
    137     onOctoPartClick: function () {
    138         if (PartKeepr.isOctoPartAvailable()) {
    139             this.editor.getForm().updateRecord();
    140             this.octoPartQueryWindow = Ext.create("PartKeepr.Components.OctoPart.SearchWindow");
    141             this.octoPartQueryWindow.show();
    142             this.octoPartQueryWindow.setPart(this.editor.record);
    143             this.octoPartQueryWindow.startSearch(this.editor.nameField.getValue());
    144             this.octoPartQueryWindow.on("refreshData", this.onRefreshData, this);
    145         } else {
    146             Ext.MessageBox.alert(i18n("Octopart is not configured"), i18n("Your administrator needs to configure the API key for Octopart in the parameters.php file - see parameters.php.dist for instructions"));
    147         }
    148     },
    149     onRefreshData: function () {
    150         this.editor.getForm().loadRecord(this.editor.record);
    151         this.octoPartQueryWindow.destroy();
    152     },
    153     /**
    154      * Called when the save button was clicked
    155      */
    156     onItemSave: function ()
    157     {
    158         if (!this.editor.getForm().isValid()) {
    159             return;
    160         }
    161 
    162         // Disable the save button to indicate progress
    163         this.saveButton.disable();
    164 
    165         // Sanity: If the save process fails, re-enable the button after 30 seconds
    166         if (this.saveButtonReenableTask === null) {
    167             this.saveButtonReenableTask = new Ext.util.DelayedTask(function ()
    168             {
    169                 this.saveButton.enable();
    170             }, this);
    171             this.on('destroy', function ()
    172             {
    173                 this.saveButtonReenableTask.cancel();
    174             }, this);
    175         }
    176         this.saveButtonReenableTask.delay(30000);
    177 
    178         if (!this.editor._onItemSave()) {
    179             this.saveButton.enable();
    180         }
    181     },
    182     /**
    183      * Called when the item was saved
    184      */
    185     onItemSaved: function ()
    186     {
    187         this.saveButton.enable();
    188     }
    189 });