PluginProbe
Polylang / 2.8.1
Polylang v2.8.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / js / term.js

term.js in Polylang 2.8.1, at js/term.js

208 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * @package Polylang
3 */
4
5 // quick edit
6 (function( $ ) {
7 $( document ).bind(
8 'DOMNodeInserted',
9 function( e ) {
10 var t = $( e.target );
11
12 // WP inserts the quick edit from
13 if ( 'inline-edit' == t.attr( 'id' ) ) {
14 var term_id = t.prev().attr( 'id' ).replace( "tag-", "" );
15
16 if ( term_id > 0 ) {
17 // language dropdown
18 var select = t.find( ':input[name="inline_lang_choice"]' );
19 var lang = $( '#lang_' + term_id ).html();
20 select.val( lang ); // populates the dropdown
21
22 // disable the language dropdown for default categories
23 var default_cat = $( '#default_cat_' + term_id ).html();
24 if ( term_id == default_cat ) {
25 select.prop( 'disabled', true );
26 }
27 }
28 }
29 }
30 );
31 })( jQuery );
32
33
34 // update rows of translated terms when adding / deleting a translation or when the language is modified in quick edit
35 // acts on ajaxSuccess event
36 (function( $ ) {
37 $( document ).ajaxSuccess(
38 function( event, xhr, settings ) {
39 function update_rows( term_id ) {
40 // collect old translations
41 var translations = new Array();
42 $( '.translation_' + term_id ).each(
43 function() {
44 translations.push( $( this ).parent().parent().attr( 'id' ).substring( 4 ) );
45 }
46 );
47
48 var data = {
49 action: 'pll_update_term_rows',
50 term_id: term_id,
51 translations: translations.join( ',' ),
52 taxonomy: $( "input[name='taxonomy']" ).val(),
53 post_type: $( "input[name='post_type']" ).val(),
54 screen: $( "input[name='screen']" ).val(),
55 _pll_nonce: $( '#_pll_nonce' ).val()
56 };
57
58 // get the modified rows in ajax and update them
59 $.post(
60 ajaxurl,
61 data,
62 function( response ) {
63 if ( response ) {
64 var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
65 $.each(
66 res.responses,
67 function() {
68 if ( 'row' == this.what ) {
69 $( "#tag-" + this.supplemental.term_id ).replaceWith( this.data );
70 }
71 }
72 );
73 }
74 }
75 );
76 }
77
78 var data = wpAjax.unserialize( settings.data ); // what were the data sent by the ajax request?
79 if ( 'undefined' != typeof( data['action'] ) ) {
80 switch ( data['action'] ) {
81 // when adding a term, the new term_id is in the ajax response
82 case 'add-tag':
83 res = wpAjax.parseAjaxResponse( xhr.responseXML, 'ajax-response' );
84 $.each(
85 res.responses,
86 function() {
87 if ( 'term' == this.what ) {
88 update_rows( this.supplemental.term_id );
89 }
90 }
91 );
92
93 // and also reset translations hidden input fields
94 $( '.htr_lang' ).val( 0 );
95 break;
96
97 // when deleting a term
98 case 'delete-tag':
99 update_rows( data['tag_ID'] );
100 break;
101
102 // in case the language is modified in quick edit and breaks translations
103 case 'inline-save-tax':
104 update_rows( data['tax_ID'] );
105 break;
106 }
107 }
108 }
109 );
110 })( jQuery );
111
112 jQuery( document ).ready(
113 function( $ ) {
114 // translations autocomplete input box
115 function init_translations() {
116 $( '.tr_lang' ).each(
117 function(){
118 var tr_lang = $( this ).attr( 'id' ).substring( 8 );
119 var td = $( this ).parent().parent().siblings( '.pll-edit-column' );
120
121 $( this ).autocomplete(
122 {
123 minLength: 0,
124 source: ajaxurl + '?action=pll_terms_not_translated' +
125 '&term_language=' + $( '#term_lang_choice' ).val() +
126 '&term_id=' + $( "input[name='tag_ID']" ).val() +
127 '&taxonomy=' + $( "input[name='taxonomy']" ).val() +
128 '&translation_language=' + tr_lang +
129 '&post_type=' + typenow +
130 '&_pll_nonce=' + $( '#_pll_nonce' ).val(),
131 select: function( event, ui ) {
132 $( '#htr_lang_' + tr_lang ).val( ui.item.id );
133 // ui.item.link is built and come from server side and is well escaped when necessary
134 td.html( ui.item.link ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
135 },
136 }
137 );
138
139 // when the input box is emptied
140 $( this ).blur(
141 function() {
142 if ( ! $( this ).val() ) {
143 $( '#htr_lang_' + tr_lang ).val( 0 );
144 // Value is retrieved from HTML already generated server side
145 td.html( td.siblings( '.hidden' ).children().clone() ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
146 }
147 }
148 );
149 }
150 );
151 }
152
153 init_translations();
154
155 // ajax for changing the term's language
156 $( '#term_lang_choice' ).change(
157 function() {
158 var value = $( this ).val();
159 var lang = $( this ).children( 'option[value="' + value + '"]' ).attr( 'lang' );
160 var dir = $( '.pll-translation-column > span[lang="' + lang + '"]' ).attr( 'dir' );
161
162 var data = {
163 action: 'term_lang_choice',
164 lang: value,
165 from_tag: $( "input[name='from_tag']" ).val(),
166 term_id: $( "input[name='tag_ID']" ).val(),
167 taxonomy: $( "input[name='taxonomy']" ).val(),
168 post_type: typenow,
169 _pll_nonce: $( '#_pll_nonce' ).val()
170 };
171
172 $.post(
173 ajaxurl,
174 data,
175 function( response ) {
176 var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
177 $.each(
178 res.responses,
179 function() {
180 switch ( this.what ) {
181 case 'translations': // translations fields
182 // Data is built and come from server side and is well escaped when necessary
183 $( "#term-translations" ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
184 init_translations();
185 break;
186 case 'parent': // parent dropdown list for hierarchical taxonomies
187 $( '#parent' ).replaceWith( this.data );
188 break;
189 case 'tag_cloud': // popular items
190 $( '.tagcloud' ).replaceWith( this.data );
191 break;
192 case 'flag': // flag in front of the select dropdown
193 // Data is built and come from server side and is well escaped when necessary
194 $( '.pll-select-flag' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
195 break;
196 }
197 }
198 );
199
200 // Modifies the text direction
201 $( 'body' ).removeClass( 'pll-dir-rtl' ).removeClass( 'pll-dir-ltr' ).addClass( 'pll-dir-' + dir );
202 }
203 );
204 }
205 );
206 }
207 );
208