JavaScript dialogues - surely it's not that complicated

JavaScript dialogues - surely it's not that complicated

by Tim Hunt -
Number of replies: 8
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I feel that I must be missing something, because I want to do something that seems simple to me, but I can't work out how, and what I have found does not make much sense.

What I want to do is, in my JavaScript code, pop up an 'are you sure' confirmation,

Are you sure you want to delete the link 'Test link'?

(Delete) (Cancel)

What I have found is core/modal_factory, as documented in https://docs.moodle.org/dev/AMD_Modal. But, I can't work out how to make it do what I want. Can anyone help? Thanks.

Average of ratings: -
In reply to Tim Hunt

Re: JavaScript dialogues - surely it's not that complicated

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

The notification AMD module provides a confirm() method for creating modal dialogs with 'Yes / No' buttons (& callbacks for each)

Average of ratings: Useful (1)
In reply to Paul Holden

Re: JavaScript dialogues - surely it's not that complicated

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Yes, that would be easier to use, but it has the comment

// Here we are wrapping YUI. This allows us to start transitioning, but
// wait for a good alternative without having inconsistent dialogues.

So I guess the plan is to deprecate that in due course, and therefore it would be best not to use it.

In reply to Tim Hunt

Re: JavaScript dialogues - surely it's not that complicated

by Damyon Wiese -

It doesn't mean you can't use it, it means that you can use it now, and later we will change the internals to call the AMD modal instead of the yui one and your code won't be affected.


Average of ratings: Useful (1)
In reply to Damyon Wiese

Re: JavaScript dialogues - surely it's not that complicated

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Can I just feed back that that was not obvious to me yesterday.

The rules for JavaScript in Moodle change so regularly, it is not at all obvious where to even look for documentation. Yesterday and integrator linked me to https://docs.moodle.org/dev/AMD_Modal as the 'right' thing to do, so I tried to make that work.

In reply to Tim Hunt

Re: JavaScript dialogues - surely it's not that complicated

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Funnily enough... exactly the same thing caught me out the other day wink

I'm not exactly a hard-core javascript programmer and need all the assurance I can get!!

In reply to Tim Hunt

Re: JavaScript dialogues - surely it's not that complicated

by lior gil -
Picture of Core developers

A bit off subject, the reason is that while YUI comes with a very large library to call upon, JQUERY comes only with its basic tools - the UI library - and nothing more.

For example, I wrote a plugin that used the YUI datatable commponent. On upgrading it to AMD I found no equivalent code to rely on and had to resort to an external solution.

This is a wrong approach because it can lead to similar libraries included in different modules unnecessarily instead of being called from a single place.

The JQUERY library needs to give an answer to every need previously provided by YUI, or else these issues will continue to happen.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: JavaScript dialogues - surely it's not that complicated

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

So, the best I have been able to come up with so far is:

var modal = ModalFactory.create({
type: ModalFactory.types.CONFIRM,
title: M.util.get_string('removeconfirmtitle', 'block_dashboardboxes'),
body: M.util.get_string('removeconfirmmessage', 'block_dashboardboxes', item.find('span').text().trim()),
// footer: '<button type="button" class="btn btn-primary" data-action="yes">' + M.util.get_string('remove') + '</button>' +
// '<button type="button" class="btn btn-secondary" data-action="cancel">' + M.util.get_string('cancel') + '</button>'
}).done(function (modal) {
modal.getRoot().on(ModalEvents.yes, t.deleteItemConfirmed);
modal.show();
}).catch(Notification.exception);
With that code:
  • It is quite slow from clicking the button to the dialogue acuitally appearing, which is presumably due to the 5(!) Ajax calls it takes to show the dialogue.
  • The dialogues does not look anything like any of the other dialogue boxes in the plugin I am making. So, if you want to theme Moodle dialogues, you now have to do it twice, once for old YUI dialogues, and one for new-style ones.
  • If you un-comment the bit that tries to set more meaningful button labels, you get an exception. I don't know what that is necessary.
  • I don't know why the .getRoot() incantation is necessary to add the event handler. I just copied it from some other code using modal_factory why doing the obvious thing did not work.

But, apart from that, it works.

Note, when I say that our old CSS for styling dialogues does not apply here, that CSS was using class names like:

.moodle-dialogue-base .moodle-dialogue-lightbox {
background-color: #464542;
opacity: 0.7;
}
.moodle-dialogue-base .moodle-dialogue {
max-width: 1280px;
}
.moodle-dialogue-base .moodle-dialogue-wrap {
border: 0;
border-radius: 0;
margin-right: 5px;
}
.moodle-dialogue-base .moodle-dialogue-wrap .moodle-dialogue-hd,
Not anything YUI-specific.
In reply to Tim Hunt

Re: JavaScript dialogues - surely it's not that complicated

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It seems the new HTML is mostly equivalent to the old HTML, but with different class names. So, you can mostly adjust your custom CSS by doing changes like

.moodle-dialogue-base .moodle-dialogue-wrap .moodle-dialogue-hd {
to

.modal-container .modal .modal-header,
.moodle-dialogue-base .moodle-dialogue-wrap .moodle-dialogue-hd {
but there are many style rules needed if you want your own dialogue design, so you have to do this sort of transform a lot.

Then you need to tests, and find the things that are different and so need different fixes in the CSS.

Base Moodle dialogues have a lot of CSS, some of which is quite odd, so doing your own design involves a lot of work to reset all that first.