xref: /plugin/sectionedit/script.js (revision c03f47361687328f0b311122e96d7c92c79b69a7)
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
16        jQuery('.editbutton_section form.btn_secedit').submit(function(event){
17
18            $currentButton = jQuery(this);
19            console.log($currentButton);
20            request({'do':'edit'}, showEditDialog);
21            return false;
22        });
23    };
24
25    var showEditDialog = function(data) {
26        var $dialog = getDialog('open').html(data);
27        initJQuery();
28        $dialog.find('.editButtons').detach();
29    };
30
31    var request = function(data, success) {
32        data['id']     = $currentButton.find('input[name=id]').val();
33        data['rev']    = $currentButton.find('input[name=rev]').val();
34        data['call']   = 'sectionedit';
35        data['target'] = $currentButton.find('input[name=target]').val();
36        data['range']  = $currentButton.find('input[name=range]').val();
37        return jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', data, success);
38    };
39
40    var saveSection = function() {
41
42        var $form = jQuery(this);
43        var objects = {
44            'do': 'save'
45        };
46
47        $form.find('input[name][value], textarea[name]').each(function(){
48
49            var $element = jQuery(this);
50            if ( this.tagName.toLowerCase() == 'input' && $element.is(':checkbox') && !$element.is(':checked') ) {
51                return;
52            }
53
54            objects[this.name] = $element.val();
55        });
56
57        request(objects, function(data){
58
59            var $toRemove = $currentButton.parent().parent().children(),
60            $tmpWrap = jQuery('<div style="display:none"></div>').html(data);  // temporary wrapper
61
62            // insert the section highlight wrapper before the last element added to $tmpStore
63            $toRemove.filter(':last').before($tmpWrap);
64            // and remove the elements
65            $toRemove.detach();
66
67            // Now remove the content again
68            $tmpWrap.before($tmpWrap.children().detach());
69            // ...and remove the section highlight wrapper
70            $tmpWrap.detach();
71
72            getDialog('close').dialog('destroy').detach();
73            initJQuery();
74        });
75    };
76
77    var initJQuery = function() {
78
79        jQuery('script[src]').each(function(){
80            var $xchange = jQuery(this);
81            var $new = jQuery('<script/>').attr('type', $xchange.attr('type')).attr('defer', 'true');
82            $xchange.before($new).detach();
83            $new.attr('src', $xchange.attr('src'));
84        });
85    };
86
87    var getDialog = function(action) {
88
89        if(!jQuery('#sectionedit__dilaog').length){
90            jQuery('body').append('<div id="sectionedit__dilaog" position="absolute" border=1 height="800px" class="dokuwiki page"><div id="sectionedit__dialog_div"></div></div>');
91            jQuery( "#sectionedit__dilaog" ).dialog({title:LANG.plugins.sectionedit['edit'],
92                height: Math.min(700,jQuery(window).height()-50),
93                width: Math.min(700,jQuery(window).width()-50),
94                autoOpen:true,
95                closeOnEscape:false,
96                modal:true,
97                buttons:[
98                    {text:LANG.plugins.sectionedit['closeDialog'],click: function() { getDialog('close') }},
99                    {text:LANG.plugins.sectionedit['save'],click: saveSection},
100                    ],
101                });
102        }
103
104        if ( action ) {
105            return jQuery('#sectionedit__dilaog').dialog(action);
106        }
107
108        return jQuery('#sectionedit__dilaog');
109    };
110
111    jQuery(document).ready(init);
112})(jQuery);
113