1/* eslint-disable */ 2 3var CREDFrontendSelect2 = CREDFrontendSelect2 || {}; 4 5/** 6 * This new set of methods are needed to transform for each cred form select fields in select2 7 * The elaboration starts from a select2FieldList that includes the list of 8 * cred forms select fields that will have to be transformed in select2 Standard or Ajax 9 */ 10CREDFrontendSelect2.init = function () { 11 12 var self = this; 13 14 self.ajaxurl = cred_select2_frontend_settings.ajaxurl; 15 self.select2FieldsList = cred_select2_frontend_settings.select2_fields_list; 16 self.lang = cred_select2_frontend_settings.cred_lang; 17 18 /** 19 * Execution of all signed select2 20 */ 21 self.executeSelect2FieldsTransformation = function () { 22 for (var htmlFormId in self.select2FieldsList) { 23 for (var fieldName in self.select2FieldsList[htmlFormId]) { 24 var action = self.select2FieldsList[htmlFormId][fieldName].action; 25 var parameter = self.select2FieldsList[htmlFormId][fieldName].parameter; 26 var fieldSettings = self.select2FieldsList[htmlFormId][fieldName].field_settings; 27 var currentOption = (_.has(self.select2FieldsList[htmlFormId][fieldName], 'current_option')) ? self.select2FieldsList[htmlFormId][fieldName].current_option : null; 28 29 //If there is action info means that is a ajax select2 30 if (!action) { 31 self.transformSelectToSelect2(htmlFormId, fieldName, currentOption, parameter, fieldSettings); 32 } else { 33 if (_.has(fieldSettings, 'is_relationship')) { 34 self.transformSelectToSelect2RelationshipAjax(htmlFormId, fieldName, currentOption, action, fieldSettings); 35 } else { 36 //Legacy Parent Back Compatibility 37 self.transformSelectToSelect2Ajax(htmlFormId, fieldName, currentOption, action, parameter, fieldSettings); 38 } 39 } 40 } 41 } 42 }; 43 44 /** 45 * Transforms a classic select field to a select2 with Ajax feature specific for Relationship Fields 46 * 47 * @param htmlFormId 48 * @param fieldName 49 * @param currentOption 50 * @param action 51 * @param fieldSettings 52 */ 53 self.transformSelectToSelect2RelationshipAjax = function (htmlFormId, fieldName, currentOption, action, fieldSettings) { 54 var is_multiple = (fieldSettings['type'] === 'multiselect'); 55 if (is_multiple) { 56 fieldName += '[]'; 57 } 58 59 // setting placeholder empty in order to respect select_text shortcode default rule 60 var placeholder = ''; 61 var hasPlaceholder = _.has(fieldSettings, 'placeholder'); 62 if (hasPlaceholder) { 63 placeholder = fieldSettings['placeholder']; 64 } 65 var hasCurrentOption = (currentOption !== null); 66 67 var $currentFieldSelector = jQuery('select[name="' + fieldName + '"]', '#' + htmlFormId); 68 69 // Check if it is a readonly selector because the related post was already set 70 var readonlySelector = (_.has(fieldSettings, 'readonly') && fieldSettings['readonly'] === true) ? true : false; 71 72 // Our rendering library turns some readonly select dropdowns into text inputs, 73 // and renders the select dropdown as an empty shell with a different, suffixed name attribute. 74 if ( 0 == $currentFieldSelector.length ) { 75 $currentFieldSelector = jQuery('select[name="' + fieldName + '_disabled"]', '#' + htmlFormId); 76 if ( 0 == $currentFieldSelector.length ) { 77 return; 78 } 79 readonlySelector = true; 80 } 81 82 // If the related post selector is readonly and has a value, set it 83 // and initialize select2 without options: the value can not be changed. 84 if ( readonlySelector && hasCurrentOption ) { 85 $currentFieldSelector.find( 'option' ).remove(); 86 $currentFieldSelector.append('<option value="' + currentOption.value + '">' + currentOption.text + '</option>'); 87 $currentFieldSelector.toolset_select2(); 88 $currentFieldSelector.toolset_select2('val', currentOption.value, true); 89 return; 90 } 91 92 // Otherwise, initialize select2 with AJAX search 93 $currentFieldSelector.toolset_select2({ 94 width: 'resolve', 95 allowClear: true, 96 placeholder: placeholder, 97 multiple: is_multiple, 98 minimumInputLength: 0, 99 maximumSelectionLength: 20, 100 ajax: { 101 url: self.ajaxurl, 102 dataType: 'json', 103 delay: 250, 104 type: 'POST', 105 cache: false, 106 data: function (params) { 107 return { 108 q: (_.has(params, 'term')) ? params.term : '', 109 page: (params.page) ? params.page : 0, 110 action: action, 111 //relationship slug 112 slug: fieldSettings.slug, 113 //target item to associate to 114 role: fieldSettings.role, 115 //source item to connect to 116 cred_post_id: fieldSettings.post_id, 117 // language, if any 118 cred_lang: self.lang, 119 orderBy: $currentFieldSelector.data( 'orderby' ), 120 order: $currentFieldSelector.data( 'order' ), 121 author: $currentFieldSelector.data( 'author' ), 122 form_type: fieldSettings.form_type || null 123 }; 124 }, 125 processResults: function (response) { 126 if (response.success) { 127 return { 128 results: response.data 129 }; 130 } else { 131 console.log(response.data.message); 132 return { 133 results: [] 134 }; 135 } 136 }, 137 error: function (ajaxContext) { 138 console.log(ajaxContext); 139 }, 140 complete: function () { 141 } 142 } 143 }); 144 145 //Set current option in Edit Form if exists 146 if ( hasCurrentOption ) { 147 $currentFieldSelector.append('<option value="' + currentOption.value + '">' + currentOption.text + '</option>'); 148 $currentFieldSelector.toolset_select2('val', currentOption.value, true); 149 } 150 151 }; 152 153 /** 154 * Transforms a classic select field to a select2 with Ajax feature 155 * 156 * @param htmlFormId 157 * @param fieldName 158 * @param currentOption 159 * @param action 160 * @param parameter 161 * @param fieldSettings 162 */ 163 self.transformSelectToSelect2Ajax = function (htmlFormId, fieldName, currentOption, action, parameter, fieldSettings) { 164 var is_multiple = (fieldSettings['type'] === 'multiselect'); 165 if (is_multiple) { 166 fieldName += '[]'; 167 } 168 169 // setting placeholder empty in order to respect select_text shortcode default rule 170 var placeholder = ''; 171 var hasPlaceholder = _.has(fieldSettings, 'placeholder'); 172 if (hasPlaceholder) { 173 placeholder = fieldSettings['placeholder']; 174 } 175 var hasCurrentOption = (currentOption !== null); 176 177 var $currentFieldSelector = jQuery('select[name="' + fieldName + '"]', '#' + htmlFormId); 178 179 // Our rendering library turns some readonly select dropdowns into text inputs, 180 // and renders the select dropdown as an empty shell with a different, suffixed name attribute. 181 if ( 0 == $currentFieldSelector.length ) { 182 $currentFieldSelector = jQuery('select[name="' + fieldName + '_disabled"]', '#' + htmlFormId); 183 if ( 0 == $currentFieldSelector.length ) { 184 return; 185 } 186 readonlySelector = true; 187 } 188 189 $currentFieldSelector.toolset_select2({ 190 width: 'resolve', 191 allowClear: true, 192 placeholder: placeholder, 193 multiple: is_multiple, 194 minimumInputLength: 0, 195 maximumSelectionLength: 20, 196 ajax: { 197 url: self.ajaxurl, 198 dataType: 'json', 199 delay: 250, 200 type: 'POST', 201 cache: false, 202 data: function (params) { 203 return { 204 q: (params.term) ? params.term : '', 205 page: (params.page) ? params.page : 0, 206 action: action, 207 parameter: parameter, 208 wpml_context: fieldSettings.wpml_context, 209 wpml_name: fieldSettings.slug, 210 // language, if any 211 cred_lang: self.lang, 212 orderBy: $currentFieldSelector.data( 'orderby' ), 213 order: $currentFieldSelector.data( 'order' ), 214 author: $currentFieldSelector.data( 'author' ) 215 }; 216 }, 217 processResults: function (response) { 218 if (response.success) { 219 return { 220 results: response.data 221 }; 222 } else { 223 console.log(response.data.message); 224 return { 225 results: [] 226 }; 227 } 228 }, 229 error: function (ajaxContext) { 230 console.log(ajaxContext); 231 }, 232 complete: function () { 233 } 234 } 235 }); 236 237 //Set current option in Edit Form if exists 238 if (hasCurrentOption) { 239 $currentFieldSelector.append('<option value="' + currentOption.value + '">' + currentOption.text + '</option>'); 240 $currentFieldSelector.toolset_select2('val', currentOption.value, true); 241 } 242 }; 243 244 /** 245 * Transforms a classic select box to a standard select2 not ajax 246 * 247 * @param htmlFormId 248 * @param fieldName 249 * @param currentOption 250 * @param parameter 251 * @param fieldSettings 252 */ 253 self.transformSelectToSelect2 = function (htmlFormId, fieldName, currentOption, parameter, fieldSettings) { 254 var is_multiple = fieldSettings['type'] === 'multiselect'; 255 if (is_multiple) { 256 fieldName += '[]'; 257 } 258 259 var placeholder = 'â Select â'; 260 var has_placeholder = _.has(fieldSettings, 'placeholder'); 261 if (has_placeholder) { 262 placeholder = fieldSettings['placeholder']; 263 } 264 265 var hasCurrentOption = (currentOption !== null); 266 267 var $currentFieldSelector = jQuery('select[name="' + fieldName + '"]', '#' + htmlFormId); 268 269 // Our rendering library turns some readonly select dropdowns into text inputs, 270 // and renders the select dropdown as an empty shell with a different, suffixed name attribute. 271 if ( 0 == $currentFieldSelector.length ) { 272 $currentFieldSelector = jQuery('select[name="' + fieldName + '_disabled"]', '#' + htmlFormId); 273 if ( 0 == $currentFieldSelector.length ) { 274 return; 275 } 276 readonlySelector = true; 277 } 278 279 $currentFieldSelector.toolset_select2({ 280 data: parameter, 281 placeholder: placeholder, 282 multiple: is_multiple, 283 minimumInputLength: 1, 284 allowClear: true 285 }); 286 287 //Set current option in Edit Form if exists 288 if (hasCurrentOption) { 289 $currentFieldSelector.append('<option value="' + currentOption.value + '">' + currentOption.text + '</option>'); 290 $currentFieldSelector.toolset_select2('val', currentOption.value, true); 291 } 292 }; 293 294 self.executeSelect2FieldsTransformation(); 295}; 296 297jQuery(function () { 298 var credSelect2FrontendSetting = new CREDFrontendSelect2.init(); 299 300 //bounding onValidatedSubmitAjaxForm 301 var boundOnCREDFrontendSelect2Init = _.bind(CREDFrontendSelect2.init, CREDFrontendSelect2); 302 //After cred form submit validation success 303 Toolset.hooks.addAction('cred_form_ajax_completed', boundOnCREDFrontendSelect2Init); 304 305});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.