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

Last change on this file since 32110 was 31067, checked in by kjdon, 8 years ago

get the 'top level menu' text from the dictionary

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