source: main/trunk/greenstone3/web/interfaces/default/js/hierarchy.js@ 31014

Last change on this file since 31014 was 31014, checked in by Georgiy Litvinov, 7 years ago

Modified hierarchy js storage, added kjdon fixes.

File size: 12.5 KB
Line 
1//hierarchy storage object
2
3//hierarchy menu Button text
4var hierarchyMenuButton = "Menu";
5//Find first ancestor element by tag name
6function findAncestorByTagName (element, tagName) {
7 while ((element.tagName != tagName) && (element = element.parentElement));
8 return element;
9}
10// Function to set Id as TEXTAREA value
11function setHierarchyId(a)
12{
13
14 var id = a.getAttribute("title");
15
16// If ID defined and not null
17 if (id && (id != null))
18 {
19 //find TR Ancestor to get TEXTAREA
20 var tr = findAncestorByTagName(a,"TR");
21 //Set value to id of clicked element
22 $(tr.getElementsByTagName("TEXTAREA")).val(id);
23 // Set button name
24 removeSuggestionsMenu(tr, $(a).text());
25 //Hide menu after click
26 $(tr).find(".metaDataHierarchyMenu").find("ul li ul li").hide();
27 //If we left TEXTAREA, hide all menus
28 //if (document.activeElement.tagName != "TEXTAREA")
29 //{
30 // $(tr).find(".metaDataHierarchyMenu").hide();
31 //}
32 tr.getElementsByTagName("TEXTAREA")[0].focus();
33
34 createSuggestionsMenu(tr);
35
36 }
37}
38function openHierarchyMenuLevel(menuItem)
39{
40 //console.log(menuItem);
41
42 var tr = findAncestorByTagName(menuItem,"TR");
43 //get current MetaDataName
44 var metaName = $(tr.getElementsByClassName("metaTableCellName")[0]).text();
45 //Get current hierarchy from storages
46 var hierarchyData = hierarchyStorage[metaName];
47 menuItem = $(menuItem);
48 if (menuItem.find('ul').length == 0)
49 {
50 //Expression to extract hierarchy identifier from menu item id
51 var getIdExp = /[0-9.]+/;
52 //Extracted hierarchy identifier
53 var id;
54 //Expression to get childs
55 var childExpr;
56 if (menuItem.attr('id'))
57 {
58 id = menuItem.attr('id').match(getIdExp);
59 }
60 //console.log("ID " + id);
61 //id.match(getIdExp);
62 if (id == null)
63 {
64 childExpr = /^[0-9]+$/;
65 }
66 else
67 {
68 childExpr = new RegExp("^" + id + "." + "[0-9]+$");
69 }
70 var levelItems = {};
71 for(var key in hierarchyData)
72 {
73 if(childExpr.test(key)){
74 levelItems[key]='<li id="'+key+'" ><button onclick="setHierarchyId(this)" title='+ hierarchyData[key][0] +'>' + hierarchyData[key][1] + '</button></li>';
75 //console.log(levelItems[key]);
76
77 }
78 }
79 //If no elements in hierarchy level
80 if (jQuery.isEmptyObject(levelItems))
81 {
82 //add empty menu. Invisible. Used by checks in setHierarchyEventsWrappers focusout to prevent menu hiding while choosing suggestion menu item leaf
83 menuItem.append("<ul></ul>");
84
85 // console.log("no elements in hierarchy level");
86 }
87 else {
88 //wrap elements in hierarchy level
89 var levelMenu = '<ul>';
90 for(var key in levelItems)
91 {
92 //Fill menu with items
93 levelMenu += levelItems[key];
94 }
95 levelMenu += "</ul>";
96 menuItem.append(levelMenu);
97 menuItem.find("li").hover(
98 function(){openHierarchyMenuLevel(this);}
99 ,
100 function(){closeHierarchyMenuLevel(this);}
101 );
102
103 //menuItem.find('ul');
104 menuItem.children('ul').slideDown();
105
106
107 //console.log("debug line 5");
108 }
109
110 } else {
111 //stop animation
112 menuItem.find('ul').stop(true, true);
113 //show menu items
114 menuItem.children('ul').children('li').show();
115 //slide down menu
116 menuItem.children('ul').slideDown();
117 }
118 menuItem.addClass("active");
119}
120function closeHierarchyMenuLevel(menuItem)
121{
122 $(menuItem).removeClass("active");
123 $(menuItem).find('ul').hide();
124}
125// Download hierarchy file and process it
126function downloadAndProcessHierarchyFile(hierarchyFileName,metaName)
127{
128
129 var xmlhttp=new XMLHttpRequest();
130 xmlhttp.open("GET",hierarchyFileName,true);
131 xmlhttp.send();
132 xmlhttp.onreadystatechange=function()
133 {
134 if (xmlhttp.readyState==4 && xmlhttp.status==200)
135 {
136 var hierarchyFile = xmlhttp.responseText;
137 var StringData = [];
138 var hierarchyData = {};
139 //var expr = /^([0-9]+(?:\.[0-9]+)*)\ ([0-9]+(?:\.[0-9]+)*)\ (.*)/m;
140 var expr = /^(\S*|\"[^\"]*\")\ +([0-9]+(?:\.[0-9]+)*)\ +(.*)/m;
141 StringData = hierarchyFile.split('\n');
142 for (var i = 0; i < StringData.length; i++)
143 {
144 var result = StringData[i].match(expr);
145 // If result not null
146 if (result != null && result.length == 4)
147 {
148 // populate hierarchy object
149 hierarchyData[result[2]] = [result[1], result[3]];
150 }
151
152 }
153 addHierarchyToStorage(metaName, hierarchyData);
154 setHierarchyEventsWrappers(metaName);
155 }
156 }
157
158}
159
160
161function setHierarchyHoverEvent(father,className)
162{
163
164 $(father).find(className).hover(function()
165 {
166 // console.log("HOVER ENTERED")
167 openHierarchyMenuLevel(this);
168 }, function() {
169 // console.log("HOVER EXITED")
170 closeHierarchyMenuLevel(this);
171 });
172
173}
174function createHierarchyMenuButton(row)
175{
176 //console.log(row)
177 //console.log(metaName)
178 //get current MetaDataName
179 var metaName = $(row.getElementsByClassName("metaTableCellName")[0]).text();
180
181 var hierarchyMenuName = 'Menu';
182 // Check if textarea already contain right menu key
183 var textAreaValue = $(row).find('TEXTAREA').val();
184
185 //Get current hierarchy from storages
186 var hierarchyData = hierarchyStorage[metaName];
187
188 if (hierarchyData[textAreaValue])
189 {
190 hierarchyMenuName = hierarchyData[textAreaValue];
191 }
192 //Menu element
193 var mainmenu = '<td class="metaDataHierarchyMenu" style="display: none;"><ul><li id="hierarchyLevel"><button class="hierarchyMenuButton" title="Menu">' + hierarchyMenuName + '</button></li></ul></td>'
194 //Insert hierarchy menu
195 $(row).find('.metaTableCellRemove').after(mainmenu);
196 //Set hover event on hierarchy menu
197 $(row).each(function(){setHierarchyHoverEvent($(this),".metaDataHierarchyMenu ul li")});
198 //Set menu name or SuggestionsMenu on change of textarea set menu name to appropriate menu item if exists
199 $(row).find('.metaTableCellArea').bind('input propertychange',function()
200 {
201 var input = $(this).val();
202 var row = this.parentElement.parentElement;
203 //RegExp to test a valid key in input
204 var KeyExp = /^[0-9]+(?:\.[0-9]+)*$/;
205 //RegExp to test a valid key start in input
206 var KeyStartExp = /^(?:[0-9]+(?:\.[0-9]+)*)?\.$/;
207 //if input valid and key found
208 if ( KeyExp.test(input) && hierarchyData[input])
209 {
210 removeSuggestionsMenu(row,hierarchyData[input]);
211 createSuggestionsMenu(row);
212 }
213 else if (KeyStartExp.test(input))
214 {
215 removeSuggestionsMenu(row,hierarchyMenuButton);
216 createSuggestionsMenu(row);
217 }
218 else {
219 removeSuggestionsMenu(row,hierarchyMenuButton);
220 }
221 });
222 //Show created menu
223 $(row).find('.metaDataHierarchyMenu').show();
224}
225
226function createSuggestionsMenu(row)
227{
228 //get current MetaDataName
229 var metaName = $(row.getElementsByClassName("metaTableCellName")[0]).text();
230 //Get current hierarchy from storages
231 var hierarchyData = hierarchyStorage[metaName];
232
233 var input = $(row.getElementsByClassName("metaTableCellArea")[0]).val();
234
235 //RegExp to get SuggestionsMenu
236 var SuggestionsMenuExp = new RegExp("^0*" + input.replace(/\./g, '\\.0*') + "\\.?[0-9]+$")
237 //Hierarchy suggestions menu
238 var SuggestionsMenu = "";
239 for(var key in hierarchyData)
240 {
241 var SuggestionsMenuItems = {};
242
243 if (SuggestionsMenuExp.test(key))
244 {
245 SuggestionsMenuItems[key]='<li class="hierarchySuggestionsMenu" id="'+key+'" ><button title='+ hierarchyData[key][0] +' onclick="setHierarchyId(this)" >' + key.substring(String(input).length) + " " + hierarchyData[key][1] + '</button></li>';
246 }
247
248 for(var key in SuggestionsMenuItems)
249 {
250 //Fill menu with items
251 SuggestionsMenu += SuggestionsMenuItems[key];
252 }
253
254 }
255
256 //Append new SuggestionsMenu
257 $(row).find(".metaDataHierarchyMenu ul").append(SuggestionsMenu);
258 //Register event
259 $(row).each(function(){setHierarchyHoverEvent($(this),".hierarchySuggestionsMenu")});
260}
261function removeSuggestionsMenu(row,menuNewText)
262{
263 //Remove old SuggestionsMenu
264 $(row).find(".hierarchySuggestionsMenu").remove();
265 //Replace text on Hierarchy menu to default
266 $(row).find(".hierarchyMenuButton").text(menuNewText);
267}
268
269function setHierarchyEventsWrappers(metaName)
270{
271
272 //Loop through every metaTableCell
273 $(".metaTableCellName").each(function() {
274 //Check if it is a hierarchy row
275 //TODO implement real check
276 //if($(this).text()=="rubricator")
277 var currentMetaName = $(this).text();
278 //console.log(metaName)
279 //console.log(metaDataName)
280 if (currentMetaName in hierarchyStorage && currentMetaName == metaName)
281 {
282 //console.log('testXX')
283 var row = this.parentElement;
284 var textArea = row.getElementsByClassName("metaTableCellArea")[0];
285
286 //Mouse leave row
287 $(row).mouseleave(function() {
288 //textArea = this.getElementsByClassName("metaTableCellArea")[0];
289 if (this.getElementsByClassName("metaDataHierarchyMenu").length != 0 && document.activeElement.tagName != "TEXTAREA")
290 {
291 $(this).find('ul').stop(true, true);
292 //Remove hierarchy menu
293 $(this).find('.metaDataHierarchyMenu').hide();
294 }
295
296 });
297 // Mouse enter row
298 $(row).mouseenter(
299 function()
300 {
301 var row = this;
302 var table = row.parentElement;
303 //If focused on TEXTAREA do nothing
304 if (document.activeElement.tagName != "TEXTAREA")
305 {
306 //Hide all menus in table except this one
307 $(table).find('.metaDataHierarchyMenu').each(function() {
308 var currentRow = this.parentElement;
309 if (!$(currentRow).is($(row)) )
310 {
311 $(this).hide();
312 }
313 });
314
315 // createHierarchyMenuButton($(row));
316 if ( $(row).find('ul').length == 0 )
317 {
318 createHierarchyMenuButton(row);
319 createSuggestionsMenu(row);
320 }
321 else
322 {
323 //Unhide menu
324 $(row).find('.metaDataHierarchyMenu').show();
325 //Minimize nested menus
326 $(row).find('.metaDataHierarchyMenu ul').find('ul').hide();
327 }
328 }
329
330
331 }
332 );
333
334
335 // Textarea focus
336 $(textArea).focus(
337 function()
338 {
339 var row = this.parentElement.parentElement;
340 var table = row.parentElement;
341
342
343 //Hide all menus in table except this one
344 $(table).find('.metaDataHierarchyMenu').each(function() {
345 var currentRow = this.parentElement;
346 if (!$(currentRow).is($(row)) )
347 {
348 $(this).hide();
349 }
350 });
351 //Create button
352 if ( $(row).find('ul').length == 0 )
353 {
354 createHierarchyMenuButton(row);
355 createSuggestionsMenu(row);
356 }
357 else
358 {
359 //Unhide menu
360 $(row).find('.metaDataHierarchyMenu').show();
361 //Minimize nested menus
362 $(row).find('.metaDataHierarchyMenu ul').find('ul').slideUp();
363 }
364
365
366 }
367 );
368 $(textArea).focusout(
369 function()
370 {
371 var row = this.parentElement.parentElement;
372 //Test if there are open submenu
373 var found = $(row).find('.metaDataHierarchyMenu ul li ul').filter(":visible")[0];
374
375 //Hide hierarchy menu if there are no open submenus
376 if ( found === undefined)
377 {
378 //Hide menu
379 $(row).find('.metaDataHierarchyMenu').hide();
380 //console.log(this);
381 //console.log(row);
382 //console.log(found);
383 }
384
385
386 }
387 );
388 }
389 });
390}
391var hierarchyStorage = {};
392function addHierarchyToStorage(metaDataName,processedHierarchy)
393{
394 hierarchyStorage[metaDataName] = processedHierarchy;
395 //console.log( hierarchyStorage)
396 //console.log( metaDataName)
397
398}
399
Note: See TracBrowser for help on using the repository browser.