~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-includes/js/tinymce/plugins/tabfocus/plugin.js

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 canSelectInOldIe(el) {
 
39
                                return el.tabIndex || el.nodeName == "INPUT" || el.nodeName == "TEXTAREA";
 
40
                        }
 
41
 
 
42
                        function canSelect(el) {
 
43
                                return ((!canSelectInOldIe(el))) && el.getAttribute("tabindex") != '-1' && canSelectRecursive(el);
 
44
                        }
 
45
 
 
46
                        each(el, function(e, i) {
 
47
                                if (e.id == editor.id) {
 
48
                                        x = i;
 
49
                                        return false;
 
50
                                }
 
51
                        });
 
52
                        if (direction > 0) {
 
53
                                for (i = x + 1; i < el.length; i++) {
 
54
                                        if (canSelect(el[i])) {
 
55
                                                return el[i];
 
56
                                        }
 
57
                                }
 
58
                        } else {
 
59
                                for (i = x - 1; i >= 0; i--) {
 
60
                                        if (canSelect(el[i])) {
 
61
                                                return el[i];
 
62
                                        }
 
63
                                }
 
64
                        }
 
65
 
 
66
                        return null;
 
67
                }
 
68
 
 
69
                v = explode(editor.getParam('tab_focus', editor.getParam('tabfocus_elements', ':prev,:next')));
 
70
 
 
71
                if (v.length == 1) {
 
72
                        v[1] = v[0];
 
73
                        v[0] = ':prev';
 
74
                }
 
75
 
 
76
                // Find element to focus
 
77
                if (e.shiftKey) {
 
78
                        if (v[0] == ':prev') {
 
79
                                el = find(-1);
 
80
                        } else {
 
81
                                el = DOM.get(v[0]);
 
82
                        }
 
83
                } else {
 
84
                        if (v[1] == ':next') {
 
85
                                el = find(1);
 
86
                        } else {
 
87
                                el = DOM.get(v[1]);
 
88
                        }
 
89
                }
 
90
 
 
91
                if (el) {
 
92
                        var focusEditor = tinymce.get(el.id || el.name);
 
93
 
 
94
                        if (el.id && focusEditor) {
 
95
                                focusEditor.focus();
 
96
                        } else {
 
97
                                window.setTimeout(function() {
 
98
                                        if (!tinymce.Env.webkit) {
 
99
                                                window.focus();
 
100
                                        }
 
101
 
 
102
                                        el.focus();
 
103
                                }, 10);
 
104
                        }
 
105
 
 
106
                        e.preventDefault();
 
107
                }
 
108
        }
 
109
 
 
110
        editor.on('init', function() {
 
111
                if (editor.inline) {
 
112
                        // Remove default tabIndex in inline mode
 
113
                        tinymce.DOM.setAttrib(editor.getBody(), 'tabIndex', null);
 
114
                }
 
115
 
 
116
                editor.on('keyup', tabCancel);
 
117
 
 
118
                // Add later so other plugins can preventDefault()
 
119
                if (tinymce.Env.gecko) {
 
120
                        editor.on('keypress keydown', tabHandler);
 
121
                } else {
 
122
                        editor.on('keydown', tabHandler);
 
123
                }
 
124
        });
 
125
});