partkeepr

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

Editor.js (3663B)


      1 Ext.define('PartKeepr.Editor', {
      2     extend: 'Ext.form.Panel',
      3     alias: 'widget.Editor',
      4     trackResetOnLoad: true,
      5     bodyPadding: 10,
      6     record: null,		// The record which is currently edited
      7     saveText: i18n("Save"),
      8     cancelText: i18n("Cancel"),
      9     model: null,
     10     layout: 'anchor',
     11     change: false,
     12     autoScroll: true,
     13     defaults: {
     14         anchor: '100%',
     15         labelWidth: 150
     16     },
     17     enableButtons: true,
     18     titleProperty: 'name',
     19     editAfterSave: true,
     20 
     21     // If false, determinates if we should sync via the store or the record itself.
     22     // If true, always syncs the record via it's own proxy.
     23     syncDirect: false,
     24 
     25     initComponent: function ()
     26     {
     27         if (this.enableButtons) {
     28             this.saveButton = Ext.create("Ext.button.Button", {
     29                 text: this.saveText,
     30                 iconCls: 'fugue-icon disk',
     31                 handler: Ext.bind(this._onItemSave, this)
     32             });
     33 
     34             this.cancelButton = Ext.create("Ext.button.Button", {
     35                 text: this.cancelText,
     36                 iconCls: 'web-icon cancel',
     37                 handler: Ext.bind(this.onCancelEdit, this)
     38             });
     39 
     40             this.bottomToolbar = Ext.create("Ext.toolbar.Toolbar", {
     41                 enableOverflow: true,
     42                 margin: '10px',
     43                 defaults: {minWidth: 100},
     44                 dock: 'bottom',
     45                 ui: 'footer',
     46                 items: [this.saveButton, this.cancelButton]
     47             });
     48 
     49             Ext.apply(this, {
     50                 dockedItems: [this.bottomToolbar]
     51             });
     52         }
     53 
     54         this.callParent();
     55     },
     56     onCancelEdit: function ()
     57     {
     58         this.record.reject();
     59         this.fireEvent("editorClose", this);
     60     },
     61     newItem: function (defaults)
     62     {
     63         Ext.apply(defaults, {});
     64         var j = Ext.create(this.model, defaults);
     65         this.editItem(j);
     66     },
     67     editItem: function (record)
     68     {
     69         this.record = record;
     70         this.getForm().loadRecord(this.record);
     71         this.show();
     72         if (this.record.get(this.titleProperty) !== "") {
     73             this.setTitle(this.record.get(this.titleProperty));
     74         }
     75 
     76         this.change = false;
     77         this.fireEvent("startEdit", this);
     78     },
     79     getRecordId: function ()
     80     {
     81         if (this.record) {
     82             return this.record.getId();
     83         } else {
     84             return null;
     85         }
     86     },
     87     _onItemSave: function ()
     88     {
     89         // Disable the save button to indicate progress
     90         if (this.enableButtons) {
     91             this.saveButton.disable();
     92 
     93             // Sanity: If the save process fails, re-enable the button after 30 seconds
     94             // @todo This is quite a hack. Needs verification if that's still required
     95             Ext.defer(function ()
     96             {
     97                 if (this.saveButton.getEl()) {
     98                     this.saveButton.enable();
     99                 }
    100             }, 30000, this);
    101         }
    102 
    103         this.getForm().updateRecord(this.record);
    104 
    105         if (this.fireEvent("itemSave", this.record)) {
    106             this.record.save({
    107                 callback: this._onSave,
    108                 scope: this
    109             });
    110             return true;
    111         } else {
    112             return false;
    113         }
    114 
    115     },
    116     _onSave: function (record, response)
    117     {
    118         if (this.enableButtons) {
    119             // Re-enable the save button
    120             this.saveButton.enable();
    121         }
    122 
    123         if (response.success === true) {
    124             this.record = record;
    125             this.fireEvent("itemSaved", this.record);
    126         }
    127 
    128         if (this.editAfterSave) {
    129             this.editItem(record);
    130         }
    131     }
    132 });