1/* DOKUWIKI:include jquery.form.js */
2
3jQuery(document).ready(function() {
4
5	var COOKIE_DESCRIPTION_NAME = 'DW_Admin_ConfManager_showDescription';
6	var COOKIE_DEFAULTS_NAME = 'DW_Admin_ConfManager_showDefaults';
7
8	var ICON_BASE_URL=DOKU_BASE+'lib/plugins/confmanager/icons/';
9	var collapse_icon = ICON_BASE_URL+'collapse.png';
10	var expand_icon = ICON_BASE_URL+'expand.png';
11
12	var readCookie = function(cookieKey) {
13		let ARRcookies=document.cookie.split(";");
14		for (let i=0;i<ARRcookies.length;i++) {
15			let key = ARRcookies[i].substring(0,ARRcookies[i].indexOf("=")+1);
16			let value = ARRcookies[i].substring(ARRcookies[i].indexOf("=")+1);
17			key = key.replace(/^\s+|\s+$/g,"");
18			if (key === cookieKey) {
19				return decodeURIComponent(value);
20			}
21		}
22	};
23
24	var setCookie = function(key, value) {
25		let expirationDate = new Date();
26		expirationDate.setDate(expirationDate.getDate()+365);
27		document.cookie = encodeURIComponent(key) + '=' + encodeURIComponent(value) + '; expires=' + expirationDate.toUTCString();
28	};
29
30	/*
31	 * defaultValue is a boolean value the function should return if the cookie is not set (undefined).
32	 * returns false if cookie is undefined and defaultValue not specified
33	 */
34	var getBooleanFromCookie = function(key, defaultValue) {
35        let cookie = readCookie(key);
36        if(cookie === null || cookie === undefined) {
37			return defaultValue === undefined ? false : defaultValue;
38		}
39		return cookie === 'true';
40	};
41
42	var setDescriptionVisible = function(show) {
43		if(show) {
44			jQuery('#description').show();
45			jQuery('#description_toggle_button').attr('src', collapse_icon);
46		} else {
47			jQuery('#description').hide();
48			jQuery('#description_toggle_button').attr('src', expand_icon);
49		}
50	};
51
52	var setDefaultsVisible = function(show) {
53		if(show) {
54			jQuery('.defaults').show();
55			jQuery('#defaults_toggle_button').attr('src', collapse_icon);
56		} else {
57			jQuery('.defaults').hide();
58			jQuery('#defaults_toggle_button').attr('src', expand_icon);
59		}
60	};
61
62	var showDescription = getBooleanFromCookie(COOKIE_DESCRIPTION_NAME, true);
63	var showDefaults = getBooleanFromCookie(COOKIE_DEFAULTS_NAME, true);
64
65	setDescriptionVisible(showDescription);
66	setDefaultsVisible(showDefaults);
67
68	jQuery('#toggleDescription').on('click', function() {
69		showDescription = !showDescription;
70		setDescriptionVisible(showDescription);
71		setCookie(COOKIE_DESCRIPTION_NAME, showDescription);
72		return false;
73	});
74
75	jQuery('#toggleDefaults').on('click', function() {
76		showDefaults = !showDefaults;
77		setDefaultsVisible(showDefaults);
78		setCookie(COOKIE_DEFAULTS_NAME, showDefaults);
79		return false;
80	});
81});
82
83jQuery(document).ready(function() {
84
85	var isInputValid = function() {
86        let result = true;
87		jQuery('.newItem').each(function(){
88			let inputString = jQuery(this).val();
89			if(inputString == null || inputString === '') {
90				result = false;
91			}
92		});
93		return result;
94	};
95
96	var submitForm = function(id) {
97		document.forms[id].submit();
98	};
99
100	jQuery('.deleteButton').on('click', function(nr) {
101		jQuery(this).parent().parent().remove();
102		jQuery('.newItem').each(function(){
103			jQuery(this).val('');
104		});
105		submitForm('configForm');
106	});
107
108    //default value cannot be deleted, but disabled in local config
109    jQuery('.disableButton').on('click', function(nr) {
110        let $row = jQuery(this).parent().parent();
111        let defaultKey = $row.find('.default_key').text();
112
113        let $newItems = jQuery('.newItem');
114        $newItems.first().each(function(){
115            //single value entries negate with !, key-value entries with empty value
116            let prefix = $newItems.length === 1 ? '!' : '';
117            jQuery(this).val(prefix + defaultKey);
118        });
119        submitForm('configForm');
120    });
121
122	jQuery('#confmanager__config__files').on('change', function(){
123		submitForm('select_config_form');
124	});
125
126	jQuery('.submitOnTab').on('keydown', function(event){
127		if(event.which !== 9) {
128			return true;
129		}
130		if(!isInputValid()) {
131			return true;
132		}
133		submitForm('configForm');
134	});
135});
136
137jQuery(document).ready(function(){
138	jQuery('.newItem').first().focus();
139});
140
141jQuery(document).ready(function(){
142
143	var popupVisible = false;
144
145	var getEntryKey = function(element) {
146        let parent = jQuery(element).parent().parent().children().first();
147        let input = jQuery(parent).children('input').first();
148        let value = jQuery(input).attr('value');
149		return value;
150	};
151    var getEntryValue = function(element) {
152        let parent = jQuery(element).parent().parent().children().first().next();
153        let input = jQuery(parent).children('input').first();
154        let value = jQuery(input).attr('value');
155        return value;
156    };
157
158	var unloadPopup = function() {
159		jQuery('.popup_mask').hide();
160		jQuery('.popup').hide();
161		jQuery('#keyParam').removeAttr('value');
162		jQuery('#configIdParam').removeAttr('value');
163		popupVisible = false;
164	};
165
166	var validate = function() {
167		let file = jQuery('#file_upload_input').val();
168		if(file === '' || file === null || file === undefined) {
169			return false;
170		}
171		jQuery('#popup_select_file').hide();
172		jQuery('#popup_show_progress').show();
173		jQuery('.popup').css('cursor', 'wait');
174		return true;
175	};
176
177	var submitOk = function() {
178		jQuery('#popup_show_progress').hide();
179		jQuery('#popup_success').show();
180		jQuery('.popup').css('cursor', 'default');
181	};
182
183	var onError = function(context) {
184
185		jQuery('#popup_show_progress').hide();
186		jQuery('#popup_error').show();
187        jQuery('<p>'+context.responseText+'</p>').insertAfter('#popup_error h3');
188		jQuery('.popup').css('cursor', 'default');
189	};
190
191	var showPopup = function() {
192        let $popup = jQuery('.popup');
193		let width = $popup.width();
194		let height = $popup.height();
195        $popup.css('left', jQuery(window).width() / 2 - width / 2);
196        $popup.css('top', jQuery(window).height() / 2 - height / 2);
197        $popup.show();
198		popupVisible = true;
199	};
200
201	var showPopupMask = function() {
202		let $popupmask = jQuery('.popup_mask');
203        $popupmask.css('width', jQuery(window).width());
204        $popupmask.css('height', jQuery(window).height());
205        $popupmask.show();
206	};
207
208	var options = {
209		beforeSubmit : validate,
210		success : submitOk,
211		error : onError
212	};
213	jQuery('#fileuploadform').ajaxForm(options);
214
215	jQuery('.upload_image_button').on('click', function(){
216		let key = getEntryKey(this);
217        let value = getEntryValue(this);
218		jQuery('#keyParam').val(key);
219        jQuery('#valueParam').val(value);
220        jQuery('#configIdParam').val(JSINFO.configId);
221		showPopup();
222		showPopupMask();
223	});
224
225	var delete_image_success = function() {
226		document.forms['configForm'].submit();
227	};
228
229	var delete_image_failed = function(jqXHR, textStatus, errorThrown) {
230		alert(errorThrown);
231	};
232
233	jQuery('.delete_image_button').on('click', function() {
234		jQuery.ajax({
235			url : DOKU_BASE + 'lib/exe/ajax.php',
236			type : 'POST',
237			data : {
238				call : 'confmanager_deleteIcon',
239				configId : JSINFO.configId,
240				key : getEntryKey(this)
241			},
242			success : delete_image_success,
243			error : delete_image_failed
244		});
245	});
246
247	jQuery('#popup_cancel').on('click',function() {
248		unloadPopup();
249		return false;
250	});
251
252	jQuery(window).on('resize', function() {
253		if(!popupVisible) {
254			return true;
255		}
256		showPopup();
257		showPopupMask();
258	});
259
260	jQuery('.continue').attr('href', window.location);
261});
262