| 1 |
// enhanced select for shipping method search, partially uses wc-enhanced-select! |
| 2 |
jQuery(function($) { |
| 3 |
function getEnhancedSelectFormatString() { |
| 4 |
var formatString = { |
| 5 |
formatMatches: function(matches) { |
| 6 |
if (1 === matches) { |
| 7 |
return wc_enhanced_select_params.i18n_matches_1; |
| 8 |
} |
| 9 |
|
| 10 |
return wc_enhanced_select_params.i18n_matches_n.replace('%qty%', matches); |
| 11 |
}, |
| 12 |
formatNoMatches: function() { |
| 13 |
return wc_enhanced_select_params.i18n_no_matches; |
| 14 |
}, |
| 15 |
formatAjaxError: function() { |
| 16 |
return wc_enhanced_select_params.i18n_ajax_error; |
| 17 |
}, |
| 18 |
formatInputTooShort: function(input, min) { |
| 19 |
var number = min - input.length; |
| 20 |
|
| 21 |
if (1 === number) { |
| 22 |
return wc_enhanced_select_params.i18n_input_too_short_1; |
| 23 |
} |
| 24 |
|
| 25 |
return wc_enhanced_select_params.i18n_input_too_short_n.replace('%qty%', number); |
| 26 |
}, |
| 27 |
formatInputTooLong: function(input, max) { |
| 28 |
var number = input.length - max; |
| 29 |
|
| 30 |
if (1 === number) { |
| 31 |
return wc_enhanced_select_params.i18n_input_too_long_1; |
| 32 |
} |
| 33 |
|
| 34 |
return wc_enhanced_select_params.i18n_input_too_long_n.replace('%qty%', number); |
| 35 |
}, |
| 36 |
formatSelectionTooBig: function(limit) { |
| 37 |
if (1 === limit) { |
| 38 |
return wc_enhanced_select_params.i18n_selection_too_long_1; |
| 39 |
} |
| 40 |
|
| 41 |
return wc_enhanced_select_params.i18n_selection_too_long_n.replace('%qty%', limit); |
| 42 |
}, |
| 43 |
formatLoadMore: function() { |
| 44 |
return wc_enhanced_select_params.i18n_load_more; |
| 45 |
}, |
| 46 |
formatSearching: function() { |
| 47 |
return wc_enhanced_select_params.i18n_searching; |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
return formatString; |
| 52 |
} |
| 53 |
|
| 54 |
$(document.body) |
| 55 |
|
| 56 |
.on('wc-postnl-enhanced-select-init', function() { |
| 57 |
|
| 58 |
// Ajax shipping method search box |
| 59 |
$(':input.wc-postnl-shipping-method-search').filter(':not(.enhanced)').each(function() { |
| 60 |
var select2_args = { |
| 61 |
allowClear: $(this).data('allow_clear') ? true : false, |
| 62 |
placeholder: $(this).data('placeholder'), |
| 63 |
minimumInputLength: $(this).data('minimum_input_length') ? $(this).data('minimum_input_length') : '3', |
| 64 |
escapeMarkup: function(m) { |
| 65 |
return m; |
| 66 |
}, |
| 67 |
ajax: { |
| 68 |
url: wc_postnl.ajax_url, |
| 69 |
dataType: 'json', |
| 70 |
quietMillis: 250, |
| 71 |
data: function(term) { |
| 72 |
return { |
| 73 |
term: term, |
| 74 |
action: 'wc_postnl_json_search_shipping_methods', |
| 75 |
security: wc_postnl.nonce, |
| 76 |
}; |
| 77 |
}, |
| 78 |
results: function(data) { |
| 79 |
var terms = []; |
| 80 |
if (data) { |
| 81 |
$.each(data, function(id, text) { |
| 82 |
terms.push({id: id, text: text}); |
| 83 |
}); |
| 84 |
} |
| 85 |
return { |
| 86 |
results: terms |
| 87 |
}; |
| 88 |
}, |
| 89 |
cache: true |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
if ($(this).data('multiple') === true) { |
| 94 |
select2_args.multiple = true; |
| 95 |
select2_args.initSelection = function(element, callback) { |
| 96 |
var data = $.parseJSON(element.attr('data-selected')); |
| 97 |
var selected = []; |
| 98 |
|
| 99 |
$(element.val().split(',')).each(function(i, val) { |
| 100 |
selected.push({ |
| 101 |
id: val, |
| 102 |
text: data[val] |
| 103 |
}); |
| 104 |
}); |
| 105 |
return callback(selected); |
| 106 |
}; |
| 107 |
select2_args.formatSelection = function(data) { |
| 108 |
return '<div class="selected-option" data-id="' + data.id + '">' + data.text + '</div>'; |
| 109 |
}; |
| 110 |
} else { |
| 111 |
select2_args.multiple = false; |
| 112 |
select2_args.initSelection = function(element, callback) { |
| 113 |
var data = { |
| 114 |
id: element.val(), |
| 115 |
text: element.attr('data-selected') |
| 116 |
}; |
| 117 |
return callback(data); |
| 118 |
}; |
| 119 |
} |
| 120 |
|
| 121 |
select2_args = $.extend(select2_args, getEnhancedSelectFormatString()); |
| 122 |
|
| 123 |
$(this).select2(select2_args).addClass('enhanced'); |
| 124 |
}); |
| 125 |
}) |
| 126 |
|
| 127 |
.trigger('wc-postnl-enhanced-select-init'); |
| 128 |
|
| 129 |
}); |