Bez popisu

wc-enhanced-select.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*global wc_enhanced_select_params */
  2. jQuery( function( $ ) {
  3. function getEnhancedSelectFormatString() {
  4. return {
  5. 'language': {
  6. errorLoading: function() {
  7. // Workaround for https://github.com/select2/select2/issues/4355 instead of i18n_ajax_error.
  8. return wc_enhanced_select_params.i18n_searching;
  9. },
  10. inputTooLong: function( args ) {
  11. var overChars = args.input.length - args.maximum;
  12. if ( 1 === overChars ) {
  13. return wc_enhanced_select_params.i18n_input_too_long_1;
  14. }
  15. return wc_enhanced_select_params.i18n_input_too_long_n.replace( '%qty%', overChars );
  16. },
  17. inputTooShort: function( args ) {
  18. var remainingChars = args.minimum - args.input.length;
  19. if ( 1 === remainingChars ) {
  20. return wc_enhanced_select_params.i18n_input_too_short_1;
  21. }
  22. return wc_enhanced_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars );
  23. },
  24. loadingMore: function() {
  25. return wc_enhanced_select_params.i18n_load_more;
  26. },
  27. maximumSelected: function( args ) {
  28. if ( args.maximum === 1 ) {
  29. return wc_enhanced_select_params.i18n_selection_too_long_1;
  30. }
  31. return wc_enhanced_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum );
  32. },
  33. noResults: function() {
  34. return wc_enhanced_select_params.i18n_no_matches;
  35. },
  36. searching: function() {
  37. return wc_enhanced_select_params.i18n_searching;
  38. }
  39. }
  40. };
  41. }
  42. try {
  43. $( document.body )
  44. .on( 'wc-enhanced-select-init', function() {
  45. // Regular select boxes
  46. $( ':input.wc-enhanced-select, :input.chosen_select' ).filter( ':not(.enhanced)' ).each( function() {
  47. var select2_args = $.extend({
  48. minimumResultsForSearch: 10,
  49. allowClear: $( this ).data( 'allow_clear' ) ? true : false,
  50. placeholder: $( this ).data( 'placeholder' )
  51. }, getEnhancedSelectFormatString() );
  52. $( this ).selectWoo( select2_args ).addClass( 'enhanced' );
  53. });
  54. $( ':input.wc-enhanced-select-nostd, :input.chosen_select_nostd' ).filter( ':not(.enhanced)' ).each( function() {
  55. var select2_args = $.extend({
  56. minimumResultsForSearch: 10,
  57. allowClear: true,
  58. placeholder: $( this ).data( 'placeholder' )
  59. }, getEnhancedSelectFormatString() );
  60. $( this ).selectWoo( select2_args ).addClass( 'enhanced' );
  61. });
  62. function display_result( self, select2_args ) {
  63. select2_args = $.extend( select2_args, getEnhancedSelectFormatString() );
  64. $( self ).selectWoo( select2_args ).addClass( 'enhanced' );
  65. if ( $( self ).data( 'sortable' ) ) {
  66. var $select = $(self);
  67. var $list = $( self ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' );
  68. $list.sortable({
  69. placeholder : 'ui-state-highlight select2-selection__choice',
  70. forcePlaceholderSize: true,
  71. items : 'li:not(.select2-search__field)',
  72. tolerance : 'pointer',
  73. stop: function() {
  74. $( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() {
  75. var id = $( this ).data( 'data' ).id;
  76. var option = $select.find( 'option[value="' + id + '"]' )[0];
  77. $select.prepend( option );
  78. } );
  79. }
  80. });
  81. // Keep multiselects ordered alphabetically if they are not sortable.
  82. } else if ( $( self ).prop( 'multiple' ) ) {
  83. $( self ).on( 'change', function(){
  84. var $children = $( self ).children();
  85. $children.sort(function(a, b){
  86. var atext = a.text.toLowerCase();
  87. var btext = b.text.toLowerCase();
  88. if ( atext > btext ) {
  89. return 1;
  90. }
  91. if ( atext < btext ) {
  92. return -1;
  93. }
  94. return 0;
  95. });
  96. $( self ).html( $children );
  97. });
  98. }
  99. }
  100. // Ajax product search box
  101. $( ':input.wc-product-search' ).filter( ':not(.enhanced)' ).each( function() {
  102. var select2_args = {
  103. allowClear: $( this ).data( 'allow_clear' ) ? true : false,
  104. placeholder: $( this ).data( 'placeholder' ),
  105. minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3',
  106. escapeMarkup: function( m ) {
  107. return m;
  108. },
  109. ajax: {
  110. url: wc_enhanced_select_params.ajax_url,
  111. dataType: 'json',
  112. delay: 250,
  113. data: function( params ) {
  114. return {
  115. term : params.term,
  116. action : $( this ).data( 'action' ) || 'woocommerce_json_search_products_and_variations',
  117. security : wc_enhanced_select_params.search_products_nonce,
  118. exclude : $( this ).data( 'exclude' ),
  119. exclude_type : $( this ).data( 'exclude_type' ),
  120. include : $( this ).data( 'include' ),
  121. limit : $( this ).data( 'limit' ),
  122. display_stock: $( this ).data( 'display_stock' )
  123. };
  124. },
  125. processResults: function( data ) {
  126. var terms = [];
  127. if ( data ) {
  128. $.each( data, function( id, text ) {
  129. terms.push( { id: id, text: text } );
  130. });
  131. }
  132. return {
  133. results: terms
  134. };
  135. },
  136. cache: true
  137. }
  138. };
  139. display_result( this, select2_args );
  140. });
  141. // Ajax Page Search.
  142. $( ':input.wc-page-search' ).filter( ':not(.enhanced)' ).each( function() {
  143. var select2_args = {
  144. allowClear: $( this ).data( 'allow_clear' ) ? true : false,
  145. placeholder: $( this ).data( 'placeholder' ),
  146. minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3',
  147. escapeMarkup: function( m ) {
  148. return m;
  149. },
  150. ajax: {
  151. url: wc_enhanced_select_params.ajax_url,
  152. dataType: 'json',
  153. delay: 250,
  154. data: function( params ) {
  155. return {
  156. term : params.term,
  157. action : $( this ).data( 'action' ) || 'woocommerce_json_search_pages',
  158. security : wc_enhanced_select_params.search_pages_nonce,
  159. exclude : $( this ).data( 'exclude' ),
  160. post_status : $( this ).data( 'post_status' ),
  161. limit : $( this ).data( 'limit' ),
  162. };
  163. },
  164. processResults: function( data ) {
  165. var terms = [];
  166. if ( data ) {
  167. $.each( data, function( id, text ) {
  168. terms.push( { id: id, text: text } );
  169. } );
  170. }
  171. return {
  172. results: terms
  173. };
  174. },
  175. cache: true
  176. }
  177. };
  178. $( this ).selectWoo( select2_args ).addClass( 'enhanced' );
  179. });
  180. // Ajax customer search boxes
  181. $( ':input.wc-customer-search' ).filter( ':not(.enhanced)' ).each( function() {
  182. var select2_args = {
  183. allowClear: $( this ).data( 'allow_clear' ) ? true : false,
  184. placeholder: $( this ).data( 'placeholder' ),
  185. minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '1',
  186. escapeMarkup: function( m ) {
  187. return m;
  188. },
  189. ajax: {
  190. url: wc_enhanced_select_params.ajax_url,
  191. dataType: 'json',
  192. delay: 1000,
  193. data: function( params ) {
  194. return {
  195. term: params.term,
  196. action: 'woocommerce_json_search_customers',
  197. security: wc_enhanced_select_params.search_customers_nonce,
  198. exclude: $( this ).data( 'exclude' )
  199. };
  200. },
  201. processResults: function( data ) {
  202. var terms = [];
  203. if ( data ) {
  204. $.each( data, function( id, text ) {
  205. terms.push({
  206. id: id,
  207. text: text
  208. });
  209. });
  210. }
  211. return {
  212. results: terms
  213. };
  214. },
  215. cache: true
  216. }
  217. };
  218. select2_args = $.extend( select2_args, getEnhancedSelectFormatString() );
  219. $( this ).selectWoo( select2_args ).addClass( 'enhanced' );
  220. if ( $( this ).data( 'sortable' ) ) {
  221. var $select = $(this);
  222. var $list = $( this ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' );
  223. $list.sortable({
  224. placeholder : 'ui-state-highlight select2-selection__choice',
  225. forcePlaceholderSize: true,
  226. items : 'li:not(.select2-search__field)',
  227. tolerance : 'pointer',
  228. stop: function() {
  229. $( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() {
  230. var id = $( this ).data( 'data' ).id;
  231. var option = $select.find( 'option[value="' + id + '"]' )[0];
  232. $select.prepend( option );
  233. } );
  234. }
  235. });
  236. }
  237. });
  238. // Ajax category search boxes
  239. $( ':input.wc-category-search' ).filter( ':not(.enhanced)' ).each( function() {
  240. var select2_args = $.extend( {
  241. allowClear : $( this ).data( 'allow_clear' ) ? true : false,
  242. placeholder : $( this ).data( 'placeholder' ),
  243. minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : 3,
  244. escapeMarkup : function( m ) {
  245. return m;
  246. },
  247. ajax: {
  248. url: wc_enhanced_select_params.ajax_url,
  249. dataType: 'json',
  250. delay: 250,
  251. data: function( params ) {
  252. return {
  253. term: params.term,
  254. action: 'woocommerce_json_search_categories',
  255. security: wc_enhanced_select_params.search_categories_nonce
  256. };
  257. },
  258. processResults: function( data ) {
  259. var terms = [];
  260. if ( data ) {
  261. $.each( data, function( id, term ) {
  262. terms.push({
  263. id: term.slug,
  264. text: term.formatted_name
  265. });
  266. });
  267. }
  268. return {
  269. results: terms
  270. };
  271. },
  272. cache: true
  273. }
  274. }, getEnhancedSelectFormatString() );
  275. $( this ).selectWoo( select2_args ).addClass( 'enhanced' );
  276. });
  277. })
  278. // WooCommerce Backbone Modal
  279. .on( 'wc_backbone_modal_before_remove', function() {
  280. $( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' )
  281. .selectWoo( 'close' );
  282. })
  283. .trigger( 'wc-enhanced-select-init' );
  284. $( 'html' ).on( 'click', function( event ) {
  285. if ( this === event.target ) {
  286. $( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' )
  287. .selectWoo( 'close' );
  288. }
  289. } );
  290. } catch( err ) {
  291. // If select2 failed (conflict?) log the error but don't stop other scripts breaking.
  292. window.console.log( err );
  293. }
  294. });