1/**
2 * Javascript for DokuWiki Plugin snippets
3 * @author Michael Klier <chi@chimeric.de>
4 * @author Myron Turner <turnermm02i@shaw.ca>
5 */
6
7snippets = {
8    keepopen: false,
9    update: false,
10
11    // Attach all events to elements
12    attach: function(obj) {
13        if(!obj) { return; }
14        if(!opener) { return; }
15
16        // add keepopen checkbox
17        var opts = jQuery('#plugin_snippets__opts');
18        if(opts) {
19            var kobox  = document.createElement('input');
20            kobox.type = 'checkbox';
21            kobox.id   = 'snippets__keepopen';
22
23            var updatebox  = document.createElement('input');
24            updatebox.type = 'checkbox';
25            updateid   = 'snippets__update';
26
27            var templ_macros = document.createElement('input');
28            templ_macros.type = 'text';
29            templ_macros.id = 'snippets__macros';
30
31            if(DokuCookie.getValue('snippets_keepopen')){
32                kobox.checked  = true;
33                kobox.defaultChecked = true; //IE wants this
34                snippets.keepopen = true;
35            }
36            jQuery(kobox).bind('click', function(event){
37                snippets.togglekeepopen(this); }
38            );
39
40            var kolbl       = document.createElement('label');
41            kolbl.htmlFor   = 'snippets__keepopen';
42            if(LANG['keepopen']) {
43            kolbl.innerHTML = " " + LANG['keepopen'];
44            }
45            else {
46            kolbl.innerHTML = " " + LANG['plugins']['snippets']['keepopen'];
47            }
48
49            var kobr = document.createElement('br');
50
51            opts.append(kobox);
52            opts.append(kolbl);
53            opts.append(kobr);
54
55            // Code for inserting update request box
56            if(DokuCookie.getValue('snippets_update')){
57                updatebox.checked  = true;
58                updatebox.defaultChecked = true; //IE wants this
59                snippets.update = true;
60            }
61            jQuery(updatebox).bind('click', function(event){
62                snippets.toggleupdate(this); }
63            );
64            var updl       = document.createElement('label');
65            var kobr2 = document.createElement('br');
66            updl.htmlFor   = 'snippets__update';
67            updl.innerHTML = " " + LANG['plugins']['snippets']['check_for_updates'];  //" Check for snippet updates"; // needs language entry
68
69            opts.append(updatebox);
70            opts.append(updl);
71            opts.append(kobr2);
72
73        if(opener.JSINFO['userreplace']) {
74           var kobr3 = document.createElement('br');
75           var macrl       = document.createElement('label');
76           macrl.htmlFor   = 'snippets__macros';
77            macrl.innerHTML ="<b>" + LANG['plugins']['snippets']['user_macros'] + "</b> ";
78           opts.append(macrl) ;
79           opts.append(kobr3);
80           opts.append (templ_macros);
81        }
82
83          templ_macros.value = opener.JSINFO['default_macro_string'] ? opener.JSINFO['default_macro_string'] : "@macro1@,val1;@macro2@,val2;. . .";
84        }
85
86        // attach events
87        links = jQuery(obj).find('a.wikilink1');
88        if(links) {
89            for(var i = 0; i < links.length; i ++) {
90                link = links[i];
91                page = link.title;
92                div  = link.parentNode;
93
94                span = document.createElement('span');
95                span.innerHTML = link.innerHTML;
96                div.removeChild(link);
97                // Exclude template snippets from preview since they have no data at this point
98                if(!link.href.match(/templ_|templ:/)) {
99                preview = document.createElement('a');
100                preview.className = 'plugin_snippets_preview';
101                preview.title = LANG['plugins']['snippets']['preview'];
102                preview.href = page;
103
104                jQuery(preview).bind('click', {'page': page}, function(event) {
105                    snippets.preview(event.data.page);
106                    return false;
107                });
108                div.appendChild(preview);
109                }
110                insert = document.createElement('a');
111                insert.className = 'plugin_snippets_insert';
112                insert.title = LANG['plugins']['snippets']['insert'];
113                insert.href = page;
114
115                jQuery(insert).bind('click', {'page': page}, function(event) {
116                    snippets.insert(event.data.page);
117                    return false;
118                });
119
120                div.appendChild(insert);
121                div.appendChild(span);
122            }
123        }
124
125        // strip out links to non-existing pages
126        links = jQuery(obj).find('a.wikilink2');
127        if(links) {
128            for(var i = 0; i < links.length; i ++) {
129                link = links[i];
130                span = document.createElement('span');
131                span.innerHTML = link.innerHTML;
132                div = link.parentNode;
133                div.removeChild(link);
134                div.appendChild(span);
135            }
136        }
137
138        // add toggle to sub lists
139        lists = jQuery(obj).find('ul');
140        if(lists) {
141            for(var i = 1; i < lists.length; i++) {
142            list = lists[i];
143                list.style.display = 'none';
144                div = list.previousSibling;
145                if(div.nodeType != 1) {
146                    // IE7 and FF treat whitespace different
147                    div = div.previousSibling;
148                }
149                div.className = 'li closed';
150                jQuery(div).bind('click', function(event) { snippets.toggle(this); });
151            }
152        }
153    },
154
155    // toggle open/close state in template list
156    toggle: function(obj) {
157        if(!obj) return;
158        list = obj.nextSibling;
159        if(list.nodeType != 1) {
160            list = list.nextSibling;
161        }
162        if(list.style.display == 'none') {
163            list.style.display = 'block';
164            obj.className = 'li open';
165        } else {
166            list.style.display = 'none';
167            obj.className = 'li closed';
168        }
169        return false;
170    },
171
172    /**
173     * Toggles the keep open state
174     *
175     * @author Andreas Gohr <andi@splitbrain.org>
176     */
177    togglekeepopen: function(cb){
178        if(cb.checked){
179            DokuCookie.setValue('snippets_keepopen',1);
180            snippets.keepopen = true;
181        }else{
182            DokuCookie.setValue('snippets_keepopen','');
183            snippets.keepopen = false;
184        }
185    },
186
187     toggleupdate: function(cb){
188        if(cb.checked){
189            DokuCookie.setValue('snippets_update',1);
190            snippets.update = true;
191        }else{
192            DokuCookie.setValue('snippets_update','');
193            snippets.update = false;
194        }
195    },
196    // perform AJAX preview
197    preview: function(page) {
198        preview = jQuery('#plugin_snippets__preview');
199        if(!preview) return;
200
201        preview.html('<img src="' + DOKU_BASE + 'lib/images/throbber.gif" />');
202
203        jQuery.post(
204            DOKU_BASE+'lib/exe/ajax.php',
205            { call: 'snippet_preview', id: page },
206            function(data){
207                if(data === '') return;
208                preview.html(data);
209            }
210        );
211
212        return false;
213    },
214
215    // perform AJAX insert
216    insert: function(page) {
217        if(!opener) return;
218        var tval = jQuery('#snippets__macros').val();
219        var which = snippets.update ? 'snippet_update' : 'snippet_insert';  // selects whether to insert with or without update request
220        jQuery.post(
221            DOKU_BASE+'lib/exe/ajax.php',
222            { call: which, id: page, curpage: opener.JSINFO['id'],macros: tval },  // curpage is used for updates
223            function(data){
224                opener.insertAtCarret('wiki__text', data, '');
225                if(!snippets.keepopen) {
226                    window.close();
227                }
228                opener.focus();
229            }
230        );
231        return false;
232    }
233};
234
235jQuery(function(){
236    var idx = jQuery('#plugin_snippets__idx');
237    if(idx.length == 0) return;
238    snippets.attach(idx);
239});
240
241function update_snippets(which) {
242  var prune = document.getElementById('snip_prune');
243
244        var debug = false;
245        var params = "update=" +encodeURIComponent(which);
246        params += "&snippet="+ encodeURIComponent(JSINFO['id']);
247        if(prune.checked) params += "&prune=" +encodeURIComponent('prune');
248
249        jQuery.ajax({
250           url: DOKU_BASE + 'lib/plugins/snippets/exe/update.php',
251           async: true,
252           data: params,
253           type: 'POST',
254           dataType: 'html',
255           success: function(data){
256               if(debug) {
257                  alert(data);
258               }
259    }
260    });
261   var span_id = '#' + which.replace(/:/g,'_');  // de-activate links that have been called
262   jQuery(span_id).css({'color':'gray', 'text-decoration':'none', 'cursor': 'initial','pointer-events': 'none'});
263}
264
265jQuery(document).ready(function() {
266 // hide and show update table in footer of snippets
267jQuery( "#snip_updates_but" ).click(function() {
268    if(this.innerHTML.match(/Hide/)) {
269        this.innerHTML="Show Updates Table";
270    } else  this.innerHTML =  "Hide Updates Table";
271    jQuery( "#snippet_update_table" ).toggle();
272});
273jQuery( "#snip_updates_but" ).mouseover(function() {
274      jQuery( "#snip_updates_but" ).css({'cursor': 'pointer', "font-weight": "bolder"});
275 });
276 jQuery( "#snip_updates_but" ).mouseout(function() {
277      jQuery( "#snip_updates_but" ).css({'cursor': 'initial', "font-weight": "initial"});
278 });
279
280if(opener) {
281    if(opener.JSINFO['updatable']) {  // initialize 'on' state of update checkbox in pop-up
282       DokuCookie.setValue('snippets_update',1)
283    }
284    else DokuCookie.setValue('snippets_update','');
285
286     DokuCookie.setValue('qs',opener.JSINFO['id']);
287  }
288
289     jQuery("a.revert").click( function (e) {
290         var _class = jQuery("#yesnosnippet").attr("class");
291         if(_class && _class=="yesnosnippet") {
292             alert(LANG['plugins']['snippets']['restore_off']);
293             return false;
294         }
295       return true;
296   });
297
298});
299
300
301   function snippets_InsertIntOldRev(which) {
302      JSINFO['snippetsInsORev'] = which;
303      DokuCookie.setValue('snippets_old_rev',which) ;
304
305   }
306  if(toolbar){
307     var url = encodeURI('lib/plugins/snippets/exe/snippets.php?ns=');
308     toolbar[toolbar.length] = {"type":"mediapopup", "title": LANG['plugins']['snippets']['title'], "key":"",
309                                "icon": "../../plugins/snippets/images/icon.png",
310                                "url":   url,
311                                'name': 'snippets',
312                                'options' : 'width=800,height=500,left=20,top=20,scrollbars=no,resizable=yes'
313                              };
314  }
315