Completed
Push — master ( 1198d5...234952 )
by Sudar
04:25 queued 03:17
created

$(document).ready   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 3
nop 0
dl 0
loc 46
rs 8.9411
c 1
b 0
f 0
1
/**
2
 * JavaScript for Bulk move Plugin
3
 *
4
 * http://sudarmuthu.com/wordpress/bulk-move
5
 *
6
 * @author: Sudar <http://sudarmuthu.com>
7
 */
8
9
/*jslint browser: true, devel: true*/
10
/*global BULK_MOVE, jQuery, document, postboxes, pagenow, ajaxurl*/
11
jQuery(document).ready(function () {
12
	jQuery( 'button[value="move_tags"], button[value="move_cats"], button[value="move_category_by_tag"], button[value="move_custom_taxonomy"]' ).click( function () {
13
		return confirm( BULK_MOVE.msg.move_warning );
14
	});
15
16
	// Enable toggles for all modules.
17
	postboxes.add_postbox_toggles( pagenow );
18
19
	jQuery( 'tr.taxonomy-select-row, tr.term-select-row, .bm_ct_filters, .bm_ct_submit' ).hide();
20
21
	/**
22
	 * Load Taxonomy on Post Type change.
23
	 */
24
	jQuery( '#smbm_mbct_post_type' ).change( function () {
25
		var selectedOption = jQuery( this ).val(),
26
			data = {
27
				'action'   : BULK_MOVE.bulk_move_posts.action_get_taxonomy,
28
				'nonce'    : BULK_MOVE.bulk_move_posts.nonce,
29
				'post_type': selectedOption
30
			};
31
32
		if ( selectedOption !== '-1' ) {
33
			jQuery.ajaxSetup( { async: false } );
34
			jQuery.post( ajaxurl, data, function( response ) {
35
				jQuery( 'tr.taxonomy-select-row' ).hide();
36
				if ( response.success ) {
37
38
					var taxonomy = response.data.taxonomy || {},
39
						message  = response.data.no_taxonomy_alert_msg;
40
41
					if ( jQuery.isEmptyObject( taxonomy ) ) {
42
						alert( message );
43
					} else {
44
						jQuery( 'tr.taxonomy-select-row' ).show();
45
46
						// Reset options on each AJAX request.
47
						jQuery( '#smbm_mbct_taxonomy' ).children( 'option' ).remove();
48
49
						jQuery( '<option/>', {
50
							'value': '-1',
51
							'text': response.data.default_select_taxonomy_label
52
						}).appendTo( '#smbm_mbct_taxonomy' );
53
54
						jQuery.each( taxonomy, function( index, val ) {
55
							jQuery( '<option/>', {
56
								'value': val,
57
								'text': val
58
							}).appendTo( '#smbm_mbct_taxonomy' );
59
						});
60
					}
61
				}
62
			});
63
		} else {
64
			jQuery( 'tr.taxonomy-select-row' ).hide();
65
			jQuery( 'tr.term-select-row' ).hide();
66
			jQuery( '.bm_ct_filters' ).hide();
67
			jQuery( '.bm_ct_submit' ).hide();
68
		}
69
	});
70
71
	/**
72
	 * Load Term on Taxonomy change.
73
	 */
74
	jQuery( '#smbm_mbct_taxonomy' ).change( function () {
75
76
		//  Selected option.
77
		var selectedOption = jQuery( this ).val(),
78
			// Data to send via AJAX.
79
			data = {
80
				'action'   : BULK_MOVE.bulk_move_posts.action_get_terms,
81
				'nonce'    : BULK_MOVE.bulk_move_posts.nonce,
82
				'taxonomy' : selectedOption
83
			};
84
85
		if ( selectedOption !== '-1' ) {
86
			jQuery.ajaxSetup( { async: false } );
87
			jQuery.post( ajaxurl, data, function( response ) {
88
89
				if ( response.success ) {
90
91
					var term     = response.data.term || {},
92
						message  = response.data.no_term_alert_msg,
93
						termIds  = Object.keys( term )
94
95
					if ( jQuery.isEmptyObject( term ) ) {
96
						alert( message );
97
					} else {
98
						jQuery( 'tr.term-select-row' ).show();
99
						// Reset options on each AJAX request.
100
						jQuery( '#smbm_mbct_selected_term, #smbm_mbct_mapped_term' ).children( 'option' ).remove();
101
102
						jQuery( '<option/>', {
103
							'value': '-1',
104
							'text': response.data.default_select_term_label
105
						}).appendTo( '#smbm_mbct_selected_term' );
106
107
						jQuery( '<option/>', {
108
							'value': '-1',
109
							'text': response.data.default_remove_term_label
110
						}).appendTo( '#smbm_mbct_mapped_term' );
111
112
						jQuery.each( termIds, function( index, val ) {
113
							console.log(  term[ val ]['term_name'] + ':' + term[ val ]['term_count'] );
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
114
115
							jQuery( '<option/>', {
116
								'value': val,
117
								'text': term[ val ]['term_name'] + '(' + term[ val ]['term_count'] + ')'
118
							}).appendTo( '#smbm_mbct_selected_term' );
119
120
							jQuery( '<option/>', {
121
								'value': val,
122
								'text': term[ val ]['term_name'] + '(' + term[ val ]['term_count'] + ')'
123
							}).appendTo( '#smbm_mbct_mapped_term' );
124
						});
125
					}
126
				}
127
			});
128
		} else {
129
			jQuery( 'tr.term-select-row' ).hide();
130
			jQuery( '.bm_ct_filters' ).hide();
131
			jQuery( '.bm_ct_submit' ).hide();
132
		}
133
	});
134
135
	jQuery( '#smbm_mbct_selected_term, #smbm_mbct_mapped_term' ).change( function() {
136
		jQuery( '.bm_ct_filters' ).show();
137
		jQuery( '.bm_ct_submit' ).show();
138
	});
139
140
});
141