# all files / ol/events/ condition.js

83.72% Statements 36/43
57.14% Branches 20/35
69.23% Functions 9/13
83.72% Lines 36/43

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    2271×

    1×
    1×
    1×
    1×

    1×
    68×
    68×

    1×

    1×

    1×

    1×

    1×

    1×
    18×

    1×
    34×

    1×
    9×

    1×
    152×
    152×

    1×

    1×
    226×
    226×

    1×
    10×
    10×
    10×

    1×
    152×

    152×

    1×
    20×
    20×

     goog.provide('ol.events.condition');

    goog.require('ol.MapBrowserEventType');
    goog.require('ol.asserts');
    goog.require('ol.functions');
    goog.require('ol.has');

    /**
     * Return `true` if only the alt-key is pressed, `false` otherwise (e.g. when
     * additionally the shift-key is pressed).
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if only the alt key is pressed.
     * @api
     */
    ol.events.condition.altKeyOnly = function(mapBrowserEvent) {
      var originalEvent = mapBrowserEvent.originalEvent;
      return (
        originalEvent.altKey &&
          !(originalEvent.metaKey || originalEvent.ctrlKey) &&
          !originalEvent.shiftKey);
    };

    /**
     * Return `true` if only the alt-key and shift-key is pressed, `false` otherwise
     * (e.g. when additionally the platform-modifier-key is pressed).
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if only the alt and shift keys are pressed.
     * @api
     */
    ol.events.condition.altShiftKeysOnly = function(mapBrowserEvent) {
      var originalEvent = mapBrowserEvent.originalEvent;
      return (
        originalEvent.altKey &&
          !(originalEvent.metaKey || originalEvent.ctrlKey) &&
          originalEvent.shiftKey);
    };

    /**
     * Return always true.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True.
     * @function
     * @api
     */
    ol.events.condition.always = ol.functions.TRUE;

    /**
     * Return `true` if the event is a `click` event, `false` otherwise.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if the event is a map `click` event.
     * @api
     */
    ol.events.condition.click = function(mapBrowserEvent) {
      return mapBrowserEvent.type == ol.MapBrowserEventType.CLICK;
    };

    /**
     * Return `true` if the event has an "action"-producing mouse button.
     *
     * By definition, this includes left-click on windows/linux, and left-click
     * without the ctrl key on Macs.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} The result.
     */
    ol.events.condition.mouseActionButton = function(mapBrowserEvent) {
      var originalEvent = mapBrowserEvent.originalEvent;
      return originalEvent.button == 0 &&
          !(ol.has.WEBKIT && ol.has.MAC && originalEvent.ctrlKey);
    };

    /**
     * Return always false.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} False.
     * @function
     * @api
     */
    ol.events.condition.never = ol.functions.FALSE;

    /**
     * Return `true` if the browser event is a `pointermove` event, `false`
     * otherwise.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if the browser event is a `pointermove` event.
     * @api
     */
    ol.events.condition.pointerMove = function(mapBrowserEvent) {
      return mapBrowserEvent.type == 'pointermove';
    };

    /**
     * Return `true` if the event is a map `singleclick` event, `false` otherwise.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if the event is a map `singleclick` event.
     * @api
     */
    ol.events.condition.singleClick = function(mapBrowserEvent) {
      return mapBrowserEvent.type == ol.MapBrowserEventType.SINGLECLICK;
    };

    /**
     * Return `true` if the event is a map `dblclick` event, `false` otherwise.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if the event is a map `dblclick` event.
     * @api
     */
    ol.events.condition.doubleClick = function(mapBrowserEvent) {
      return mapBrowserEvent.type == ol.MapBrowserEventType.DBLCLICK;
    };

    /**
     * Return `true` if no modifier key (alt-, shift- or platform-modifier-key) is
     * pressed.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True only if there no modifier keys are pressed.
     * @api
     */
    ol.events.condition.noModifierKeys = function(mapBrowserEvent) {
      var originalEvent = mapBrowserEvent.originalEvent;
      return (
        !originalEvent.altKey &&
          !(originalEvent.metaKey || originalEvent.ctrlKey) &&
          !originalEvent.shiftKey);
    };

    /**
     * Return `true` if only the platform-modifier-key (the meta-key on Mac,
     * ctrl-key otherwise) is pressed, `false` otherwise (e.g. when additionally
     * the shift-key is pressed).
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if only the platform modifier key is pressed.
     * @api
     */
    ol.events.condition.platformModifierKeyOnly = function(mapBrowserEvent) {
      var originalEvent = mapBrowserEvent.originalEvent;
      return (
        !originalEvent.altKey &&
          (ol.has.MAC ? originalEvent.metaKey : originalEvent.ctrlKey) &&
          !originalEvent.shiftKey);
    };

    /**
     * Return `true` if only the shift-key is pressed, `false` otherwise (e.g. when
     * additionally the alt-key is pressed).
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if only the shift key is pressed.
     * @api
     */
    ol.events.condition.shiftKeyOnly = function(mapBrowserEvent) {
      var originalEvent = mapBrowserEvent.originalEvent;
      return (
        !originalEvent.altKey &&
          !(originalEvent.metaKey || originalEvent.ctrlKey) &&
          originalEvent.shiftKey);
    };

    /**
     * Return `true` if the target element is not editable, i.e. not a `<input>`-,
     * `<select>`- or `<textarea>`-element, `false` otherwise.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True only if the target element is not editable.
     * @api
     */
    ol.events.condition.targetNotEditable = function(mapBrowserEvent) {
      var target = mapBrowserEvent.originalEvent.target;
      var tagName = target.tagName;
      return (
        tagName !== 'INPUT' &&
          tagName !== 'SELECT' &&
          tagName !== 'TEXTAREA');
    };

    /**
     * Return `true` if the event originates from a mouse device.
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if the event originates from a mouse device.
     * @api
     */
    ol.events.condition.mouseOnly = function(mapBrowserEvent) {
      ol.asserts.assert(mapBrowserEvent.pointerEvent, 56); // mapBrowserEvent must originate from a pointer event
      // see http://www.w3.org/TR/pointerevents/#widl-PointerEvent-pointerType
      return /** @type {ol.MapBrowserEvent} */ (mapBrowserEvent).pointerEvent.pointerType == 'mouse';
    };

    /**
     * Return `true` if the event originates from a primary pointer in
     * contact with the surface or if the left mouse button is pressed.
     * @see http://www.w3.org/TR/pointerevents/#button-states
     *
     * @param {ol.MapBrowserEvent} mapBrowserEvent Map browser event.
     * @return {boolean} True if the event originates from a primary pointer.
     * @api
     */
    ol.events.condition.primaryAction = function(mapBrowserEvent) {
      var pointerEvent = mapBrowserEvent.pointerEvent;
      return pointerEvent.isPrimary && pointerEvent.button === 0;
    };