@@ -68,23 +68,123 @@ function constructTree(
6868 setSelected ,
6969 proxyData = null
7070) {
71+
7172 const { languages, tree } = data . language_tree ;
72- const languageMap = { } ;
73+ const languageMap = { 'common' : { } } ;
7374
7475 if ( tree === null ) {
7576 return null ;
7677 }
7778
78- function merge_trees ( ) {
79- if ( proxyData === null ) {
80- return languages ;
81- }
79+ if ( proxyData === null ) {
80+
81+ languages . forEach ( language => {
82+ languageMap [ 'common' ] [ compositeIdToString ( language . id ) ] = language ;
83+ } ) ;
84+
85+ } else {
86+
87+ // Merging local and proxy language maps
88+
8289 const { languages : proxyLanguages , tree : proxyTree } = proxyData . language_tree ;
83- }
90+ const languageData = { local : languages , proxy : proxyLanguages } ;
91+ const dictionaryMap = { } ;
92+ const perspectiveMap = { } ;
8493
85- languages . forEach ( language => {
86- languageMap [ compositeIdToString ( language . id ) ] = language ;
87- } ) ;
94+ // Getting maps of languages, dictionaries and perspectives by id
95+ [ 'local' , 'proxy' ] . forEach ( side => {
96+
97+ languageMap [ side ] = { } ;
98+ dictionaryMap [ side ] = { } ;
99+ perspectiveMap [ side ] = { } ;
100+
101+ languageData [ side ] . forEach ( language => {
102+ languageMap [ side ] [ compositeIdToString ( language . id ) ] = language ;
103+
104+ language . dictionaries . forEach ( dictionary => {
105+ dictionaryMap [ side ] [ compositeIdToString ( dictionary . id ) ] = dictionary ;
106+
107+ dictionary . perspectives . forEach ( perspective => {
108+ perspectiveMap [ side ] [ compositeIdToString ( perspective . id ) ] = perspective ;
109+ } ) ;
110+ } ) ;
111+ } ) ;
112+ } ) ;
113+
114+ // Getting diffs and intersection of lang/dict/pers lists between local and proxy sides
115+ // !!!!!!!! keys() ?????????????????
116+ [ languageMap , dictionaryMap , perspectiveMap ] . forEach ( amap => {
117+
118+ amap [ 'local_diff' ] = amap [ 'local' ] . difference ( amap [ 'proxy' ] ) ;
119+ amap [ 'proxy_diff' ] = amap [ 'proxy' ] . difference ( amap [ 'local' ] ) ;
120+ amap [ 'union' ] = amap [ 'local' ] . union ( amap [ 'proxy' ] ) ;
121+ amap [ 'intersection' ] = amap [ 'local' ] . intersection ( amap [ 'proxy' ] ) ;
122+ } ) ;
123+
124+ // Marking object in input map as 'single' if corresponding '_diff' list includes its id
125+ [ languageMap , dictionaryMap , perspectiveMap ] . forEach ( amap => {
126+ [ 'local' , 'proxy' ] . forEach ( side => {
127+ amap [ side ] . forEach ( ( obj , id ) => {
128+ if ( amap [ `${ side } _diff` ] . has ( id ) ) {
129+ obj . single = side ;
130+ }
131+ } ) ;
132+ } ) ;
133+ } ) ;
134+
135+ // Iterate through language_union (local+proxy)
136+ // collect languages into common map
137+ languageMap [ 'union' ] . forEach ( lang_id => {
138+
139+ // If language exists only on proxy side
140+ if ( languageMap [ 'proxy_diff' ] . has ( lang_id ) ) {
141+ const lang_result = languageMap [ 'proxy' ] [ lang_id ] ;
142+ languageMap [ 'common' ] [ lang_id ] = lang_result ;
143+
144+ } else {
145+ const lang_result = languageMap [ 'local' ] [ lang_id ] ;
146+ languageMap [ 'common' ] [ lang_id ] = lang_result ;
147+
148+ // If language is on the both sides
149+ if ( languageMap [ 'intersection' ] . has ( lang_id ) ) {
150+ const dict_union = [
151+ ...languageMap [ 'local' ] [ lang_id ] . dictionaries ,
152+ ...languageMap [ 'proxy' ] [ lang_id ] . dictionaries ] ;
153+
154+ // Iterate through dictionary_union for current language
155+ dict_union . forEach ( dict_id => {
156+
157+ // If dictionary exists only on proxy side
158+ if ( dictionaryMap [ 'proxy_diff' ] . has ( dict_id ) ) {
159+ const dict_result = dictionaryMap [ 'proxy' ] [ dict_id ] ;
160+ lang_result . dictionaries . push ( dict_result ) ;
161+
162+ } else {
163+ const dict_result = dictionaryMap [ 'local' ] [ dict_id ] ;
164+ lang_result . dictionaries . push ( dict_result ) ;
165+
166+ // If dictionary is on the both sides
167+ if ( dictionaryMap [ 'intersection' ] . has ( dict_id ) ) {
168+ const pers_union = [
169+ ...dictionaryMap [ 'local' ] [ dict_id ] . perspectives ,
170+ ...dictionaryMap [ 'proxy' ] [ dict_id ] . perspectives ] ;
171+
172+ // Iterate through perspective_union for current dictionary
173+ pers_union . forEach ( pers_id => {
174+
175+ // If perspective exists only on proxy side
176+ if ( perspectiveMap [ 'proxy_diff' ] . has ( pers_id ) ) {
177+ const pers_result = perspectiveMap [ 'proxy' ] [ pers_id ] ;
178+ dict_result . perspectives . push ( pers_result ) ;
179+ }
180+ } ) ;
181+ }
182+ }
183+ } ) ;
184+ }
185+ }
186+ } ) ;
187+ }
88188
89189 let groupMap = undefined ;
90190 let groupDictionaryIdSetMap = undefined ;
@@ -111,7 +211,7 @@ function constructTree(
111211 < LanguageNode
112212 key = { index }
113213 node = { node }
114- languageMap = { languageMap }
214+ languageMap = { languageMap [ 'common' ] }
115215 selected = { selected }
116216 setSelected = { setSelected }
117217 proxyData = { proxyPermission }
@@ -120,7 +220,7 @@ function constructTree(
120220 ) : (
121221 < LanguageNode
122222 node = { tree }
123- languageMap = { languageMap }
223+ languageMap = { languageMap [ 'common' ] }
124224 selected = { selected }
125225 setSelected = { setSelected }
126226 proxyData = { proxyPermission }
@@ -135,7 +235,7 @@ function constructTree(
135235 node = { node }
136236 groupMap = { groupMap }
137237 dictionaryIdSet = { groupDictionaryIdSetMap [ String ( node [ 0 ] ) ] }
138- languageMap = { languageMap }
238+ languageMap = { languageMap [ 'common' ] }
139239 selected = { selected }
140240 setSelected = { setSelected }
141241 proxyData = { proxyPermission }
@@ -144,7 +244,7 @@ function constructTree(
144244 < IndividualNode
145245 key = { index }
146246 node = { node }
147- languageMap = { languageMap }
247+ languageMap = { languageMap [ 'common' ] }
148248 dictionaryIdSet = { groupDictionaryIdSetMap [ "" ] }
149249 selected = { selected }
150250 setSelected = { setSelected }
@@ -157,7 +257,7 @@ function constructTree(
157257 node = { [ Number ( entityId ) , tree [ 1 ] ] }
158258 groupMap = { groupMap }
159259 dictionaryIdSet = { groupDictionaryIdSetMap [ entityId ] }
160- languageMap = { languageMap }
260+ languageMap = { languageMap [ 'common' ] }
161261 selected = { selected }
162262 setSelected = { setSelected }
163263 proxyData = { proxyPermission }
0 commit comments