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() || JSINFO['id'];
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        var hasParentsWithDokuWikiContent = function( $nodeWithParents ) {
59
60            if ( $nodeWithParents.is( 'body' ) ) {
61                return false;
62            }
63
64            if ( $nodeWithParents.children().filter('h1,h2,h3,h4,h5,h6,.level1,.level2,.level3,.level4,.level5,.level6').length ) {
65                return true;
66            }
67
68            return hasParentsWithDokuWikiContent( $nodeWithParents.parent() );
69        };
70
71        request(objects, function(){
72            // We need to wrap this once more, because otherwise the edit buttons will have the wrong ranges
73            request({}, function(data){
74
75                var $toRemove = $currentButton.parent(),
76                $tmpWrap = jQuery('<div style="display:none"></div>').html(data);  // temporary wrapper
77
78                while( hasParentsWithDokuWikiContent( $toRemove.parent() ) ) {
79                    $toRemove = $toRemove.parent();
80                }
81
82                $toRemove = $toRemove.children();
83
84                // insert the section highlight wrapper before the last element added to $tmpStore
85                $toRemove.filter(':last').before($tmpWrap);
86                // and remove the elements
87                $toRemove.detach();
88
89                // Now remove the content again
90                $tmpWrap.before($tmpWrap.children().detach());
91                // ...and remove the section highlight wrapper
92                $tmpWrap.detach();
93
94                closeDialog();
95                initJQuery();
96            });
97        });
98    };
99
100    var closeDialog = function() {
101
102            getDialog('close').dialog('destroy').detach();
103
104            // This is being set by the edit.js - needs reset before unloading
105            window.onbeforeunload = '';
106            deleteDraft && deleteDraft();
107            textChanged = false;
108            dw_locktimer.clear();
109    };
110
111    var initJQuery = function() {
112
113        // Remove current edit handler
114        jQuery('.editbutton_section form.btn_secedit').unbind('submit', loadEditDialog);
115        jQuery('script[src]').each(function(){
116            var $xchange = jQuery(this);
117            var $new = jQuery('<script/>').attr('type', $xchange.attr('type')).attr('defer', 'true');
118            $xchange.before($new).detach();
119            $new.attr('src', $xchange.attr('src'));
120        });
121    };
122
123    var getDialog = function(action) {
124
125        if(!jQuery('#sectionedit__dilaog').length){
126            jQuery('body').append('<div id="sectionedit__dilaog" position="absolute" border=1 height="800px" class="dokuwiki page"><div id="sectionedit__dialog_div"></div></div>');
127            jQuery( "#sectionedit__dilaog" ).dialog({title:LANG.plugins.sectionedit['edit'],
128                height: Math.min(700,jQuery(window).height()-50),
129                width: Math.min(700,jQuery(window).width()-50),
130                autoOpen:true,
131                closeOnEscape:false,
132                modal:true,
133                buttons:[
134                    {text:LANG.plugins.sectionedit['closeDialog'],click: closeDialog },
135                    {text:LANG.plugins.sectionedit['save'],click: saveSection},
136                    ],
137                });
138        }
139
140        if ( action ) {
141            return jQuery('#sectionedit__dilaog').dialog(action);
142        }
143
144        return jQuery('#sectionedit__dilaog');
145    };
146
147    jQuery(document).ready(init);
148})(jQuery);
149