Using YUI Gallery modules in a course format.

Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Number of replies: 17
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi,

I'm currently making improvements to the Grid course format for accessibility in M2.5 to be back ported to M2.3 and M2.4.  I'm using the YUI Gallery module 'gallery-event-nav-keys' from http://yuilibrary.com/gallery/show/event-nav-keys.  I am incorporating it within the format because I want it to work when the Moodle install does not have access to the CDN.  So, I looked at the 'yui_modules' method but its specification is:

public function yui_module($modules, $function, array $arguments = null, $galleryversion = null, $ondomready = false)

However the module has no 'init()' function and I did not want to add one and change the code, therefore I'm having to use:

$PAGE->requires->js('/course/format/grid/javascript/gridkeys.js'); -> https://github.com/gjb2048/moodle-courseformat_grid/blob/MOODLE_25_CONTRIB_3240/renderer.php#L147

Which then performs a 'use':

YUI().use('moodle-format_grid-galleryeventnavkeys',function(Y) -> https://github.com/gjb2048/moodle-courseformat_grid/blob/MOODLE_25_CONTRIB_3240/javascript/gridkeys.js#L28

And as you can see, I've had to change the name so that it can be found in the folder:

https://github.com/gjb2048/moodle-courseformat_grid/tree/MOODLE_25_CONTRIB_3240/yui/galleryeventnavkeys

Now, I read up about YUI modules and Shifter on the developer docs and initially created the folder:

https://github.com/gjb2048/moodle-courseformat_grid/tree/MOODLE_25_CONTRIB_3240/yui/build/moodle-format_grid-gallery-event-nav-keys

But that did not work, even after adding 'moodle-format_grid' frankenstyle prefix which I thought was required.

Ok smile, what I would like to know please is if I'm doing it correctly or is there a better way?

Thanks,

Gareth

P.S. The documentation pages I read were:

http://docs.moodle.org/dev/Javascript/Shifter and http://docs.moodle.org/dev/JavaScript_guidelines, hence seeing that I needed the frankenstyle prefix.

P.P.S. As this is such a useful module, would it be considered for distribution in core?  It makes the task of capturing key presses on a page a doddle so that 'Enter', 'TAB' and 'Shift-TAB' which I'm told are 'Select', 'Next' and 'Previous' respectfully for accessibility.

Average of ratings: -
In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Gareth,

Right... let's have a look at all of this. One of the things high up on my todo list is to improve the JS documentation that you're referring to here. It's getting better all the time, but it's not quite there yet.

So to start with, since you're backporting to Moodle 2.3, and 2.4, you need to be aware that we only started supporting Shifted Modules in Moodle 2.5. In older versions, you'll have to go for the un-shifted modules. There is a workaround which I'll note below to make it much easier. If you get the JS layout working well in 2.5, then it should just be a case of adding the workaround to support the older versions so that's what I'd recommend doing.

In summary, you're trying to use the event-nav-keys gallery module. I'm not very familiar with that module, but it seems that most of it is already supported by the core event-key module. There is some documentation at http://yuilibrary.com/yui/docs/event/key.html which details how you do it. Additionally, we don't yet properly support gallery modules fully in core though I'm hoping to fix that really soon now (famous last words). See MDL-36334 for some information on that. It is still possible, but again via workaroud.

You don't actually need to include the YUI gallery module from the PHP, you can do so by including the source in your JavaScript - this is really easy with Shiter. I'll mention how to do so in a moment after a quick summary of Shifter.

As you've seen the Shifter docs, you'll see that the directory structure is something like:

1682 moodle/lib/yui:master> tree src/tooltip/
src/tooltip/
├── build.json
├── js
├── tooltip.js
├── meta
    └── tooltip.json

2 directories, 3 files

You then run shifter from your JS module's directory. This shifter run creates the build directory and the three versions of the output:

1681 moodle/lib/yui:master> tree build/moodle-core-tooltip/
build/moodle-core-tooltip/
    ├── moodle-core-tooltip-debug.js
    ├── moodle-core-tooltip.js
    └── moodle-core-tooltip-min.js

0 directories, 3 files

In your case, your directory structure would be:

moodle/course/format/grid/yui/src/gridkeys/
├── build.json
├── js
│   └── gridkeys.js
└── meta
    └── gridkeys.json

You then just open the gridkeys src directory, and run Shifter.

In Moodle 2.5, to use the code, you can simply add one line of PHP:

$PAGE->requires->yui_module('moodle-format_grid-gridkeys', 'M.format_grid.gridkeys.init');

In order to include your gallery module (at least until we get MDL-36334 implemented), you can then add the minified version of the gallery module to the js directory, and in your build.json add:

"prependfiles": [ "js/gallery-event-nav-keys.js" ]

See the Shifter documentation for a more exact example: http://yui.github.io/shifter/#build.json

So once you've got that working for 2.5, you can easily backport it to 2.3/2.4 by creating the older-style directory, and copying the -min.js file into place.

In the tooltip example above, I'd just run something like:

1713 moodle/lib/yui:master> mkdir tooltip
1714 moodle/lib/yui:master> cp build/moodle-core-tooltip/moodle-core-tooltip-min.js tooltip/tooltip.js

(sorry, I don't know what the Windows equivelant would be)

If you want to be fancy, you can add a postexec to the build.json to do this for you each time you run shifter (I'll let you read up on that). You should be able to do so in a cross-platform fashion using node if you really want to. I might write something quickly to make it easier in.

Ping me if you need a hand,

Andrew

In reply to Andrew Lyons

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Dear Andrew,

Thank you so much for your lengthy reply, a lot to take in! smile - I'll have a bash as need to install Shifter as the event key gallery module was from it's output.

Thanks again,

Gareth

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Thankfully, Shifter is fairly easy to install - even on Windows.

You can download the built source of the gallery module from https://github.com/yui/yui3-gallery/tree/master/build/gallery-event-nav-keys

Andrew

In reply to Andrew Lyons

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Andrew,

I've got Shifter installed thanks smile

And retrieved the built source of the gallery module https://github.com/gjb2048/moodle-courseformat_grid/tree/MOODLE_25_CONTRIB_3240/yui/build/moodle-format_grid-gallery-event-nav-keys - so are you saying the original files from https://github.com/yui/yui3-gallery/tree/master/build/gallery-event-nav-keys should moodle/course/format/grid/yui/src/gridkeys/js folder such that Shifter will build them into the gridkey's module?

Ta,

Gareth

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Yup that's right. You need to add it to the list of prependfiles in the build.json file.

Here's a quick script I wrote to backport a YUI module to a version of Moodle not supporting Shifter:

https://gist.github.com/andrewnicols/a34ba183b42ec9155261

Just create a scripts directory in your src directory, and add this to the "postexec" array:

"postexec": [
    "./scripts/backport.js moodle-format_grid-gridkeys"
]

Your complete build.json should look something like:

{
  "name": "moodle-core-tooltip",
  "builds": {
    "moodle-core-tooltip": {
      "prependfiles": [
        "js/gallery-event-nav-keys.js"
      ],
      "jsfiles": [
        "tooltip.js"
      ],
      "postexec": [
        "./scripts/backport.js moodle-core-tooltip"
      ]
    }
  }
}

You may find the shifter --watch option particularly helpful.

Andrew

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

You may also need to put the gallery-event-nav-keys module into a 'use' array instead of 'requires' when defining the module metadata otherwise the loader will try (and fail) to load it.

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Odd, I'm getting a "yui: NOT loaded: M.format_grid.gridkeys yui_combo.php?3.9.1/build/simpleyui/simpleyui.js&3.9.1/build/loader/loader.js:19958" - I'll see if it's where I'm putting the gallery JS.  For M2.3 and M2.4, you mean a 'js' folder like '/course/format/grid/js'?

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

I have:

M.format_grid.gridkeys.init = function(Y) {
Y().use('gallery-event-nav-keys', function(Y) {

....

}, '@VERSION@', {"requires": ["gallery-event-nav-keys"]});

In the build.

A 'use' array?

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

build and src folders now on https://github.com/gjb2048/moodle-courseformat_grid/tree/MOODLE_25_CONTRIB_3240/yui - ignore the other as current backup for older mechanism.

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Nearly,

The prependfiles belongs to the build - it should be on the line beneath the jsfiles array.

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Yup,in https://github.com/gjb2048/moodle-courseformat_grid/blob/MOODLE_25_CONTRIB_3240/yui/src/gridkeys/meta/gridkeys.json change your "requires" to "use". This indicates to the Loader that it needs to attach the module, but that it's included in some other way. Without it, the loader will try and retrieve the file from other sources.

Andrew

In reply to Andrew Lyons

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Humm, now getting 

  1. Uncaught TypeError: Cannot call method 'init' of undefined view.php?id=10:498

Scrub that, just noticed module name error.  Nope! Not it, humm.

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Nothing else?
I'm not on a machine I can test on right now, but you need to complete defining the namespace - https://github.com/gjb2048/moodle-courseformat_grid/blob/MOODLE_25_CONTRIB_3240/yui/src/gridkeys/js/gridkeys.js#L26

M.format_grid.gridkeys = M.format_grid.gridkeys || {};

Should be:

M.format_grid = M.format_grid || {};
M.format_grid.gridkeys = M.format_grid.gridkeys || {};

This may not be the issue, but equally it may. I assume the file is loaded by the loader correctly at least? Andrew

In reply to Andrew Lyons

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Added that thanks, but no joy, same error changes pushed.

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Odd, pulling in gallery-event-nav-keys from CDN and not locally as intended -> http://yui.yahooapis.com/combo?gallery-2013.02.27-21-03/build/gallery-event-nav-keys/gallery-event-nav-keys.js

I'm going to switch back renderer.php on line 147 to the old code as others are testing the functionality.

Thanks Andrew, going to sleep on it!

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Bingo!  Was build.json wrong and did need 'requires' and not 'use' in gridkeys.json.

In reply to Gareth J Barnard

Re: Using YUI Gallery modules in a course format.

by Andrew Lyons -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

That looks like you have the argument to yui_module() incorrect - it's trying to load M.format_grid.gridkeys instead of moodle-format_grid-gridkeys.

 

They don't need to go into a JS folder, but they do need to go into the YUI folder:

/course/format/grid/yui/src/gridkeys - where your code goes

/course/format/grid/yui/gridkeys - 2.3/2.4 backport

/course/format/grid/yui/build - created by Shifter

 

https://github.com/gjb2048/moodle-courseformat_grid/blob/MOODLE_25_CONTRIB_3240/renderer.php#L148 should read:

 
$PAGE->requires->yui_module('moodle-format_grid-gridkeys', 'M.format_grid.gridkeys.init');