partkeepr

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

Manager.js (5399B)


      1 Ext.define("PartKeepr.BarcodeScanner.Manager", {
      2     monitor: false,
      3     monitoredKeys: "",
      4 
      5     barcodeInputField: null,
      6 
      7     registerBarcodeScannerHotkey: function ()
      8     {
      9         this.monitor = false;
     10         this.runnerTask = new Ext.util.DelayedTask(function ()
     11         {
     12             this.stopKeyMonitoring();
     13         }, this);
     14 
     15         Ext.get(document).on("keydown", this.onKeyPress, this, {
     16             priority: 10000
     17         });
     18     },
     19     /**
     20      * Stops monitoring and executes the action found in the intercepted keys.
     21      */
     22     stopKeyMonitoring: function ()
     23     {
     24         this.monitor = false;
     25         this.runnerTask.cancel();
     26 
     27         this.executeAction(this.monitoredKeys);
     28         this.monitoredKeys = "";
     29     },
     30     /**
     31      * Starts monitoring for input events, up to a configured timeout.
     32      */
     33     startKeyMonitoring: function ()
     34     {
     35         this.monitoredKeys = "";
     36         this.monitor = true;
     37         this.runnerTask.delay(PartKeepr.getApplication().getSystemPreference("partkeepr.barcodeScanner.timeout", 500));
     38     },
     39     /**
     40      * Intercepts keypresses when a barcode scanner hotkey was detected up to the configured timeout.
     41      */
     42     onKeyPress: function (e)
     43     {
     44         var hotKeyPressed = false;
     45 
     46         var hotKey = PartKeepr.getApplication().getSystemPreference("partkeepr.barcodeScanner.key", "");
     47 
     48         if (hotKey === "") {
     49             return;
     50         }
     51 
     52         if (e.event.key === hotKey) {
     53             hotKeyPressed = true;
     54         }
     55 
     56         if (PartKeepr.getApplication().getSystemPreference("partkeepr.barcodeScanner.modifierCtrl", false)) {
     57             if (!e.ctrlKey) {
     58                 hotKeyPressed = false;
     59             }
     60         }
     61 
     62         if (PartKeepr.getApplication().getSystemPreference("partkeepr.barcodeScanner.modifierShift", false)) {
     63             if (!e.shiftKey) {
     64                 hotKeyPressed = false;
     65             }
     66         }
     67 
     68         if (PartKeepr.getApplication().getSystemPreference("partkeepr.barcodeScanner.modifierAlt", false)) {
     69             if (!e.altKey) {
     70                 hotKeyPressed = false;
     71             }
     72         }
     73 
     74         if (hotKeyPressed) {
     75             this.startKeyMonitoring();
     76             return;
     77         }
     78 
     79 
     80         if (this.monitor) {
     81             if (PartKeepr.getApplication().getSystemPreference("partkeepr.barcodeScanner.enter", true)) {
     82                 if (e.event.code == "Enter") {
     83                     this.stopKeyMonitoring();
     84                     return;
     85                 }
     86             }
     87 
     88             if (!e.isSpecialKey()) {
     89                 this.monitoredKeys += e.event.key;
     90             }
     91             this.runnerTask.delay(
     92                 PartKeepr.getApplication().getSystemPreference("partkeepr.barcodeScanner.timeout", 500));
     93             e.stopEvent();
     94         }
     95     },
     96     /**
     97      * Returns a list of all class names which provide actions.
     98      *
     99      * @return {Array} An array of action class names
    100      */
    101     getActions: function ()
    102     {
    103         var actions = [
    104             "PartKeepr.BarcodeScanner.Actions.SearchPart",
    105             "PartKeepr.BarcodeScanner.Actions.AddRemoveStock",
    106             "PartKeepr.BarcodeScanner.Actions.AddPart"
    107         ];
    108 
    109         return actions;
    110     },
    111     /**
    112      * Executes an action by parsing the input and deciding which action to execute.
    113      *
    114      * @param {String} input The intercepted keys
    115      */
    116     executeAction: function (input)
    117     {
    118         var actions = this.getActionsByInput(input);
    119 
    120         for (var i = 0; i < actions.length; i++) {
    121             if (actions[i] !== null) {
    122                 actions[i].execute();
    123             }
    124         }
    125     },
    126     getActionsByInput: function (input)
    127     {
    128         var i, actions = PartKeepr.getApplication().getSystemPreference("partkeepr.barcodeScanner.actions", []),
    129             foundActions = [];
    130 
    131         var barcodeScannerActionsStore = Ext.create("Ext.data.Store", {
    132             fields: ["code", "action", "configuration"],
    133             data: []
    134         });
    135 
    136         var actionStore = Ext.create("PartKeepr.Data.store.BarcodeScannerActionsStore");
    137 
    138         for (i = 0; i < actions.length; i++) {
    139             var item = actions[i];
    140 
    141             barcodeScannerActionsStore.add({
    142                 code: item.code,
    143                 action: actionStore.findRecord("action", item.action),
    144                 configuration: item.config
    145             });
    146         }
    147 
    148         barcodeScannerActionsStore.sort(
    149             function (data1, data2)
    150             {
    151                 if (data1.get("code").length == data2.get("code").length) {
    152                     return 0;
    153                 }
    154                 if (data1.get("code").length > data2.get("code").length) {
    155                     return -1;
    156                 } else {
    157                     return 1;
    158                 }
    159             }
    160         );
    161 
    162         var barcodeScannerActions = barcodeScannerActionsStore.getData();
    163         var code, className, config;
    164 
    165         for (i = 0; i < barcodeScannerActions.getCount(); i++) {
    166             code = barcodeScannerActions.getAt(i).get("code");
    167 
    168             if (input.substr(0, code.length) === code) {
    169                 className = barcodeScannerActions.getAt(i).get("action").get("action");
    170                 config = barcodeScannerActions.getAt(i).get("configuration");
    171 
    172                 foundActions.push(Ext.create(className, config, input.substr(code.length)));
    173             }
    174         }
    175 
    176         return foundActions;
    177     }
    178 });