menus-sync.js
5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var WPML_core = WPML_core || {};
(function () {
"use strict";
jQuery(function () {
jQuery("#icl_msync_cancel").click(function () {
location.href = WPML_core.sanitize(location.href).replace(/#(.)$/, '');
});
var icl_msync_confirm = jQuery('#icl_msync_confirm');
var check_all = icl_msync_confirm.find('thead :checkbox');
//Remove already assigned events: that's what makes that this slow!
check_all.off('click');
check_all.off('change');
check_all.on('change', function () {
var on = jQuery(this).prop('checked');
var checkboxes = icl_msync_confirm.find('tbody :checkbox');
if (on) {
checkboxes.each( function( i, el ) {
jQuery( el ).prop( 'checked', 'checked' );
});
jQuery( '#icl_msync_submit' ).prop( 'disabled', false );
} else {
checkboxes.each( function( i, el ) {
jQuery( el ).removeProp( 'checked' );
});
jQuery( '#icl_msync_submit' ).prop( 'disabled', true );
}
});
icl_msync_confirm.find('tbody :checkbox').on('change', function () {
if (jQuery(this).attr('readonly') == 'readonly') {
jQuery(this).prop('checked', !jQuery(this).prop('checked'));
}
var checked_items = icl_msync_confirm.find('tbody :checkbox:checked');
var checked_count = checked_items.length;
jQuery('#icl_msync_submit').prop('disabled', !checked_count);
if (checked_count && checked_items.length == icl_msync_confirm.find('tbody :checkbox').length) {
jQuery('#icl_msync_confirm').find('thead :checkbox').prop('checked', true);
} else {
jQuery('#icl_msync').find('thead :checkbox').prop('checked', false);
}
WPML_core.icl_msync_validation();
});
jQuery('#icl_msync_submit').on('click', function () {
jQuery(this).prop('disabled', true);
var total_menus = jQuery('input[name^=sync]:checked').length;
var spinner = jQuery('<span class="spinner"></span>');
jQuery('#icl_msync_message').before(spinner);
spinner.css({display: 'inline-block',
float: 'none',
'visibility': 'visible'
});
WPML_core.sync_menus(total_menus);
});
var max_vars_warning = jQuery('#icl_msync_max_input_vars');
if (max_vars_warning.length) {
var menu_sync_check_box_count = jQuery('input[name^=sync]').length;
var max_vars_extra = 10; // Allow for a few other items as well. eg. nonce, etc
if (menu_sync_check_box_count + max_vars_extra > max_vars_warning.data('max_input_vars')) {
var warning_text = max_vars_warning.html();
warning_text = warning_text.replace('!NUM!', menu_sync_check_box_count + max_vars_extra);
max_vars_warning.html(warning_text);
max_vars_warning.show();
}
}
});
WPML_core.icl_msync_validation = function () {
jQuery('#icl_msync_confirm').find('tbody :checkbox').each(function () {
var mnthis = jQuery(this);
mnthis.prop('readonly', false);
if (jQuery(this).attr('name') == 'menu_translation[]') {
var spl = jQuery(this).val().split('#');
var menu_id = spl[0];
jQuery('#icl_msync_confirm').find('tbody :checkbox').each(function () {
if (jQuery(this).val().search('newfrom-' + menu_id + '-') == 0 && jQuery(this).prop('checked')) {
mnthis.prop('checked', true);
mnthis.prop('readonly', true);
}
});
}
});
};
WPML_core.sync_menus = function (total_menus) {
var message;
var data = 'action=icl_msync_confirm';
data += '&_icl_nonce_menu_sync=' + jQuery('#_icl_nonce_menu_sync').val();
var number_to_send = 50;
var menus = jQuery('input[name^=sync]:checked:not(:disabled)');
var icl_msync_message = jQuery('#icl_msync_message');
if (menus.length) {
for (var i = 0; i < Math.min(number_to_send, menus.length); i++) {
data += '&' + jQuery(menus[i]).serialize();
jQuery(menus[i]).prop('disabled', true);
}
message = jQuery('#icl_msync_submit').data('message');
message = message.replace('%1', total_menus - menus.length);
message = message.replace('%2', total_menus);
icl_msync_message.text(message);
jQuery.ajax({
url: ajaxurl,
type: "POST",
data: data,
success: function (response) {
if (response.success) {
WPML_core.sync_menus(total_menus);
}
}
});
} else {
icl_msync_message.hide();
message = jQuery('#icl_msync_submit').data('message-complete');
icl_msync_message.text(message);
jQuery('.spinner').remove();
jQuery('#icl_msync_cancel').fadeOut();
icl_msync_message.fadeIn('slow');
jQuery.ajax({
url: ajaxurl,
data: {
'action': 'wpml_get_links_for_menu_strings_translation',
'_nonce': menus_sync.menusSyncNonce,
},
success: function (response) {
if (response.success && response.data.items) {
var element = jQuery('<p></p>');
element.text(menus_sync.text1);
element.append('<br>' + menus_sync.text2 + ' ' );
var items = 0;
for (var key in response.data.items) {
if (response.data.items.hasOwnProperty(key)) {
if(items>0) {
element.append(', ');
}
var link = jQuery('<a></a>');
link.attr('href', response.data.items[key]);
link.text(key);
link.appendTo(element);
items++;
}
}
element.append( '<br>' + menus_sync.text3);
element.appendTo(jQuery('#icl_msync_confirm_form'));
}
},
error: function (jqXHR, status, error) {
var parsed_response = jqXHR.statusText || status || error;
alert(parsed_response);
},
});
}
};
}());