##// END OF EJS Templates
jshint
Jonathan Frederic -
Show More
@@ -42,12 +42,12 var IPython = (function (IPython) {
42 42 // These apply to Firefox and Opera
43 43 var _mozilla_keycodes = {
44 44 '; :': 59, '= +': 61, '- _': 173, 'meta': 224
45 }
45 };
46 46
47 47 // This apply to Webkit and IE
48 48 var _ie_keycodes = {
49 49 '; :': 186, '= +': 187, '- _': 189,
50 }
50 };
51 51
52 52 var browser = IPython.utils.browser[0];
53 53 var platform = IPython.utils.platform;
@@ -63,15 +63,15 var IPython = (function (IPython) {
63 63 for (var name in _keycodes) {
64 64 var names = name.split(' ');
65 65 if (names.length === 1) {
66 var n = names[0]
67 keycodes[n] = _keycodes[n]
68 inv_keycodes[_keycodes[n]] = n
66 var n = names[0];
67 keycodes[n] = _keycodes[n];
68 inv_keycodes[_keycodes[n]] = n;
69 69 } else {
70 70 var primary = names[0];
71 71 var secondary = names[1];
72 keycodes[primary] = _keycodes[name]
73 keycodes[secondary] = _keycodes[name]
74 inv_keycodes[_keycodes[name]] = primary
72 keycodes[primary] = _keycodes[name];
73 keycodes[secondary] = _keycodes[name];
74 inv_keycodes[_keycodes[name]] = primary;
75 75 }
76 76 }
77 77
@@ -111,7 +111,7 var IPython = (function (IPython) {
111 111 return false;
112 112 }
113 113 }
114 }
114 };
115 115
116 116 if (platform === 'MacOS') {
117 117 default_common_shortcuts['cmd+s'] =
@@ -165,11 +165,11 var IPython = (function (IPython) {
165 165 var cell = IPython.notebook.get_selected_cell();
166 166 if (cell && cell.at_top()) {
167 167 event.preventDefault();
168 IPython.notebook.command_mode()
168 IPython.notebook.command_mode();
169 169 IPython.notebook.select_prev();
170 170 IPython.notebook.edit_mode();
171 171 return false;
172 };
172 }
173 173 }
174 174 },
175 175 'down' : {
@@ -179,11 +179,11 var IPython = (function (IPython) {
179 179 var cell = IPython.notebook.get_selected_cell();
180 180 if (cell && cell.at_bottom()) {
181 181 event.preventDefault();
182 IPython.notebook.command_mode()
182 IPython.notebook.command_mode();
183 183 IPython.notebook.select_next();
184 184 IPython.notebook.edit_mode();
185 185 return false;
186 };
186 }
187 187 }
188 188 },
189 189 'alt+-' : {
@@ -210,7 +210,7 var IPython = (function (IPython) {
210 210 help : 'tooltip',
211 211 help_index : 'ed',
212 212 },
213 }
213 };
214 214
215 215 if (platform === 'MacOS') {
216 216 default_edit_shortcuts['cmd+/'] =
@@ -266,7 +266,7 var IPython = (function (IPython) {
266 266 IPython.notebook.select_prev();
267 267 var cell = IPython.notebook.get_selected_cell();
268 268 cell.focus_cell();
269 };
269 }
270 270 return false;
271 271 }
272 272 },
@@ -279,7 +279,7 var IPython = (function (IPython) {
279 279 IPython.notebook.select_next();
280 280 var cell = IPython.notebook.get_selected_cell();
281 281 cell.focus_cell();
282 };
282 }
283 283 return false;
284 284 }
285 285 },
@@ -292,7 +292,7 var IPython = (function (IPython) {
292 292 IPython.notebook.select_prev();
293 293 var cell = IPython.notebook.get_selected_cell();
294 294 cell.focus_cell();
295 };
295 }
296 296 return false;
297 297 }
298 298 },
@@ -305,7 +305,7 var IPython = (function (IPython) {
305 305 IPython.notebook.select_next();
306 306 var cell = IPython.notebook.get_selected_cell();
307 307 cell.focus_cell();
308 };
308 }
309 309 return false;
310 310 }
311 311 },
@@ -540,23 +540,23 var IPython = (function (IPython) {
540 540 return false;
541 541 }
542 542 },
543 }
543 };
544 544
545 545
546 546 // Shortcut manager class
547 547
548 548 var ShortcutManager = function (delay) {
549 this._shortcuts = {}
550 this._counts = {}
551 this._timers = {}
549 this._shortcuts = {};
550 this._counts = {};
551 this._timers = {};
552 552 this.delay = delay || 800; // delay in milliseconds
553 }
553 };
554 554
555 555 ShortcutManager.prototype.help = function () {
556 556 var help = [];
557 557 for (var shortcut in this._shortcuts) {
558 var help_string = this._shortcuts[shortcut]['help'];
559 var help_index = this._shortcuts[shortcut]['help_index'];
558 var help_string = this._shortcuts[shortcut].help;
559 var help_index = this._shortcuts[shortcut].help_index;
560 560 if (help_string) {
561 561 if (platform === 'MacOS') {
562 562 shortcut = shortcut.replace('meta', 'cmd');
@@ -576,45 +576,45 var IPython = (function (IPython) {
576 576 return 0;
577 577 });
578 578 return help;
579 }
579 };
580 580
581 581 ShortcutManager.prototype.normalize_key = function (key) {
582 582 return inv_keycodes[keycodes[key]];
583 }
583 };
584 584
585 585 ShortcutManager.prototype.normalize_shortcut = function (shortcut) {
586 586 // Sort a sequence of + separated modifiers into the order alt+ctrl+meta+shift
587 587 shortcut = shortcut.replace('cmd', 'meta').toLowerCase();
588 588 var values = shortcut.split("+");
589 589 if (values.length === 1) {
590 return this.normalize_key(values[0])
590 return this.normalize_key(values[0]);
591 591 } else {
592 592 var modifiers = values.slice(0,-1);
593 593 var key = this.normalize_key(values[values.length-1]);
594 594 modifiers.sort();
595 595 return modifiers.join('+') + '+' + key;
596 596 }
597 }
597 };
598 598
599 599 ShortcutManager.prototype.event_to_shortcut = function (event) {
600 600 // Convert a jQuery keyboard event to a strong based keyboard shortcut
601 601 var shortcut = '';
602 var key = inv_keycodes[event.which]
602 var key = inv_keycodes[event.which];
603 603 if (event.altKey && key !== 'alt') {shortcut += 'alt+';}
604 604 if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';}
605 605 if (event.metaKey && key !== 'meta') {shortcut += 'meta+';}
606 606 if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';}
607 607 shortcut += key;
608 return shortcut
609 }
608 return shortcut;
609 };
610 610
611 611 ShortcutManager.prototype.clear_shortcuts = function () {
612 612 this._shortcuts = {};
613 }
613 };
614 614
615 615 ShortcutManager.prototype.add_shortcut = function (shortcut, data) {
616 616 if (typeof(data) === 'function') {
617 data = {help: '', help_index: '', handler: data}
617 data = {help: '', help_index: '', handler: data};
618 618 }
619 619 data.help_index = data.help_index || '';
620 620 data.help = data.help || '';
@@ -625,19 +625,19 var IPython = (function (IPython) {
625 625 shortcut = this.normalize_shortcut(shortcut);
626 626 this._counts[shortcut] = 0;
627 627 this._shortcuts[shortcut] = data;
628 }
628 };
629 629
630 630 ShortcutManager.prototype.add_shortcuts = function (data) {
631 631 for (var shortcut in data) {
632 632 this.add_shortcut(shortcut, data[shortcut]);
633 633 }
634 }
634 };
635 635
636 636 ShortcutManager.prototype.remove_shortcut = function (shortcut) {
637 637 shortcut = this.normalize_shortcut(shortcut);
638 638 delete this._counts[shortcut];
639 639 delete this._shortcuts[shortcut];
640 }
640 };
641 641
642 642 ShortcutManager.prototype.count_handler = function (shortcut, event, data) {
643 643 var that = this;
@@ -646,7 +646,7 var IPython = (function (IPython) {
646 646 var timer = null;
647 647 if (c[shortcut] === data.count-1) {
648 648 c[shortcut] = 0;
649 var timer = t[shortcut];
649 timer = t[shortcut];
650 650 if (timer) {clearTimeout(timer); delete t[shortcut];}
651 651 return data.handler(event);
652 652 } else {
@@ -657,13 +657,13 var IPython = (function (IPython) {
657 657 t[shortcut] = timer;
658 658 }
659 659 return false;
660 }
660 };
661 661
662 662 ShortcutManager.prototype.call_handler = function (event) {
663 663 var shortcut = this.event_to_shortcut(event);
664 664 var data = this._shortcuts[shortcut];
665 665 if (data) {
666 var handler = data['handler'];
666 var handler = data.handler;
667 667 if (handler) {
668 668 if (data.count === 1) {
669 669 return handler(event);
@@ -673,7 +673,7 var IPython = (function (IPython) {
673 673 }
674 674 }
675 675 return true;
676 }
676 };
677 677
678 678
679 679
@@ -701,14 +701,14 var IPython = (function (IPython) {
701 701 KeyboardManager.prototype.handle_keydown = function (event) {
702 702 var notebook = IPython.notebook;
703 703
704 if (event.which === keycodes['esc']) {
704 if (event.which === keycodes.esc) {
705 705 // Intercept escape at highest level to avoid closing
706 706 // websocket connection with firefox
707 707 event.preventDefault();
708 708 }
709 709
710 710 if (!this.enabled) {
711 if (event.which === keycodes['esc']) {
711 if (event.which === keycodes.esc) {
712 712 // ESC
713 713 notebook.command_mode();
714 714 return false;
@@ -722,25 +722,25 var IPython = (function (IPython) {
722 722 return this.command_shortcuts.call_handler(event);
723 723 }
724 724 return true;
725 }
725 };
726 726
727 727 KeyboardManager.prototype.edit_mode = function () {
728 728 this.last_mode = this.mode;
729 729 this.mode = 'edit';
730 }
730 };
731 731
732 732 KeyboardManager.prototype.command_mode = function () {
733 733 this.last_mode = this.mode;
734 734 this.mode = 'command';
735 }
735 };
736 736
737 737 KeyboardManager.prototype.enable = function () {
738 738 this.enabled = true;
739 }
739 };
740 740
741 741 KeyboardManager.prototype.disable = function () {
742 742 this.enabled = false;
743 }
743 };
744 744
745 745 KeyboardManager.prototype.register_events = function (e) {
746 746 var that = this;
@@ -758,7 +758,7 var IPython = (function (IPython) {
758 758 that.enable();
759 759 }
760 760 });
761 }
761 };
762 762
763 763
764 764 IPython.keycodes = keycodes;
General Comments 0
You need to be logged in to leave comments. Login now