-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClickSaver.user.js
1594 lines (1488 loc) · 55 KB
/
ClickSaver.user.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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name WME ClickSaver
// @namespace https://greasyfork.org/users/45389
// @version 2024.07.21.000
// @description Various UI changes to make editing faster and easier.
// @author MapOMatic
// @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
// @license GNU GPLv3
// @connect sheets.googleapis.com
// @connect greasyfork.org
// @contributionURL https://github.com/WazeDev/Thank-The-Authors
// @grant GM_xmlhttpRequest
// @grant GM_addElement
// @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
// @downloadURL https://update.greasyfork.org/scripts/369629/WME%20ClickSaver.user.js
// @updateURL https://update.greasyfork.org/scripts/369629/WME%20ClickSaver.meta.js
// ==/UserScript==
/* global W */
/* globals $ */
/* global I18n */
/* global OpenLayers */
/* global WazeWrap */
(function main() {
"use strict";
const UPDATE_MESSAGE = "";
const SCRIPT_NAME = GM_info.script.name;
const SCRIPT_VERSION = GM_info.script.version;
const DOWNLOAD_URL = "https://greasyfork.org/scripts/369629-wme-clicksaver/code/WME%20ClickSaver.user.js";
const FORUM_URL = "https://www.waze.com/forum/viewtopic.php?f=819&t=199894";
const TRANSLATIONS_URL = "https://sheets.googleapis.com/v4/spreadsheets/1ZlE9yhNncP9iZrPzFFa-FCtYuK58wNOEcmKqng4sH1M/values/ClickSaver";
const API_KEY = "YTJWNVBVRkplbUZUZVVGMFl6aFVjMjVOTW0wNU5GaG5kVE40TUZoNWJVZEhWbU5rUjNacVdtdFlWUT09";
const DEC = s => atob(atob(s));
const EXTERNAL_SETTINGS = {
toggleTwoWaySegDrawingShortcut: null,
copyCoordinatesShortcut: null,
};
const EXTERNAL_SETTINGS_NAME = "clicksaver_settings_ext";
// This function is injected into the page.
function clicksaver(argsObject) {
/* eslint-disable object-curly-newline */
const ROAD_TYPE_DROPDOWN_SELECTOR = "wz-select[name=\"roadType\"]";
const ROAD_TYPE_CHIP_SELECTOR = "wz-chip-select[class=\"road-type-chip-select\"]";
// const PARKING_SPACES_DROPDOWN_SELECTOR = 'select[name="estimatedNumberOfSpots"]';
// const PARKING_COST_DROPDOWN_SELECTOR = 'select[name="costType"]';
const SETTINGS_STORE_NAME = "clicksaver_settings";
const DEFAULT_TRANSLATION = {
roadTypeButtons: {
St: { text: "St" },
PS: { text: "PS" },
mH: { text: "mH" },
MH: { text: "MH" },
Fw: { text: "Fw" },
Rmp: { text: "Rmp" },
OR: { text: "OR" },
PLR: { text: "PLR" },
PR: { text: "PR" },
Fer: { text: "Fer" },
WT: { text: "WT" },
PB: { text: "PB" },
Sw: { text: "Sw" },
RR: { text: "RR" },
RT: { text: "RT" },
Pw: { text: "Pw" },
},
prefs: {
dropdownHelperGroup: "DROPDOWN HELPERS",
roadTypeButtons: "Add road type buttons",
useOldRoadColors: "Use old road colors (requires refresh)",
setStreetCityToNone: "Set Street/City to None (new seg's only)",
// eslint-disable-next-line camelcase
setStreetCityToNone_Title:
"NOTE: Only works if connected directly or indirectly"
+ " to a segment with State / Country already set.",
setCityToConnectedSegCity: "Set City to connected segment's City",
parkingCostButtons: "Add PLA cost buttons",
parkingSpacesButtons: "Add PLA estimated spaces buttons",
timeSaversGroup: "TIME SAVERS",
discussionForumLinkText: "Discussion Forum",
showAddAltCityButton: "Show \"Add alt city\" button",
showSwapDrivingWalkingButton:
"Show \"Swap driving<->walking segment type\" button",
// eslint-disable-next-line camelcase
showSwapDrivingWalkingButton_Title:
"Swap between driving-type and walking-type segments. WARNING! This will DELETE and recreate the segment. Nodes may need to be reconnected.",
addCompactColors: "Add colors to compact mode road type buttons",
},
swapSegmentTypeWarning:
"This will DELETE the segment and recreate it. Any speed data will be lost, and nodes will need to be reconnected. This message will only be displayed once. Continue?",
// eslint-disable-next-line camelcase
swapSegmentTypeError_Paths:
"Paths must be removed from segment before changing between driving and pedestrian road type.",
addAltCityButtonText: "Add alt city",
};
const ROAD_TYPES = {
St: {
val: 1,
wmeColor: "#ffffeb",
svColor: "#ffffff",
category: "streets",
visible: true,
},
PS: {
val: 2,
wmeColor: "#f0ea58",
svColor: "#cba12e",
category: "streets",
visible: true,
},
Pw: {
val: 22,
wmeColor: "#64799a",
svColor: "#64799a",
category: "streets",
visible: false,
},
mH: {
val: 7,
wmeColor: "#69bf88",
svColor: "#ece589",
category: "highways",
visible: true,
},
MH: {
val: 6,
wmeColor: "#45b8d1",
svColor: "#c13040",
category: "highways",
visible: true,
},
Fw: {
val: 3,
wmeColor: "#c577d2",
svColor: "#387fb8",
category: "highways",
visible: false,
},
Rmp: {
val: 4,
wmeColor: "#b3bfb3",
svColor: "#58c53b",
category: "highways",
visible: false,
},
OR: {
val: 8,
wmeColor: "#867342",
svColor: "#82614a",
category: "otherDrivable",
visible: false,
},
PLR: {
val: 20,
wmeColor: "#ababab",
svColor: "#2282ab",
category: "otherDrivable",
visible: true,
},
PR: {
val: 17,
wmeColor: "#beba6c",
svColor: "#00ffb3",
category: "otherDrivable",
visible: true,
},
Fer: {
val: 15,
wmeColor: "#d7d8f8",
svColor: "#ff8000",
category: "otherDrivable",
visible: false,
},
RR: {
val: 18,
wmeColor: "#c62925",
svColor: "#ffffff",
category: "nonDrivable",
visible: false,
},
RT: {
val: 19,
wmeColor: "#ffffff",
svColor: "#00ff00",
category: "nonDrivable",
visible: false,
},
WT: {
val: 5,
wmeColor: "#b0a790",
svColor: "#00ff00",
category: "pedestrian",
visible: false,
},
PB: {
val: 10,
wmeColor: "#9a9a9a",
svColor: "#0000ff",
category: "pedestrian",
visible: false,
},
Sw: {
val: 16,
wmeColor: "#999999",
svColor: "#b700ff",
category: "pedestrian",
visible: false,
},
};
/* eslint-enable object-curly-newline */
let _settings = {};
let _trans; // Translation object
// Do not make these const values. They may get assigned before require() is defined. Trust me. Don't do it.
let UpdateObject;
let UpdateFeatureAddress;
let MultiAction;
let AddSeg;
let Segment;
let DelSeg;
// function log(message) {
// console.log('ClickSaver:', message);
// }
function logDebug(message) {
console.debug("ClickSaver:", message);
}
// function logWarning(message) {
// console.warn('ClickSaver:', message);
// }
// function logError(message) {
// console.error('ClickSaver:', message);
// }
function isChecked(checkboxId) {
return $(`#${checkboxId}`).is(":checked");
}
function isSwapPedestrianPermitted() {
const { user } = W.loginManager;
const rank = user.attributes.rank + 1;
return rank >= 4 || (rank === 3 && user.attributes.isAreaManager);
}
function setChecked(checkboxId, checked) {
$(`#${checkboxId}`).prop("checked", checked);
}
function loadSettingsFromStorage() {
const loadedSettings = $.parseJSON(
localStorage.getItem(SETTINGS_STORE_NAME),
);
const defaultSettings = {
lastVersion: null,
roadButtons: true,
roadTypeButtons: [
"St",
"PS",
"mH",
"MH",
"Fw",
"Rmp",
"PLR",
"PR",
"PB",
],
parkingCostButtons: true,
parkingSpacesButtons: true,
setNewPLRStreetToNone: true,
setNewPLRCity: true,
setNewPRStreetToNone: false,
setNewPRCity: false,
setNewRRStreetToNone: true, // added by jm6087
setNewRRCity: false, // added by jm6087
setNewPBStreetToNone: true, // added by jm6087
setNewPBCity: true, // added by jm6087
setNewORStreetToNone: false,
setNewORCity: false,
addAltCityButton: true,
addSwapPedestrianButton: false,
useOldRoadColors: false,
warnOnPedestrianTypeSwap: true,
addCompactColors: true,
};
_settings = loadedSettings || defaultSettings;
Object.keys(defaultSettings).forEach(prop => {
if (!_settings.hasOwnProperty(prop)) {
_settings[prop] = defaultSettings[prop];
}
});
setChecked("csRoadTypeButtonsCheckBox", _settings.roadButtons);
if (_settings.roadTypeButtons) {
Object.keys(ROAD_TYPES).forEach(roadTypeAbbr1 => {
setChecked(
`cs${roadTypeAbbr1}CheckBox`,
_settings.roadTypeButtons.indexOf(roadTypeAbbr1) !== -1,
);
});
}
if (_settings.roadButtons) {
$(".csRoadTypeButtonsCheckBoxContainer").show();
} else {
$(".csRoadTypeButtonsCheckBoxContainer").hide();
}
// setChecked('csParkingSpacesButtonsCheckBox', _settings.parkingSpacesButtons);
// setChecked('csParkingCostButtonsCheckBox', _settings.parkingCostButtons);
setChecked("csSetNewPLRCityCheckBox", _settings.setNewPLRCity);
setChecked("csClearNewPLRCheckBox", _settings.setNewPLRStreetToNone);
setChecked("csSetNewPRCityCheckBox", _settings.setNewPRCity);
setChecked("csClearNewPRCheckBox", _settings.setNewPRStreetToNone);
setChecked("csSetNewRRCityCheckBox", _settings.setNewRRCity);
setChecked("csClearNewRRCheckBox", _settings.setNewRRStreetToNone); // added by jm6087
setChecked("csSetNewPBCityCheckBox", _settings.setNewPBCity);
setChecked("csClearNewPBCheckBox", _settings.setNewPBStreetToNone); // added by jm6087
setChecked("csSetNewORCityCheckBox", _settings.setNewORCity);
setChecked("csClearNewORCheckBox", _settings.setNewORStreetToNone);
setChecked("csUseOldRoadColorsCheckBox", _settings.useOldRoadColors);
setChecked("csAddAltCityButtonCheckBox", _settings.addAltCityButton);
setChecked(
"csAddSwapPedestrianButtonCheckBox",
_settings.addSwapPedestrianButton,
);
setChecked("csAddCompactColorsCheckBox", _settings.addCompactColors);
}
function saveSettingsToStorage() {
if (localStorage) {
const settings = {
lastVersion: argsObject.scriptVersion,
roadButtons: _settings.roadButtons,
parkingCostButtons: _settings.parkingCostButtons,
parkingSpacesButtons: _settings.parkingSpacesButtons,
setNewPLRCity: _settings.setNewPLRCity,
setNewPLRStreetToNone: _settings.setNewPLRStreetToNone,
setNewPRCity: _settings.setNewPRCity,
setNewPRStreetToNone: _settings.setNewPRStreetToNone,
setNewRRCity: _settings.setNewRRCity,
setNewRRStreetToNone: _settings.setNewRRStreetToNone, // added by jm6087
setNewPBCity: _settings.setNewPBCity,
setNewPBStreetToNone: _settings.setNewPBStreetToNone, // added by jm6087
setNewORCity: _settings.setNewORCity,
setNewORStreetToNone: _settings.setNewORStreetToNone,
useOldRoadColors: _settings.useOldRoadColors,
addAltCityButton: _settings.addAltCityButton,
addSwapPedestrianButton: _settings.addSwapPedestrianButton,
warnOnPedestrianTypeSwap: _settings.warnOnPedestrianTypeSwap,
addCompactColors: _settings.addCompactColors,
};
settings.roadTypeButtons = [];
Object.keys(ROAD_TYPES).forEach(roadTypeAbbr => {
if (_settings.roadTypeButtons.indexOf(roadTypeAbbr) !== -1) {
settings.roadTypeButtons.push(roadTypeAbbr);
}
});
localStorage.setItem(SETTINGS_STORE_NAME, JSON.stringify(settings));
logDebug("Settings saved");
}
}
function isPedestrianTypeSegment(segment) {
return [5, 10, 16].includes(segment.attributes.roadType);
}
function getConnectedSegmentIDs(segmentID) {
const IDs = [];
const segment = W.model.segments.getObjectById(segmentID);
[
W.model.nodes.getObjectById(segment.attributes.fromNodeID),
W.model.nodes.getObjectById(segment.attributes.toNodeID),
].forEach(node => {
if (node) {
node.attributes.segIDs.forEach(segID => {
if (segID !== segmentID) {
IDs.push(segID);
}
});
}
});
return IDs;
}
function getFirstConnectedSegmentAddress(startSegment) {
const nonMatches = [];
const segmentIDsToSearch = [startSegment.getID()];
while (segmentIDsToSearch.length > 0) {
const startSegmentID = segmentIDsToSearch.pop();
startSegment = W.model.segments.getObjectById(startSegmentID);
const connectedSegmentIDs = getConnectedSegmentIDs(startSegmentID);
for (let i = 0; i < connectedSegmentIDs.length; i++) {
const addr = W.model.segments
.getObjectById(connectedSegmentIDs[i])
.getAddress();
if (!addr.isEmpty()) {
return addr;
}
}
nonMatches.push(startSegmentID);
connectedSegmentIDs.forEach(segmentID => {
if (
nonMatches.indexOf(segmentID) === -1
&& segmentIDsToSearch.indexOf(segmentID) === -1
) {
segmentIDsToSearch.push(segmentID);
}
});
}
return undefined;
}
function setStreetAndCity(setCity) {
const segments = getSelectedSegments();
if (segments.length === 0) {
return;
}
const actions = [];
segments.forEach(segment => {
if (segment.attributes.primaryStreetID === null) {
const addr = getFirstConnectedSegmentAddress(segment);
if (addr && !addr.isEmpty()) {
const cityNameToSet = setCity && !addr.getCity().isEmpty() ? addr.getCityName() : "";
const action = new UpdateFeatureAddress(
segment,
{
countryID: addr.getCountry().getID(),
stateID: addr.getState().getID(),
cityName: cityNameToSet,
emptyStreet: true,
emptyCity: !setCity,
},
{ streetIDField: "primaryStreetID" },
);
actions.push(action);
}
}
});
if (actions.length) {
W.model.actionManager.add(new MultiAction(actions));
}
}
class WaitForElementError extends Error {}
function waitForElem(selector) {
return new Promise((resolve, reject) => {
function checkIt(tries = 0) {
if (tries < 150) {
// try for about 3 seconds;
const elem = document.querySelector(selector);
setTimeout(() => {
if (!elem) {
checkIt(++tries);
} else {
resolve(elem);
}
}, 20);
} else {
reject(
new WaitForElementError(
`Element was not found within 3 seconds: ${selector}`,
),
);
}
}
checkIt();
});
}
async function waitForShadowElem(parentElemSelector, shadowElemSelectors) {
const parentElem = await waitForElem(parentElemSelector);
return new Promise((resolve, reject) => {
shadowElemSelectors.forEach((shadowElemSelector, idx) => {
function checkIt(parent, tries = 0) {
if (tries < 150) {
// try for about 3 seconds;
const shadowElem = parent.shadowRoot.querySelector(shadowElemSelector);
setTimeout(() => {
if (!shadowElem) {
checkIt(parent, ++tries);
} else if (idx === shadowElemSelectors.length - 1) {
resolve({ shadowElem, parentElem });
} else {
checkIt(shadowElem, 0);
}
}, 20);
} else {
reject(
new WaitForElementError(
`Shadow element was not found within 3 seconds: ${shadowElemSelector}`,
),
);
}
}
checkIt(parentElem);
});
});
}
async function onAddAltCityButtonClick() {
const streetID = getSelectedSegments()[0].attributes.primaryStreetID;
$("wz-button[class=\"add-alt-street-btn\"]").click();
const elem = await waitForElem("wz-autocomplete.alt-street-name");
elem.focus();
let result = await waitForShadowElem("wz-autocomplete.alt-street-name", [
"wz-text-input",
]);
result.shadowElem.value = W.model.streets.getObjectById(streetID).attributes.name;
result = await waitForShadowElem("wz-autocomplete.alt-city-name", [
"wz-text-input",
]);
result.shadowElem.value = null;
result.parentElem.focus();
}
function onRoadTypeButtonClick(roadTypeVal) {
const segments = getSelectedSegments();
let action;
if (segments.length > 1) {
const actions = [];
segments.forEach(segment => {
const subAction = new UpdateObject(segment, {
roadType: roadTypeVal,
});
actions.push(subAction);
});
action = new MultiAction(actions);
} else {
action = new UpdateObject(segments[0], {
roadType: roadTypeVal,
});
}
W.model.actionManager.add(action);
if (
roadTypeVal === 20
&& isChecked("csClearNewPLRCheckBox")
&& typeof require !== "undefined"
) {
setStreetAndCity(isChecked("csSetNewPLRCityCheckBox"));
} else if (
roadTypeVal === 17
&& isChecked("csClearNewPRCheckBox")
&& typeof require !== "undefined"
) {
setStreetAndCity(isChecked("csSetNewPRCityCheckBox"));
} else if (
roadTypeVal === 18
&& isChecked("csClearNewRRCheckBox")
&& typeof require !== "undefined"
) {
// added by jm6087
setStreetAndCity(isChecked("csSetNewRRCityCheckBox")); // added by jm6087
} else if (
roadTypeVal === 10
&& isChecked("csClearNewPBCheckBox")
&& typeof require !== "undefined"
) {
// added by jm6087
setStreetAndCity(isChecked("csSetNewPBCityCheckBox")); // added by jm6087
} else if (
roadTypeVal === 8
&& isChecked("csClearNewORCheckBox")
&& typeof require !== "undefined"
) {
setStreetAndCity(isChecked("csSetNewORCityCheckBox"));
}
}
function addRoadTypeButtons() {
const segment = getSelectedSegments()[0];
if (!segment) return;
const isPed = isPedestrianTypeSegment(segment);
const $dropDown = $(ROAD_TYPE_DROPDOWN_SELECTOR);
$("#csRoadTypeButtonsContainer").remove();
const $container = $("<div>", {
id: "csRoadTypeButtonsContainer",
class: "cs-rt-buttons-container",
style: "display: inline-table;",
});
const $street = $("<div>", {
id: "csStreetButtonContainer",
class: "cs-rt-buttons-group",
});
const $highway = $("<div>", {
id: "csHighwayButtonContainer",
class: "cs-rt-buttons-group",
});
const $otherDrivable = $("<div>", {
id: "csOtherDrivableButtonContainer",
class: "cs-rt-buttons-group",
});
const $nonDrivable = $("<div>", {
id: "csNonDrivableButtonContainer",
class: "cs-rt-buttons-group",
});
const $pedestrian = $("<div>", {
id: "csPedestrianButtonContainer",
class: "cs-rt-buttons-group",
});
const divs = {
streets: $street,
highways: $highway,
otherDrivable: $otherDrivable,
nonDrivable: $nonDrivable,
pedestrian: $pedestrian,
};
Object.keys(ROAD_TYPES).forEach(roadTypeKey => {
if (_settings.roadTypeButtons.includes(roadTypeKey)) {
const roadType = ROAD_TYPES[roadTypeKey];
const isDisabled = $dropDown[0].hasAttribute("disabled")
&& $dropDown[0].getAttribute("disabled") === "true";
if (
!isDisabled
&& ((roadType.category === "pedestrian" && isPed)
|| (roadType.category !== "pedestrian" && !isPed))
) {
const $div = divs[roadType.category];
$div.append(
$("<div>", {
class: `btn cs-rt-button cs-rt-button-${roadTypeKey} btn-positive`,
title: I18n.t("segment.road_types")[roadType.val],
})
.text(_trans.roadTypeButtons[roadTypeKey].text)
.prop("checked", roadType.visible)
.data("val", roadType.val)
.click(function rtbClick() {
onRoadTypeButtonClick($(this).data("val"));
}),
);
}
}
});
if (isPed) {
$container.append($pedestrian);
} else {
$container
.append($street)
.append($highway)
.append($otherDrivable)
.append($nonDrivable);
}
$dropDown.before($container);
}
// Function to add an event listener to the chip select for the road type in compact mode
function addCompactRoadTypeChangeEvents() {
const chipSelect = document.getElementsByClassName(
"road-type-chip-select",
)[0];
chipSelect.addEventListener("chipSelected", evt => {
const rtValue = evt.detail.value;
onRoadTypeButtonClick(rtValue);
});
}
// Function to add road type colors to the chips in compact mode
async function addCompactRoadTypeColors() {
// TODO: Clean this up. Was combined from two functions.
try {
if (
W.prefs.attributes.compactDensity
&& isChecked("csAddCompactColorsCheckBox")
&& getSelectedSegments().length
) {
const useOldColors = _settings.useOldRoadColors;
await waitForElem(".road-type-chip-select wz-checkable-chip");
$(".road-type-chip-select wz-checkable-chip").addClass(
"cs-compact-button",
);
Object.keys(ROAD_TYPES).forEach(roadTypeKey => {
const roadType = ROAD_TYPES[roadTypeKey];
const bgColor = useOldColors ? roadType.svColor : roadType.wmeColor;
const rtChip = $(
`.road-type-chip-select wz-checkable-chip[value=${roadType.val}]`,
);
if (rtChip.length !== 1) return;
waitForShadowElem(
`.road-type-chip-select wz-checkable-chip[value='${roadType.val}']`,
["div"],
).then(result => {
const $elem = $(result.shadowElem);
const padding = $elem.hasClass("checked") ? "0px 7px" : "0px 8px";
$elem.css({
backgroundColor: bgColor,
padding,
color: "black",
});
});
});
const result = await waitForShadowElem(
".road-type-chip-select wz-checkable-chip[checked=\"\"]",
["div"],
);
$(result.shadowElem).css({
border: "black 2px solid",
padding: "0px 7px",
});
$(".road-type-chip-select wz-checkable-chip").each(
function updateRoadTypeChip() {
const style = {};
if (this.getAttribute("checked") === "false") {
style.border = "";
style.padding = "0px 8px";
} else {
style.border = "black 2px solid";
style.padding = "0px 7px";
}
$(this.shadowRoot.querySelector("div")).css(style);
},
);
}
} catch (ex) {
if (ex instanceof WaitForElementError) {
// waitForElem will throw an error if Undo causes a deselection. Ignore it.
} else {
throw ex;
}
}
}
// function isPLA(item) {
// return (item.model.type === 'venue') && item.model.attributes.categories.includes('PARKING_LOT');
// }
// function addParkingSpacesButtons() {
// const $dropDown = $(PARKING_SPACES_DROPDOWN_SELECTOR);
// const selItems = W.selectionManager.getSelectedFeatures();
// const item = selItems[0];
// // If it's not a PLA, exit.
// if (!isPLA(item)) return;
// $('#csParkingSpacesContainer').remove();
// const $div = $('<div>', { id: 'csParkingSpacesContainer' });
// const dropdownDisabled = $dropDown.attr('disabled') === 'disabled';
// const optionNodes = $(`${PARKING_SPACES_DROPDOWN_SELECTOR} option`);
// for (let i = 0; i < optionNodes.length; i++) {
// const $option = $(optionNodes[i]);
// const text = $option.text();
// const selected = $option.val() === $dropDown.val();
// $div.append(
// // TODO css
// $('<div>', {
// class: `btn waze-btn waze-btn-white${selected ? ' waze-btn-blue' : ''}${dropdownDisabled ? ' disabled' : ''}`,
// style: 'margin-bottom: 5px; height: 22px; padding: 2px 8px 0px 8px; margin-right: 3px;'
// })
// .text(text)
// .data('val', $option.val())
// // eslint-disable-next-line func-names
// .hover(() => { })
// .click(function onParkingSpacesButtonClick() {
// if (!dropdownDisabled) {
// $(PARKING_SPACES_DROPDOWN_SELECTOR).val($(this).data('val')).change();
// addParkingSpacesButtons();
// }
// })
// );
// }
// $dropDown.before($div);
// $dropDown.hide();
// }
// function addParkingCostButtons() {
// const $dropDown = $(PARKING_COST_DROPDOWN_SELECTOR);
// const selItems = W.selectionManager.getSelectedFeatures();
// const item = selItems[0];
// // If it's not a PLA, exit.
// if (!isPLA(item)) return;
// $('#csParkingCostContainer').remove();
// const $div = $('<div>', { id: 'csParkingCostContainer' });
// const dropdownDisabled = $dropDown.attr('disabled') === 'disabled';
// const optionNodes = $(`${PARKING_COST_DROPDOWN_SELECTOR} option`);
// for (let i = 0; i < optionNodes.length; i++) {
// const $option = $(optionNodes[i]);
// const text = $option.text();
// const selected = $option.val() === $dropDown.val();
// $div.append(
// $('<div>', {
// class: `btn waze-btn waze-btn-white${selected ? ' waze-btn-blue' : ''}${dropdownDisabled ? ' disabled' : ''}`,
// // TODO css
// style: 'margin-bottom: 5px; height: 22px; padding: 2px 8px 0px 8px; margin-right: 4px;'
// })
// .text(text !== '' ? text : '?')
// .data('val', $option.val())
// // eslint-disable-next-line func-names
// .hover(() => { })
// .click(function onParkingCostButtonClick() {
// if (!dropdownDisabled) {
// $(PARKING_COST_DROPDOWN_SELECTOR).val($(this).data('val')).change();
// addParkingCostButtons();
// }
// })
// );
// }
// $dropDown.before($div);
// $dropDown.hide();
// }
function addAddAltCityButton() {
const segments = getSelectedSegments();
const streetID = segments[0].attributes.primaryStreetID;
// Only show the button if every segment has the same primary city and street.
if (
segments.length > 1
&& !segments.every(
segment => segment.attributes.primaryStreetID === streetID,
)
) return;
const id = "csAddAltCityButton";
if ($(`#${id}`).length === 0) {
$("div.address-edit")
.prev("wz-label")
.append(
$("<a>", {
href: "#",
// TODO css
style:
"float: right;text-transform: none;"
+ "font-family: \"Helvetica Neue\", Helvetica, \"Open Sans\", sans-serif;color: #26bae8;"
+ "font-weight: normal;",
})
.text(_trans.addAltCityButtonText)
.click(onAddAltCityButtonClick),
);
}
}
function addSwapPedestrianButton(displayMode) {
// Added displayMode argument to identify compact vs. regular mode.
const id = "csSwapPedestrianContainer";
$(`#${id}`).remove();
const segments = getSelectedSegments();
if (segments.length === 1) {
// TODO css
const $container = $("<div>", {
id,
style: "white-space: nowrap;float: right;display: inline;",
});
const $button = $("<div>", {
id: "csBtnSwapPedestrianRoadType",
title: "",
// TODO css
style: "display:inline-block;cursor:pointer;",
});
$button
.append(
"<i class=\"w-icon w-icon-streetview w-icon-lg\"></i><i class=\"fa fa-arrows-h fa-lg\" style=\"color: #e84545;vertical-align: top;\"></i><i class=\"w-icon w-icon-car w-icon-lg\"></i>",
)
.attr({
title: _trans.prefs.showSwapDrivingWalkingButton_Title,
});
$container.append($button);
// Insert swap button in the correct location based on display mode.
if (displayMode === "compact") {
const $label = $(
"#segment-edit-general > form > div.road-type-control.form-group > wz-label",
);
$label.css({ display: "inline" }).append($container);
} else {
const $label = $(
"#segment-edit-general > form > div.road-type-control.form-group > wz-label",
);
$label.css({ display: "inline" }).append($container);
}
// TODO css
$("#csBtnSwapPedestrianRoadType").click(onSwapPedestrianButtonClick);
}
}
function onSwapPedestrianButtonClick() {
if (_settings.warnOnPedestrianTypeSwap) {
_settings.warnOnPedestrianTypeSwap = false;
saveSettingsToStorage();
if (!confirm(_trans.swapSegmentTypeWarning)) {
return;
}
}
// Check for paths before deleting.
const segment = W.selectionManager.getSelectedDataModelObjects()[0];
if (segment.hasPaths()) {
WazeWrap.Alerts.error(SCRIPT_NAME, _trans.swapSegmentTypeError_Paths);
return;
}
const actions = [];
// Copy the selected segment geometry and attributes, then delete it.
const newGeom = { type: "LineString", coordinates: [] };
const oldPrimaryStreetID = segment.attributes.primaryStreetID;
const oldAltStreetIDs = segment.attributes.streetIDs.slice();
segment.getGeometry().coordinates.forEach(coord => {
newGeom.coordinates.push(coord.slice());
});
actions.push(new DelSeg(segment));
// create the replacement segment in the other segment type (pedestrian -> road & vice versa)
const newRoadType = isPedestrianTypeSegment(segment) ? 1 : 5;
const feature = new Segment({
geoJSONGeometry: newGeom,
roadType: newRoadType,
primaryStreetID: oldPrimaryStreetID,
streetIDs: oldAltStreetIDs,
});
feature.state = OpenLayers.State.INSERT;
actions.push(
new AddSeg(feature, {
createNodes: !0,
openAllTurns: W.prefs.get("enableTurnsByDefault"),
createTwoWay: W.prefs.get("twoWaySegmentsByDefault"),
// 2024-03-23 (mapomatic) I'm not sure what snappedFeatures is supposed to do, but it
// was not working with [null, null] after a recent WME update.
// snappedFeatures: [null, null]
}),
);
const description = `Change segment type to ${newRoadType === 1 ? "drivable" : "pedestrian"}`;
W.model.actionManager.add(new MultiAction(actions, { description }));
// Get the new segment and select it.
const newId = W.model.repos.segments.idGenerator.lastValue;
const newSegment = W.model.segments.getObjectById(newId);
W.selectionManager.setSelectedModels([newSegment]);
}
/* eslint-disable no-bitwise, no-mixed-operators */
function shadeColor2(color, percent) {
const f = parseInt(color.slice(1), 16);
const t = percent < 0 ? 0 : 255;
const p = percent < 0 ? percent * -1 : percent;
const R = f >> 16;
const G = (f >> 8) & 0x00ff;
const B = f & 0x0000ff;
return `#${(
0x1000000
+ (Math.round((t - R) * p) + R) * 0x10000
+ (Math.round((t - G) * p) + G) * 0x100
+ (Math.round((t - B) * p) + B)
)
.toString(16)
.slice(1)}`;
}
/* eslint-enable no-bitwise, no-mixed-operators */
function buildRoadTypeButtonCss() {
const lines = [];
const useOldColors = _settings.useOldRoadColors;
Object.keys(ROAD_TYPES).forEach(roadTypeAbbr => {
const roadType = ROAD_TYPES[roadTypeAbbr];
const bgColor = useOldColors ? roadType.svColor : roadType.wmeColor;
let output = `.cs-rt-buttons-container .cs-rt-button-${roadTypeAbbr} {background-color:${
bgColor
};box-shadow:0 2px ${shadeColor2(bgColor, -0.5)};border-color:${shadeColor2(bgColor, -0.15)};}`;
output += ` .cs-rt-buttons-container .cs-rt-button-${roadTypeAbbr}:hover {background-color:${shadeColor2(
bgColor,
0.2,
)}}`;
lines.push(output);
});
return lines.join(" ");
}
function injectCss() {
const css = [
// Road type button formatting
".csRoadTypeButtonsCheckBoxContainer {margin-left:15px;}",
".cs-rt-buttons-container {margin-bottom:5px;height:21px;}",
".cs-rt-buttons-container .cs-rt-button {font-size:11px;line-height:20px;color:black;padding:0px 4px;height:20px;"
+ "margin-right:2px;border-style:solid;border-width:1px;}",
buildRoadTypeButtonCss(),
".btn.cs-rt-button:active {box-shadow:none;transform:translateY(2px)}",
"div .cs-rt-buttons-group {float:left; margin: 0px 5px 5px 0px;}",
"#sidepanel-clicksaver .controls-container {padding:0px;}",
"#sidepanel-clicksaver .controls-container label {white-space: normal;}",
"#sidepanel-clicksaver {font-size:13px;}",
// Compact moad road type button formatting.
".cs-compact-button[checked=\"false\"] {opacity: 0.65;}",
// Lock button formatting
".cs-group-label {font-size: 11px; width: 100%; font-family: Poppins, sans-serif;"
+ " text-transform: uppercase; font-weight: 700; color: #354148; margin-bottom: 6px;}",