-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslideSet.js
452 lines (375 loc) · 11.4 KB
/
slideSet.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*builds an individual selector widget--the set is controlled by balancer (below)--
args are: id= HTML id of container in which they go, txt--text that will describe them
at the top of the widget, qual--actual name of attribute, value--original starting value,
master --the callback function to the balancer so it can report to it, and factor-
which represents the 'weight' the set is to have*/
function buildWidget(id, txt, qual, value, master, factor) {
var x = {};
x.val = value;
x.isLocked = false;
x.qual = qual;
x.off = 0;
x.max = 100;
x.trans = 0;
x.width = 1;
x.callBack = master;
x.trigger = false;
x.factor = factor;
var target = document.getElementById(id);
var inner = '';
//build widget structure here here
inner += '<div id="descriptq" class="descript">';
inner += '<span id="descSpanq" class="descSpan">';
inner += txt;
inner += '</span>';
inner += '</div>'; //end of description
inner += '<div id="holderq" class="holder">';
inner += '<div id="barq" class="bar">';
inner += '</div>';
inner += '</div>'; //end of holder
inner += '<div id="controlq" class="control">';
inner += '<span id="lockq" class="lock">LOCK';
inner += '</span>';
inner += '<input type ="checkbox" id="checkq" class="check" />';
inner += '<button id="minusBq" class="minusB">-</button>';
inner += '<button id="plusBq" class = "plusB">+</button>';
inner += '</span>';
inner += '<span id="counterq" class ="counter">0</span>';
inner += '</div>'; //end of control area
//append to parent
x.out = document.createElement('div');
x.out.className = "slider";
target.appendChild(x.out);
x.out.innerHTML = inner;
//get the refs and store them in x.l object
x.l = {};
var div = x.out.getElementsByTagName('div');
x.l.sign = div[0].firstChild;
x.l.holder = div[1];
x.l.bar = div[2];
var ctr = div[3];
var button = ctr.getElementsByTagName('button');
x.l.minus = button[0];
x.l.plus = button[1];
var inp = ctr.getElementsByTagName('input');
x.l.check = inp[0];
var display = ctr.getElementsByTagName('span');
x.l.display = display[1];
//
//get true cords
x.trueCords = function() {
x.off = 0;
/*
var node = x.l.holder;
do{
x.off += node.offsetLeft;
node = node.parentNode;
} while(node.nodeType == 3);
x.off = x.l.holder.parentNode.offsetWidth;
*/
//KLUDGE
x.off = document.getElementById('dashB').offsetLeft;
x.width = x.l.holder.offsetWidth;
//get translation factor
var divW = x.l.holder.offsetWidth;
x.trans = divW / 100;
}
x.l.holder.onclick = function(e) {
if (x.isLocked) {
return false;
}
x.trueCords();
var xx = e.clientX - x.off;
xx = xx / x.trans;
xx = Math.round(xx);
if (xx == 99) {
xx = 100;
}
if (xx == 1) {
xx = 0;
}
x.sendData(xx);
//x.l.display.innerHTML = val;
//x.l.bar.style.width = val + '%';
}
x.setVal = function(val) { //how the balancer sets the value
x.val = val;
var alt = val * x.factor;
alt = Math.round(alt);
var r = Math.round(x.val);
x.l.display.innerHTML = alt;
x.l.bar.style.width = r + '%';
}
x.l.plus.onclick = function() { //when + button is clicked
if (x.val == 100) {
return;
}
var tmp = x.val + (1 / x.factor);
x.sendData(tmp);
}
x.l.minus.onclick = function() { //when minus button is clicked
if (x.val < 1) {
return;
}
var tmp = x.val - (1 / x.factor);
x.sendData(tmp);
}
//locking
x.l.check.onclick = function() {
if (x.l.check.checked) {
x.isLocked = true;
} else {
x.isLocked = false;
}
}
/*whenever user interacts with the slider (other than to lock/unlock, it sends data
to the balancer through x.sendData*/
x.sendData = function(request) {
var m = {};
m['name'] = x.qual;
m['request'] = request;
m['val'] = x.val;
x.callBack(m);
}
x.message = function(txt) { //used to send error messages to replace description text
var fnc, m = x.l.sign.innerHTML;
x.l.sign.innerHTML = txt;
fnc = function() {
x.l.sign.innerHTML = m;
}
setTimeout(fnc, 3000); //replaces original text after 3 seconds
}
x.setText = function(txt) { //"manual" setting of description text for the widget
x.l.sign.innerHTML = txt;
}
x.setVal(value);
return x;
}
/*=================================================================*/
//The brain for the selector widgets
function balancer(a, b, c, d) {
var x = {};
x.data = {};
x.numLocked = 0;
x.quantity = 0;
x.locked = 0;
x.factor = 1;
/*put in here to harmonize with amended geothermal app
'set1dest, set1weight, set1, set1alias' is order of args */
x.doTrans = function(destination, weight, attArr, alArr) {
var out, value, tmp, i, len = attArr.length;
value = len / 100;
value = Math.round(value);
out = {};
out.id = destination;
out.factor = weight;
out.obj = [];
for (i = 0; i < len; i++) {
tmp = {};
tmp.qual = attArr[i];
tmp.txt = alArr[i];
tmp.val = value;
out.obj.push(tmp);
}
return out;
}
var input = x.doTrans(a, b, c, d);
//end of insert
var name, i, z, len = input.obj.length;
var target = input.id;
x.factor = input.factor / 100;
//this function is invoked whenever someone clicks on a slider--unless its locked
//in which case the slider ignores the user--the balancer decides how to adjust
//the values of the sliders in the set
x.info = function(m) {
var request = m.request,
name = m.name,
value = m.val;
var i, tmp, taken = 0,
direction, empty = 0,
active = 0,
available = 0;
if (request == value) { //no change from present setting--ignore
return;
}
direction = "push"; //shrinking or expanding a current value?
if (request > value) {
direction = "pull";
}
for (i in x.data) { //determine number of points that are locked
if (x.data[i].isLocked) {
taken += x.data[i].val;
continue;
}
if (x.data[i].val == 0) {
empty++;
}
active++;
}
if (active < 2) { //nothing to push to or pull from
return;
}
if (direction == "push" && ((active - empty) < 2)) {
x.data[name].message("NO ACTIVE BOXES TO PUSH TO");
return;
} //pushing to empties is not allowed
available = 100 - taken;
if (request > available) { //special case--sucks the others dry
x.data[name].setVal(available);
for (i in x.data) {
if ((x.data[i].isLocked) || (i == name)) {
continue;
}
x.data[i].setVal(0);
}
x.data[name].message("NOT ENOUGH UNLOCKED POINTS");
return false;
}
available = 100 - (taken + request);;
x.data[name].setVal(request);
x.synchSet(name, available);
}
//
x.synchSet = function(name, goal) {
//identify non-zero non-locked properties
var i, len, box = [],
total = 0,
ratio, tmp, off;
for (i in x.data) {
if (x.data[i].isLocked || x.data[i].val == 0 || (i == name)) {
continue;
}
box.push(i);
total += x.data[i].val;
}
ratio = goal / total;
len = box.length;
for (i = 0; i < len; i++) {
off = box[i];
tmp = x.data[off].val;
tmp = tmp * ratio;
if (tmp > 100) {
tmp = 100;
}
x.data[off].setVal(tmp);
}
}
//object generated when a calculation is made--returns object consisting
// of keys as attribute names and value as currently set by slider
x.giveSet = function() {
var i, out = {};
for (i in x.data) {
out[i] = (x.data[i].val * x.factor);
}
return out;
}
//will change text in description box
x.alterText = function(prop, txt) {
if (x.data[prop]) {
x.data[prop].setText(txt);
}
}
// create the widgets from the info in var input
for (i = 0; i < len; i++) {
z = input.obj[i];
name = z.qual;
x.data[name] = buildWidget(target, z.txt, name, z.val, x.info, x.factor);
x.quantity++;
}
// set initial values of the slider set to the same value
var www = 100 / len;
www = Math.round(www);
for (i in x.data) {
x.data[i].setVal(www);
}
return x;
}
//=================================master slide
makeMaster = function(id) {
var x = {};
var html = '<div class="masterHolder" style="width: 100%">';
html += '<div class="masterSelect" id = "masterSelect" style="width: 100%">';
html += '</div></div>';
html += '<input type="button" value="@" id = "set1">';
html += '<span id="leftSet">SET ONE: 50%</span>';
html += '<span id="rightSet">SET TWO: 50%</span>';
html += '<input type="button" value="@" id = "set2">';
var target = document.getElementById(id);
var cont = document.createElement('div');
cont.id = "masterSlide";
cont.className = "masterSlide";
target.appendChild(cont);
cont.innerHTML = html;
var slide = document.getElementById('masterSelect');
slide.style.height = '100%';
slide.style.width = '50%';
x.value = 50;
x.left = false;
x.right = false;
x.displayL = document.getElementById("leftSet");
x.displayR = document.getElementById("rightSet");
//user presses button for first set preference
document.getElementById('set1').onmousedown = function() {
if (x.value == 0) {
return;
}
x.left = true;
x.shiftLeft();
}
document.getElementById('set1').onmouseup = function() {
x.left = false;
}
document.getElementById('set1').onmouseout = function() {
x.left = false;
}
x.shiftLeft = function() {
if (x.value === 0 || x.left === false) {
return;
}
x.value--;
x.flash();
document.getElementById('masterSelect').style.width = x.value + '%';
setTimeout(x.shiftLeft, 100);
}
document.getElementById('set2').onmousedown = function() {
if (x.value == 100) {
return;
}
x.right = true;
x.shiftRight();
}
document.getElementById('set2').onmouseup = function() {
x.right = false;
}
document.getElementById('set2').onmouseout = function() {
x.right = false;
}
x.shiftRight = function() {
if (!x.right || x.value === 100) {
return;
}
x.value++;
x.flash();
document.getElementById('masterSelect').style.width = x.value + '%';
setTimeout(x.shiftRight, 100);
}
x.flash = function() {
x.displayL.innerHTML = 'SET ONE: ' + x.value + '%';
x.displayR.innerHTML = 'SET TWO: ' + (100 - x.value) + '%';
}
x.adjustOne = function(obj) {
var i, val = x.value / 100;
for (i in obj) {
obj[i] = obj[i] * val;
}
return obj;
}
x.adjustTwo = function(obj) {
var i, val = (100 - x.value) / 100;
for (i in obj) {
obj[i] = obj[i] * val;
}
return obj;
}
return x;
}