xref: /plugin/sectionedit/script.js (revision 463e69e9a67661235800721af72d6e4e2c007d15)
1/**
2 * DokuWiki Plugin TagSections (JavaScript Component)
3 *
4 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author gamma
6 */
7
8(function(jQuery) {
9
10    // Use the internal reference of jQuery.
11    // this is due to jQuery reloading in initJQuery() so that we keep the correct reference
12    var currentNamespace = JSINFO['namespace'];
13    var $currentButton = null;
14    var init = function() {
15        jQuery('.editbutton_section form.btn_secedit').submit(loadEditDialog);
16    };
17
18    var loadEditDialog = function(event){
19
20        $currentButton = jQuery(this);
21        console.log($currentButton);
22        request({'do':'edit'}, showEditDialog);
23        return false;
24    };
25
26    var showEditDialog = function(data) {
27        var $dialog = getDialog('open').html(data);
28        initJQuery();
29        $dialog.find('.editButtons').detach();
30    };
31
32    var request = function(data, success) {
33        data['id']     = $currentButton.find('input[name=id]').val();
34        data['rev']    = $currentButton.find('input[name=rev]').val();
35        data['call']   = 'sectionedit';
36        data['target'] = $currentButton.find('input[name=target]').val();
37        data['range']  = $currentButton.find('input[name=range]').val();
38        return jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', data, success);
39    };
40
41    var saveSection = function() {
42
43        var $form = jQuery(this);
44        var objects = {
45            'do': 'save'
46        };
47
48        $form.find('input[name][value], textarea[name]').each(function(){
49
50            var $element = jQuery(this);
51            if ( this.tagName.toLowerCase() == 'input' && $element.is(':checkbox') && !$element.is(':checked') ) {
52                return;
53            }
54
55            objects[this.name] = $element.val();
56        });
57
58        request(objects, function(){
59            // We need to wrap this once more, because otherwise the edit buttons will have the wrong ranges
60            request({}, function(data){
61                var $toRemove = $currentButton.parent().parent().children(),
62                $tmpWrap = jQuery('<div style="display:none"></div>').html(data);  // temporary wrapper
63
64                // insert the section highlight wrapper before the last element added to $tmpStore
65                $toRemove.filter(':last').before($tmpWrap);
66                // and remove the elements
67                $toRemove.detach();
68
69                // Now remove the content again
70                $tmpWrap.before($tmpWrap.children().detach());
71                // ...and remove the section highlight wrapper
72                $tmpWrap.detach();
73
74                closeDialog();
75                initJQuery();
76            });
77        });
78    };
79
80    var closeDialog = function() {
81
82            getDialog('close').dialog('destroy').detach();
83
84            // This is being set by the edit.js - needs reset before unloading
85            window.onbeforeunload = '';
86            deleteDraft && deleteDraft();
87            textChanged = false;
88            dw_locktimer.clear();
89    };
90
91    var initJQuery = function() {
92
93        // Remove current edit handler
94        jQuery('.editbutton_section form.btn_secedit').unbind('submit', loadEditDialog);
95        jQuery('script[src]').each(function(){
96            var $xchange = jQuery(this);
97            var $new = jQuery('<script/>').attr('type', $xchange.attr('type')).attr('defer', 'true');
98            $xchange.before($new).detach();
99            $new.attr('src', $xchange.attr('src'));
100        });
101    };
102
103    var getDialog = function(action) {
104
105        if(!jQuery('#sectionedit__dilaog').length){
106            jQuery('body').append('<div id="sectionedit__dilaog" position="absolute" border=1 height="800px" class="dokuwiki page"><div id="sectionedit__dialog_div"></div></div>');
107            jQuery( "#sectionedit__dilaog" ).dialog({title:LANG.plugins.sectionedit['edit'],
108                height: Math.min(700,jQuery(window).height()-50),
109                width: Math.min(700,jQuery(window).width()-50),
110                autoOpen:true,
111                closeOnEscape:false,
112                modal:true,
113                buttons:[
114                    {text:LANG.plugins.sectionedit['closeDialog'],click: closeDialog },
115                    {text:LANG.plugins.sectionedit['save'],click: saveSection},
116                    ],
117                });
118        }
119
120        if ( action ) {
121            return jQuery('#sectionedit__dilaog').dialog(action);
122        }
123
124        return jQuery('#sectionedit__dilaog');
125    };
126
127    jQuery(document).ready(init);
128})(jQuery);
129