Wanikani Open Framework [developer thread]

What was confusing to me was that arrays allowed the assignment and still displayed as an array, when it wasn’t really behaving as one anymore

Ah, thanks. That this is allowed by the array seemed a bit strange, especially as the array methods still work, but I see the sense in this now.

3 Likes

Welcome to weak dynamic type systems, where you can basically do anything you want.

3 Likes

Fixed. There was a second spot in the code that I missed. (It was actually the main spot to fix the problem. The other fix corrects the max-height when you click a tab).

I glanced at this again (or tried to…). Unfortunately, it seems this one is an issue with jquery UI. Wkof doesn’t handle the dragging :slightly_frowning_face:
Would be interesting to see if it’s fixed in newer versions…

4 Likes

Yep. It is fixed on my end. Many thanks for the quick response.

1 Like

@rfindley is there a documentation somewhere on how to write a filter for the framework? Or do I have to look at some other script to see how it is done?

There are plenty of examples in my script if there’s no documentation. It’s pretty straightforward.

1 Like

In its simplest form you only need to provide a function

wkof.ItemData.registry.sources.wk_items.filters.<filter name> = {
    filter_func: function(filter_value, item) {
        // return bool
    },
};

filter value is the value that you or the user pass to the filter
item is a singular item that the filter has to either pass or reject

1 Like

And the return value is ‘true’ if the item should be kept, or ‘false’ if it should be removed.

[edit: Forgot to mention… No, the filter interface is undocumented.]

2 Likes

Thank you @rfindley @Kumirei @seanblue

I also have to figure how to handle the registry and the settings interface but I have found it in @seanblue 's code and in the Self Study Quiz code. I am good with this.

3 Likes

@rfindley I want to implement some datetime filters. The problem is datetime is not supported as a native data type by WKOF. The implications:

  • Datetime are stored in the settings as text strings and not binary count of milliseconds.
  • Datetime format must be validated by the validate callback, but the user interface generation for filters in Self Study Quiz does not support these callbacks.
  • The built-in regex validation is not an option for the same reason: no support in Self Study Quiz.
  • It follows that the filter will be provided its datetime parameter in the form of an unvalidated text string.
  • The filter will have to parse the datetime at every invocation with no sensible mean to report datetime format errors. The best it can do is to log something at the console. Given how many times a filter is called this will cause a lot of console spam.

I don’t think I can come up with a sensible filter with these limitations. Anyone sees a way around it? Am I missing something?

I have looked at the button and html options provided by WKOF. Self Study Quiz does not support html and there is no value attached to a button. I don’t see how to turn these options into a filter.

Sorry, I can’t do an in-depth look at the technical details at the moment, but:

Have you looked at the ‘filter_value_map’ parameter of the filter? It lets you add a function to pre-parse the filter value to another format before iteratively passing the new format to the filter function. Example:

filter_value_map: function(datestring) {
  return datestring.get_time();
}

Also, in your copy of Self-Study, try adding support for validation and let me know if you get it working, and I’ll merge it. Example:

case 'text':
case 'number':
case 'input':
  flt_content[src_name+'_flt_'+flt_name] = {
    type:flt.type,
    label:flt.label,
    validate: flt.validate,   //Add validation func from filter
    placeholder:flt.placeholder,
    default:flt.default,
    path:'@ipresets[@active_ipreset].content["'+src_name+'"].filters["'+flt_name+'"].value',
    hover_tip:flt.hover_tip
  }
  break;
1 Like

Thanks for the advice. I will look into this.

For the validate parameter there is one issue I can foresee. I need to make a function declared in my script available in the name space of Self Study Quiz. I risk getting some reference error or something. What is a good procedure for making a function available in both namespaces?

I didn’t know there is a ‘filter_value_map’ parameter. It is not used in @seanblue 's code I used as a reference. The lack of documentation hurt here.

Can you provide a complete list of available filter parameters?

I don’t know if this is good practice or not, but what I have been doing is exposing my functions as window.<script name>.<function>

1 Like

Thanks. If nobody comes up with something better I will use this.

It’s not a problem. When you set filter_value_map, the actual function is being passed (via internal js pointer), so there is no namespace to be concerned about. Even anonymous functions are fine.

Sorry about the documentation. The lack of docs is due to the filters essentially being an extension of Self-Study Quiz rather than a feature of wkof itself. For a long time, only @seanblue and I were using them, and we added features as the need for them became apparent.

The list of parameters actually used by wkof are:

  • type - Field type (“text”, “number”, “dropdown”, etc)
  • label - Appears next to the field in the dialog
  • placeholder - Value of ‘placeholder’ attribute on the field.
  • default - Default field value
  • filter_value_map - Function that receives the filter value set by the user, and returns a transformed value to be passed to the filter_func function.
  • filter_func - Function that is passed each WK item and a filter value, and returns true if item is to be kept (i.e. not filtered out).
  • hover_tip - Text that appears if you hover over the field.

Technically, you can add any parameter to a filter, and it’s up to the scripts that use filters to know what to do with them.

2 Likes

Thanks a lot. You are a great help.

Now there are three of us. Item Inspector is replicating Self Study code to use filters to build its tables. Putting filters in the framework even though Self Study was the lone application was a great foresight. :+1:

I have a need to run filters over a set array of items instead of fetching them from the framework. What is a good way to go about this? Is there something I can call? Or do I need to iterate over the registry and manually call the filters? Can you point me to some sample code?

The only thing wkof.ItemData.get_items() does is pull the full items list from cache and iterate over the specified filters. So, if you want to do a subset of items, just create a filter that extracts the subset from the full set.

1 Like

I made some filters (JLPT, Joyo, Frequency, Visually similar, Subject ID) in Feb 2019, so you’re the fourth 295787090669469698

1 Like

Thanks for pointing this out. I will look at how to integrate these filters into Item Inspector.

Are JLPT, Joyo and Frequency the same filters than those I see in Self Study by default? I think they are in the default registry because I see them in Item Inspector too.

Visually Similar is interesting. I now only display the Wanikani visually similar available from the API. I should use this github page information somehow, not just for filtering.

I don’t find Subject ID. Where can I get it?

This is another no user interface filter. I don’t want this to show up in the settings. I want to control whether this filter is on or off programmatically depending on whether or not I want a subset.