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.

150 lines
3.2 KiB

  1. package ;
  2. import Common;
  3. import js.JQuery;
  4. /**
  5. *
  6. * Tag products with categories
  7. *
  8. * @author fbarbut<francois.barbut@gmail.com>
  9. */
  10. @:keep
  11. class Tagger
  12. {
  13. var contractId : Int;
  14. var data:TaggerInfos;
  15. var pids : Array<Int>; //selected product Ids
  16. public function new(cid:Int)
  17. {
  18. contractId = cid;
  19. pids = [];
  20. }
  21. public function init() {
  22. var req = new haxe.Http("/product/categorizeInit/"+contractId);
  23. req.onData = function(_data) {
  24. data = haxe.Json.parse(_data);
  25. render();
  26. }
  27. req.request();
  28. }
  29. function render() {
  30. var html = new StringBuf();
  31. html.add("<table class='table'>");
  32. for (p in data.products) {
  33. html.add("<tr class='p"+p.product.id+"'>");
  34. var checked = Lambda.has(pids,p.product.id) ? "checked" : "";
  35. html.add('<td><input type="checkbox" name="p${p.product.id}" $checked/></td>');
  36. html.add("<td>" + p.product.name+"</td>");
  37. var tags = [];
  38. //trace('product tags ${p.categories} from tags ${data.categories}');
  39. for (c in p.categories) {
  40. //trouve le nom du tag
  41. var name = "";
  42. var color = "";
  43. for (gc in data.categories) {
  44. for ( t in gc.tags) {
  45. if (c == t.id) {
  46. name = t.name;
  47. color = gc.color;
  48. break;
  49. }
  50. }
  51. }
  52. //var bt = App.jq("<a>[X]</a>")
  53. tags.push("<span class='tag t"+c+"' style='background-color:"+color+";cursor:pointer;'>"+name+"</span>");
  54. }
  55. html.add("<td class='tags'>"+ tags.join(" ") +"</td>");
  56. html.add("</tr>");
  57. }
  58. html.add("</table>");
  59. App.jq("#tagger").html(html.toString());
  60. App.jq("#tagger .tag").click(function(e) {
  61. var el : js.html.Element = cast e.currentTarget;
  62. //find tag Id
  63. var tid = Std.parseInt(el.getAttribute('class').split(" ")[1].substr(1));
  64. //find product Id
  65. var pid = Std.parseInt(el.parentElement.parentElement.getAttribute('class').substr(1));
  66. //remove element
  67. el.remove();
  68. //datas
  69. remove(tid,pid);
  70. });
  71. }
  72. public function add() {
  73. var tagId = Std.parseInt(App.jq("#tag").val());
  74. if (tagId == 0) js.Browser.alert("Impossible de trouver la catégorie selectionnée");
  75. pids = [];
  76. for ( e in App.jq("#tagger input:checked").elements() ) {
  77. pids.push(Std.parseInt(e.attr("name").substr(1)));
  78. }
  79. if (pids.length == 0) js.Browser.alert("Sélectionnez un produit afin de pouvoir lui attribuer une catégorie");
  80. for (p in pids) {
  81. addTag(tagId, p);
  82. }
  83. render();
  84. }
  85. public function remove(tagId:Int,productId:Int) {
  86. //data
  87. for ( p in data.products) {
  88. if (p.product.id == productId) {
  89. for ( t in p.categories) {
  90. if (t == tagId) p.categories.remove(t);
  91. }
  92. }
  93. }
  94. }
  95. function addTag(tagId:Int, productId:Int) {
  96. //check for doubles
  97. for ( p in data.products) {
  98. if (p.product.id == productId) {
  99. for (t in p.categories) {
  100. if (t == tagId) return;
  101. }
  102. }
  103. }
  104. //data
  105. for ( p in data.products) {
  106. if (p.product.id == productId) {
  107. p.categories.push(tagId);
  108. break;
  109. }
  110. }
  111. }
  112. public function submit() {
  113. var req = new haxe.Http("/product/categorizeSubmit/" + contractId);
  114. req.addParameter("data", haxe.Json.stringify(data));
  115. req.onData = function(_data) {
  116. js.Browser.alert(_data);
  117. }
  118. req.request(true);
  119. }
  120. }