partkeepr

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

CategoryEditorTree.js (6737B)


      1 Ext.define("PartKeepr.CategoryEditorTree", {
      2     alias: 'widget.CategoryEditorTree',
      3     extend: 'PartKeepr.CategoryTree',
      4     viewConfig: {
      5         plugins: {
      6             ptype: 'treeviewdragdrop',
      7             sortOnDrop: true
      8         }
      9     },
     10     hideHeaders: true,
     11     categoryModel: null,
     12     categoryService: null,
     13     categoryEditActions: true,
     14 
     15     initComponent: function ()
     16     {
     17         this.columns = [{
     18             xtype: 'treecolumn',
     19             header: 'Name',
     20             dataIndex: 'name',
     21             flex: 1
     22         }];
     23 
     24         if (PartKeepr.getApplication().getUserPreference("partkeepr.categorytree.showdescriptions", false) === true) {
     25             this.columns.push(
     26                 {
     27                     xtype: 'gridcolumn',
     28                     header: 'Description',
     29                     dataIndex: 'description',
     30                     flex: 0.5
     31                 });
     32 
     33         }
     34         this.createToolbar();
     35 
     36 
     37         this.callParent();
     38 
     39         this.getView().on("drop", Ext.bind(this.onCategoryDrop, this));
     40         this.getView().on("beforedrop", Ext.bind(this.onBeforeDrop, this));
     41         this.on("selectionchange", Ext.bind(this.onItemSelect, this));
     42     },
     43     onBeforeDrop: function (node, data, overModel, dropPosition, dropHandlers)
     44     {
     45         var draggedRecords = data.records;
     46         var droppedOn = this.getView().getRecord(node);
     47 
     48         for (var draggedRecord in draggedRecords) {
     49             if (!(draggedRecord instanceof PartKeepr.data.HydraTreeModel)) {
     50                 // Workaround for EXTJS-13725 where dropping of non-tree-models cause issues
     51                 dropHandlers.cancelDrop();
     52             }
     53         }
     54 
     55         this.fireEvent("foreignModelDrop", draggedRecords, droppedOn);
     56     },
     57     onItemSelect: function (selected) {
     58         if (selected.getCount() === 0) {
     59             this.toolbarAddButton.disable();
     60             this.toolbarDeleteButton.disable();
     61             this.toolbarEditButton.disable();
     62         }
     63 
     64         this.toolbarAddButton.enable();
     65         this.toolbarEditButton.enable();
     66         this.toolbarDeleteButton.enable();
     67 
     68         var record = selected.getSelection()[0];
     69 
     70 
     71         if (!(record instanceof PartKeepr.data.HydraTreeModel)) {
     72             return;
     73         }
     74 
     75         if (record.parentNode !== null && record.parentNode.isRoot()) {
     76             this.toolbarDeleteButton.disable();
     77         }
     78 
     79         if (record.hasChildNodes()) {
     80             this.toolbarDeleteButton.disable();
     81         }
     82 
     83     },
     84     onCategoryDrop: function (node, data, overModel, dropPosition)
     85     {
     86         var draggedRecord = data.records[0];
     87 
     88         if (draggedRecord instanceof PartKeepr.data.HydraTreeModel) {
     89             var targetRecord;
     90 
     91             if (dropPosition === "after" || dropPosition === "before") {
     92                 targetRecord = overModel.parentNode;
     93             } else {
     94                 targetRecord = overModel;
     95             }
     96 
     97             draggedRecord.callPutAction("move", {
     98                 "parent": targetRecord.getId()
     99             });
    100         }
    101     },
    102     createToolbar: function ()
    103     {
    104         this.toolbarExpandButton = Ext.create("Ext.button.Button", {
    105             iconCls: 'fugue-icon toggle-expand',
    106             tooltip: i18n("Expand All"),
    107             handler: this._onExpandClick,
    108             scope: this
    109         });
    110 
    111         this.toolbarCollapseButton = Ext.create("Ext.button.Button", {
    112             iconCls: 'fugue-icon toggle',
    113             tooltip: i18n("Collapse All"),
    114             handler: this._onCollapseClick,
    115             scope: this
    116         });
    117 
    118         this.toolbarReloadButton = Ext.create("Ext.button.Button", {
    119             iconCls: 'x-tbar-loading',
    120             tooltip: i18n("Reload"),
    121             handler: this._onReloadClick,
    122             scope: this
    123         });
    124 
    125         this.toolbarAddButton = Ext.create("Ext.button.Button", {
    126             tooltip: i18n("Add Category"),
    127             handler: Ext.bind(this.showCategoryAddDialog, this),
    128             iconCls: 'web-icon folder_add',
    129             disabled: true
    130         });
    131 
    132         this.toolbarDeleteButton = Ext.create("Ext.button.Button", {
    133             tooltip: i18n("Delete Category"),
    134             handler: Ext.bind(this.confirmCategoryDelete, this),
    135             iconCls: 'web-icon folder_delete',
    136             disabled: true
    137         });
    138 
    139         this.toolbarEditButton = Ext.create("Ext.button.Button", {
    140             tooltip: i18n("Edit Category"),
    141             handler: Ext.bind(this.showCategoryEditDialog, this),
    142             iconCls: 'web-icon folder_edit',
    143             disabled: true
    144         });
    145 
    146         var actions = [
    147             this.toolbarExpandButton,
    148             this.toolbarCollapseButton,
    149             this.toolbarReloadButton
    150         ];
    151 
    152         if (this.categoryEditActions) {
    153             actions.push(
    154                 {
    155                     xtype: 'tbseparator'
    156                 },
    157                 this.toolbarAddButton,
    158                 this.toolbarEditButton,
    159                 this.toolbarDeleteButton
    160             );
    161         }
    162         this.toolbar = Ext.create("Ext.toolbar.Toolbar", {
    163             enableOverflow: true,
    164             dock: 'top',
    165             items: actions
    166         });
    167 
    168         Ext.apply(this, {
    169             dockedItems: [this.toolbar]
    170         });
    171     },
    172     _onReloadClick: function ()
    173     {
    174         this.store.load();
    175     },
    176     _onExpandClick: function ()
    177     {
    178         this.getRootNode().firstChild.expand(true);
    179     },
    180     _onCollapseClick: function ()
    181     {
    182         this.getRootNode().firstChild.collapse(true);
    183     },
    184     confirmCategoryDelete: function ()
    185     {
    186         Ext.Msg.confirm(i18n("Confirm Category Delete"),
    187             sprintf(i18n("Do you really wish to delete the category %s?"), this.getSelection()[0].get("name")),
    188             this.onCategoryDelete, this);
    189     },
    190     showCategoryAddDialog: function ()
    191     {
    192         var j = Ext.create("PartKeepr.CategoryEditorWindow", {
    193             record: Ext.create(this.categoryModel),
    194             categoryModel: this.categoryModel,
    195             parentRecord: this.getSelection()[0],
    196             listeners: {
    197                 save: Ext.bind(this.onUpdateRecord, this)
    198             }
    199         });
    200 
    201         j.show();
    202     },
    203     showCategoryEditDialog: function ()
    204     {
    205         var j = Ext.create("PartKeepr.CategoryEditorWindow", {
    206             record: this.getSelection()[0],
    207             parentRecord: null,
    208             categoryModel: this.categoryModel,
    209             listeners: {
    210                 save: Ext.bind(this.onUpdateRecord, this)
    211             }
    212         });
    213 
    214         j.show();
    215     },
    216     onUpdateRecord: function ()
    217     {
    218         this.store.load();
    219     },
    220     onCategoryDelete: function (btn)
    221     {
    222         if (btn === "yes") {
    223             this.getSelection()[0].erase();
    224         }
    225     }
    226 });