1/**
2 * Implements the Word and Char counter
3 *
4 * @param {string} edid
5 * @link http://www.dokuwiki.org/tips:wordcounter
6 * @constructor
7 */
8var ToolboxCounter = function (edid) {
9
10    var textarea = jQuery('#' + edid)[0];
11
12    /**
13     * Count the charactes in the given text
14     *
15     * @param {string} text
16     */
17    function charcounter(text) {
18        var list = text.split(/[^\w\-_]+/);
19        var len = text.length;
20        if (list[len - 1] == '') len--;
21        if (list[0] == '') len--;
22        if (len < 0) len = 0;
23        return len;
24    }
25
26    /**
27     * Count the words in the given text
28     *
29     * @param {string} text
30     */
31    function wordcounter(text) {
32        var list = text.split(/[^\w\-_]+/);
33        var len = list.length;
34        if (list[len - 1] == '') len--;
35        if (list[0] == '') len--;
36        if (len < 0) len = 0;
37        return len;
38    }
39
40    /**
41     * Returns the counts of the different parts
42     *
43     * @return {{call: int, wall: int, csec: int, wsec: int, csel: int, wsel: int}}
44     */
45    function agggregateCounts() {
46        var counts = {
47            call: 0,
48            wall: 0,
49            csec: charcounter(textarea.value),
50            wsec: wordcounter(textarea.value),
51            csel: 0,
52            wsel: 0
53        };
54        counts.call += counts.csec;
55        counts.wall += counts.wsec;
56
57        if (textarea.form.elements.prefix && textarea.form.elements.prefix.value) {
58            counts.call += charcounter(textarea.form.elements.prefix.value);
59            counts.wall += wordcounter(textarea.form.elements.prefix.value);
60        }
61        if (textarea.form.elements.suffix && textarea.form.elements.suffix.value) {
62            counts.call += charcounter(textarea.form.elements.suffix.value);
63            counts.wall += wordcounter(textarea.form.elements.suffix.value);
64        }
65
66        var selection = DWgetSelection(textarea);
67        if (selection.getLength()) {
68            var text = selection.getText();
69            counts.csel = charcounter(text);
70            counts.wsel = wordcounter(text);
71        }
72
73        return counts;
74    }
75
76    /**
77     * Return the HTML for one count section
78     *
79     * Returns an empty string if chars is 0
80     *
81     * @param {string} head headline for this section
82     * @param {int} chars character count
83     * @param {int} words word count
84     * @return {string}
85     */
86    function html(head, chars, words) {
87        var out = '';
88
89        if (!chars) return out;
90
91        out += '<dt>' + head + '</dt>';
92        out += '<dd>' + toolbox_lang.chars.replace('%d', chars) + '</dd>';
93        out += '<dd>' + toolbox_lang.words.replace('%d', words) + '</dd>';
94        return out;
95    }
96
97    // prepare the dialog
98    var counts = agggregateCounts();
99    var $dialog = jQuery(
100        '<div><dl>' +
101        html(toolbox_lang.total, counts.call, counts.wall) +
102        html(toolbox_lang.section, counts.csec, counts.wsec) +
103        html(toolbox_lang.selection, counts.csel, counts.wsel) +
104        '</dl></div>'
105    );
106
107    // show the edialog
108    $dialog.dialog({
109        modal: true,
110        title: toolbox_lang.counter,
111        resizable: false,
112        buttons: {
113            'Ok': function () {
114                $dialog.dialog('close')
115            }
116        },
117        // clean up on close
118        close: function () {
119            $dialog.dialog('destroy');
120            $dialog.remove();
121        }
122    });
123};
124