Completed
Push — master ( 4f6f4e...0094c4 )
by Warwick
02:24
created

WETU_IMPORTER.watchTourUpdate   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
            this.watchTourUpdate();
15
		}
16
	},
17
	myAccommodationSearch: function() {
18
		jQuery('#wetu-importer-search-form').on( 'click', '.search-toggle', function(event) {
19
			event.preventDefault();
20
21
			var keyword = '';
22
			if(jQuery(this).hasClass('published')){
23
                keyword = 'publish';
24
			}else if(jQuery(this).hasClass('pending')){
25
                keyword = 'pending';
26
            }else if(jQuery(this).hasClass('draft')){
27
                keyword = 'draft';
28
            }else if(jQuery(this).hasClass('import')){
29
                keyword = 'import';
30
            }
31
            jQuery(this).parents('#wetu-importer-search-form').find('input.keyword').val(keyword);
32
			jQuery('#wetu-importer-search-form').submit();
33
            jQuery(this).parents('#wetu-importer-search-form').find('input.keyword').val('');
34
		});
35
	},		
36
	watchSearch: function() {
37
		jQuery('#wetu-importer-search-form').on( 'submit', function(event) {
38
			event.preventDefault();
39
40
			jQuery('#posts-filter tbody').html('<tr><td style="text-align:center;" colspan="4">'+jQuery('#wetu-importer-search-form .ajax-loader').html()+'</td></tr>');
41
42
			var type = jQuery('#wetu-importer-search-form').attr('data-type');
43
			var keywords = [];
44
45
			if('' != jQuery('#wetu-importer-search-form input.keyword').val()){
46
				keywords.push(jQuery('#wetu-importer-search-form input.keyword').val());
47
			}
48
49
			if(undefined != jQuery('#wetu-importer-search-form .advanced-search textarea').val()){
50
				var bulk_keywords = jQuery('#wetu-importer-search-form .advanced-search textarea').val().split('\n');
51
				var arrayLength = bulk_keywords.length;
52
				for (var i = 0; i < arrayLength; i++) {
53
				    keywords.push(bulk_keywords[i]);
54
				}				
55
			}		
56
57
			console.log(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...
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
58
59
			jQuery.post(lsx_tour_importer_params.ajax_url,
60
	        {
61
	            'action' 	: 			'lsx_tour_importer',
62
	            'type'		: 			type,
63
	            'keyword' 	: 			keywords
64
	        },
65
	        function(response) {
66
	        	jQuery('#posts-filter tbody').html(response);
67
	        });
68
			return false;
69
		});	
70
	},
71
	watchAdvancedSearch: function() {
72
		jQuery('#wetu-importer-search-form .advanced-search-toggle').on( 'click', function(event) {
73
			event.preventDefault();
74
			if(jQuery('#wetu-importer-search-form .advanced-search').hasClass('hidden')){
75
				jQuery('#wetu-importer-search-form .advanced-search').fadeIn('fast').removeClass('hidden');
76
				jQuery('#wetu-importer-search-form .normal-search').hide('fast');
77
				jQuery('#wetu-importer-search-form .normal-search input.keyword').val('');
78
79
			}else{
80
				jQuery('#wetu-importer-search-form .advanced-search').fadeOut('fast').addClass('hidden');
81
				jQuery('#wetu-importer-search-form .advanced-search textarea').val('');
82
				jQuery('#wetu-importer-search-form .normal-search').fadeIn('fast');
83
84
			}
85
		});	
86
	},	
87
	watchClearButton: function() {
88
		jQuery('#posts-filter input.button.clear').on('click',function(event){
89
			event.preventDefault();
90
			jQuery('#posts-filter tbody').html('');	
91
			jQuery('#wetu-importer-search-form input[type="text"]').val('');
92
		});
93
	},	
94
	watchAddToListButton: function() {
95
		jQuery('#posts-filter input.button.add').on('click',function(event){
96
			
97
			event.preventDefault();
98
			jQuery('.import-list-wrapper').fadeIn('fast');	
99
100
			jQuery('#posts-filter tbody tr input:checked').each(function(){
101
		        jQuery('#import-list tbody').append(jQuery(this).parent().parent());
102
			});	
103
104
			jQuery('#import-list tbody tr input:checked').each(function(){
105
				jQuery(this).parent().parent().fadeIn('fast');
106
			});
107
		});
108
	},	
109
110
	importRow: function(args,row) {
111
		var $this = this;
112
		var $row = row;
113
114
	    jQuery.ajax( {
115
	        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...
116
	        data : args,
117
	        method : 'POST'
118
	    } )
119
        .always( function( data, textStatus, response ) {
0 ignored issues
show
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...
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...
120
			if('none' == jQuery('.completed-list-wrapper').css('display')){
121
				jQuery('.completed-list-wrapper').fadeIn('fast');
122
			}
123
			jQuery('.completed-list-wrapper ul').append(data);
124
        	$row.fadeOut('fast', 
125
	    		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...
126
	            	jQuery(this).fadeOut('fast').remove();
127
	        	});
128
        } )	    
129
        .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...
130
        	$this.importNext();
131
        } )
132
        .fail( function( reason ) {
133
            // Handles errors only
134
            console.debug( reason );
135
        } );
136
	},	
137
138
	importNext: function() {
139
		var checkbox = 	jQuery('#import-list tr input.queued:checked:not(.importing):first');
140
141
		if(1 == checkbox.length){
142
			checkbox.addClass('importing');
143
144
			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...
145
			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...
146
			var type = jQuery('#wetu-importer-search-form').attr('data-type');
147
148
			var team_members = [];
149
			if('undefined' != jQuery('#import-list input.team').length){
150
				jQuery('#import-list input.team').each(function(){
151
					if(jQuery(this).attr('checked')){
152
						team_members.push(jQuery(this).val());
153
					}
154
				});
155
			}
156
			var content = [];
157
			if('undefined' != jQuery('#import-list input.content').length){
158
				jQuery('#import-list input.content').each(function(){
159
					if(jQuery(this).attr('checked')){
160
						content.push(jQuery(this).val());
161
					}
162
				});
163
			}	
164
			var safari_brands = [];
165
			if('undefined' != jQuery('#import-list input.accommodation-brand').length){
166
				jQuery('#import-list input.accommodation-brand').each(function(){
167
					if(jQuery(this).attr('checked')){
168
						safari_brands.push(jQuery(this).val());
169
					}
170
				});
171
			}	
172
			
173
			var wetu_id = checkbox.attr('data-identifier');
174
			var post_id = checkbox.val();
175
			var row = checkbox.parents('tr');
176
			var data = {
177
	            'action' 	: 			'lsx_import_items',
178
	            'type'		: 			type,
179
	            'wetu_id' 	: 			wetu_id,
180
	            'post_id'	:			post_id,
181
	            'team_members' : 		team_members,
182
	            'safari_brands' : 		safari_brands,
183
	            'content'	: 			content
184
	        };	
185
			this.importRow(data,row);
186
		}
187
	},
188
	watchImportButton: function() {
189
		var $this = this;
190
191
		jQuery('#import-list input[type="submit"]').on('click',function(event){
192
193
			event.preventDefault();
194
			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...
195
			var false_click = true;
196
197
			jQuery('#import-list tr input:checked:not(.queued)').each(function(){
198
				jQuery(this).hide().addClass('queued');
199
				jQuery(this).parents('tr').find('.check-column').append(jQuery('#wetu-importer-search-form .ajax-loader-small').html());
200
				false_click = false;
201
			});
202
203
			if(true != false_click){
204
				$this.importNext();
205
			}else{
206
				alert('Make sure you have some items selected.');
207
			}
208
		});
209
	},
210
	watchBannerButton: function() {
211
		jQuery('#banners-filter input.button.download').on('click',function(event){
212
213
			event.preventDefault();
214
			jQuery('#banners-filter tbody tr input:checked').each(function(){
215
				var post_id = jQuery(this).val();
216
				var current_row = jQuery(this).parents('tr');
217
218
				jQuery(this).hide();
219
				jQuery(this).parents('tr').find('.check-column').append(jQuery('#banners-filter .ajax-loader-small').html());
220
221
				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...
222
		        {
223
		            'action' 	: 			'lsx_import_sync_banners',
224
		            'post_id'	:			post_id,
225
		        },
226
		        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...
227
		        	current_row.fadeOut('fast', 
228
		        	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...
229
			            jQuery(this).fadeOut('fast').remove();
230
			        });
231
		        });				
232
			});	
233
		});
234
	},
235
	watchConnectButton: function() {
236
		jQuery('#connect-accommodation-filter input.button.connect').on('click',function(event){
237
238
			event.preventDefault();
239
			jQuery('#connect-accommodation-filter tbody tr input:checked').each(function(){
240
				var post_id = jQuery(this).val();
241
				var type = 'connect_accommodation';
242
				var wetu_id = jQuery(this).attr('data-identifier');
243
244
				var current_row = jQuery(this).parents('tr');
245
246
				jQuery(this).hide();
247
				jQuery(this).parents('tr').find('.check-column').append(jQuery('#connect-accommodation-filter .ajax-loader-small').html());
248
249
				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...
250
		        {
251
		            'action' 	: 			'lsx_import_connect_accommodation',
252
		            'post_id'	:			post_id,
253
		            'type'		:			type,
254
		            'wetu_id'	:			wetu_id,
255
		        },
256
		        function(response) {
257
					if('none' == jQuery('.completed-list-wrapper').css('display')){
258
						jQuery('.completed-list-wrapper').fadeIn('fast');
259
					}
260
					jQuery('.completed-list-wrapper ul').append(response);		        	
261
		        	current_row.fadeOut('fast', 
262
		        	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...
263
			            jQuery(this).fadeOut('fast').remove();
264
			        });
265
		        });				
266
			});	
267
		});
268
	},
269
    watchCheckBoxes: function() {
270
        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...
271
            var thisOBJ = jQuery(this);
272
            if('all' === thisOBJ.val()){
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
			}else{
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
282
283
			}
284
285
286
        });
287
288
        var lastChecked = null;
289
290
        jQuery('#the-list input[type="checkbox"], #import-list tbody input[type="checkbox"]').on('click',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...
291
            if(!lastChecked) {
292
                lastChecked = this;
293
                return;
294
            }
295
296
            if(e.shiftKey) {
0 ignored issues
show
Bug introduced by
The variable e seems to be never declared. If this is a global, consider adding a /** global: e */ 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...
297
                var start = $chkboxes.index(this);
0 ignored issues
show
Bug introduced by
The variable $chkboxes seems to be never declared. If this is a global, consider adding a /** global: $chkboxes */ 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...
298
                var end = $chkboxes.index(lastChecked);
299
300
                $chkboxes.slice(Math.min(start,end), Math.max(start,end)+ 1).prop('checked', lastChecked.checked);
301
302
            }
303
            lastChecked = this;
304
        });
305
    },
306
307
    watchTourUpdate: function() {
308
        jQuery('.wetu-status.tour-wetu-status h3 a').on('click',function(event){
309
			event.preventDefault();
310
			jQuery(this).parents('.wetu-status.tour-wetu-status').find('form').submit();
311
        });
312
313
    }
314
}
315
jQuery(document).ready( function() {
316
	WETU_IMPORTER.init();
317
});