Passed
Branch master (40a760)
by Chris
07:38
created

vendor/lsx-field-post-search-ajax/js/mag-post-search-ajax.js   A

Complexity

Total Complexity 28
Complexity/F 1.75

Size

Lines of Code 177
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 28
eloc 124
mnd 12
bc 12
fnc 16
dl 0
loc 177
rs 10
bpm 0.75
cpm 1.75
noi 11
c 0
b 0
f 0
1
var mag_ajax_js = Object.create( null );
2
3
;( function( $, window, document, undefined ) {
4
5
	'use strict';
6
7
	mag_ajax_js.init_ajax = function( ) {
8
		$('.cmb-post-search-ajax').each(
9
			function () {
10
				var fid 		= $(this).attr('name');
11
				var name        = $(this).attr('id');
12
				var query_args 	= $(this).attr('data-queryargs');
13
				var object		= $(this).attr('data-object');
14
	
15
				name = name.replace( "][", "_" );
16
				name = name.replace( "]", "" );
17
				name = name.replace( "[", "_" );
18
19
				// We need to replace the _store with the new ID,
20
				fid = fid.replace( "][", "_" );
21
				fid = fid.replace( "]", "" );
22
				fid = fid.replace( "[", "_" );
23
24
				var storeReplace = $(this).parents('.cmb-row.cmb-type-post-search-ajax');
25
				console.log('====== CMBROW =======');
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...
26
				console.log(storeReplace);
27
				console.log(name);
28
				console.log(fid);
29
				if ( storeReplace.hasClass( 'cmb-repeat-group-field' ) ) {
30
					console.log('====== hasClass =======');
31
					console.log(storeReplace);
32
					var fieldReplace = undefined;
0 ignored issues
show
Unused Code Comprehensibility introduced by
The assignment of undefined is not necessary as fieldReplace is implicitly marked as undefined by the declaration.
Loading history...
33
					fieldReplace = storeReplace.find( '.cmb-td' ).find('input.cmb-post-search-ajax-store');
34
					if ( 0 < fieldReplace.length ) {
35
						fieldReplace.attr('name',fid + '_store');
36
					}
37
				}
38
	
39
				$(this).devbridgeAutocomplete({
40
					serviceUrl: psa.ajaxurl,
0 ignored issues
show
Bug introduced by
The variable psa seems to be never declared. If this is a global, consider adding a /** global: psa */ 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...
41
					type: 'POST',
42
					triggerSelectOnValidInput: false,
43
					showNoSuggestionNotice: true,
44
					transformResult: function(r) {
45
						var suggestions = $.parseJSON(r);
46
						if($('input#'+name+' li').length){
47
							var selected_vals 	= Array();
48
							var d 				= 0;
49
							$('input#'+name+' input').each(function(index, element) {
0 ignored issues
show
Unused Code introduced by
The parameter element 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 index 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...
50
								selected_vals.push( $(this).val() );
51
							});
52
							$(suggestions).each(function(ri, re){
53
								if($.inArray((re.data).toString(), selected_vals) > -1){
54
									suggestions.splice(ri-d, 1);
55
									d++;
56
								}
57
							});
58
						}
59
						$(suggestions).each(function(ri, re){
60
							re.value = $('<textarea />').html(re.value).text();
61
						});
62
						return {suggestions: suggestions};
63
					},
64
					params:{
65
						action  	: 'cmb_post_search_ajax_get_results',
66
						psacheck	: psa.nonce,
67
						object		: object,
68
						query_args	: query_args,
69
					},
70
					onSearchStart: function(){
71
						$(this).next('img.cmb-post-search-ajax-spinner').css('display', 'inline-block');
72
					},
73
					onSearchComplete: function(){
74
						$(this).next('img.cmb-post-search-ajax-spinner').hide();
75
					},
76
					onSelect: function (suggestion) {
77
						$(this).devbridgeAutocomplete('clearCache');
78
						var lid 	 = $(this).attr('id') + '_results';
79
						var name     = $(this).attr('id');
80
						var original = $(this).attr('id');
81
						var groupName = $(this).attr('name');
82
						name = name.replace( "][", "_" );
83
						name = name.replace( "]", "" );
84
						name = name.replace( "[", "_" );
85
86
						console.log('====== LID =======');
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...
87
						console.log(lid);
88
						console.log('====== name =======');
89
						console.log(name);
90
						console.log('====== suggestion =======');
91
						console.log(suggestion);
92
93
						console.log($(this).parents('.cmb-row').hasClass('cmb-repeat-group-field'));
94
						console.log($(this).parents('.cmb-row').hasClass('lsx-field-connect-field'));
95
						if ( $(this).parents('.cmb-row').hasClass('cmb-repeat-group-field') && $(this).parents('.cmb-row').hasClass('lsx-field-connect-field') ) {
96
							name = original;
97
						}
98
						console.log('====== Original =======');
99
						console.log(original);
100
						console.log('====== new name =======');
101
						console.log(name);
102
	
103
						var limit 	 = $(this).attr('data-limit');
104
						var sortable = $(this).attr('data-sortable');
105
						if ( limit > 1 ) {
106
							var handle = (sortable == 1) ? '<span class="hndl"></span>' : '';
0 ignored issues
show
Best Practice introduced by
Comparing sortable to 1 using the == operator is not safe. Consider using === instead.
Loading history...
107
							$(this).closest('.cmb-row').find( '.cmb-post-search-ajax-results' ).append('<li>'+handle+'<input type="hidden" name="'+lid+'[]" value="'+suggestion.data+'"><a href="'+suggestion.guid+'" target="_blank" class="edit-link">'+suggestion.value+'</a><a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a></li>');
108
109
							$(this).val('');
110
							if ( limit === $('input#'+name + '_results li').length ){
111
								$(this).prop( 'disabled', 'disabled' );
112
							} else {
113
								$(this).focus();
114
							}
115
						} else {
116
							$('input#'+name).val( suggestion.data );
117
						}
118
119
						if ( $(this).closest('.cmb-row').hasClass('cmb-repeat-group-field') && limit <= 1 ) {
120
							console.log('cmb-group');
121
							groupName = groupName.replace( "][", "_" );
122
							groupName = groupName.replace( "]", "" );
123
							groupName = groupName.replace( "[", "_" );
124
							console.log( groupName );
125
							$( 'input[name='+groupName+'_store]' ).val( suggestion.data );
126
						}
127
	
128
					}
129
				});			
130
			
131
				if($(this).attr('data-sortable') == 1){
0 ignored issues
show
Best Practice introduced by
Comparing $(this).attr("data-sortable") to 1 using the == operator is not safe. Consider using === instead.
Loading history...
132
					$('input#'+name).sortable({ 
133
						handle				 : '.hndl', 
134
						placeholder			 : 'ui-state-highlight', 
135
						forcePlaceholderSize : true 
136
					});	
137
				}
138
				
139
				if($(this).attr('data-limit') == 1){
0 ignored issues
show
Best Practice introduced by
Comparing $(this).attr("data-limit") to 1 using the == operator is not safe. Consider using === instead.
Loading history...
140
					$(this).on('blur', function(){
141
						if($(this).val() === ''){
142
							var name     = $(this).attr('id');
143
							name = name.replace( "][", "_" );
144
							name = name.replace( "]", "" );
145
							name = name.replace( "[", "_" );
146
							$('input#'+name).val('');
147
						}
148
					});
149
				}
150
			
151
			}
152
		);
153
	};
154
155
	mag_ajax_js.watch_remove_click = function() {
156
		$('.cmb-post-search-ajax-results').on( 'click', 'a.remover', function(){
157
			$(this).parent('li').fadeOut( 400, function(){ 
158
				var iid = $(this).parents('ul').attr('id').replace('_results', '');
159
				$(this).remove(); 
160
				$('#' + iid).removeProp( 'disabled' );
161
				$('#' + iid).devbridgeAutocomplete('clearCache');
162
			});
163
		});
164
	};
165
166
	mag_ajax_js.event_init = function( evt, $row ) {
0 ignored issues
show
Unused Code introduced by
The parameter evt 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 $row 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...
167
		mag_ajax_js.init_ajax();
168
	};
169
170
	$(document).ready(function() {
171
		mag_ajax_js.init_ajax();
172
		mag_ajax_js.watch_remove_click();
173
		$( document ).on( 'cmb2_add_row', mag_ajax_js.event_init )
174
	});
175
176
177
} )( jQuery, window, document );
178