xref: /plugin/todo/script.js (revision d5adcab96bfde5074b964053793c1053ccdfa1e7)
1/**
2 * @date 20130405 Leo Eibler <dokuwiki@sprossenwanne.at> \n
3 *                replace old sack() method with new jQuery method and use post instead of get - see https://www.dokuwiki.org/devel:jqueryfaq \n
4 * @date 20130407 Leo Eibler <dokuwiki@sprossenwanne.at> \n
5 *                use jQuery for finding the elements \n
6 * @date 20130408 Christian Marg <marg@rz.tu-clausthal.de> \n
7 *                change only the clicked todoitem instead of all items with the same text \n
8 * @date 20130408 Leo Eibler <dokuwiki@sprossenwanne.at> \n
9 *                migrate changes made by Christian Marg to current version of plugin (use jQuery) \n
10 * @date 20130410 by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at \n
11 *                bugfix: encoding html code (security risk <todo><script>alert('hi')</script></todo>) - bug reported by Andreas \n
12 * @date 20130413 Christian Marg <marg@rz.tu-clausthal.de> \n
13 *                bugfix: chk.attr('checked') returns checkbox state from html - use chk.is(':checked') - see http://www.unforastero.de/jquery/checkbox-angehakt.php \n
14 * @date 20130413 by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at \n
15 *                bugfix: config option Strikethrough \n
16 */
17
18/**
19 * html-layout:
20 *
21 * +input[checkbox].todocheckbox
22 * +span.todotext
23 * -del
24 * --span.todoinnertext
25 * ---anchor with text or text only
26 */
27
28var ToDoPlugin = {
29    /**
30     * lock to prevent simultanous requests
31     */
32    locked: false,
33
34    /**
35     * @brief onclick method for input element
36     *
37     * @param {jQuery} $chk the jQuery input element
38     */
39    todo: function ($chk) {
40        //skip when locked
41        if (ToDoPlugin.locked) {
42            return;
43        }
44        //set lock
45        ToDoPlugin.locked = true;
46
47
48        var $spanTodoinnertext = $chk.nextAll("span.todotext:first").find("span.todoinnertext"),
49            param = $chk.data(), // contains: index, pageid, date, strikethrough
50            checked = !$chk.is(':checked');
51
52        // if the data-index attribute is set, this is a call from the page where the todos are defined
53        if (param.index === undefined) param.index = -1;
54
55        if ($spanTodoinnertext.length) {
56
57            /**
58             * Callback function update the todoitem when save request succeed
59             *
60             * @param {Array} data returned by ajax request
61             */
62            var whenCompleted = function (data) {
63                //update date after edit and show alert when needed
64                if (data.date) {
65                    jQuery('input.todocheckbox').data('date', data.date);
66                }
67                if (data.message) {
68                    alert(data.message);
69                }
70                //apply styling, or undo checking checkbox
71                if (data.succeed) {
72                    $chk.prop('checked', checked);
73
74                    if (checked) {
75                        if (param.strikethrough && !$spanTodoinnertext.parent().is("del")) {
76                            $spanTodoinnertext.wrap("<del></del>");
77                        }
78                    } else {
79                        if ($spanTodoinnertext.parent().is("del")) {
80                            $spanTodoinnertext.unwrap();
81                        }
82                    }
83                }
84
85                //release lock
86                ToDoPlugin.locked = false;
87            };
88
89            jQuery.post(
90                DOKU_BASE + 'lib/exe/ajax.php',
91                {
92                    call: 'plugin_todo',
93                    index: param.index,
94                    pageid: param.pageid,
95                    checked: checked ? "1" : "0",
96                    date: param.date
97                },
98                whenCompleted,
99                'json'
100            );
101        } else {
102            alert("Appropriate javascript element not found.\nReverting checkmark.");
103        }
104
105    }
106
107
108};
109
110jQuery(function(){
111
112    // add handler to checkbox
113    jQuery('input.todocheckbox').click(function(e){
114        e.preventDefault();
115        e.stopPropagation();
116
117        var $this = jQuery(this);
118        // undo checking the checkbox
119        $this.prop('checked', !$this.is(':checked'));
120
121        ToDoPlugin.todo($this);
122    });
123
124    // add click handler to todotext spans when marked with 'clickabletodo'
125    jQuery('span.todotext.clickabletodo').click(function(){
126        //Find the checkbox node we need
127        var $chk = jQuery(this).prevAll('input.todocheckbox:first');
128
129        ToDoPlugin.todo($chk);
130    });
131
132});
133