partkeepr

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

Ext.ux.TreePicker-setValueWithObject.js (2447B)


      1 /**
      2  * Enhancements for Ext.ux.TreePicker:
      3  *
      4  * - Allow setValue to be a model. If it's a model, select by the idProperty
      5  * - Use | as separator for getPath/selectPath, because the default "/" separator doesn't work with JSON-LD IDs
      6  */
      7 Ext.define("PartKeepr.ux.TreePicker", {
      8     override: "Ext.ux.TreePicker",
      9 
     10    /**
     11      * Sets the specified value into the field
     12      * @param {Mixed} value
     13      * @return {Ext.ux.TreePicker} this
     14      */
     15     setValue: function(value) {
     16         var me = this,
     17             record;
     18 
     19         me.value = value;
     20 
     21         if (me.store.loading) {
     22             // Called while the Store is loading. Ensure it is processed by the onLoad method.
     23             return me;
     24         }
     25 
     26         // try to find a record in the store that matches the value
     27         record = value ? me.store.getNodeById(value) : me.store.getRoot();
     28         if (value === undefined || value === null) {
     29             record = me.store.getRoot().firstChild;
     30             me.value = record.getId();
     31         } else {
     32             if (value.isModel) {
     33                 record = me.store.getNodeById(value.getId());
     34             } else {
     35                 record = me.store.getNodeById(value);
     36             }
     37         }
     38 
     39         // set the raw value to the record's display field if a record was found
     40         me.setRawValue(record ? record.get(me.displayField) : '');
     41 
     42         return me;
     43     },
     44     /**
     45      * Runs when the picker is expanded.  Selects the appropriate tree node based on the value of the input element,
     46      * and focuses the picker so that keyboard navigation will work.
     47      * @private
     48      */
     49     onExpand: function()
     50     {
     51         var me = this,
     52             picker = me.picker,
     53             store = picker.store,
     54             value = me.value,
     55             node;
     56 
     57         if (value) {
     58             if (value.isModel) {
     59                 node = store.getNodeById(value.getId());
     60             } else {
     61                 node = store.getNodeById(value);
     62             }
     63         }
     64 
     65         if (!node) {
     66             node = store.getRoot();
     67         }
     68 
     69         var path = node.getPath("@id", "|");
     70 
     71         picker.selectPath(path, "@id", "|");
     72     },
     73      /**
     74      * Changes the selection to a given record and closes the picker
     75      * @private
     76      * @param {Ext.data.Model} record
     77      */
     78     selectItem: function(record) {
     79         var me = this;
     80         me.setValue(record);
     81         me.fireEvent('select', me, record);
     82         me.collapse();
     83     },
     84 });