Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[sheetworker wizardry] getSectionIDsOrdered() a version of getSectionIDs() that returns the IDs in the order they are displayed.

1490453846
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
In case anybody else wants it.  // a version of getSectionIDs that returns the IDs in the order that they are displayed.  // getSectionIDs returns in the order the repeating section lines were created. The user has the ability to reorder the lines.  // getAttrs( [ "_reporder_repeating_" + section_name] , ... ) returns an empty object if the lines were not reordered, and may return an incomplete list. // This returns a complete list in the order that they are displayed.  var getSectionIDsOrdered = function fnGetSectionIDsOrdered( section_name, callback ) {         'use strict'; getAttrs( [ "_reporder_repeating_" + section_name] , function gaGetSectionIDsOrdered(values) { 'use strict'; if( !values["_reporder_repeating_" + section_name] ) getSectionIDs("repeating_" + section_name, callback ); // There is no ordering, so just call getSectionIDs and tell it to run the callback passed to us. else  getSectionIDs("repeating_" + section_name, function gsGetSectionIDsOrdered(idarray) { 'use strict'; var ids = _.union( values["_reporder_repeating_" + section_name].toLowerCase().split( ","), idarray); callback( ids ); }); }); } // end getSectionIDsOrdered()
1490458382

Edited 1490475230
Jakob
Sheet Author
API Scripter
Nice! Nitpicking: this actually returns invalid section ids if the user has deleted any rows since the last reordering. If you want it to work even when some rows have been deleted, you should extend it a bit: var getSectionIDsOrdered = function (sectionName, callback) {   'use strict';   getAttrs([`_reporder_repeating_${sectionName}`], function (values) {     getSectionIDs(`repeating_${sectionName}`, function (idArray) {       let reporder = values[`_reporder_repeating_${sectionName}`],         reporderArray = reporder ? reporder.toLowerCase().split(',') : [],         ids = [...new Set(reporderArray.filter(x => idArray.has(x)).concat(idArray))];       callback(ids);     });   }); }; Edit: ES6 version instead of Underscore.
1490516020

Edited 1490516148
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
Ah, thanks. I did not realize that _reporder_repeating_  was not updated when rows were deleted.  Thanks. Your code had a bug in it and will not run as written.   idArray is an array, not a set, so has no ".has()" method, so it would not parse. Changing .has(x) to idArray.includes(x) fixed that.  It took me a while to figure that out, since your version had quite a lot of things that I had quite frankly never even seen before. Template literals, spread syntax, sets, arrow function prototypes, etc. So figuring it out was quite a learning experience for me.  Also another mistake that I had made was that I had accidentally changed the input the routine is expecting. I want it to accept and return the exact same types of parameters as getSectionIDs(), which means it should be passed "_repeating_" as part of sectionName.  So anyway, below is my latest version, It parses, it works in all known cases, it has been tightened up a lot, and can be substituted for getSectionIDs() by simply adding the word "Ordered" to the call name with no other modifications to surounding code.  // a version of getSectionIDs that returns the IDs in the order that they are displayed.  // getSectionIDs returns in the order the repeating section lines were created. The user has the ability to reorder the lines.  // getAttrs( [ "_reporder_repeating_" + section_name] , ... ) returns an empty object if the lines were not reordered,  // and may return an incomplete list or a list with items that were since deleted. // This returns a complete and accurate list in the order that they are displayed.  var getSectionIDsOrdered = function (sectionName, callback) { 'use strict'; getAttrs([`_reporder_${sectionName}`], function (values) { let reporder = values[`_reporder_${sectionName}`]; if( !reporder ) getSectionIDs( sectionName, callback ); // There is no ordering, so just call the standard getSectionIDs and tell it to run the callback passed to us. else getSectionIDs( sectionName, function (idArray) { callback([...new Set(reporder.toLowerCase().split(",").filter(x => idArray.includes(x)).concat(idArray))]); }); }); }; // end getSectionIDsOrdered()
1490518518

Edited 1490518731
Jakob
Sheet Author
API Scripter
I hope my buggy code and usage of ES6 features could help you discover something new! Template literals and arrow functions are very nice, I basically want to use them all the time. This is the first time I used sets for anything myself. At this point, this should probably go in the wiki so it doesn't get lost. :)
1490529820
Chris D.
Pro
Sheet Author
API Scripter
Compendium Curator
I don't know where in the wiki it would go. I don't think it is a major enough snippet to warrant it's own page.  I do like posting things like this here though, with the hope that anybody who needs it might find it with google.