mousetrapjs 0.1.0 → 0.1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Mousetrapjs
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2.2"
3
3
  end
@@ -16,7 +16,7 @@
16
16
  * Mousetrap is a simple keyboard shortcut library for Javascript with
17
17
  * no external dependencies
18
18
  *
19
- * @version 1.1.3
19
+ * @version 1.2.2
20
20
  * @url craig.is/killing/mice
21
21
  */
22
22
  (function() {
@@ -178,7 +178,7 @@
178
178
  *
179
179
  * @type {boolean|string}
180
180
  */
181
- _inside_sequence = false;
181
+ _sequence_type = false;
182
182
 
183
183
  /**
184
184
  * loop through the f keys, f1 to f19 and add them to the map
@@ -205,7 +205,8 @@
205
205
  */
206
206
  function _addEvent(object, type, callback) {
207
207
  if (object.addEventListener) {
208
- return object.addEventListener(type, callback, false);
208
+ object.addEventListener(type, callback, false);
209
+ return;
209
210
  }
210
211
 
211
212
  object.attachEvent('on' + type, callback);
@@ -237,25 +238,6 @@
237
238
  return String.fromCharCode(e.which).toLowerCase();
238
239
  }
239
240
 
240
- /**
241
- * should we stop this event before firing off callbacks
242
- *
243
- * @param {Event} e
244
- * @return {boolean}
245
- */
246
- function _stop(e) {
247
- var element = e.target || e.srcElement,
248
- tag_name = element.tagName;
249
-
250
- // if the element has the class "mousetrap" then no need to stop
251
- if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
252
- return false;
253
- }
254
-
255
- // stop for input, select, and textarea
256
- return tag_name == 'INPUT' || tag_name == 'SELECT' || tag_name == 'TEXTAREA' || (element.contentEditable && element.contentEditable == 'true');
257
- }
258
-
259
241
  /**
260
242
  * checks if two arrays are equal
261
243
  *
@@ -273,14 +255,14 @@
273
255
  * @param {Object} do_not_reset
274
256
  * @returns void
275
257
  */
276
- function _resetSequences(do_not_reset) {
258
+ function _resetSequences(do_not_reset, max_level) {
277
259
  do_not_reset = do_not_reset || {};
278
260
 
279
261
  var active_sequences = false,
280
262
  key;
281
263
 
282
264
  for (key in _sequence_levels) {
283
- if (do_not_reset[key]) {
265
+ if (do_not_reset[key] && _sequence_levels[key] > max_level) {
284
266
  active_sequences = true;
285
267
  continue;
286
268
  }
@@ -288,7 +270,7 @@
288
270
  }
289
271
 
290
272
  if (!active_sequences) {
291
- _inside_sequence = false;
273
+ _sequence_type = false;
292
274
  }
293
275
  }
294
276
 
@@ -396,8 +378,14 @@
396
378
  * @param {Event} e
397
379
  * @returns void
398
380
  */
399
- function _fireCallback(callback, e) {
400
- if (callback(e) === false) {
381
+ function _fireCallback(callback, e, combo) {
382
+
383
+ // if this event should not happen stop here
384
+ if (Mousetrap.stopCallback(e, e.target || e.srcElement, combo)) {
385
+ return;
386
+ }
387
+
388
+ if (callback(e, combo) === false) {
401
389
  if (e.preventDefault) {
402
390
  e.preventDefault();
403
391
  }
@@ -419,15 +407,10 @@
419
407
  * @returns void
420
408
  */
421
409
  function _handleCharacter(character, e) {
422
-
423
- // if this event should not happen stop here
424
- if (_stop(e)) {
425
- return;
426
- }
427
-
428
410
  var callbacks = _getMatches(character, _eventModifiers(e), e),
429
411
  i,
430
412
  do_not_reset = {},
413
+ max_level = 0,
431
414
  processed_sequence_callback = false;
432
415
 
433
416
  // loop through matching callbacks for this key event
@@ -441,24 +424,28 @@
441
424
  if (callbacks[i].seq) {
442
425
  processed_sequence_callback = true;
443
426
 
427
+ // as we loop through keep track of the max
428
+ // any sequence at a lower level will be discarded
429
+ max_level = Math.max(max_level, callbacks[i].level);
430
+
444
431
  // keep a list of which sequences were matches for later
445
432
  do_not_reset[callbacks[i].seq] = 1;
446
- _fireCallback(callbacks[i].callback, e);
433
+ _fireCallback(callbacks[i].callback, e, callbacks[i].combo);
447
434
  continue;
448
435
  }
449
436
 
450
437
  // if there were no sequence matches but we are still here
451
438
  // that means this is a regular match so we should fire that
452
- if (!processed_sequence_callback && !_inside_sequence) {
453
- _fireCallback(callbacks[i].callback, e);
439
+ if (!processed_sequence_callback && !_sequence_type) {
440
+ _fireCallback(callbacks[i].callback, e, callbacks[i].combo);
454
441
  }
455
442
  }
456
443
 
457
444
  // if you are inside of a sequence and the key you are pressing
458
445
  // is not a modifier key then we should reset all sequences
459
446
  // that were not matched by this key event
460
- if (e.type == _inside_sequence && !_isModifier(character)) {
461
- _resetSequences(do_not_reset);
447
+ if (e.type == _sequence_type && !_isModifier(character)) {
448
+ _resetSequences(do_not_reset, max_level);
462
449
  }
463
450
  }
464
451
 
@@ -472,7 +459,9 @@
472
459
 
473
460
  // normalize e.which for key events
474
461
  // @see http://stackoverflow.com/questions/4285627/javascript-keycode-vs-charcode-utter-confusion
475
- e.which = typeof e.which == "number" ? e.which : e.keyCode;
462
+ if (typeof e.which !== 'number') {
463
+ e.which = e.keyCode;
464
+ }
476
465
 
477
466
  var character = _characterFromEvent(e);
478
467
 
@@ -590,7 +579,7 @@
590
579
  * @returns void
591
580
  */
592
581
  var _increaseSequence = function(e) {
593
- _inside_sequence = action;
582
+ _sequence_type = action;
594
583
  ++_sequence_levels[combo];
595
584
  _resetSequenceTimer();
596
585
  },
@@ -603,7 +592,7 @@
603
592
  * @returns void
604
593
  */
605
594
  _callbackAndReset = function(e) {
606
- _fireCallback(callback, e);
595
+ _fireCallback(callback, e, combo);
607
596
 
608
597
  // we should ignore the next key up if the action is key down
609
598
  // or keypress. this is so if you finish a sequence and
@@ -650,7 +639,8 @@
650
639
  // if this pattern is a sequence of keys then run through this method
651
640
  // to reprocess each pattern one key at a time
652
641
  if (sequence.length > 1) {
653
- return _bindSequence(combination, sequence, callback, action);
642
+ _bindSequence(combination, sequence, callback, action);
643
+ return;
654
644
  }
655
645
 
656
646
  // take the keys from this pattern and figure out what the actual
@@ -727,7 +717,7 @@
727
717
  _addEvent(document, 'keydown', _handleKey);
728
718
  _addEvent(document, 'keyup', _handleKey);
729
719
 
730
- var mousetrap = {
720
+ var Mousetrap = {
731
721
 
732
722
  /**
733
723
  * binds an event to mousetrap
@@ -797,14 +787,32 @@
797
787
  _callbacks = {};
798
788
  _direct_map = {};
799
789
  return this;
790
+ },
791
+
792
+ /**
793
+ * should we stop this event before firing off callbacks
794
+ *
795
+ * @param {Event} e
796
+ * @param {Element} element
797
+ * @return {boolean}
798
+ */
799
+ stopCallback: function(e, element, combo) {
800
+
801
+ // if the element has the class "mousetrap" then no need to stop
802
+ if ((' ' + element.className + ' ').indexOf(' mousetrap ') > -1) {
803
+ return false;
804
+ }
805
+
806
+ // stop for input, select, and textarea
807
+ return element.tagName == 'INPUT' || element.tagName == 'SELECT' || element.tagName == 'TEXTAREA' || (element.contentEditable && element.contentEditable == 'true');
800
808
  }
801
809
  };
802
810
 
803
811
  // expose mousetrap to the global object
804
- window.Mousetrap = mousetrap;
812
+ window.Mousetrap = Mousetrap;
805
813
 
806
814
  // expose mousetrap as an AMD module
807
- if (typeof define == 'function' && define.amd) {
808
- define('mousetrap', function() { return mousetrap; });
815
+ if (typeof define === 'function' && define.amd) {
816
+ define(Mousetrap);
809
817
  }
810
818
  }) ();
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mousetrapjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-01 00:00:00.000000000 Z
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Gem for Mousetrap, a simple library for handling keyboard shortcuts in
15
15
  Javascript
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  version: '0'
49
49
  requirements: []
50
50
  rubyforge_project:
51
- rubygems_version: 1.8.24
51
+ rubygems_version: 1.8.23
52
52
  signing_key:
53
53
  specification_version: 3
54
54
  summary: Mousetrap is a simple library for handling keyboard shortcuts in Javascript