Passed
Branch master (57a609)
by Chris
07:41
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 178
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 124
c 0
b 0
f 0
dl 0
loc 178
rs 10
wmc 28
mnd 12
bc 12
fnc 16
bpm 0.75
cpm 1.75
noi 11
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
108
							$(this).parents('.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>');
109
110
							$(this).val('');
111
							if ( limit === $('input#'+name + '_results li').length ){
112
								$(this).prop( 'disabled', 'disabled' );
113
							} else {
114
								$(this).focus();
115
							}
116
						} else {
117
							$('input#'+name).val( suggestion.data );
118
						}
119
120
						if ( $(this).parents('.cmb-row').hasClass('cmb-repeat-group-field') && limit <= 1 ) {
121
							console.log('cmb-group');
122
							groupName = groupName.replace( "][", "_" );
123
							groupName = groupName.replace( "]", "" );
124
							groupName = groupName.replace( "[", "_" );
125
							console.log( groupName );
126
							$( 'input[name='+groupName+'_store]' ).val( suggestion.data );
127
						}
128
	
129
					}
130
				});			
131
			
132
				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...
133
					$('input#'+name).sortable({ 
134
						handle				 : '.hndl', 
135
						placeholder			 : 'ui-state-highlight', 
136
						forcePlaceholderSize : true 
137
					});	
138
				}
139
				
140
				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...
141
					$(this).on('blur', function(){
142
						if($(this).val() === ''){
143
							var name     = $(this).attr('id');
144
							name = name.replace( "][", "_" );
145
							name = name.replace( "]", "" );
146
							name = name.replace( "[", "_" );
147
							$('input#'+name).val('');
148
						}
149
					});
150
				}
151
			
152
			}
153
		);
154
	};
155
156
	mag_ajax_js.watch_remove_click = function() {
157
		$('.cmb-post-search-ajax-results').on( 'click', 'a.remover', function(){
158
			$(this).parent('li').fadeOut( 400, function(){ 
159
				var iid = $(this).parents('ul').attr('id').replace('_results', '');
160
				$(this).remove(); 
161
				$('#' + iid).removeProp( 'disabled' );
162
				$('#' + iid).devbridgeAutocomplete('clearCache');
163
			});
164
		});
165
	};
166
167
	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...
168
		mag_ajax_js.init_ajax();
169
	};
170
171
	$(document).ready(function() {
172
		mag_ajax_js.init_ajax();
173
		mag_ajax_js.watch_remove_click();
174
		$( document ).on( 'cmb2_add_row', mag_ajax_js.event_init )
175
	});
176
177
178
} )( jQuery, window, document );
179