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 ======='); |
|
|
|
|
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
|
|
|
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, |
|
|
|
|
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) { |
|
|
|
|
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 ======='); |
|
|
|
|
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>' : ''; |
|
|
|
|
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){ |
|
|
|
|
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){ |
|
|
|
|
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 ) { |
|
|
|
|
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
|
|
|
|