diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2024-10-01 12:27:59 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2024-10-01 12:32:04 +0800 |
commit | 5072cf2805e7815524c9320ddd7970dd9625f024 (patch) | |
tree | 10f0a37dab29e9265cfffcfe9cbefb8d6e592776 /src/dumbymap.mjs | |
parent | 312da714f8fc56d2f4ddbbc0a9759bf19b9c81d4 (diff) |
refactor: menu-item only call methods from dumbymap.utils
Diffstat (limited to 'src/dumbymap.mjs')
-rw-r--r-- | src/dumbymap.mjs | 77 |
1 files changed, 4 insertions, 73 deletions
diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs index c7bdc92..72936ec 100644 --- a/src/dumbymap.mjs +++ b/src/dumbymap.mjs | |||
@@ -21,76 +21,6 @@ const layouts = [ | |||
21 | ]; | 21 | ]; |
22 | const mapCache = {}; | 22 | const mapCache = {}; |
23 | 23 | ||
24 | // FUNCTION: Get DocLinks from special anchor element {{{ | ||
25 | /** | ||
26 | * CreateDocLink. | ||
27 | * | ||
28 | * @param {HTMLElement} Elements contains anchor elements for doclinks | ||
29 | */ | ||
30 | export const createDocLink = link => { | ||
31 | link.classList.add('with-leader-line', 'doclink'); | ||
32 | link.lines = []; | ||
33 | |||
34 | link.onmouseover = () => { | ||
35 | const label = decodeURIComponent(link.href.split('#')[1]); | ||
36 | const selector = link.title.split('=>')[1] ?? '#' + label; | ||
37 | const target = document.querySelector(selector); | ||
38 | if (!target?.checkVisibility()) return; | ||
39 | |||
40 | const line = new LeaderLine({ | ||
41 | start: link, | ||
42 | end: target, | ||
43 | middleLabel: LeaderLine.pathLabel({ | ||
44 | text: label, | ||
45 | fontWeight: 'bold', | ||
46 | }), | ||
47 | hide: true, | ||
48 | path: 'magnet', | ||
49 | }); | ||
50 | link.lines.push(line); | ||
51 | line.show('draw', { duration: 300 }); | ||
52 | }; | ||
53 | link.onmouseout = () => { | ||
54 | link.lines.forEach(line => line.remove()); | ||
55 | link.lines.length = 0; | ||
56 | }; | ||
57 | }; | ||
58 | // }}} | ||
59 | // FUNCTION: Get GeoLinks from special anchor element {{{ | ||
60 | /** | ||
61 | * Create geolinks, which points to map by geo schema and id | ||
62 | * | ||
63 | * @param {HTMLElement} Elements contains anchor elements for doclinks | ||
64 | * @returns {Boolean} ture is link is created, false if coordinates are invalid | ||
65 | */ | ||
66 | export const createGeoLink = (link, callback = null) => { | ||
67 | const url = new URL(link.href); | ||
68 | const xyInParams = url.searchParams.get('xy'); | ||
69 | const xy = xyInParams | ||
70 | ? xyInParams.split(',')?.map(Number) | ||
71 | : url?.href | ||
72 | ?.match(/^geo:([0-9.,]+)/) | ||
73 | ?.at(1) | ||
74 | ?.split(',') | ||
75 | ?.reverse() | ||
76 | ?.map(Number); | ||
77 | |||
78 | if (!xy || isNaN(xy[0]) || isNaN(xy[1])) return false; | ||
79 | |||
80 | // Geo information in link | ||
81 | link.url = url; | ||
82 | link.xy = xy; | ||
83 | link.classList.add('with-leader-line', 'geolink'); | ||
84 | link.targets = link.url.searchParams.get('id')?.split(',') ?? null; | ||
85 | |||
86 | // LeaderLine | ||
87 | link.lines = []; | ||
88 | callback?.call(this, link); | ||
89 | |||
90 | return true; | ||
91 | }; | ||
92 | // }}} | ||
93 | |||
94 | export const markdown2HTML = (container, mdContent) => { | 24 | export const markdown2HTML = (container, mdContent) => { |
95 | // Render: Markdown -> HTML {{{ | 25 | // Render: Markdown -> HTML {{{ |
96 | container.replaceChildren(); | 26 | container.replaceChildren(); |
@@ -184,7 +114,9 @@ export const generateMaps = (container, { delay, mapCallback }) => { | |||
184 | 114 | ||
185 | // LeaderLine {{{ | 115 | // LeaderLine {{{ |
186 | 116 | ||
187 | Array.from(container.querySelectorAll(docLinkSelector)).filter(createDocLink); | 117 | Array.from(container.querySelectorAll(docLinkSelector)).filter( |
118 | utils.createDocLink, | ||
119 | ); | ||
188 | 120 | ||
189 | // Get anchors with "geo:" scheme | 121 | // Get anchors with "geo:" scheme |
190 | htmlHolder.anchors = []; | 122 | htmlHolder.anchors = []; |
@@ -202,7 +134,7 @@ export const generateMaps = (container, { delay, mapCallback }) => { | |||
202 | }; | 134 | }; |
203 | const geoLinks = Array.from( | 135 | const geoLinks = Array.from( |
204 | container.querySelectorAll(geoLinkSelector), | 136 | container.querySelectorAll(geoLinkSelector), |
205 | ).filter(l => createGeoLink(l, geoLinkCallback)); | 137 | ).filter(l => utils.createGeoLink(l, geoLinkCallback)); |
206 | 138 | ||
207 | const isAnchorPointedBy = link => anchor => { | 139 | const isAnchorPointedBy = link => anchor => { |
208 | const mapContainer = anchor.closest('.mapclay'); | 140 | const mapContainer = anchor.closest('.mapclay'); |
@@ -527,7 +459,6 @@ export const generateMaps = (container, { delay, mapCallback }) => { | |||
527 | }); | 459 | }); |
528 | }); | 460 | }); |
529 | // }}} | 461 | // }}} |
530 | |||
531 | // Menu {{{ | 462 | // Menu {{{ |
532 | const menu = document.createElement('div'); | 463 | const menu = document.createElement('div'); |
533 | menu.className = 'menu'; | 464 | menu.className = 'menu'; |