src/js/libs/mousetrap-global-bind.js
author ymh <ymh.work@gmail.com>
Fri, 13 Feb 2015 16:57:53 +0100
changeset 1033 c20df1c080e6
child 1057 3f20f286d43e
permissions -rw-r--r--
integrate changes from github
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1033
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
/**
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
 * adds a bindGlobal method to Mousetrap that allows you to
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * bind specific keyboard shortcuts that will still work
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * inside a text input field
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 *
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * usage:
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * Mousetrap.bindGlobal('ctrl+s', _saveChanges);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/* global Mousetrap:true */
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
Mousetrap = (function(Mousetrap) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    var _globalCallbacks = {},
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
        _originalStopCallback = Mousetrap.stopCallback;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    Mousetrap.stopCallback = function(e, element, combo, sequence) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
        if (_globalCallbacks[combo] || _globalCallbacks[sequence]) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
            return false;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        return _originalStopCallback(e, element, combo);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    };
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    Mousetrap.bindGlobal = function(keys, callback, action) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        Mousetrap.bind(keys, callback, action);
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        if (keys instanceof Array) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
            for (var i = 0; i < keys.length; i++) {
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
                _globalCallbacks[keys[i]] = true;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
            }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            return;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        }
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        _globalCallbacks[keys] = true;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    };
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    return Mousetrap;
c20df1c080e6 integrate changes from github
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
}) (Mousetrap);