You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.7 KiB

  1. package react.product;
  2. import react.ReactDOM;
  3. import react.ReactComponent;
  4. import react.ReactMacro.jsx;
  5. import Common;
  6. import react.Typeahead;
  7. typedef ProductInputProps = {
  8. formName:String,
  9. txpProductId:Int,
  10. productName:String,
  11. }
  12. typedef ProductInputState = {
  13. txpProductId:Int,
  14. productName:String,
  15. categoryId:Int,
  16. breadcrumb:String,
  17. }
  18. /**
  19. * Product Text Input with autocompletion
  20. *
  21. * @author fbarbut
  22. */
  23. class ProductInput extends react.ReactComponentOfPropsAndState<ProductInputProps,ProductInputState>
  24. {
  25. public static var DICO : TxpDictionnary = null;
  26. var options : Array<{id:Int,label:String}>;
  27. public function new(props:ProductInputProps)
  28. {
  29. super(props);
  30. options = [];
  31. this.state = {
  32. txpProductId : props.txpProductId,
  33. productName : props.productName,
  34. categoryId : 0,
  35. breadcrumb : ""
  36. };
  37. }
  38. override public function render(){
  39. var inputName :String = props.formName+"_name";
  40. var txpProductInputName :String = props.formName+"_txpProductId";
  41. return jsx('
  42. <div className="row">
  43. <div className="col-md-8">
  44. <AsyncTypeahead
  45. placeholder="Saisissez un nom de produit"
  46. options=$options
  47. onSearch=$onSearch
  48. minLength={3}
  49. style={{width:"350px"}}
  50. onChange=$onChange
  51. onInputChange=$onInputChange
  52. selected={["${state.productName}"]}
  53. isLoading=$true
  54. />
  55. <div className = "txpProduct" > ${state.breadcrumb}</div>
  56. <input className="txpProduct" type="hidden" name="$txpProductInputName" value="${state.txpProductId}" />
  57. <input className="txpProduct" type="hidden" name="$inputName" value="${state.productName}" />
  58. </div>
  59. <div className="col-md-4">
  60. <img ref="image" className="img-thumbnail" />
  61. </div>
  62. </div>
  63. ');
  64. }
  65. /**
  66. * Called when typing is stopped
  67. * @param o
  68. */
  69. function onSearch(o){
  70. //trace("on search : "+o);
  71. }
  72. /**
  73. * Each time a single letter change in the input
  74. * @param input
  75. */
  76. function onInputChange(input:String){
  77. trace('on input change $input');
  78. this.setState({productName:input});
  79. }
  80. /**
  81. * Called when an item is selected in suggestions
  82. */
  83. function onChange(selection:Array<{label:String,id:Int}>){
  84. if (selection == null || selection.length == 0) return;
  85. trace("on change "+selection[0]);
  86. var product = Lambda.find(DICO.products, function(x) return x.id == selection[0].id);
  87. setTaxo(product);
  88. this.setState({productName:selection[0].label});
  89. }
  90. /**
  91. * init typeahead auto-completion features when component is mounted
  92. */
  93. override function componentDidMount(){
  94. //get dictionnary
  95. if (DICO == null){
  96. var r = new haxe.Http("/product/getTaxo");
  97. r.onData = function(data){
  98. //load dico
  99. DICO = haxe.Unserializer.run(data);
  100. for ( p in DICO.products){
  101. options.push({label:p.name,id:p.id});
  102. }
  103. //default values of input
  104. if (props.txpProductId != null){
  105. var txp = Lambda.find(DICO.products, function(x) return x.id == props.txpProductId);
  106. setTaxo(txp);
  107. }
  108. };
  109. r.request();
  110. }
  111. }
  112. function setTaxo(txp:{id:Int, name:String, category:Int, subCategory:Int}){
  113. if (txp == null) return;
  114. //trace(txp);
  115. this.setState({
  116. categoryId:txp.category,
  117. txpProductId:txp.id,
  118. breadcrumb:getBreadcrumb(txp)/*,
  119. productName:product.name //do not override product name ! */
  120. });
  121. this.refs.image.src="/img/taxo/cat"+txp.category+".png";
  122. }
  123. /**
  124. * generate string like "fruits & vegetables / vegetables / carrots"
  125. * @param name
  126. */
  127. function getBreadcrumb(product){
  128. //cat
  129. var str = DICO.categories.get(product.category).name;
  130. if (product.subCategory != null){
  131. str += " / " + DICO.subCategories.get(product.subCategory).name;
  132. }
  133. str += " / " + product.name;
  134. return str;
  135. }
  136. }