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