1/* DOKUWIKI:include_once vendor/pnotify/jquery.pnotify.js */
2/**
3 * DokuWiki Plugin ajaxedit (JavaScript Component)
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author lisps
7 */
8
9
10var LASTMOD = JSINFO?JSINFO['lastmod']:null;
11jQuery.pnotify.defaults.styling = "jqueryui";
12jQuery.pnotify.defaults.delay = 2000;
13jQuery.pnotify.defaults.history = false;
14
15
16var ajaxedit_queue_ = [];
17var ajaxedit_queue_working = false;
18function ajaxedit_queue(callback) {
19    ajaxedit_queue_.push(callback);
20    if(!ajaxedit_queue_working) {
21        ajaxedit_queue_working = true;
22        ajaexedit_queue_next();
23    }
24}
25function ajaexedit_queue_next(){
26    var c = ajaxedit_queue_.pop();
27    if(c) c();
28    else  ajaxedit_queue_working = false;
29}
30
31function ajaxedit_send_(url,data,fcnSuccess) {
32    data['lastmod']=LASTMOD;
33    jQuery.post(
34		url,
35		data,
36		function(data) {
37            fcnSuccess(data);
38            ajaexedit_queue_next();
39        }
40	);
41}
42
43/**
44 * ajaxedit_send is a wrapper for jQuery's post function
45 * it automatically adds the current pageid, lastmod and the security token
46 *
47 * @param string   plugin plugin name
48 * @param int      idx_tag the id counter
49 * @param function fcnSuccess callback function
50 * @param hash     data additional data
51 */
52function ajaxedit_send(plugin,idx_tag,fcnSuccess,data){
53	data['pageid']=data['pageid']?data['pageid']:JSINFO['id'];
54	data['sectok']=JSINFO['sectok'];
55	data['id']=idx_tag;
56	data['index']=idx_tag;
57
58	var url = DOKU_BASE+'lib/plugins/'+plugin+'/ajax.php';
59    ajaxedit_queue(function(){ajaxedit_send_(url,data,fcnSuccess)});
60
61}
62
63/**
64 * ajaxedit_send is a wrapper for jQuery's post function
65 * it automatically adds the current pageid, lastmod and the security token
66 *
67 * @param string   plugin plugin name
68 * @param int      idx_tag the id counter
69 * @param function fcnSuccess callback function
70 * @param hash     data additional data
71 */
72function ajaxedit_send2(plugin,idx_tag,fcnSuccess,data){
73	data['pageid']=data['pageid']?data['pageid']:JSINFO['id'];
74	data['sectok']=JSINFO['sectok'];
75	data['id']=idx_tag;
76	data['index']=idx_tag;
77	data['call']='plugin_'+plugin;
78	var url = DOKU_BASE+'lib/exe/ajax.php';
79    ajaxedit_queue(function(){ajaxedit_send_(url,data,fcnSuccess)});
80
81}
82
83/**
84 * ajaxedit_parse simply parses the json response using JSON.parse()
85 *
86 * @param json   data
87 * @return mixed false if there is no data/ the response
88 */
89function ajaxedit_parse(data){
90	if(!data) return false;
91	return JSON.parse(data);
92}
93
94/**
95 * ajaxedit_checkResponse checks if the server error flag is set and displays a jQuery Dialog, with the Message
96 *
97 * @param  hash    response the parsed @see ajaxedit_parse server response
98 * @return boolean false on error
99 */
100function ajaxedit_checkResponse(response){
101	if(response.error != 0) {
102		if(jQuery('#ajaxedit__dialog')){
103			jQuery('body').append('<div id="ajaxedit__dialog" position="absolute" border=1 ><div id="ajaxedit__dialog_div"></div></div>');
104			jQuery( "#ajaxedit__dialog" ).dialog({title:'Error',height:300,width:400,autoOpen:true,modal:true});
105		}
106		jQuery('#ajaxedit__dialog_div').html(response.msg);
107		return false;
108	} else if(response.msg){
109
110        jQuery.pnotify({
111            title: false,
112            text: response.msg?response.msg:'gespeichert',
113            type: 'success',
114            icon: false,
115            delay: response.notifyDelay?response.notifyDelay*1000:2000,
116            animate_speed: 100,
117            animation: {
118                effect_in:'bounce',
119                effect_out:'drop',
120            }
121        });
122    }
123	if(response.pageid === JSINFO['id']) LASTMOD = response.lastmod; //refresh LASTMOD
124	return true;
125}
126
127
128function ajaxedit_getIdxByIdClass(id,classname) {
129	var tag_type = jQuery("#"+id).prop('tagName');
130    id = jQuery("#"+id).attr('id');
131	var $els = jQuery(tag_type+"."+classname);
132
133	for(var ii=0, kk=0; ii < $els.length; ii++){
134		if($els[ii].id == id) return kk;
135		kk++;
136	}
137}
138
139function ajaxedit_getIdxByIdClassNodeid(id,classname,nodeid) {
140	var tag_type = jQuery("#"+id).prop('tagName');
141	id = jQuery("#"+id).attr('id');
142    var $els = jQuery('#'+nodeid +" > "+tag_type+"."+classname);
143
144	for(var ii=0; ii<$els.length; ii++){
145		if($els[ii].id == id) {
146			return ii;
147		}
148	}
149}
150
151jQuery(window).on('beforeunload',function(e){
152    if(ajaxedit_queue_working) {
153        return LANG.plugins.ajaxedit.tasks_left.replace('{0}',(ajaxedit_queue_.length *1 +1)) ;
154    }
155});
156