You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

121 lines
2.4 KiB

  1. /**
  2. * plugin.js
  3. *
  4. * Released under LGPL License.
  5. * Copyright (c) 1999-2015 Ephox Corp. All rights reserved
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /*global tinymce:true */
  11. tinymce.PluginManager.add('tabfocus', function(editor) {
  12. var DOM = tinymce.DOM, each = tinymce.each, explode = tinymce.explode;
  13. function tabCancel(e) {
  14. if (e.keyCode === 9 && !e.ctrlKey && !e.altKey && !e.metaKey) {
  15. e.preventDefault();
  16. }
  17. }
  18. function tabHandler(e) {
  19. var x, el, v, i;
  20. if (e.keyCode !== 9 || e.ctrlKey || e.altKey || e.metaKey || e.isDefaultPrevented()) {
  21. return;
  22. }
  23. function find(direction) {
  24. el = DOM.select(':input:enabled,*[tabindex]:not(iframe)');
  25. function canSelectRecursive(e) {
  26. return e.nodeName === "BODY" || (e.type != 'hidden' &&
  27. e.style.display != "none" &&
  28. e.style.visibility != "hidden" && canSelectRecursive(e.parentNode));
  29. }
  30. function canSelect(el) {
  31. return /INPUT|TEXTAREA|BUTTON/.test(el.tagName) && tinymce.get(e.id) && el.tabIndex != -1 && canSelectRecursive(el);
  32. }
  33. each(el, function(e, i) {
  34. if (e.id == editor.id) {
  35. x = i;
  36. return false;
  37. }
  38. });
  39. if (direction > 0) {
  40. for (i = x + 1; i < el.length; i++) {
  41. if (canSelect(el[i])) {
  42. return el[i];
  43. }
  44. }
  45. } else {
  46. for (i = x - 1; i >= 0; i--) {
  47. if (canSelect(el[i])) {
  48. return el[i];
  49. }
  50. }
  51. }
  52. return null;
  53. }
  54. v = explode(editor.getParam('tab_focus', editor.getParam('tabfocus_elements', ':prev,:next')));
  55. if (v.length == 1) {
  56. v[1] = v[0];
  57. v[0] = ':prev';
  58. }
  59. // Find element to focus
  60. if (e.shiftKey) {
  61. if (v[0] == ':prev') {
  62. el = find(-1);
  63. } else {
  64. el = DOM.get(v[0]);
  65. }
  66. } else {
  67. if (v[1] == ':next') {
  68. el = find(1);
  69. } else {
  70. el = DOM.get(v[1]);
  71. }
  72. }
  73. if (el) {
  74. var focusEditor = tinymce.get(el.id || el.name);
  75. if (el.id && focusEditor) {
  76. focusEditor.focus();
  77. } else {
  78. tinymce.util.Delay.setTimeout(function() {
  79. if (!tinymce.Env.webkit) {
  80. window.focus();
  81. }
  82. el.focus();
  83. }, 10);
  84. }
  85. e.preventDefault();
  86. }
  87. }
  88. editor.on('init', function() {
  89. if (editor.inline) {
  90. // Remove default tabIndex in inline mode
  91. tinymce.DOM.setAttrib(editor.getBody(), 'tabIndex', null);
  92. }
  93. editor.on('keyup', tabCancel);
  94. if (tinymce.Env.gecko) {
  95. editor.on('keypress keydown', tabHandler);
  96. } else {
  97. editor.on('keydown', tabHandler);
  98. }
  99. });
  100. });