-
Notifications
You must be signed in to change notification settings - Fork 8
/
render_html_conveyor.js
846 lines (837 loc) · 30.2 KB
/
render_html_conveyor.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//var g_menu_options_default=[[1,"impossible"],[1,"not-available"],[1,"end-level-manuf"],[1,"intermediate-manuf"],[1,"recommended-runs"]];
//var g_menu_options_default_len=5;
//var g_menu_sort_default='name';
//var g_menu_sort_order_default=1; // 0-desc, 1-asc
// Conveyor Options storage (prepare)
ls = window.localStorage;
//-----------
// утилиты
//-----------
function numLikeEve(x) {
if (x < 1.0) return x;
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
(function() {
/**
* Корректировка округления десятичных дробей.
*
* @param {String} type Тип корректировки.
* @param {Number} value Число.
* @param {Integer} exp Показатель степени (десятичный логарифм основания корректировки).
* @returns {Number} Скорректированное значение.
*/
function decimalAdjust(type, value, exp) {
// Если степень не определена, либо равна нулю...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// Если значение не является числом, либо степень не является целым числом...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Сдвиг разрядов
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Обратный сдвиг
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// Десятичное округление к ближайшему
if (!Math.round10) {
Math.round10 = function(value, exp) {
return decimalAdjust('round', value, exp);
};
}
// Десятичное округление вниз
if (!Math.floor10) {
Math.floor10 = function(value, exp) {
return decimalAdjust('floor', value, exp);
};
}
// Десятичное округление вверх
if (!Math.ceil10) {
Math.ceil10 = function(value, exp) {
return decimalAdjust('ceil', value, exp);
};
}
})();
function eveCeiling(isk) {
if (isk < 100.0) ;
else if (isk < 1000.0) isk = Math.ceil10(isk * 10.0) / 10.0;
else if (isk < 10000.0) isk = Math.ceil10(isk);
else if (isk < 100000.0) isk = Math.round10(isk+5, 1);
else if (isk < 1000000.0) isk = Math.round10(isk+50, 2);
else if (isk < 10000000.0) isk = Math.round10(isk+500, 3);
else if (isk < 100000000.0) isk = Math.round10(isk+5000, 4);
else if (isk < 1000000000.0) isk = Math.round10(isk+50000, 5);
else if (isk < 10000000000.0) isk = Math.round10(isk+500000, 6);
else if (isk < 100000000000.0) isk = Math.round10(isk+5000000, 7);
else isk = null;
return isk;
}
function changeElemVisibility(el, show){
if(show)
el.removeClass('hidden');
else
el.addClass('hidden');
}
//-----------
// работа с локальным хранилищем
//-----------
function setOption(grp, opt, val) {
var o = grp+':'+opt;
ls.setItem(o, val);
}
function resetOptionToDefault(grp, opt, def) {
var o = grp+':'+opt;
if (!ls.getItem(o)) ls.setItem(o, def);
}
function getOption(grp, opt) { // for checkboxes
var o = grp+':'+opt;
val = (ls.getItem(o) == 1) ? 1 : 0;
return val;
}
function getOptionValue(grp, opt) { // for strings and numbers
var o = grp+':'+opt;
var val = ls.getItem(o);
return (val === null) ? '' : val;
}
//function isMenuActivated(btn){
// return btn.hasClass('qind-btn-active');
//}
function toggleMenuMarker(btn, show){
if(show){
btn.addClass('qind-btn-active');
changeElemVisibility(btn.find('span'), 1);
}else{
btn.removeClass('qind-btn-active');
changeElemVisibility(btn.find('span'), 0);
}
}
function toggleSortMarkers(mode, asc){
$('button.qind-btn-sort').each(function() {
if ($(this).attr('qind-group') == mode) {
//changeElemVisibility($(this).find('span.symb'), 1);
changeElemVisibility($(this).find('span.nm'), 1);
$(this).addClass('active');
changeElemVisibility($(this).find('span.asc'), asc);
changeElemVisibility($(this).find('span.desc'), !asc);
}
else {
//changeElemVisibility($(this).find('span.symb'), 1);
changeElemVisibility($(this).find('span.nm'), 0);
$(this).removeClass('active');
changeElemVisibility($(this).find('span.asc'), 0);
changeElemVisibility($(this).find('span.desc'), 0);
}
});
}
function rebuildOptionsMenu() {
$('a.qind-btn-settings').each(function() {
var opt = $(this).attr('qind-group');
toggleMenuMarker($(this), getOption('option', opt));
});
toggleSortMarkers(
getOptionValue('sort', 'mode'),
(getOption('sort', 'order') == 1) ? true : false
);
}
//-----------
//сортировка содержимого таблицы конвейера
//-----------
const g_tbl_summary_unused_priority = -1;
const g_tbl_summary_priority_multiplier = 100000; // приоритетов десять: 0..9
const g_tbl_summary_activity_multiplier = Math.floor(g_tbl_summary_priority_multiplier / 10); // activity от 1 до 9
const g_tbl_summary_half_activity = Math.floor(g_tbl_summary_activity_multiplier / 2);
//-----------
function getSortKey(x, asc, what) { // what: 0-name, 1-duration, 2-priority, 3-demand, 4-profit
var row = $(x);
var priority = g_tbl_summary_unused_priority;
var activity = 0;
var val = 0;
if (row.hasClass('row-conveyor')) {
var tag = row.data('tag');
if (tag === undefined) return 0;
priority = (tag.p === null) ? g_tbl_summary_unused_priority : tag.p;
activity = asc ? Math.min(...tag.a) : Math.max(...tag.a);
val = 0;
} else {
var sort = row.data('sort');
if (sort === undefined) return 0;
priority = sort.p;
activity = asc ? Math.min(...sort.a) : Math.max(...sort.a);
switch (what) {
case 0: // name
priority = g_tbl_summary_unused_priority;
let txt = row.find('qname').html();
if (sort.lp == undefined)
val = asc ? txt : ('±' + txt);
else // незапускаемые работы/строки всегда внизу
val = asc ? ('±' + txt) : txt;
break;
case 2: // priority
if (sort.lp == undefined)
val = asc ? sort.n : (g_tbl_summary_half_activity + sort.n);
else // незапускаемые работы/строки всегда внизу
val = asc ? (g_tbl_summary_half_activity + sort.n) : sort.n;
break;
case 1: // duration (аналогичено profit)
priority = g_tbl_summary_unused_priority;
if (!(sort.d == undefined))
val = sort.d;
else if (!(sort.d1 == undefined))
val = (asc ? sort.d1 : sort.d2);
else // duration неизвестен (это не чертёж для запуска) - всегда внизу
val = -sort.n;
break;
case 4: // profit (аналогичено duration)
priority = g_tbl_summary_unused_priority;
if (!(sort.i == undefined))
val = sort.i;
else if (!(sort.i1 == undefined))
val = (asc ? sort.i1 : sort.i2);
else // profit неизвестен (чёртеж нестандартный) - всегда внизу
val = -sort.n;
break;
case 3: { // demand
priority = g_tbl_summary_unused_priority;
val1 = null;
val2 = null;
row.find('td a.qind-info-btn').each(function() {
let data_product = $(this).data('product');
if (!(data_product === undefined)) {
if (!(data_product.l === undefined) && (data_product.l > 0)) {
let limit = data_product.l;
var exist = 0;
if (!(data_product.a === undefined) && (data_product.a > 0)) exist += data_product.a;
if (!(data_product.j === undefined) && (data_product.j > 0)) exist += data_product.j;
if (!(data_product.s === undefined) && (data_product.s > 0)) exist += data_product.s;
if ((activity == 8) && !(data_product.q === undefined) && (data_product.q > 0)) exist += data_product.q;
if (val1 === null)
val1 = 1.0 - (exist / limit);
else {
if (val2 === null) val2 = val1;
val1 = Math.min(val1, 1.0 - (exist / limit));
val2 = Math.max(val2, 1.0 - (exist / limit));
}
}
}
});
if (!(val2 === null))
val = g_tbl_summary_half_activity + (asc ? val1 : val2);
else if (!(val1 === null))
val = g_tbl_summary_half_activity + val1;
else
val = -sort.n; // пороги производства неизвестны - всегда внизу
//e = row.find('qname');
//e.html(activity + ' ' + val1 + ' ' + val2);
break; }
}
}
return [g_tbl_summary_priority_multiplier * (priority+1) + g_tbl_summary_activity_multiplier * activity, val];
}
function sortConveyorInternal(table, asc, what) {
var tbody = table.find('tbody');
tbody.find('tr').sort(function(a, b) {
const keyA = getSortKey(a, asc, what);
const keyB = getSortKey(b, asc, what);
/*console.log(
'a:', keyA,
'b:', keyB,
'x:', $(a).find('td:eq(0)').html().substring(0, 11),
'y:', $(b).find('td:eq(0)').html().substring(0, 11),
keyA[0] == keyB[0],
(keyA[1]>keyB[1])?1:-1,
(asc?1:-1) * ((keyA[0]>keyB[0])?1:-1));*/
switch (what) {
case 2: // priority
if (keyA[0] == keyB[0]) {
if (keyA[1] == 0) return -1;
if (keyB[1] == 0) return 1;
if (keyA[1] == keyB[1]) return 0;
return (keyA[1]>keyB[1])?1:-1;
}
return (asc?1:-1) * ((keyA[0]>keyB[0])?1:-1);
case 0: // name
case 1: // duration
case 3: // demand
case 4: // profit
if (keyA[0] == keyB[0]) {
if (keyA[1] == 0) return -1;
if (keyB[1] == 0) return 1;
if (keyA[1] == keyB[1]) return 0;
if (keyA[1] < 0) {
if (keyB[1] < 0) return (keyA[1]<keyB[1])?1:-1;
return 1;
} else if (keyB[1] < 0) {
return -1;
}
return (asc?1:-1) * ((keyA[1]>keyB[1])?1:-1);
}
return (keyA[0]>keyB[0])?1:-1;
}
}).appendTo(tbody);
return 0;
}
function sortConveyor() {
var sort_by = getOptionValue('sort', 'mode');
var what = 0;
switch (sort_by) {
//case 'name': what = 0; break;
case 'duration': what = 1; break;
case 'priority': what = 2; break;
case 'demand': what = 3; break;
case 'profit': what = 4; break;
}
$('tr.row-conveyor').each(function() {
var tag = $(this).data('tag');
if (tag === undefined) return;
changeElemVisibility($(this), ((tag.p === null)?-1:2) == ((what==2)?2:-1));
});
sortConveyorInternal(
$('table.tbl-summary'),
(getOption('sort', 'order') == 1) ? true : false,
what
);
}
//-----------
//работа с видимостью контейнеров (групп по приоритетам)
//-----------
function conveyorTagToStr(row) {
var tag = row.data('tag');
if (tag === undefined) return;
return tag.p+'_'+tag.a.join('_');
}
function toggleConveyorVisibility(btn, hide) {
var tr = btn.parent().parent();
var tag = conveyorTagToStr(tr);
//-
var icon = btn.find('span');
if (hide === undefined) {
var already_hidden = icon.hasClass('glyphicon-eye-close') == true;
hide = already_hidden == false;
}
if (hide == false) {
setOption('conveyor', tag, 0); // 0 = show
icon.removeClass('glyphicon-eye-close');
icon.addClass('glyphicon-eye-open');
btn.removeClass('qind-btn-hide-close');
btn.addClass('qind-btn-hide-open');
} else {
setOption('conveyor', tag, 1); // 1 = hide
icon.removeClass('glyphicon-eye-open');
icon.addClass('glyphicon-eye-close');
btn.removeClass('qind-btn-hide-open');
btn.addClass('qind-btn-hide-close');
}
rebuildConveyorTable();
}
function initConveyorVisibility() {
$('tr.row-conveyor * a.qind-btn-hide').each(function() {
var tr = $(this).parent().parent();
var tag = conveyorTagToStr(tr);
var hide = getOption('conveyor', tag) == 1;
toggleConveyorVisibility($(this), hide);
});
}
//-----------
//работа с видимостью станций роутеров
//-----------
function routerTagToStr(row) {
var tag = row.data('tag');
if (tag === undefined) return;
return tag.r;
}
function toggleRouterVisibility(btn, hide) {
var tr = btn.parent().parent();
var tag = routerTagToStr(tr);
//-
var icon = btn.find('span');
if (hide === undefined) {
var already_hidden = icon.hasClass('glyphicon-eye-close') == true;
hide = already_hidden == false;
}
if (hide == false) {
setOption('router', tag, 0); // 0 = show
icon.removeClass('glyphicon-eye-close');
icon.addClass('glyphicon-eye-open');
btn.removeClass('qind-btn-hide-close');
btn.addClass('qind-btn-hide-open');
} else {
setOption('router', tag, 1); // 1 = hide
icon.removeClass('glyphicon-eye-open');
icon.addClass('glyphicon-eye-close');
btn.removeClass('qind-btn-hide-open');
btn.addClass('qind-btn-hide-close');
}
rebuildRouterTable();
}
function initRouterVisibility() {
$('tr.row-station * a.qind-btn-hide').each(function() {
var tr = $(this).parent().parent();
var tag = routerTagToStr(tr);
var hide = getOption('router', tag) == 1;
toggleRouterVisibility($(this), hide);
});
}
//-----------
//работа с динамическим содержимым страницы
//-----------
function getMaterialImg(tid, sz) {
return '<img class="icn'+sz+'" src="'+g_tbl_stock_img_src.replace("{tid}", tid)+'">';
}
function getMaterialText(choose, q, na, nm) {
if (choose == false) {
if (na > 0)
return q+' нет '+na;
else
return q;
}
if (na == 0)
return nm+' <b>x'+q+'</b>';
else if (q == na)
return nm+' <b>x'+q+'</b> (нет в наличии)';
else
return nm+' <b>x'+q+'</b> (нет '+na+')';
}
function replaceAssetItemHtml(elem) {
let tid = elem.attr('tid');
if (!(tid === undefined)) {
let icn = elem.attr('icn');
let cl = elem.attr('cl');
var nm = getSdeItemName(tid);
if (nm === null) nm = tid;
if (icn === undefined) icn = 24;
if (cl === undefined) cl = ''; else cl = ' '+cl;
s = getMaterialImg(tid, icn)+' '+nm+' <a' +
' data-target="#" role="button" data-tid="'+tid+'" class="qind-copy-btn'+cl+'" data-toggle="tooltip"' +
' data-original-title="" title=""><span class="glyphicon glyphicon-copy" aria-hidden="true"></span></a>';
elem.replaceWith(s);
}
}
function initMaterialNames() {
$('table.tbl-stock tbody tr td qnm').each(function() {
var data_tid = $(this).data('tid');
if (!(data_tid === undefined)) {
var nm = getSdeItemName(data_tid);
if (!(nm === null)) $(this).replaceWith(nm);
else $(this).replaceWith(data_tid);
}
});
$('table.tbl-stock tbody tr td qimg24').each(function() {
var data_tid = $(this).data('tid');
if (!(data_tid === undefined)) {
$(this).replaceWith(getMaterialImg(data_tid, 24));
}
});
$('table.tbl-stock tbody tr td qmaterial,'+
'table.tbl-summary tbody tr td qproduct,'+
'table.tbl-requirements tbody tr td qmaterial').each(function() {
replaceAssetItemHtml($(this));
});
}
function initMaterialsOfBlueprints(used_materials, not_available) {
$('table.tbl-summary tbody tr td div qmaterials').each(function() {
if (!used_materials && !not_available) {
$(this).html('');
} else {
var s = '';
var data_arr = $(this).data('arr');
if (!(data_arr === undefined) && !(data_arr === "") && (data_arr.length>0)) {
for (var i=0; i<data_arr.length; ++i) {
if (!used_materials && not_available && data_arr[i][2]==0) continue;
if (s) s += ' ';
s += '<qmat';
if (data_arr[i][2]>0) s += ' class="absent"';
s += ' data-idx="'+i+'">';
s += getMaterialImg(data_arr[i][0], 16)+' <txt>'+getMaterialText(false, data_arr[i][1], data_arr[i][2])+'</txt></qmat>';
}
}
$(this).html('<br>'+s);
}
});
}
function getMaterialInfo(qmat) {
var qmaterials = qmat.parent();
if (qmaterials === undefined) return null;
var data_arr = qmaterials.data('arr');
if ((data_arr === undefined) || (data_arr === "") || (data_arr.length==0)) return null;
var idx = qmat.data('idx');
if (idx >= data_arr.length) return null;
return data_arr[idx];
}
function chooseMaterial(qmat) {
var info = getMaterialInfo(qmat);
if ((info === null) || (info === undefined)) return
var tid = info[0];
var nm = getSdeItemName(tid);
if (nm === null) nm = tid;
var is_choose = qmat.hasClass('choose') == false;
$('qmaterials qmat').each(function() {
i = getMaterialInfo($(this));
if (tid != i[0]) return;
if (is_choose) {
$(this).find('txt').html(getMaterialText(true, i[1], i[2], nm));
$(this).addClass('choose');
} else {
$(this).find('txt').html(getMaterialText(false, i[1], i[2]));
$(this).removeClass('choose');
}
});
}
//-----------
//работа с содержимом страницы
//-----------
function rebuildConveyorTable() {
var show_possible = (getOption('option', 'run-possible') == 1) ? 1 : 0;
var show_impossible = (getOption('option', 'run-impossible') == 1) ? 1 : 0;
var show_lost = (getOption('option', 'lost-items') == 1) ? 1 : 0;
var show_overstock = (getOption('option', 'overstock-product') == 1) ? 1 : 0;
var show_unprofitable = (getOption('option', 'unprofitable-product') == 1) ? 1 : 0;
var show_phantom = (getOption('option', 'phantom-blueprints') == 1) ? 1 : 0;
var show_required = (getOption('option', 'required-blueprints') == 1) ? 1 : 0;
var show_active = (getOption('option', 'job-active') == 1) ? 1 : 0;
var show_completed = (getOption('option', 'job-completed') == 1) ? 1 : 0;
var hide_conveyor = 0;
var rows = $('table.tbl-summary tbody').children('tr');
for (var i=0,cnt=rows.length;i<cnt;++i) {
var tr = rows.eq(i);
var multiple = false;
if (tr.hasClass('row-conveyor')) {
var tag = conveyorTagToStr(tr);
hide_conveyor = (getOption('conveyor', tag) == 1) ? 0 : 1;
}
else {
x1 = tr.hasClass('row-impossible')?1:0;
x2 = tr.hasClass('row-overstock')?1:0;
x3 = tr.hasClass('row-unprofitable')?1:0;
if (x1||x2||x3) {
// row-overstock и row-unprofitable встречается со всеми остальными row-классами
changeElemVisibility(tr, hide_conveyor * (x1*show_impossible + x2*show_overstock + x3*show_unprofitable));
multiple = true;
}
else if (tr.hasClass('row-possible')) {
changeElemVisibility(tr, hide_conveyor * show_possible);
multiple = true;
}
else if (tr.hasClass('row-optional')) {
changeElemVisibility(tr, hide_conveyor * (show_possible + show_impossible));
multiple = true;
}
else if (tr.hasClass('row-multiple')) {
changeElemVisibility(tr, hide_conveyor * (show_possible + show_impossible));
multiple = true;
}
else if (tr.hasClass('lost-blueprints') || tr.hasClass('lost-assets') || tr.hasClass('lost-jobs'))
changeElemVisibility(tr, show_lost); // если потеряшки включены, то они отображаются всегда
else if (tr.hasClass('phantom-blueprints'))
changeElemVisibility(tr, hide_conveyor * show_phantom);
else if (tr.hasClass('required-blueprints'))
changeElemVisibility(tr, hide_conveyor * show_required);
else if (tr.hasClass('job-active'))
changeElemVisibility(tr, hide_conveyor * show_active);
else if (tr.hasClass('job-completed'))
changeElemVisibility(tr, hide_conveyor * show_completed);
}
if (multiple) {
tr.find('td div').each(function() {
if ($(this).hasClass('run-possible'))
changeElemVisibility($(this), hide_conveyor * show_possible);
else if ($(this).hasClass('run-impossible'))
changeElemVisibility($(this), hide_conveyor * show_impossible);
else if ($(this).hasClass('run-optional'))
changeElemVisibility($(this), hide_conveyor * (show_possible + show_impossible));
});
}
}
$('div.overstock-product').each(function() {
changeElemVisibility($(this), hide_conveyor * show_overstock);
});
$('div.unprofitable-product').each(function() {
changeElemVisibility($(this), hide_conveyor * show_unprofitable);
});
var used_materials = (getOption('option', 'used-materials') == 1) ? 1 : 0;
var not_available = (getOption('option', 'not-available') == 1) ? 1 : 0;
initMaterialsOfBlueprints(used_materials, not_available);
var industry_product = (getOption('option', 'industry-product') == 1) ? 1 : 0;
$('div.industry-product').each(function() { changeElemVisibility($(this), industry_product); });
$('div.invent-product').each(function() { changeElemVisibility($(this), industry_product); }); // когда продукт один
var industry_profit = (getOption('option', 'industry-profit') == 1) ? 1 : 0;
$('profit').each(function() { changeElemVisibility($(this), industry_profit); });
}
function rebuildRouterTable() {
var show_endlvl_manuf = (getOption('option', 'end-level-manuf') == 1) ? 1 : 0;
var show_entry_purch = (getOption('option', 'entry-level-purchasing') == 1) ? 1 : 0;
var show_interm_manuf = (getOption('option', 'intermediate-manuf') == 1) ? 1 : 0;
var hide_router = 0;
var rows = $('table.tbl-router tbody').children('tr');
for (var i=0,cnt=rows.length;i<cnt;++i) {
var tr = rows.eq(i);
if (tr.hasClass('row-station')) {
var tag = routerTagToStr(tr);
hide_router = (getOption('router', tag) == 1) ? 0 : 1;
} else {
changeElemVisibility(tr, hide_router);
}
}
}
//-----------
// обработчики нажатий на кнопки
//-----------
$('a.qind-btn-settings').on('click', function () {
var opt = $(this).attr('qind-group');
var val = (getOption('option', opt) == 1) ? 0 : 1;
setOption('option', opt, val);
if (opt == 'used-materials') {
if (val == 1)
setOption('option', 'not-available', 0);
}
else if (opt == 'not-available') {
if (val == 1)
setOption('option', 'used-materials', 0);
}
rebuildOptionsMenu();
rebuildConveyorTable();
});
//---
function formatTime(sec) {
const seconds = Math.floor(Math.abs(sec));
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.round(seconds % 60);
const t = [h, m > 9 ? m : h ? '0' + m : m || '0', s > 9 ? s : '0' + s]
.filter(Boolean)
.join(':');
return sec < 0 && seconds ? `-${t}` : t;
}
function recalcLifetimeTimestamps() {
var now = new Date();
var utc = new Date(now.getTime() + now.getTimezoneOffset() * 60000);
let js_ts = Math.floor(utc / 1000); // browser time
let js_offset = js_ts - g_server_time;
var s = now.toISOString();
s = s.replace('T',' ').replace('Z','');
$('#browser-time').html(s);
$('table.tbl-lifetime * left').each(function() {
var data_ts = $(this).data('ts');
var data_w = $(this).data('w');
if (!(data_ts === undefined) && !(data_w === undefined)) {
var left_ts = 0 + (g_server_time - data_ts) + js_offset;
var esi_cacheed;
if (data_w == 'o') esi_cacheed = 1200;
else if (data_w == 'j') esi_cacheed = 300;
else /*if (data_w == 'a' || data_w == 'b')*/ esi_cacheed = 3600;
let left_cacheed = esi_cacheed - left_ts;
if (left_cacheed >= -270 && left_cacheed <= 0)
$(this).html(formatTime(left_cacheed)); // cron timeout
else if (left_cacheed > 0)
$(this).html(formatTime(left_cacheed % esi_cacheed));
else /*if (left_cacheed < 0)*/ {
let left_cacheed_abs = left_cacheed % esi_cacheed;
$(this).html(formatTime(esi_cacheed + left_cacheed_abs));
}
}
});
}
//---
function showIndustryProductModal(elem) {
let data_product = elem.data('product');
if (data_product === undefined) return;
if (!(data_product.p === undefined)) {
$('#modalIndustryProductLabel icon').html(getMaterialImg(data_product.p));
$('#product-name').html(getSdeItemName(data_product.p));
}
var txt;
const nodata = '0'; // '<mute>-</mute>';
if (!(data_product.a === undefined) && (data_product.a > 0)) txt = data_product.a; else txt = nodata;
$('#product-in-assets').html(txt);
if (!(data_product.j === undefined) && (data_product.j > 0)) txt = data_product.j; else txt = nodata;
$('#product-in-jobs').html(txt);
if (!(data_product.s === undefined) && (data_product.s > 0)) txt = data_product.s; else txt = nodata;
$('#product-in-sale').html(txt);
if (!(data_product.b === undefined) && (data_product.b > 0)) txt = data_product.b; else txt = nodata;
$('#product-in-blueprints').html(txt);
if (!(data_product.r === undefined) && (data_product.r > 0)) txt = data_product.r; else txt = nodata;
$('#product-in-blueprint-runs').html(txt);
if (!(data_product.q === undefined) && (data_product.q > 0)) txt = data_product.q; else txt = nodata;
$('#product-in-blueprint-qty').html(txt);
//
if (!(data_product.l === undefined) && (data_product.l > 0)) txt = data_product.l; else txt = 'порог не задан';
$('#product-limit').html(txt);
$('#modalIndustryProduct').modal('show');
}
//-----------
// работа с пунктами меню
//-----------
function resetOptionsMenuToDefault() {
for (let i = 0; i < g_menu_options_default_len; ++i) {
var show = g_menu_options_default[i][0] ? 1 : 0;
var opt = g_menu_options_default[i][1];
resetOptionToDefault('option', opt, show);
}
resetOptionToDefault('sort', 'mode', g_menu_sort_default);
resetOptionToDefault('sort', 'order', g_menu_sort_order_default);
}
//-----------
// работа с буфером обмена
//-----------
function copyToClipboard(elem, data_copy) {
var $temp = $("<textarea>");
$("body").append($temp);
$temp.val(data_copy).select();
try {
success = document.execCommand("copy");
if (success) {
elem.trigger('copied', ['Copied!']);
}
} finally {
$temp.remove();
}
}
//-----------
// работа с буфером обмена (больше вариантов вода)
//-----------
function doCopyToClipboard(elem) {
// ожидаем либо data-tid="type_id"; либо data-copy="some value"; либо data-source="table"; либо data-source="span"
var data_tid = elem.data('tid');
if (!(data_tid === undefined)) {
var nm = getSdeItemName(data_tid);
if (!(nm === null)) copyToClipboard(elem, nm);
else copyToClipboard(elem, data_tid);
return;
}
var data_copy = elem.data('copy');
if (!(data_copy === undefined)) {
copyToClipboard(elem, data_copy);
return;
}
data_copy = '';
var data_source = elem.data('source');
if (data_source == 'table') {
var tr = elem.parent().parent();
var tbody = tr.parent();
var rows = tbody.children('tr');
var start_row = rows.index(tr);
rows.each( function(idx) {
var tr = $(this);
if (!(start_row === undefined) && (idx > start_row)) {
var td = tr.find('td').eq(0);
if (!(td.attr('class') === undefined))
start_row = undefined;
else {
var q1 = tr.find('td').eq(1).data('q'); q1 = (q1==undefined)?0:parseInt(q1,10);
var q2 = tr.find('td').eq(2).data('q'); q2 = (q2==undefined)?0:parseInt(q2,10);
var qq = q1 + q2;
if (qq == 0) return;
if (data_copy) data_copy += "\\n";
data_copy += td.data('nm') + "\\t" + qq;
}
}
});
} else if (data_source == 'span') {
var div = elem.parent().find('div.qind-tid');
if (!(div === undefined)) {
var tids = div.children('tid');
if (!(tids === undefined)) {
tids.each( function(idx) {
var tid = $(this);
if (data_copy) data_copy += "\\n";
data_copy += getSdeItemName(tid.data('tid')) + "\\t" + tid.data('q');
});
}
}
}
if (data_copy) copyToClipboard(elem, data_copy);
}
//-----------
// начальное заполнение страницы
//-----------
function firstInit() {
resetOptionsMenuToDefault();
rebuildOptionsMenu();
initConveyorVisibility();
initRouterVisibility();
sortConveyor(); // используется до rebuildConveyorTable
rebuildConveyorTable();
rebuildRouterTable();
}
$(document).ready(function(){
$('#qind-btn-reset').on('click', function () {
ls.clear();
firstInit();
});
// first init
firstInit();
//rebuildStocksDropdown();
//rebuildStockMaterials();
// init popover menus
//initPopoverMenus();
$('body').delegate('qmat', 'click', function () {
chooseMaterial($(this));
});
$('tr.row-conveyor * a.qind-btn-hide').bind('click', function () {
toggleConveyorVisibility($(this));
});
$('tr.row-station * a.qind-btn-hide').bind('click', function () {
toggleRouterVisibility($(this));
});
$('button.qind-btn-sort').on('click', function () {
var mode = $(this).attr('qind-group');
if (getOptionValue('sort', 'mode') == mode) {
var asc = getOption('sort', 'order');
setOption('sort', 'order', asc ? 0 : 1);
} else {
setOption('sort', 'mode', mode);
setOption('sort', 'order', 1);
}
rebuildOptionsMenu();
sortConveyor();
rebuildConveyorTable();
});
$('a.qind-info-btn').on('click', function () {
showIndustryProductModal($(this));
});
// Working with clipboard
$('a.qind-copy-btn').each(function() {
$(this).tooltip();
});
$('body').delegate('a.qind-copy-btn', 'click', function () {
doCopyToClipboard($(this));
});
var lifetime_timer = 0;
$('#modalLifetime').on('shown.bs.modal', function () {
recalcLifetimeTimestamps();
lifetime_timer = setInterval(() => recalcLifetimeTimestamps(), 1000);
});
$('#modalLifetime').on('hide.bs.modal', function () {
clearInterval(lifetime_timer);
});
$('a.qind-copy-btn').bind('copied', function(event, message) {
$(this).attr('title', message)
.tooltip('fixTitle')
.tooltip('show')
.attr('title', "Copy to clipboard")
.tooltip('fixTitle');
});
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
// какой-то код ...
$('a.qind-copy-btn').each(function() { changeElemVisibility($(this), 0); });
} else {
$('table.tbl-router tbody tr.last-factory td:nth-child(4) num').each(function() { $(this).tooltip(); });
$('table.tbl-router tbody tr.last-factory td:nth-child(4) num').bind('click', function () {
var data_copy = $(this).html();
data_copy = data_copy.replace(/\s/g, '').replace(/<\/?[^>]+(>|$)/g, "").toLowerCase();
copyToClipboard($(this), data_copy);
});
$('table.tbl-router tbody tr.last-factory td:nth-child(4) num').bind('copied', function(event, message) {
var t = $(this).attr('title', message)
.tooltip('fixTitle')
.tooltip('show');
setTimeout(function() { t.tooltip('destroy'); }, 1500);
});
}
// Delayed and low priority operations
initMaterialNames();
});