1/**
2 * DokuWiki Plugin TagAdd (JavaScript Component)
3 *
4 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author lisps
6 */
7
8/**
9 * Set when a request is active
10 **/
11var TAGADD__loadActive = false;
12
13/**
14 * Submit the selected Tags
15 **/
16function tagadd__ajax_submitFormTags()
17{
18      ajaxedit_send2('tagadd',false,tagadd__submitFormTagsDone,{
19          form:jQuery('#tagadd__form').serializeArray(),
20          action:'saveTags',
21      });
22}
23
24/**
25 * Callback from tagadd__ajax_submitFormTags
26 * check ajax response and close dialog
27 *
28 * @param json data jQuery ajax requested data
29 **/
30function tagadd__submitFormTagsDone(data){
31    ret = ajaxedit_parse(data);
32    if(ajaxedit_checkResponse(ret)) {
33        jQuery("#tagadd__dialog").dialog("close");
34        if(jQuery("div.tags span").length){
35            jQuery("div.tags span").html(ret.links);
36        }
37        else {
38            jQuery('div.page').append('<div class="tags"><span>'+ret.links+'</span></div>');
39        }
40    }
41}
42
43/**
44 * Create dialog if not exists and request the tags for the given namespace
45 *
46 * @param string ns namespace
47 **/
48function tagadd__loadForm(ns){
49	if(!(JSINFO && JSINFO['acl_write'] === '1')) return;
50
51    if (TAGADD__loadActive) return ;
52    TAGADD__loadActive = true;
53
54    if(!jQuery('#tagadd__dialog').length){
55        jQuery('body').append('<div id="tagadd__dialog" position="absolute" border=1 height="800px"><div id="tagadd__dialog_div"></div></div>');
56        jQuery( "#tagadd__dialog" ).dialog({title:LANG.plugins.tagadd['choose tags'],
57            height:600,
58            width: Math.min(700,jQuery(window).width()-50),
59            autoOpen:true,
60            buttons:[
61                {text:LANG.plugins.tagadd['closeDialog'],click: function() {jQuery(this).dialog('close');}},
62                {text:LANG.plugins.tagadd['save'],click: function() {tagadd__ajax_submitFormTags();}},
63                ],
64            });
65    }
66    jQuery('#tagadd__dialog').addClass('loading');
67    ajaxedit_send2('tagadd',false,tagadd__submitLoadFormDone,{
68        action:'loadForm',
69        ns:ns,
70        from:jQuery('#tagadd__form').serializeArray(),
71    });
72
73}
74
75/**
76 * Callback from tagadd__loadForm
77 * opens dialog
78 * @param json data jquery data
79 **/
80function tagadd__submitLoadFormDone(data){
81    ret = ajaxedit_parse(data);
82    if(ajaxedit_checkResponse(ret)) {
83        jQuery("#tagadd__dialog_div").empty();
84        jQuery("#tagadd__dialog_div").html(ret.form);
85
86        jQuery("#tagadd__dialog").dialog("open");
87        jQuery("#tagadd__accordion").accordion({heightStyle: 'content',collapsible:true});
88        //jQuery("#tagadd__accordion").accordion('activate',false);
89    }
90    TAGADD__loadActive = false;
91    jQuery('#tagadd__dialog').removeClass('loading');
92}
93
94//add Shortcut
95jQuery(document).ready(function() {
96    if(JSINFO && JSINFO['act'] === 'show') {
97        jQuery('li.plugin_tagadd__addtags').click(function(){
98            tagadd__loadForm(JSINFO['currentNamespace']);
99            return false;
100        });
101
102        jQuery(document).keypress(function(e) {
103        	var code = (e.keyCode ? e.keyCode : e.which) + '';
104
105        	var conf_code = JSINFO['tagadd_keyCode']; //array
106        	var conf_ctrl = JSINFO['tagadd_ctrlKey'];
107        	var conf_alt  = JSINFO['tagadd_altKey'];
108
109        	var ctrlKey = conf_ctrl ? e.ctrlKey : 1;
110        	var altKey  = conf_alt  ? e.altKey  : 1;
111
112        	var keyCode = (jQuery.inArray(code,conf_code) > -1);
113            if (ctrlKey && altKey && keyCode) {
114            	tagadd__loadForm(JSINFO['currentNamespace']);
115            }
116        });
117    }
118});
119