Passed
Push — add/multiplan ( f03a15...eb47db )
by Chris
04:55
created

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

Complexity

Total Complexity 33
Complexity/F 1.83

Size

Lines of Code 214
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 149
c 0
b 0
f 0
dl 0
loc 214
rs 9.76
wmc 33
mnd 15
bc 15
fnc 18
bpm 0.8333
cpm 1.8333
noi 14
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).closest('.cmb-row.cmb-type-post-search-ajax');
25
				console.log('====== CMBROW =======');
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;
33
					var emptyResultsList = false;
34
35
					// Run through the store fields and replace the results list.
36
					fieldReplace = storeReplace.find( '.cmb-td' ).find('input.cmb-post-search-ajax-store');
37
					if ( 0 < fieldReplace.length ) {
38
						fieldReplace.attr('name',fid + '_store');
39
					}
40
41
					if ( '' === fieldReplace.val() ) {
42
						storeReplace.find( '.cmb-post-search-ajax-results' ).empty();
43
					}
44
				}
45
	
46
				$(this).devbridgeAutocomplete({
47
					serviceUrl: psa.ajaxurl,
48
					type: 'POST',
49
					triggerSelectOnValidInput: false,
50
					showNoSuggestionNotice: true,
51
					transformResult: function(r) {
52
						var suggestions = $.parseJSON(r);
53
						if($('input#'+name+' li').length){
54
							var selected_vals 	= Array();
55
							var d 				= 0;
56
							$('input#'+name+' input').each(function(index, element) {
57
								selected_vals.push( $(this).val() );
58
							});
59
							$(suggestions).each(function(ri, re){
60
								if($.inArray((re.data).toString(), selected_vals) > -1){
61
									suggestions.splice(ri-d, 1);
62
									d++;
63
								}
64
							});
65
						}
66
						$(suggestions).each(function(ri, re){
67
							re.value = $('<textarea />').html(re.value).text();
68
						});
69
						return {suggestions: suggestions};
70
					},
71
					params:{
72
						action  	: 'cmb_post_search_ajax_get_results',
73
						psacheck	: psa.nonce,
74
						object		: object,
75
						query_args	: query_args,
76
					},
77
					onSearchStart: function(){
78
						$(this).next('img.cmb-post-search-ajax-spinner').css('display', 'inline-block');
79
					},
80
					onSearchComplete: function(){
81
						$(this).next('img.cmb-post-search-ajax-spinner').hide();
82
					},
83
					onSelect: function (suggestion) {
84
						$(this).devbridgeAutocomplete('clearCache');
85
						var lid 	 = $(this).attr('id') + '_results';
86
						var name     = $(this).attr('id');
87
						var original = $(this).attr('id');
88
						var groupName = $(this).attr('name');
89
						name = name.replace( "][", "_" );
90
						name = name.replace( "]", "" );
91
						name = name.replace( "[", "_" );
92
93
						console.log('====== LID =======');
94
						console.log(lid);
95
						console.log('====== name =======');
96
						console.log(name);
97
						console.log('====== suggestion =======');
98
						console.log(suggestion);
99
100
						console.log($(this).closest('.cmb-row').hasClass('cmb-repeat-group-field'));
101
						console.log($(this).closest('.cmb-row').hasClass('lsx-field-connect-field'));
102
						if ( $(this).closest('.cmb-row').hasClass('cmb-repeat-group-field') && $(this).closest('.cmb-row').hasClass('lsx-field-connect-field') ) {
103
							name = original;
104
						}
105
						console.log('====== Original =======');
106
						console.log(original);
107
						console.log('====== new name =======');
108
						console.log(name);
109
	
110
						var limit 	 = $(this).attr('data-limit');
111
						var sortable = $(this).attr('data-sortable');
112
						if ( limit > 1 ) {
113
							var handle = (sortable == 1) ? '<span class="hndl"></span>' : '';
114
							$(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>');
115
116
							$(this).val('');
117
							if ( limit === $('input#'+name + '_results li').length ){
118
								$(this).prop( 'disabled', 'disabled' );
119
							} else {
120
								$(this).focus();
121
							}
122
123
							mag_ajax_js.populateStoreField( $(this), name );
124
125
						} else {
126
							$('input#'+name).val( suggestion.data );
127
						}
128
129
						if ( $(this).closest('.cmb-row').hasClass('cmb-repeat-group-field') ) {
130
							console.log('cmb-group');
131
							console.log( groupName );
132
							groupName = groupName.replace( "][", "_" );
133
							groupName = groupName.replace( "]", "" );
134
							groupName = groupName.replace( "[", "_" );
135
							
136
							mag_ajax_js.populateStoreField( $(this), groupName );
137
						}
138
					}
139
				});			
140
			
141
				if($(this).attr('data-sortable') == 1){
142
					$('input#'+name).sortable({ 
143
						handle				 : '.hndl', 
144
						placeholder			 : 'ui-state-highlight', 
145
						forcePlaceholderSize : true 
146
					});	
147
				}
148
				
149
				if($(this).attr('data-limit') == 1){
150
					$(this).on('blur', function(){
151
						if($(this).val() === ''){
152
							var name     = $(this).attr('id');
153
							name = name.replace( "][", "_" );
154
							name = name.replace( "]", "" );
155
							name = name.replace( "[", "_" );
156
							$('input#'+name).val('');
157
						}
158
					});
159
				}
160
			
161
			}
162
		);
163
	};
164
165
	mag_ajax_js.populateStoreField = function( ele, name ) {
166
		var resultValues = ele.closest('.cmb-row').find( '.cmb-post-search-ajax-results li input' ).map( function(){return $(this).val();} ).get();
167
		console.log(ele);
168
		console.log(ele.closest('.cmb-row'));
169
		console.log(resultValues);
170
		if ( 0 === resultValues.length ) {
171
			$( 'input[name='+name+'_store]' ).val( '' );
172
		} else {
173
			$( 'input[name='+name+'_store]' ).val( resultValues.join(',') );
174
		}
175
	};
176
177
	mag_ajax_js.watch_remove_click = function() {
178
		$('.cmb-post-search-ajax-results').on( 'click', 'a.remover', function(){
179
			$(this).parent('li').fadeOut( 0, function(){ 
180
				var iid     = $(this).parent('ul').attr('id').replace('_results', '');
181
				var cmb_row = $(this).closest('.cmb-row');
182
183
				console.log( iid );
184
				$(this).remove(); 
185
				$('#' + iid).removeProp( 'disabled' );
186
				$('#' + iid).devbridgeAutocomplete('clearCache');
187
188
				if ( cmb_row.hasClass('cmb-repeat-group-field') ) {				
189
					var groupName = $(this).find('input').attr('name').replace('_results[]', '');
190
					groupName = groupName.replace( "][", "_" );
191
					groupName = groupName.replace( "]", "" );
192
					groupName = groupName.replace( "[", "_" );
193
					console.log( groupName );
194
					console.log( cmb_row );
195
					mag_ajax_js.populateStoreField( cmb_row, groupName );
196
				} else {
197
					mag_ajax_js.populateStoreField( cmb_row, iid );
198
				}
199
			});
200
		});
201
	};
202
203
	mag_ajax_js.event_init = function( evt, $row ) {
204
		mag_ajax_js.init_ajax();
205
	};
206
207
	$(document).ready(function() {
208
		mag_ajax_js.init_ajax();
209
		mag_ajax_js.watch_remove_click();
210
		$( document ).on( 'cmb2_add_row', mag_ajax_js.event_init )
211
	});
212
213
214
} )( jQuery, window, document );
215