partkeepr

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

PresetComboBox.js (8861B)


      1 Ext.define("PartKeepr.PresetCombobox", {
      2     extend: "Ext.form.field.ComboBox",
      3 
      4     xtype: 'presetcombo',
      5 
      6     config: {
      7         /**
      8          * The model to use
      9          * @var string
     10          */
     11         model: null,
     12 
     13         /**
     14          * The name of the field which contains the serialized configuration
     15          * @var string
     16          */
     17         configurationField: "configuration",
     18 
     19         /**
     20          * Additional fields to set. Contains a list of objects
     21          * which are defined like this:
     22          *
     23          * { fieldName: "baseEntity", value: "myvalue" }
     24          * @var array
     25          */
     26         additionalFields: [],
     27 
     28         /**
     29          * Defines the name of the preset name field
     30          * @var string
     31          */
     32         nameField: "name",
     33 
     34         /**
     35          * Preset for a blank configuration
     36          * @var object
     37          */
     38         blankConfiguration: {},
     39 
     40         /**
     41          * Defines if the configuration should be serialized or not
     42          * @var bool
     43          */
     44         serializeConfiguration: true,
     45 
     46         /**
     47          * Defines if the mark as default feature should be available
     48          */
     49         allowMarkAsDefault: false,
     50 
     51         /**
     52          * Defines which property defines if the preset is the default
     53          */
     54         markedAsDefaultProperty: null
     55 
     56     },
     57 
     58     returnObject: true,
     59     editable: false,
     60     emptyText: i18n("No preset selected"),
     61     forceSelection: true,
     62     allowBlank: true,
     63     valueField: '@id',
     64 
     65     triggers: {
     66         save: {
     67             cls: "x-form-trigger-save",
     68             weight: 1,
     69             tooltip: i18n("Save Preset"),
     70             handler: 'onSavePreset',
     71             scope: 'this'
     72         },
     73         add: {
     74             cls: "x-form-trigger-add",
     75             weight: 2,
     76             handler: 'onAddPreset',
     77             tooltip: i18n("Create blank preset"),
     78             scope: 'this'
     79         },
     80         delete: {
     81             cls: "x-form-trigger-delete",
     82             weight: 3,
     83             handler: 'onDeletePreset',
     84             scope: 'this',
     85             tooltip: i18n("Delete selected preset"),
     86             hidden: true
     87         },
     88         default: {
     89             cls: "x-form-trigger-markdefault",
     90             weight: 4,
     91             handler: 'onMarkPresetAsDefault',
     92             scope: 'this',
     93             tooltip: i18n("Mark selected preset as default"),
     94             hidden: true
     95         }
     96     },
     97 
     98     listeners: {
     99         change: 'onSelectionChange',
    100         scope: 'this'
    101     },
    102 
    103     /**
    104      * @event selectPreset
    105      * Fires after a preset has been selected
    106      * @param {String} The configuration which was selected
    107      */
    108 
    109     initComponent: function ()
    110     {
    111         console.log(this.markedAsDefaultProperty);
    112         if (this.allowMarkAsDefault) {
    113             this.tpl = Ext.create('Ext.XTemplate',
    114             '<ul class="x-list-plain"><tpl for=".">',
    115             '<li role="option" class="x-boundlist-item">',
    116             '<tpl if="'+this.markedAsDefaultProperty+' == 1">',
    117             '<span class="web-icon fugue-icon flag"></span>',
    118             '<tpl else>',
    119             '<span style="width: 16px; height: 16px; display: inline-block;"></span>',
    120             '</tpl> ',
    121             '{'+this.displayField+'}',
    122             '</li>',
    123             '</tpl></ul>'
    124         );
    125         }
    126 
    127         if (this.getModel() === null) {
    128             Ext.raise("The configuration property 'model' is not configured, but is required.");
    129         }
    130 
    131         if (this.getConfigurationField() === null) {
    132             Ext.raise("The configuration property 'configurationField' is not configured, but is required.");
    133         }
    134 
    135         this.store = Ext.create("Ext.data.Store", {
    136             model: this.model,
    137             autoLoad: true
    138         });
    139 
    140         this.on("select", this.onPresetSelect, this);
    141 
    142         this.callParent(arguments);
    143     },
    144     /**
    145      * Event handler to handle a preset selection. Fires the "selectPreset" event.
    146      */
    147     onPresetSelect: function ()
    148     {
    149         var configuration;
    150         if (this.serializeConfiguration) {
    151             // @todo check if the json string is correct and if it is not, display an error
    152             configuration = Ext.decode(this.getSelection().get(this.configurationField), true);
    153         } else {
    154             configuration = this.getSelection().get(this.configurationField);
    155         }
    156         this.fireEvent("selectPreset", configuration);
    157     },
    158     /**
    159      * Event handler to ask the user for the preset name, then calls onPresetNameEntered.
    160      */
    161     onSavePreset: function ()
    162     {
    163         var presetName;
    164 
    165         if (this.getSelectedRecord() !== null) {
    166             presetName = this.getSelectedRecord().get(this.nameField);
    167         } else {
    168             presetName = "New Preset";
    169         }
    170 
    171         Ext.Msg.prompt(i18n("Save Preset"), i18n("Preset Name"), this.onPresetNameEntered, this, false, presetName);
    172     },
    173     /**
    174      * Event handler to save the preset. Checks if a valid name was given, then calls savePreset on success.
    175      *
    176      * @param {String} buttonId The button ID which was clicked
    177      * @param {String} value The preset name
    178      */
    179     onPresetNameEntered: function (buttonId, value)
    180     {
    181         if (buttonId === "ok") {
    182             this.savePreset(value);
    183             // The previous method where save was only allowed if the preset name doesn't exist was removed. Unclear
    184             // why this was added in the first place.
    185         }
    186     },
    187     /**
    188      * Saves a preset with a given name
    189      * @param {String} presetName The preset name
    190      */
    191     savePreset: function (presetName)
    192     {
    193         var presetRecord = this.getSelectedRecord();
    194 
    195         if (presetRecord === null) {
    196             presetRecord = Ext.create(this.model);
    197         } else {
    198             if (presetName !== presetRecord.get(this.nameField)) {
    199                 presetRecord = Ext.create(this.model);
    200             }
    201         }
    202 
    203         if (this.serializeConfiguration) {
    204             presetRecord.set(this.configurationField, Ext.encode(this.configuration));
    205         } else {
    206             presetRecord.set(this.configurationField, this.configuration);
    207         }
    208         presetRecord.set(this.nameField, presetName);
    209 
    210         var additionalField;
    211 
    212         for (var i = 0; i < this.additionalFields.length; i++) {
    213             additionalField = this.additionalFields[i];
    214 
    215             presetRecord.set(additionalField.fieldName, additionalField.value);
    216         }
    217 
    218         presetRecord.save({
    219             success: this.onAfterSave,
    220             scope: this
    221         });
    222 
    223     },
    224     /**
    225      * Handler to reload the store and select the newly added preset
    226      * @param {Ext.data.Model} presetRecord The preset to select
    227      */
    228     onAfterSave: function (presetRecord)
    229     {
    230         this.getStore().reload({
    231             scope: this,
    232             callback: function ()
    233             {
    234                 this.select(presetRecord);
    235             }
    236         });
    237     },
    238     /**
    239      * Handler to confirm deletion. Calls deletePreset
    240      */
    241     onDeletePreset: function ()
    242     {
    243         Ext.Msg.confirm(
    244             i18n("Confirm preset deletion"),
    245             i18n("Are you sure to delete the preset?"),
    246             this.deletePreset,
    247             this);
    248     },
    249     /**
    250      * Deletes the currently selected preset if the yes button was clicked
    251      * @param {String} buttonId The selected button
    252      */
    253     deletePreset: function (buttonId)
    254     {
    255         if (buttonId === "yes") {
    256             var presetRecord = this.getSelectedRecord();
    257             presetRecord.erase();
    258             this.setSelection(null);
    259             this.setValue(null);
    260             this.fireEvent("selectPreset", this.blankConfiguration);
    261         }
    262     },
    263     /**
    264      * Creates a blank preset. Sets the selection to null and fires
    265      * selectPreset with blankConfiguration.
    266      */
    267     onAddPreset: function ()
    268     {
    269         this.setSelection(null);
    270         this.setValue(null);
    271         this.fireEvent("selectPreset", this.blankConfiguration);
    272     },
    273     /**
    274      * Sets the configuration for the preset
    275      */
    276     setConfiguration: function (configuration)
    277     {
    278         this.configuration = configuration;
    279     },
    280     /**
    281      * Sets the additional fields to use when creating or saving a record
    282      */
    283     setAdditionalFields: function (additionalFields)
    284     {
    285         this.additionalFields = additionalFields;
    286     },
    287     onMarkPresetAsDefault: function () {
    288         this.fireEvent("markAsDefault", this.getSelectedRecord());
    289     },
    290     /**
    291      * Handler to show/hide the delete button
    292      */
    293     onSelectionChange: function ()
    294     {
    295         if (this.getSelectedRecord() !== null) {
    296             this.triggers.delete.show();
    297 
    298             if (this.allowMarkAsDefault) {
    299                 this.triggers.default.show();
    300             }
    301         } else {
    302             this.triggers.delete.hide();
    303             this.triggers.default.hide();
    304         }
    305     }
    306 });