diff options
Diffstat (limited to 'src/dumbyUtils.mjs')
-rw-r--r-- | src/dumbyUtils.mjs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs index e7edee3..71dc290 100644 --- a/src/dumbyUtils.mjs +++ b/src/dumbyUtils.mjs | |||
@@ -1,3 +1,5 @@ | |||
1 | import LeaderLine from 'leader-line'; | ||
2 | |||
1 | export function focusNextMap(reverse = false) { | 3 | export function focusNextMap(reverse = false) { |
2 | const renderedList = this.utils.renderedMaps(); | 4 | const renderedList = this.utils.renderedMaps(); |
3 | const index = renderedList.findIndex(e => e.classList.contains('focus')); | 5 | const index = renderedList.findIndex(e => e.classList.contains('focus')); |
@@ -56,3 +58,70 @@ export function switchToNextLayout(reverse = false) { | |||
56 | export function removeBlockFocus() { | 58 | export function removeBlockFocus() { |
57 | this.blocks.forEach(b => b.classList.remove('focus')); | 59 | this.blocks.forEach(b => b.classList.remove('focus')); |
58 | } | 60 | } |
61 | |||
62 | /** | ||
63 | * Create geolinks, which points to map by geo schema and id | ||
64 | * | ||
65 | * @param {HTMLElement} Elements contains anchor elements for doclinks | ||
66 | * @returns {Boolean} ture is link is created, false if coordinates are invalid | ||
67 | */ | ||
68 | export const createGeoLink = (link, callback = null) => { | ||
69 | const url = new URL(link.href); | ||
70 | const xyInParams = url.searchParams.get('xy'); | ||
71 | const xy = xyInParams | ||
72 | ? xyInParams.split(',')?.map(Number) | ||
73 | : url?.href | ||
74 | ?.match(/^geo:([0-9.,]+)/) | ||
75 | ?.at(1) | ||
76 | ?.split(',') | ||
77 | ?.reverse() | ||
78 | ?.map(Number); | ||
79 | |||
80 | if (!xy || isNaN(xy[0]) || isNaN(xy[1])) return false; | ||
81 | |||
82 | // Geo information in link | ||
83 | link.url = url; | ||
84 | link.xy = xy; | ||
85 | link.classList.add('with-leader-line', 'geolink'); | ||
86 | link.targets = link.url.searchParams.get('id')?.split(',') ?? null; | ||
87 | |||
88 | // LeaderLine | ||
89 | link.lines = []; | ||
90 | callback?.call(this, link); | ||
91 | |||
92 | return true; | ||
93 | }; | ||
94 | |||
95 | /** | ||
96 | * CreateDocLink. | ||
97 | * | ||
98 | * @param {HTMLElement} Elements contains anchor elements for doclinks | ||
99 | */ | ||
100 | export const createDocLink = link => { | ||
101 | link.classList.add('with-leader-line', 'doclink'); | ||
102 | link.lines = []; | ||
103 | |||
104 | link.onmouseover = () => { | ||
105 | const label = decodeURIComponent(link.href.split('#')[1]); | ||
106 | const selector = link.title.split('=>')[1] ?? '#' + label; | ||
107 | const target = document.querySelector(selector); | ||
108 | if (!target?.checkVisibility()) return; | ||
109 | |||
110 | const line = new LeaderLine({ | ||
111 | start: link, | ||
112 | end: target, | ||
113 | middleLabel: LeaderLine.pathLabel({ | ||
114 | text: label, | ||
115 | fontWeight: 'bold', | ||
116 | }), | ||
117 | hide: true, | ||
118 | path: 'magnet', | ||
119 | }); | ||
120 | link.lines.push(line); | ||
121 | line.show('draw', { duration: 300 }); | ||
122 | }; | ||
123 | link.onmouseout = () => { | ||
124 | link.lines.forEach(line => line.remove()); | ||
125 | link.lines.length = 0; | ||
126 | }; | ||
127 | }; | ||