1+ {{/*
2+ huge/common/assets/GetSelectedConfig
3+ This function brings consistency to the way Scripts and Styles retrieves data from config.
4+ It reads a list of selected assets to be processed, and try and match them with declared assets,
5+ before returning the data to be consumed by each features' respective `GetSelectedScripts/GetSelectedStyles`
6+
7+ @author @regisphilibert
8+
9+ @context Slice
10+ - Slice of maps (Declared Assets to try and match from)
11+ - Slice of maps/string (Selected Assets from the top most context)
12+
13+ @access private
14+
15+ @returns Slice of Maps
16+ See scripts/private/GetDeclaredScripts@returns
17+
18+ @uses
19+ - huge/scripts/private/GetDeclaredScripts
20+
21+ */}}
22+
23+ {{/* First item in the context slice is the assets found in config */}}
24+ {{ $declared_assets := index $ 0 }}
25+ {{/* Second item is the assets passed through the partial /tags context. */}}
26+ {{ $selected_assets := index $ 1 }}
27+
28+ {{ $assets := slice }}
29+ {{ range $asset_ref := $selected_assets }}
30+ {{ $entry := dict }}
31+ {{/* If entry is a map we'll store it and then try and match map from config with the same .name */}}
32+ {{ if reflect.IsMap . }}
33+ {{ $entry = . }}
34+ {{ with .name }}
35+ {{ with where $declared_assets "name" . }}
36+ {{ with index . 0 }}
37+ {{/* If matching config map found, we merge the this one on top of it
38+ This ways user can overwrite config settings from the templates. */}}
39+ {{ $entry = merge . $entry }}
40+ {{ end }}
41+ {{ end }}
42+ {{ end }}
43+ {{ else }}
44+ {{/* If entry is not a map, then it must be a string representing the name of a map stored in config.transition
45+ We must therefor find its match */}}
46+ {{ with $declared_assets }}
47+ {{ with where . "name" $asset_ref }}
48+ {{ with index . 0 }}
49+ {{ $entry = . }}
50+ {{ end }}
51+ {{ else }}
52+ {{ partialCached "huge/console/warn" (printf "An asset named %s is invoked but not declared" $asset_ref) }}
53+ {{ end }}
54+ {{ end }}
55+ {{ end }}
56+
57+ {{/* We only use the entry if it holds a .path */}}
58+ {{ with $entry.path }}
59+ {{ $assets = $assets | append $entry }}
60+ {{ end }}
61+
62+ {{ end }}
63+
64+ {{ return $assets }}
0 commit comments