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.
 
 
 
 
 
 

151 lines
3.2 KiB

package ;
import Common;
import js.JQuery;
/**
*
* Tag products with categories
*
* @author fbarbut<francois.barbut@gmail.com>
*/
@:keep
class Tagger
{
var contractId : Int;
var data:TaggerInfos;
var pids : Array<Int>; //selected product Ids
public function new(cid:Int)
{
contractId = cid;
pids = [];
}
public function init() {
var req = new haxe.Http("/product/categorizeInit/"+contractId);
req.onData = function(_data) {
data = haxe.Json.parse(_data);
render();
}
req.request();
}
function render() {
var html = new StringBuf();
html.add("<table class='table'>");
for (p in data.products) {
html.add("<tr class='p"+p.product.id+"'>");
var checked = Lambda.has(pids,p.product.id) ? "checked" : "";
html.add('<td><input type="checkbox" name="p${p.product.id}" $checked/></td>');
html.add("<td>" + p.product.name+"</td>");
var tags = [];
//trace('product tags ${p.categories} from tags ${data.categories}');
for (c in p.categories) {
//trouve le nom du tag
var name = "";
var color = "";
for (gc in data.categories) {
for ( t in gc.tags) {
if (c == t.id) {
name = t.name;
color = gc.color;
break;
}
}
}
//var bt = App.jq("<a>[X]</a>")
tags.push("<span class='tag t"+c+"' style='background-color:"+color+";cursor:pointer;'>"+name+"</span>");
}
html.add("<td class='tags'>"+ tags.join(" ") +"</td>");
html.add("</tr>");
}
html.add("</table>");
App.jq("#tagger").html(html.toString());
App.jq("#tagger .tag").click(function(e) {
var el : js.html.Element = cast e.currentTarget;
//find tag Id
var tid = Std.parseInt(el.getAttribute('class').split(" ")[1].substr(1));
//find product Id
var pid = Std.parseInt(el.parentElement.parentElement.getAttribute('class').substr(1));
//remove element
el.remove();
//datas
remove(tid,pid);
});
}
public function add() {
var tagId = Std.parseInt(App.jq("#tag").val());
if (tagId == 0) js.Browser.alert("Impossible de trouver la catégorie selectionnée");
pids = [];
for ( e in App.jq("#tagger input:checked").elements() ) {
pids.push(Std.parseInt(e.attr("name").substr(1)));
}
if (pids.length == 0) js.Browser.alert("Sélectionnez un produit afin de pouvoir lui attribuer une catégorie");
for (p in pids) {
addTag(tagId, p);
}
render();
}
public function remove(tagId:Int,productId:Int) {
//data
for ( p in data.products) {
if (p.product.id == productId) {
for ( t in p.categories) {
if (t == tagId) p.categories.remove(t);
}
}
}
}
function addTag(tagId:Int, productId:Int) {
//check for doubles
for ( p in data.products) {
if (p.product.id == productId) {
for (t in p.categories) {
if (t == tagId) return;
}
}
}
//data
for ( p in data.products) {
if (p.product.id == productId) {
p.categories.push(tagId);
break;
}
}
}
public function submit() {
var req = new haxe.Http("/product/categorizeSubmit/" + contractId);
req.addParameter("data", haxe.Json.stringify(data));
req.onData = function(_data) {
js.Browser.alert(_data);
}
req.request(true);
}
}