1/**
2 * AutoIndentControl Plugin for DokuWiki / script.js
3 *
4 * Provides a toggle switch to control auto-indent function in edit window.
5 *
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author  Kazutaka Miyasaka <kazmiya@gmail.com>
8 */
9
10(function() {
11    if (typeof JSINFO !== 'object' || !JSINFO.plugin_autoindentcontrol) {
12        return;
13    }
14
15    // compatibility check
16    if (
17        typeof DEPRECATED === 'function' ||
18        typeof addInitEvent === 'undefined'
19    ) {
20        // for DokuWiki Angua or later
21        jQuery(_Angua);
22    } else {
23        // for DokuWiki Anteater or earlier
24        addInitEvent(_Anteater);
25    }
26
27    /**
28     * for DokuWiki Angua
29     */
30    function _Angua() {
31        var $editor,
32            auto;
33
34        $editor = jQuery('#wiki__text');
35
36        if (!$editor.length || $editor.attr('readOnly')) {
37            return;
38        }
39
40        auto = JSINFO.plugin_autoindentcontrol;
41        auto.enabled = true;
42
43        // init toggle button
44        if (auto.showToggle) {
45            initAutoIndentControl();
46        }
47
48        // set initial state of auto-indent functoinality
49        userOFF = DokuCookie.getValue('autoIndentOFF');
50
51        if (
52            (!auto.showToggle && auto.defaultOFF) ||
53            (auto.showToggle &&
54                (userOFF || typeof userOFF === 'undefined' && auto.defaultOFF)
55            )
56        ) {
57            toggleAutoIndent();
58        }
59
60        /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
61
62        /**
63         * Initializes auto-indent control button
64         */
65        function initAutoIndentControl() {
66            jQuery('<div />')
67                .attr('id', 'plugin__autoindentcontrol__switch')
68                .attr('class', 'autoIndentON')
69                .attr('title', 'Auto-Indent: ON')
70                .html('<span>Auto-Indent: ON</span>')
71                .insertAfter('#size__ctl')
72                .bind('click', toggleAutoIndent);
73        }
74
75        /**
76         * Toggles auto-indent functionality
77         */
78        function toggleAutoIndent() {
79            var type,
80                onoff;
81
82            type = jQuery.browser.opera ? 'keypress' : 'keydown';
83
84            // handle events on edit window
85            if (auto.enabled) {
86                onoff = 'OFF';
87                auto.enabled = false;
88                $editor.unbind(type);
89                DokuCookie.setValue('autoIndentOFF', 1);
90            } else {
91                onoff = 'ON';
92                auto.enabled = true;
93                $editor.bind(type, dw_editor.keyHandler);
94                DokuCookie.setValue('autoIndentOFF', '');
95            }
96
97            // switch toggle button
98            jQuery('#plugin__autoindentcontrol__switch')
99                .attr('title', 'Auto-Indent: ' + onoff)
100                .attr('class', 'autoIndent' + onoff)
101                .html('<span>Auto-Indent: ' + onoff + '</span>');
102        }
103    }
104
105    /**
106     * for DokuWiki Anteater or earlier
107     */
108    function _Anteater() {
109        var editor,
110            auto,
111            userOFF;
112
113        // check if we are in edit window
114        editor = $('wiki__text');
115
116        if (!editor || editor.readOnly) {
117            return;
118        }
119
120        auto = JSINFO.plugin_autoindentcontrol;
121        auto.enabled = true;
122
123        // init toggle button
124        if (auto.showToggle) {
125            initAutoIndentControl();
126        }
127
128        // set initial state of auto-indent functoinality
129        userOFF = DokuCookie.getValue('autoIndentOFF');
130
131        if (
132            (!auto.showToggle && auto.defaultOFF) ||
133            (auto.showToggle &&
134                (userOFF || isUndefined(userOFF) && auto.defaultOFF)
135            )
136        ) {
137            toggleAutoIndent();
138        }
139
140        /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
141
142        /**
143         * Initializes auto-indent control button
144         */
145        function initAutoIndentControl() {
146            var resizer,
147                button;
148
149            resizer = $('size__ctl');
150
151            if (!resizer) {
152                return;
153            }
154
155            // create a toggle button to control auto-indent function
156            button = document.createElement('div');
157            button.id = 'plugin__autoindentcontrol__switch';
158            button.title = 'Auto-Indent: ON';
159            button.innerHTML = '<span>Auto-Indent: ON</span>';
160            button.className = 'autoIndentON';
161
162            // insert toggle button
163            resizer.parentNode.insertBefore(button, resizer.nextSibling);
164
165            addEvent(button, 'click', toggleAutoIndent);
166        }
167
168        /**
169         * Toggles auto-indent functionality
170         */
171        function toggleAutoIndent() {
172            var type,
173                guid,
174                func,
175                button,
176                onoff;
177
178            type = (is_opera && !auto.isLemming) ? 'keypress' : 'keydown';
179
180            // handle events on edit window
181            if (auto.enabled) {
182                auto.enabled = false;
183
184                if (editor.events) {
185                    for (guid in editor.events[type]) {
186                        func = editor.events[type][guid].toString();
187
188                        if (func.match(/^function\s+keyHandler\b/)) {
189                            removeEvent(editor, type, editor.events[type][guid]);
190                        }
191                    }
192                }
193            } else {
194                auto.enabled = true;
195                addEvent(editor, type, keyHandler);
196            }
197
198            button = $('plugin__autoindentcontrol__switch');
199
200            // switch toggle button
201            if (button) {
202                onoff = auto.enabled ? 'ON' : 'OFF';
203
204                button.title = 'Auto-Indent: ' + onoff;
205                button.innerHTML = '<span>Auto-Indent: ' + onoff + '</span>';
206                button.className = 'autoIndent' + onoff;
207                DokuCookie.setValue('autoIndentOFF', auto.enabled ? '' : 1);
208            }
209        }
210    }
211})();
212