component.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. function init_components_part(input){
  2. if(input.is(":visible")) {
  3. var partPicker = input.clone();
  4. input.before(partPicker);
  5. if(input.parent().hasClass("input-group")){
  6. input.parent().after(input.detach());
  7. input.before('<div id="part-anchor" class="dropdown-anchor"></div>');
  8. }
  9. input.hide();
  10. partPicker.addClass('data-type-part').removeAttr("data-type").removeAttr('name').removeAttr('id').removeAttr('onchange');
  11. partPicker.attr('data-source',input.attr('id'));
  12. }else{
  13. partPicker = $('[data-source="'+input.attr('id')+'"]');
  14. }
  15. input.change(function(){
  16. partPicker.prop('disabled',true).val('Chargement...');
  17. $.action({
  18. action : 'get_part_by_id',
  19. id : input.val(),
  20. before : input.attr('data-before'),
  21. },function(r){
  22. partPicker.prop('disabled',input.prop('disabled'));
  23. if(r.part && r.part.label){
  24. partPicker.val(r.part.label);
  25. }else{
  26. partPicker.val('');
  27. }
  28. });
  29. });
  30. if(input.val() !=''){
  31. partPicker.prop('disabled',true).val('Chargement...');
  32. $.action({
  33. action : 'get_part_by_id',
  34. id : input.val(),
  35. before : input.attr('data-before'),
  36. },function(r){
  37. partPicker.prop('disabled',input.prop('disabled'));
  38. if(r.part && r.part.label){
  39. partPicker.val(r.part.label);
  40. }else{
  41. partPicker.val('');
  42. }
  43. });
  44. }
  45. partPicker.keyup(function(){
  46. input.val('');
  47. });
  48. var parent = input.attr('data-parent');
  49. partPicker.autocomplete({
  50. action : 'autocomplete_part',
  51. data : {
  52. before : input.attr('data-before'),
  53. parent:function(){ return $(parent).val(); }
  54. },
  55. skin : function(item){
  56. var html = '';
  57. name = item.name;
  58. /*var re = new RegExp(partPicker.val(),"gi");
  59. name = item.name.replace(re, function (x) {
  60. return '<strong>'+x+'</strong>';
  61. });*/
  62. html += '<div class="part-logo d-inline mr-2"><img src="data:'+item.picture+'" class="avatar-mini avatar-rounded"></div>';
  63. html += '<div class="user-infos d-inline"><span>'+name+'</span>';
  64. html += '<div class="clear"></div>';
  65. return html;
  66. },
  67. highlight : function(item){
  68. return item;
  69. },
  70. onClick : function(selected,element){
  71. console.log(selected,element);
  72. var li = $(element).closest('li');
  73. li.attr('data-part',selected.id);
  74. li.find('.price input').val(selected.price);
  75. li.find('.brand input').val(selected.brand);
  76. li.find('.part-image').css('background','url(data:'+selected.picture+') 0% 0% / cover');
  77. li.find('.part-image').attr('data-stream','data:'+selected.picture);
  78. input.val(selected.id);
  79. input.trigger('change');
  80. },
  81. onBlur : function(selected,element){
  82. if(input.attr('data-force')!='false' && input.val()=='') partPicker.val('');
  83. if(partPicker.val()=='') {
  84. input.val('');
  85. }
  86. input.trigger('blur');
  87. }
  88. });
  89. }