Javascript help required

Are you developing tools to extend the LiveCode environment? This is the place to talk about the nuts and bolts of extending our nuts and bolts. If you want to use a LiveCode or third party Environment extension, visit the Using Evironment Extensions forum.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 285
Joined: Thu Sep 04, 2008 6:23 am
Location: Melbourne Australia

Javascript help required

Post by jameshale » Sun Oct 28, 2018 3:11 am

In compiling the livecode docset for DASH I need to link items provided in the list of associations with the relevant page in the docs.

I didn't think this would be too difficult and the current version of the docset works fine in most cases.
However there are some entries which fail my code for parsing the correct link.
Rather than hard code the exceptions I thought I would try to decipher what LC's dictionary does when it loads a page from the api database given this is the same raw info I have to use.

Unfortunately my knowledge of javascript is less than zero and am asking for some help in deciphering some code.

Rather than risk email mangling of the code I am asking on the forum for help.

I am looking at this file on git:
livecode-ide/Documentation/html_viewer/js/dictionary_functions.js

And in it there is the following construction which I haven't been able to find info on.

Code: Select all

	$.each(dataGet(), function(index, value) {
I figure it is a loop construction and it will step through each item retrieved by the "dataGet()" function.
Now following the comma there is "function(index,value)"
I don't know how to interpret this.
Is it a function call (if so to what?)
Is it a function definition (meaning?)

the full block is...

Code: Select all

	function entryIdToIndex(pId){
		var tIndex = 0;
	
		$.each(dataGet(), function(index, value) {
			if(value.id == pId){
				tIndex = index;
				return false;
			}
			
		});
		return tIndex;
	}
Given this is representative could someone rewrite this in english for me :-) ?

Also,

does...

Code: Select all

if (value.name.toLowerCase() != pName 
			    && value["display name"].toLowerCase() != pName)

mean

if (lowercase of name does not equal pName) AND (lowercase of display name does not equal pName)

All this to to ultimately decipher what I think are the two main functions I need to understand...

Code: Select all

//first block

case "associations":
if($.isArray(value)){
	tHTML += '<div class="col-md-2 lcdoc_section_title">'+index+'</div><div class="col-md-10" style="margin-bottom:10px">';
	var association_html = "";
	$.each(value, function(index2, value2) 
	{
		var tIndex = resolve_association_index(value2, tEntryObject.library);
		
		var tAssociation;
		if (isDefined(tIndex))
		{
			var tName = value2;
			if (tState.data[tIndex].hasOwnProperty("display name"))
				tName = tState.data[tIndex]["display name"];

			tAssociation = click_text_from_index(tName, tIndex);
		}
		else
			tAssociation = value2;
		
		if (association_html == "") 
			association_html = tAssociation;
		else 
			association_html += ',' + tAssociation;
	});
	tHTML += association_html+'</div>';
}
break;

// second block					
					


	// Associations must be one of the following:	
	const s_association_types = ["object","library","glossary","module","widget"];
	function resolve_association_index(pName, pAPI)
	{
		var tIndex;
		$.each(s_association_types, function(tTypeIndex, tType) {
			tIndex = entryNameToIndex(pName, tType, pAPI)
			if (isDefined(tIndex))
				return false;
		});
		return tIndex;
	}

James

bwmilby
Posts: 216
Joined: Wed Jun 07, 2017 5:37 am
Location: New Orleans, LA
Contact:

Re: Javascript help required

Post by bwmilby » Mon Oct 29, 2018 1:03 am

jameshale wrote:
Sun Oct 28, 2018 3:11 am

Code: Select all

	$.each(dataGet(), function(index, value) {
I figure it is a loop construction and it will step through each item retrieved by the "dataGet()" function.
Now following the comma there is "function(index,value)"
http://api.jquery.com/jquery.each/

What you are seeing is an anonymous function (not positive on the terminology though). dataGet() will return an array or array-like object, $.each will iterate over every element of the array, function(index, value) creates the function that will be called on each element of the list. The parameters to the function are the index and value for each element of the list. The code between the { and } is the body of that function. Returning false will break out of the loop. Returning any other value will continue to the next iteration of the loop.

I've done some work on this code, but not where the actual HTML is generated. If I get some time, I'll see if I can provide some additional details.
Brian Milby

Script Tracker https://github.com/bwmilby/lc-misc/tree/master/ScriptTracker

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1808
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Javascript help required

Post by [-hh] » Mon Oct 29, 2018 4:32 pm

You are interpreting everything correctly:

The logical "!=" ( = LCS is not), "&&" ( = LCS is not) and "||" ( = LCS or).

For example getData() returns an array.
"each" is an array element and
index( = LCS key) and value ( = LCS element) are parameter names.
The function is applied to each array element.

Indexing is zero based.

The last two functions create a html string:
  • + means string concatenation (LCS "&"),
  • left += right means append left to right (put left after right),
  • single quotes are for use within quoted strings and can also surround a string.
Here lists are used. Then "each" is a list element which has a numeric index and a value.
Simply think of items of a string that you split by comma into an numeric array.
shiftLock happens

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 285
Joined: Thu Sep 04, 2008 6:23 am
Location: Melbourne Australia

Re: Javascript help required

Post by jameshale » Tue Oct 30, 2018 6:38 am

Thanks you both (Brian and HH)

I was able to glean enough to track down my error and restore all the links for the "Associations" entries in the make_docset stack. The key was the "Dictionary" the entry was assigned to. With all the additional modules that are (and can be) added to LC now. Of course it also helps if you check on the keys actually used and not assume there will be no abbreviations (cue head scratching while not understanding why the array key "Dictionary" didn't work until realizing it should read "Dict"!)

James

Post Reply

Return to “Extending LiveCode”