var weighValues = [500,200,100,50,20,10]; function onTemplateLoadedCallback(app) { $.each(weighValues, function(){ $("#weights").append($(Mustache.render(weightTemplate, {weight: this}))); }); $("#weights > .weight").draggable({helper: "clone"}); $("#leftScale").droppable({ accept: ".object", drop: function(event, ui) { if($(ui.draggable).hasClass("inScale")) return; var object = $(ui.draggable).clone(); object.data("weight", $(ui.draggable).data("weight")); object.addClass("inScale"); $("#leftScale").append(object); placeObject($("#leftScale"), object); refreshScales(); object.draggable({ stop: function(event, ui) { if($(ui.helper).hasClass("onOut")) { $(ui.helper).remove(); refreshScales(); } } }); }, out: function(event, ui) { $(ui.draggable).addClass("onOut"); }, over: function(event, ui) { $(ui.draggable).removeClass("onOut"); } }); $("#rightScale").droppable({ accept: ".weight.right", drop: function(event, ui) { if($(ui.draggable).hasClass("inScale")) return; var object = createWeight(app, $(ui.draggable).data("weight")); $("#rightScale").append(object); placeObject($("#rightScale"), object); refreshScales(); }, out: function(event, ui) { $(ui.draggable).addClass("onOut"); }, over: function(event, ui) { $(ui.draggable).removeClass("onOut"); } }); $("#objects button[role=add]").click(function(){ var object = createObject(app); $(this).before(object); }); } function addObjectWithId(app, id) { var objects = app.parameters.value("Objects"); app.parameters.value("Objects", objects === undefined ? id : objects+","+id); } function removeObjectWithId(app, id) { var objects = app.parameters.value("Objects"); objects = objects.split(","); for(var i=0 ; i .object").remove(); refreshScales(); var objects = app.parameters.value("Objects"); if(objects === undefined) { var object = createObject(app); setWeightFor(app.parameters, object.attr("id"), 5); objects = app.parameters.value("Objects") } if(objects !== undefined) { objects = objects.split(","); for(var i=0 ; i= 0 ? Math.min(maxAngle, angle) : Math.max(-maxAngle, angle); var rad = angle * Math.PI / 180; var w = $("#tray").width()/2; var dx = w - (w * Math.cos(rad)); var dy = -(w * Math.sin(rad)); log("Rotate to "+angle); log("Tanslate to ["+dx+" , "+dy+"]"); var rotate = "rotate("+angle+"deg)"; var leftTranslate = "translate("+dx+"px, "+dy+"px)"; var rightTranslate = "translate("+(-dx)+"px, "+(-dy)+"px)"; $("#tray").css("transform", rotate); $("#leftScale").css("transform", leftTranslate); $("#rightScale").css("transform", rightTranslate); } function getWeightForSelector(selector) { var weight = 0; $(selector).each(function(){ var temp = $(this).data("weight"); if(temp !== undefined) weight += parseInt(temp); }); return weight; } function getLeftWeight() { return getWeightForSelector("#leftScale > div"); } function getRightWeight() { return getWeightForSelector("#rightScale > div"); } function reloadCallback(parameter) { if(parameter === undefined) reloadApp(this); } var maxAngle; var maxWeight; $(document).ready(function(){ var w = $("#scales").width(); var h = $("#scales").height(); maxAngle = 17;//Math.atan((h/2)/(w/2)) * (180/Math.PI); maxWeight = 10; log(w+"-"+h+":"+maxAngle); var callbacks = { onTemplatesLoaded: onTemplateLoadedCallback, onEdit: reloadApp, onView: reloadApp }; init(reloadCallback, {toolbar: toolbarTemplate, parameters: parametersTemplate}, callbacks); });