-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrids-init.js
More file actions
300 lines (199 loc) · 8.31 KB
/
grids-init.js
File metadata and controls
300 lines (199 loc) · 8.31 KB
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
var grids_init = {
init: function(){
// 1. Table of Grids (listing on the left)
// 1.1 - Buttons underneath grids listing table
// Button Handler: ADD
// new grid by duplicating hidden template
$("#grids-buttons #add").click(function(){
if(grids.selected_row_i != undefined){
//add new grid and select its (new) row index...
grids.selected_row_i = DM.grid_Add();
grids.regenerate_table(); //refresh view
}
});
// Button Handler: DELETE
// removes selected
$("#grids-buttons #delete").click(function(){
if(grids.selected_row_i != undefined){
DM.grid_Delete(grids.selected_row_i);
//now, leave selected either replacing row in same position, or final row
grids.selected_row_i = Math.min(grids.selected_row_i, DM.gridArray.length-1);
grids.regenerate_table(); //refresh view
}
});
// 2. Grids control pane
// 2.1 - Line sets control boxes
// 2.1.1 - DOM manipulations to initialise display
// Create box 2 by cloning box 1 (cuts down duplication in the HTML)
$("#line-set-2").append($("#line-set-1").clone().html());
$("#line-set-2 .title").text("Line Set 2");
// Move SVGs into position
$("#svg-angle-1").appendTo("#line-set-1 .angle-indicator-icon");
$("#svg-angle-2").appendTo("#line-set-2 .angle-indicator-icon");
// 2.1.2 - Functionality of ALL of the Grid Properties SmartInput Boxes
// Part A - Define the generalised on_Change callback
var GA_mod = function(ls, key){
if(grids.selected_row_i != undefined){
var Grid_i = DM.gridArray[grids.selected_row_i];
var old_val = Grid_i.line_sets[ls][key];
$("#line-set-"+(ls+1)+" .ls-param."+key+" input").SmartInput("update", {change_underlying_from_DOM: true});
var new_val = Grid_i.line_sets[ls][key];
//For angle changes (1) animate svg (2) logic for locking angles
if(key=="angle"){
grids.update_preview_svg_angle(ls, new_val);
if(grids.lock_angles){
Grid_i.line_sets[1-ls].angle -= (new_val - old_val);
// visually update other angle
$("#line-set-"+(2-ls)+" .ls-param.angle input").SmartInput("update", {data_change: true});
grids.update_preview_svg_angle(1-ls, Grid_i.line_sets[1-ls].angle);
}
}
//animated grid change...
grids.grid_change();
}
};
// Part B - Apply it in SmartInput initialisation for each (of the 6) elements
[0,1].forEach(function(ls) {
[{k:"spacing"}, {k:"shift"}, {k:"angle"}].forEach(function(TY) {
//whether %, px or qty, minimum value of 1 for spacing. It prevents script freezing.
// however, I don't really like the offset of 1 this causes on the steps.
var dco_dict = {
"spacing": {min: 1},
"angle": undefined,
"shift": {steps: [1, 10, 25]}
};
//INITIATE
var $input = $("#line-set-"+(ls+1)+" .ls-param."+TY.k+" input");
$input.SmartInput({
//underlying_obj: - this property set on row-select, it varies with row...
underlying_key: TY.k,
data_class: "dimentionless", // Must supply something. Correct unit set on row-select
data_class_override: dco_dict[TY.k],
underlying_from_DOM_onChange: false, // cannot do this - full logic uses old value
cb_change: function(){GA_mod(ls, TY.k);}//all the graphical change...
});
});
});
// 2.1.3 - Functionality of Units change MutexActionLinks (for SPACING only) -- px / % / qty
// Part A - Define the on_Click callback
var GAu_mod = function(ls, units_new){
if(grids.selected_row_i != undefined){
// CONVERT the spacing value (such that it is equivalent with new units)
// This function updates the object it is passed by reference ("side-effect")
var Grid_i = DM.gridArray[grids.selected_row_i];
grids.spacing_unit_objectUpdater(Grid_i.line_sets[ls], units_new);
$("#line-set-"+(ls+1)+" .ls-param.spacing input").SmartInput("update", {
UI_enable: false,
data_change: true,
new_dc_key: units_new // is there a way this parameter could not be passed. It is in the data?
});
//also set the relevant action-link...
var en_state = [units_new != 'pixels', units_new != 'percent', units_new != 'quantity'];
$("#line-set-"+(ls+1)+" .px-pc-qty.act-mutex").MutexActionLink(en_state);
}
};
// Part B - Apply it to MutexActionLinks of Line Set 1 and Line Set 2
[0,1].forEach(function(ls) {
//determine starting state
var Grid_i = DM.gridArray[grids.selected_row_i];
var my_unit = Grid_i.line_sets[ls].spacing_unit;
var en_state = [my_unit != 'pixels', my_unit != 'percent', my_unit != 'quantity'];
$("#line-set-"+(ls+1)+" .px-pc-qty.act-mutex").MutexActionLink(en_state, [
function(){GAu_mod(ls, 'pixels');},
function(){GAu_mod(ls, 'percent');},
function(){GAu_mod(ls, 'quantity');},
]);
});
// 2.2 - above line sets control boxes
// 2.2.1 - switch between 1D and 2D grid
$("#lines-v-grid.act-mutex").MutexActionLink([1, 0], [
function(){ grids.set_grid_to_2D(false); grids.grid_change(); },
function(){ grids.set_grid_to_2D(true); grids.grid_change(); }
]);
// 2.3 - beneath line sets control boxes
// 2.3.1 - Convert Grid - Iso / Square / Diamond
// Part A - Define grid conversion callback
var AdjustGridToPresetType = function(type){
var Grid_i = DM.gridArray[grids.selected_row_i];
var LS = Grid_i.line_sets
// (1.) Manage angles...
if(type == "iso"){
if(LS[0].angle > 60){
LS[0].angle -=60;//rotating back by 60 keeps within range...
}
LS[1].angle = 60 - LS[0].angle;
grids.lock_angles = false;//resets lock
$("#link-angles.act-mutex").MutexActionLink([1, 0]);//show unlinked
}
if(type == "squ"){
LS[1].angle = 90 - LS[0].angle;
grids.lock_angles = false;//resets lock
$("#link-angles.act-mutex").MutexActionLink([1, 0]);//show unlinked
}
if(type == "dia"){
var isRect = ((LS[0].angle == 0) && (LS[1].angle == 90)) || ((LS[0].angle == 90) && (LS[1].angle == 0));
if(grids.lock_angles || isRect){
var ave_angle = (LS[0].angle + LS[1].angle)/2;
LS[0].angle = ave_angle;
LS[1].angle = ave_angle;
}else{
if(LS[0].angle != 0){
LS[1].angle = LS[0].angle;
}
}
}
// (2.) Manage spacings. All options involve setting them to equal.
// we cannot possibly use spacing units of quantity.
// So unless both dimentions are in units of percent, set them both to pixels:
if((LS[0].spacing_unit != "percent")||(LS[1].spacing_unit != "percent")){
GAu_mod(0, 'pixels');
GAu_mod(1, 'pixels');
}
// set spacings equal, and set spacing units equal.
var av_spacing = (LS[0].spacing + LS[1].spacing)/2;
LS[0].spacing = av_spacing;
LS[1].spacing = av_spacing;
//update display. Input elems and grid.
grids.update_all_input_elements_values(Grid_i);
grids.grid_change();
};
// Part B - Apply function to the 3-way MutexActionLink
$("#preset-grid.act-mutex").MutexActionLink([1, 1, 1], [
function(){AdjustGridToPresetType("iso")},
function(){AdjustGridToPresetType("squ")},
function(){AdjustGridToPresetType("dia")}
]);
// 2.3.2 - Lock & Unlock angles
$("#link-angles.act-mutex").MutexActionLink([1, 0], [
function(){grids.lock_angles = true;},
function(){grids.lock_angles = false;}
]);
// 2.4 - Preview Options box
// 2.4.1 - Show vs hide intersection points...
$("#show-points.act-mutex").MutexActionLink([0, 1], [//firstly initialise
function(){ grids.update_grid_intersection_points({display: false}); },
function(){ grids.update_grid_intersection_points({display: true }); }
]);
grids.setMutexState_intersection_points();//disable the dots
// 2.4.2 - Overall visibility controls
// Button Handler: SHOW
$("#Tab-grid .button#show").click(function(){
if((grids.selected_row_i != undefined) && (!$(this).hasClass("ui-disabled"))){
grids.showing_preview = true;
grids.grid_change();
$(this).addClass("ui-disabled");
grids.setMutexState_intersection_points();//allow the dots
}
});
// Action Link Handler: HIDE
$("#Tab-grid .action-link#hide").click(function(){
grids.grid_change({hide: true});
grids.showing_preview = false;
$("#Tab-grid .button#show").removeClass("ui-disabled");
grids.setMutexState_intersection_points();//disable the dots
});
// 3. Background SVG
//Call this last so as to ensure pre-requestite initialisation (of SmartInputs etc.) has happened.
grids.regenerate_table();
}
};