1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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<objects.length ; i++) {
if(id === objects[i]) {
objects.splice(i, 1);
break;
}
}
app.parameters.value("Objects", objects.join(","));
}
function addWeightWithId(app, id) {
var objects = app.parameters.value("Weights");
app.parameters.value("Weights", objects === undefined ? id : objects+","+id);
}
function removeWeightWithId(app, id) {
var objects = app.parameters.value("Weights");
objects = objects.split(",");
for(var i=0 ; i<objects.length ; i++) {
if(id === objects[i]) {
objects.splice(i, 1);
break;
}
}
app.parameters.value("Weights", objects.join(","));
}
function objectForGUID(app, guid) {
window.object = guid;
var objectUi = $(Mustache.render(objectTemplate, window));
var weight = getWeightFor(app.parameters, guid);
if(!app.onEdit && (weight == undefined || $.trim(weight) == ""))
weight = weighValues[Math.floor(Math.random()*weighValues.length)];
if(weight !== undefined) {
objectUi.data("weight", weight);
objectUi.find("input[name=weight]").val(weight);
}
if(app.onEdit) {
objectUi.find("input[name=weight]").change(function(event) {
var o = objectUi;
o.data("weight", $(this).val());
setWeightFor(app.parameters, o.attr("id"), $(this).val());
});
objectUi.find("button[role=remove]").click(function(){
removeObjectWithId(app, guid);
objectUi.remove();
});
objectUi.find("button[role=duplicate]").click(function(){
$("#objects button[role=add]").before(createObject(app, guid));
});
}else{
objectUi.find("input[name=weight]").attr("disabled","disabled");
objectUi.draggable({
containment: "#scene",
scroll: false,
helper: "clone"
});
}
return objectUi;
}
function weightForGUID(app, guid) {
var weight = getWeightValueFor(app.parameters, guid);
window.object = guid;
window.weight = weight;
var objectUi = $(Mustache.render(weightTemplate, window));
var position = getPositionFor(app.parameters, guid);
objectUi.css("left", position.left);
objectUi.css("top", position.top);
objectUi.addClass("inScale");
if(app.onEdit) {
objectUi.draggable({
stop: function(event, ui) {
if($(ui.helper).hasClass("onOut")) {
removeWeightWithId(app, $(ui.helper).attr('id'));
$(ui.helper).remove();
refreshScales();
}else{
var id = ui.helper.attr("id");
var left = ui.position.left ;
var top = ui.position.top;
log(left+"-"+top);
setPositionFor(app.parameters, id, ui.position);
}
}
});
}else{
}
return objectUi;
}
function placeObject($container, $object) {
var width = $object.outerHeight();
var height = $object.outerHeight()+15;
var count = $container.children().size() - 1;
var left = width * (count % 4) + 5;
var bottom = height * (Math.floor(count / 4)) + 12;
log("Place at ["+left+" , "+bottom+"]");
$object.css("left", left+"px").css("bottom", bottom+"px");
}
function createObject(app, duplicateID) {
var id = app.utils.guid();
addObjectWithId(app, id);
//TODO Duplication : Recupérer les images du dupliqué
return objectForGUID(app, id);
}
function createWeight(app, weight) {
var id = app.utils.guid();
addWeightWithId(app, id);
setWeightValueFor(app.parameters, id, weight);
return weightForGUID(app, id);
}
function getWeightFor(parameters, id) {
return parameters.value("Object#"+id+"Weight");
}
function setWeightFor(parameters, id, weight) {
parameters.value("Object#"+id+"Weight", weight);
}
function getWeightValueFor(parameters, id) {
return parameters.value("Weight#"+id+"Weight");
}
function setWeightValueFor(parameters, id, weight) {
parameters.value("Weight#"+id+"Weight", weight);
}
function getPositionFor(parameters, id) {
var left = parameters.value("Weight#"+id+"left");
var top = parameters.value("Weight#"+id+"top");
return {left: parseInt(left), top: parseInt(top)};
}
function setPositionFor(parameters, id, position) {
parameters.value("Weight#"+id+"left", position.left);
parameters.value("Weight#"+id+"top", position.top);
}
function reloadApp(app) {
$("#leftScale, #rightScale").empty();
$("#objects > .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<objects.length ; i++) {
var id = objects[i];
var object = objectForGUID(app, id);
$("#objects button[role=add]").before(object);
}
}
var weights = app.parameters.value("Weights");
if(weights === undefined) {
createWeight(app, 20);
weights = app.parameters.value("Weights");
}
if(weights !== undefined) {
weights = weights.split(",");
for(var i=0 ; i<weights.length ; i++) {
var id = weights[i];
var weight = weightForGUID(app, id);
$("#rightScale").append(weight);
placeObject($("#rightScale"), weight);
}
}
refreshScales();
if(app.onEdit) {
}
}
function refreshScales() {
var leftWeight = getLeftWeight();
var rightWeight = getRightWeight();
var dWeight = rightWeight - leftWeight;
var angle = maxAngle * dWeight / maxWeight;
angle = angle >= 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);
});