-
Notifications
You must be signed in to change notification settings - Fork 6
/
logic.js
122 lines (104 loc) · 3.46 KB
/
logic.js
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
/**
* Created by synopia on 23/06/14.
*/
webix.editors.myselect = webix.extend({
focus:function(){},
getValue:function(){
return this.getInputNode().getSelectedId().id||this.getInputNode().getSelectedId()||"";
},
setValue:function(value){
var suggest = this.config.collection || this.config.options(logic.editingId);
var list = this.getInputNode();
if (suggest)
this.getPopup().getList().data.importData(suggest);
this.getPopup().show(this.node);
if(value){
webix.assert(list.exists(value), "Option with ID "+value+" doesn't exist");
if(list.exists(value)){
list.select(value);
list.showItem(value);
}
}else{
list.unselect();
list.showItem(list.getFirstId());
}
},
getInputNode:function(){
return this.getPopup().getList();
},
popupInit:function(popup){
popup.attachEvent("onValueSuggest", function(data){
webix.delay(function(){
webix.callEvent("onEditEnd",[data.id]);
});
});
popup.linkInput(document.body);
},
popupType:"richselect"
}, webix.editors.popup);
var logic = {
targetSpeed : 0,
editingId : null,
ratioTree : null,
recipeTree : null,
recipes : [],
init: function() {
logic.recipeTree = new Tree("recipe_tree");
logic.ratioTree = new Tree("ratio_tree");
},
selectInserters: function(id) {
var selectedInserters = [];
$.each(inserters, function(index, inserter) {
selectedInserters.push({ id: inserter.id, value:inserter.name+" ("+helpers.formatNumber(inserter.speed*60, 60)+" u/m)"});
});
return selectedInserters;
},
selectFactories : function(id) {
var item = model.treeLines[id.row].item;
var factories = helpers.findFactories(item);
var selectableFactories = [];
var itemSpeed = 1;
if( recipes[item] ) {
itemSpeed = recipes[item].speed;
}
$.each(factories, function(index, factory){
selectableFactories.push({ id:factory.id, value:factory.name+" ("+helpers.formatNumber(itemSpeed*factory.speed, 60)+" u/m)"});
});
return selectableFactories;
},
addRecipe : function(recipe) {
logic.recipes.push(recipe);
logic.updateRecipes();
},
removeRecipe : function(recipe) {
var index = logic.recipes.indexOf(recipe);
if( index!=-1 ) {
logic.recipes.splice(index)
logic.updateRecipes();
}
},
reset : function() {
logic.recipes = [];
logic.updateRecipes();
},
updateRecipes : function() {
model.init();
var recipeTree = $.map(logic.recipes, function(recipe) {
return model.buildProductionTree(null, recipe, 1);
});
var ratioTree = model.buildRatioTree();
$$("recipe_tree").clearAll();
$$("recipe_tree").parse(recipeTree);
$$("ratio_tree").clearAll();
$$("ratio_tree").parse(ratioTree);
logic.updateTargetSpeed();
},
updateTargetSpeed : function( targetSpeed ) {
if( targetSpeed ) {
var timeExponent = $$("selected_unit").getValue();
logic.targetSpeed = targetSpeed / Math.pow(60, timeExponent);
}
logic.recipeTree.updateTargetSpeed();
logic.ratioTree.updateTargetSpeed();
}
};