xref: /plugin/sectionedit/script.js (revision 7809e56aee3694dbd6fc1b3c267a6ddea7796dcb)
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
74            // This is being set by the edit.js - needs reset before unloading
75            window.onbeforeunload = '';
76            textChanged = false;
77            dw_locktimer.clear();
78
79            initJQuery();
80        });
81    };
82
83    var initJQuery = function() {
84
85        jQuery('script[src]').each(function(){
86            var $xchange = jQuery(this);
87            var $new = jQuery('<script/>').attr('type', $xchange.attr('type')).attr('defer', 'true');
88            $xchange.before($new).detach();
89            $new.attr('src', $xchange.attr('src'));
90        });
91    };
92
93    var getDialog = function(action) {
94
95        if(!jQuery('#sectionedit__dilaog').length){
96            jQuery('body').append('<div id="sectionedit__dilaog" position="absolute" border=1 height="800px" class="dokuwiki page"><div id="sectionedit__dialog_div"></div></div>');
97            jQuery( "#sectionedit__dilaog" ).dialog({title:LANG.plugins.sectionedit['edit'],
98                height: Math.min(700,jQuery(window).height()-50),
99                width: Math.min(700,jQuery(window).width()-50),
100                autoOpen:true,
101                closeOnEscape:false,
102                modal:true,
103                buttons:[
104                    {text:LANG.plugins.sectionedit['closeDialog'],click: function() { getDialog('close') }},
105                    {text:LANG.plugins.sectionedit['save'],click: saveSection},
106                    ],
107                });
108        }
109
110        if ( action ) {
111            return jQuery('#sectionedit__dilaog').dialog(action);
112        }
113
114        return jQuery('#sectionedit__dilaog');
115    };
116
117    jQuery(document).ready(init);
118})(jQuery);
119