1/*  DokuWiki MoaiEditor Captcha.js file
2    Author  : MoaiTools <info@moaitools.org>
3    License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */
4
5/* This class integrates www.dokuwiki.org/plugin:captcha into the editor.
6
7   Functions of this class:
8    - Moves the captcha wrapper to a suitable position depending on mode (desktop, mobile).
9    - Intercepts the Save button click if the captcha input is empty, so that instead of
10      submitting the form the user is reminded to fill the captcha.
11*/
12MoaiEditor.Captcha = class {
13
14    constructor() {
15        this.userWasReminded = false;
16    }
17    // ┌───────────────────────────────────┐
18    // │ Public                            │
19    // └───────────────────────────────────┘
20
21    initBeforeLayout () {
22        if (!this.exists())
23            return;
24
25        // Add the form attribute to the input elements (as they are no longer inside the form)
26        const inputs = this.wrapper.querySelectorAll("input");
27        for (let input of inputs)
28            input.setAttribute('form', 'dw__editform');
29
30        // Reduce margins and paddings
31        this.wrapper.style.margin = '0';
32        this.wrapper.style.padding = '5px 10px';
33
34        // Intercept the Save button action
35        const original_onclick_save = moaiEditor.buttons.save.__onClick;
36        moaiEditor.buttons.save.__onClick = function (event) {
37
38            // Submit the form if the captcha input is not empty, or if we reminded the user already
39            const self = moaiEditor.plugins.captcha;
40            const textinput = self.wrapper.querySelector("input[type=text]");
41            if (textinput.value.length > 0  ||  self.userWasReminded)
42                return original_onclick_save.bind(moaiEditor.buttons.save, event);
43
44            // Else, remind the user
45            const msg = "Please fill the captcha below.";
46            event.preventDefault();                     // Prevent form submit
47            moaiEditor.dokuMessage(msg, 2);             // Show message
48            self.wrapper.style.background = "#ffc";     // Highlight form
49            moaiEditor.layout.goLeft();                 // In mobile mode scroll to the left side to show the form
50            self.userWasReminded = true;                // Remind the user only once (then make the Save button work as normal again)
51        }
52    }
53    onSpecificLayout(mode) {
54        if (this.wrapper === null)
55            return;
56        if (mode == 'desktop') {
57            moaiEditor.layout.desktop.footerLeft.appendChild(this.wrapper);
58        }
59        if (mode == 'vphone') {
60            const container = document.querySelector("#moaied__phone_left_main");
61            const separator = document.querySelector("#moaied__phone_left_main_separator");
62            const element = moaiEditor.createHTML('<div><label>Captcha</label></div>');
63            //this.wrapper.style.background = "rgba(255,255,255,0.4)";
64            element.appendChild(this.wrapper);
65            container.insertBefore (element, separator);
66        }
67    }
68    // ┌───────────────────────────────────┐
69    // │ Input events                      │
70    // └───────────────────────────────────┘
71    // ┌───────────────────────────────────┐
72    // │ Private                           │
73    // └───────────────────────────────────┘
74
75    exists () {
76        this.wrapper = document.querySelector("#wiki__editbar div.plugin__captcha_wrapper");
77        if (this.wrapper === null)
78            return false;
79        return true;
80    }
81
82
83}; // End Class
84
85
86
87