Completed
Push — master ( d652f3...3704f2 )
by Warwick
04:03
created

assets/js/wetu-importer.js   D

Complexity

Total Complexity 62
Complexity/F 1.55

Size

Lines of Code 288
Function Count 40

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 62
dl 0
loc 288
rs 4.8235
c 1
b 0
f 0
cc 0
nc 1
mnd 4
bc 64
fnc 40
bpm 1.6
cpm 1.55
noi 16

13 Functions

Rating   Name   Duplication   Size   Complexity  
B WETU_IMPORTER.watchSearch 0 35 1
A $(document).ready 0 3 1
B WETU_IMPORTER.importNext 0 50 5
B WETU_IMPORTER.importRow 0 27 1
B WETU_IMPORTER.watchBannerButton 0 25 1
A WETU_IMPORTER.init 0 13 2
A WETU_IMPORTER.watchCheckBoxes 0 16 1
A WETU_IMPORTER.watchImportButton 0 22 1
A WETU_IMPORTER.watchAdvancedSearch 0 16 1
B WETU_IMPORTER.watchConnectButton 0 34 1
A WETU_IMPORTER.watchAddToListButton 0 15 1
A WETU_IMPORTER.myAccommodationSearch 0 19 1
A WETU_IMPORTER.watchClearButton 0 7 1

How to fix   Complexity   

Complexity

Complex classes like assets/js/wetu-importer.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
var WETU_IMPORTER = {
2
3
	init : function() {
4
		if(jQuery('body').hasClass('tour-operator_page_wetu-importer')){
5
			this.myAccommodationSearch();
6
			this.watchSearch();
7
			this.watchAdvancedSearch();
8
			this.watchImportButton();
9
			this.watchAddToListButton();
10
			this.watchClearButton();
11
			this.watchBannerButton();
12
			this.watchConnectButton();
13
            this.watchCheckBoxes();
14
		}
15
	},
16
	myAccommodationSearch: function() {
17
		jQuery('#wetu-importer-search-form').on( 'click', '.search-toggle', function(event) {
18
			event.preventDefault();
19
20
			var keyword = '';
21
			if(jQuery(this).hasClass('published')){
22
                keyword = 'publish';
23
			}else if(jQuery(this).hasClass('pending')){
24
                keyword = 'pending';
25
            }else if(jQuery(this).hasClass('draft')){
26
                keyword = 'draft';
27
            }else if(jQuery(this).hasClass('import')){
28
                keyword = 'import';
29
            }
30
            jQuery(this).parents('#wetu-importer-search-form').find('input.keyword').val(keyword);
31
			jQuery('#wetu-importer-search-form').submit();
32
            jQuery(this).parents('#wetu-importer-search-form').find('input.keyword').val('');
33
		});
34
	},		
35
	watchSearch: function() {
36
		jQuery('#wetu-importer-search-form').on( 'submit', function(event) {
37
			event.preventDefault();
38
39
			jQuery('#posts-filter tbody').html('<tr><td style="text-align:center;" colspan="4">'+jQuery('#wetu-importer-search-form .ajax-loader').html()+'</td></tr>');
40
41
			var type = jQuery('#wetu-importer-search-form').attr('data-type');
42
			var keywords = [];
43
44
			if('' != jQuery('#wetu-importer-search-form input.keyword').val()){
45
				keywords.push(jQuery('#wetu-importer-search-form input.keyword').val());
46
			}
47
48
			if(undefined != jQuery('#wetu-importer-search-form .advanced-search textarea').val()){
49
				var bulk_keywords = jQuery('#wetu-importer-search-form .advanced-search textarea').val().split('\n');
50
				var arrayLength = bulk_keywords.length;
51
				for (var i = 0; i < arrayLength; i++) {
52
				    keywords.push(bulk_keywords[i]);
53
				}				
54
			}		
55
56
			console.log(keywords);	
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...
57
58
			jQuery.post(lsx_tour_importer_params.ajax_url,
0 ignored issues
show
Bug introduced by
The variable lsx_tour_importer_params seems to be never declared. If this is a global, consider adding a /** global: lsx_tour_importer_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
59
	        {
60
	            'action' 	: 			'lsx_tour_importer',
61
	            'type'		: 			type,
62
	            'keyword' 	: 			keywords
63
	        },
64
	        function(response) {
65
	        	jQuery('#posts-filter tbody').html(response);
66
	        });
67
			return false;
68
		});	
69
	},
70
	watchAdvancedSearch: function() {
71
		jQuery('#wetu-importer-search-form .advanced-search-toggle').on( 'click', function(event) {
72
			event.preventDefault();
73
			if(jQuery('#wetu-importer-search-form .advanced-search').hasClass('hidden')){
74
				jQuery('#wetu-importer-search-form .advanced-search').fadeIn('fast').removeClass('hidden');
75
				jQuery('#wetu-importer-search-form .normal-search').hide('fast');
76
				jQuery('#wetu-importer-search-form .normal-search input.keyword').val('');
77
78
			}else{
79
				jQuery('#wetu-importer-search-form .advanced-search').fadeOut('fast').addClass('hidden');
80
				jQuery('#wetu-importer-search-form .advanced-search textarea').val('');
81
				jQuery('#wetu-importer-search-form .normal-search').fadeIn('fast');
82
83
			}
84
		});	
85
	},	
86
	watchClearButton: function() {
87
		jQuery('#posts-filter input.button.clear').on('click',function(event){
88
			event.preventDefault();
89
			jQuery('#posts-filter tbody').html('');	
90
			jQuery('#wetu-importer-search-form input[type="text"]').val('');
91
		});
92
	},	
93
	watchAddToListButton: function() {
94
		jQuery('#posts-filter input.button.add').on('click',function(event){
95
			
96
			event.preventDefault();
97
			jQuery('.import-list-wrapper').fadeIn('fast');	
98
99
			jQuery('#posts-filter tbody tr input:checked').each(function(){
100
		        jQuery('#import-list tbody').append(jQuery(this).parent().parent());
101
			});	
102
103
			jQuery('#import-list tbody tr input:checked').each(function(){
104
				jQuery(this).parent().parent().fadeIn('fast');
105
			});
106
		});
107
	},	
108
109
	importRow: function(args,row) {
110
		var $this = this;
111
		var $row = row;
112
113
	    jQuery.ajax( {
114
	        url : lsx_tour_importer_params.ajax_url,
0 ignored issues
show
Bug introduced by
The variable lsx_tour_importer_params seems to be never declared. If this is a global, consider adding a /** global: lsx_tour_importer_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
115
	        data : args,
116
	        method : 'POST'
117
	    } )
118
        .always( function( data, textStatus, response ) {
0 ignored issues
show
Unused Code introduced by
The parameter response is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
119
			if('none' == jQuery('.completed-list-wrapper').css('display')){
120
				jQuery('.completed-list-wrapper').fadeIn('fast');
121
			}
122
			jQuery('.completed-list-wrapper ul').append(data);
123
        	$row.fadeOut('fast', 
124
	    		function(here){ 
0 ignored issues
show
Unused Code introduced by
The parameter here is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
125
	            	jQuery(this).fadeOut('fast').remove();
126
	        	});
127
        } )	    
128
        .done( function( data ) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
129
        	$this.importNext();
130
        } )
131
        .fail( function( reason ) {
132
            // Handles errors only
133
            console.debug( reason );
134
        } );
135
	},	
136
137
	importNext: function() {
138
		var checkbox = 	jQuery('#import-list tr input.queued:checked:not(.importing):first');
139
140
		if(1 == checkbox.length){
141
			checkbox.addClass('importing');
142
143
			var post_type = jQuery('.post_type').val();
0 ignored issues
show
Unused Code introduced by
The variable post_type seems to be never used. Consider removing it.
Loading history...
144
			var array_import = [];
0 ignored issues
show
Unused Code introduced by
The variable array_import seems to be never used. Consider removing it.
Loading history...
145
			var type = jQuery('#wetu-importer-search-form').attr('data-type');
146
147
			var team_members = [];
148
			if('undefined' != jQuery('#import-list input.team').length){
149
				jQuery('#import-list input.team').each(function(){
150
					if(jQuery(this).attr('checked')){
151
						team_members.push(jQuery(this).val());
152
					}
153
				});
154
			}
155
			var content = [];
156
			if('undefined' != jQuery('#import-list input.content').length){
157
				jQuery('#import-list input.content').each(function(){
158
					if(jQuery(this).attr('checked')){
159
						content.push(jQuery(this).val());
160
					}
161
				});
162
			}	
163
			var safari_brands = [];
164
			if('undefined' != jQuery('#import-list input.accommodation-brand').length){
165
				jQuery('#import-list input.accommodation-brand').each(function(){
166
					if(jQuery(this).attr('checked')){
167
						safari_brands.push(jQuery(this).val());
168
					}
169
				});
170
			}	
171
			
172
			var wetu_id = checkbox.attr('data-identifier');
173
			var post_id = checkbox.val();
174
			var row = checkbox.parents('tr');
175
			var data = {
176
	            'action' 	: 			'lsx_import_items',
177
	            'type'		: 			type,
178
	            'wetu_id' 	: 			wetu_id,
179
	            'post_id'	:			post_id,
180
	            'team_members' : 		team_members,
181
	            'safari_brands' : 		safari_brands,
182
	            'content'	: 			content
183
	        };	
184
			this.importRow(data,row);
185
		}
186
	},
187
	watchImportButton: function() {
188
		var $this = this;
189
190
		jQuery('#import-list input[type="submit"]').on('click',function(event){
191
192
			event.preventDefault();
193
			counter = 0;
0 ignored issues
show
Bug introduced by
The variable counter seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.counter.
Loading history...
194
			var false_click = true;
195
196
			jQuery('#import-list tr input:checked:not(.queued)').each(function(){
197
				jQuery(this).hide().addClass('queued');
198
				jQuery(this).parents('tr').find('.check-column').append(jQuery('#wetu-importer-search-form .ajax-loader-small').html());
199
				false_click = false;
200
			});
201
202
			if(true != false_click){
203
				$this.importNext();
204
			}else{
205
				alert('Make sure you have some items selected.');
206
			}
207
		});
208
	},
209
	watchBannerButton: function() {
210
		jQuery('#banners-filter input.button.download').on('click',function(event){
211
212
			event.preventDefault();
213
			jQuery('#banners-filter tbody tr input:checked').each(function(){
214
				var post_id = jQuery(this).val();
215
				var current_row = jQuery(this).parents('tr');
216
217
				jQuery(this).hide();
218
				jQuery(this).parents('tr').find('.check-column').append(jQuery('#banners-filter .ajax-loader-small').html());
219
220
				jQuery.post(lsx_tour_importer_params.ajax_url,
0 ignored issues
show
Bug introduced by
The variable lsx_tour_importer_params seems to be never declared. If this is a global, consider adding a /** global: lsx_tour_importer_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
221
		        {
222
		            'action' 	: 			'lsx_import_sync_banners',
223
		            'post_id'	:			post_id,
224
		        },
225
		        function(response) {
0 ignored issues
show
Unused Code introduced by
The parameter response is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
226
		        	current_row.fadeOut('fast', 
227
		        	function(here){ 
0 ignored issues
show
Unused Code introduced by
The parameter here is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
228
			            jQuery(this).fadeOut('fast').remove();
229
			        });
230
		        });				
231
			});	
232
		});
233
	},
234
	watchConnectButton: function() {
235
		jQuery('#connect-accommodation-filter input.button.connect').on('click',function(event){
236
237
			event.preventDefault();
238
			jQuery('#connect-accommodation-filter tbody tr input:checked').each(function(){
239
				var post_id = jQuery(this).val();
240
				var type = 'connect_accommodation';
241
				var wetu_id = jQuery(this).attr('data-identifier');
242
243
				var current_row = jQuery(this).parents('tr');
244
245
				jQuery(this).hide();
246
				jQuery(this).parents('tr').find('.check-column').append(jQuery('#connect-accommodation-filter .ajax-loader-small').html());
247
248
				jQuery.post(lsx_tour_importer_params.ajax_url,
0 ignored issues
show
Bug introduced by
The variable lsx_tour_importer_params seems to be never declared. If this is a global, consider adding a /** global: lsx_tour_importer_params */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
249
		        {
250
		            'action' 	: 			'lsx_import_connect_accommodation',
251
		            'post_id'	:			post_id,
252
		            'type'		:			type,
253
		            'wetu_id'	:			wetu_id,
254
		        },
255
		        function(response) {
256
					if('none' == jQuery('.completed-list-wrapper').css('display')){
257
						jQuery('.completed-list-wrapper').fadeIn('fast');
258
					}
259
					jQuery('.completed-list-wrapper ul').append(response);		        	
260
		        	current_row.fadeOut('fast', 
261
		        	function(here){ 
0 ignored issues
show
Unused Code introduced by
The parameter here is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
262
			            jQuery(this).fadeOut('fast').remove();
263
			        });
264
		        });				
265
			});	
266
		});
267
	},
268
    watchCheckBoxes: function() {
269
        jQuery('form#import-list .settings-all input[type="checkbox"]').on('change',function(event){
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
270
            var thisOBJ = jQuery(this);
271
            if('all' === thisOBJ.val()){
272
273
                thisOBJ.parents('form').find('.settings-all input[type="checkbox"]:not([value="all"])').each(function(){
274
275
                	if('checked' === thisOBJ.attr('checked')) {
276
                        jQuery(this).attr('checked', 'checked');
277
                    }else{
278
                        jQuery(this).removeAttr('checked');
279
					}
280
                });
281
			}
282
        });
283
    }
284
285
}
286
jQuery(document).ready( function() {
287
	WETU_IMPORTER.init();
288
});