(Just to clarify here: what I say here are my current beliefs about what is best for Moodle. However, I am still learning here.)
Why do we use any JS library in core?
Becuse, you cannot do write-once / run-anywhere with JavaScript (yet). Browser support is sufficiently inconsistent that using a JS library that covers most of the worst inconsistencies is a huge saving for developers.
(In answer something Colin said above, Software execution time is not the only think you want to optimise. One is also interested in optimising Software writing time or developer productivity. If it is quick and easy to write new Moodle features, we will get lost more good features.)
Why do we want to try to limit it to a single JS library in Moodle core?
Well, you can change that to "a single ... Moodle core". Consistency is important to keep the software maintainable.
And we don't have consistency with JavaScript yet. We have really old JS that has never been cleaned up. We have YUI2 code, and we have YUI3 code.
We have had a policy for at least the last 18 months that the direction we would head in to get consistency is to try to move everything to modular YUI3 code. That is still not a bad plan. Where we have done that, it leads to code that is better than what we had before.
However, if there is a better plan we can adopt, then we can change direction yet again. Especially since, as Martin says, there is about to be a lot of UI work.
Some thoughts about where the JS belongs in the Moodle code
Here are some observations based on both making core changes to the quiz module display, and then also trying to make ou-specific customisations to that display for the OU in our theme and renderer:
- The JavaScript has to depend on the HTML that is output (because it has to look for certain DOM nodes and manipulate them).
- Therefore, the JavaScript must be added to the page by the same bit of code that generates the HTML.
- That is, $PAGE->requires->... calls should only appear in renderer code. Nowhere else.
Hopefully, if you only make small changes to the renderer output, you don't need to change the JS, but you need to have the flexibility to do so, if necessary.
If possible, we should structure the standard JS so that if you only want to make a small (incompatible) change to the JS, then you only need to provide a small amount of JavaScript. To give a more specific example, think about the quiz timer. The JavaScritpt therer should be in two parts. One that does the work to compute the time remaining, and the other that updates the DOM to display it, so that if your theme wants to change how the time remaining is displayed, you only have to provide new code to do the update DOM part.
Also, if we move to a situation where we only include JS in renderers, we ought to change the way we do it, so that it becomes $this->output_requires->... in the renderer code.
Some thoughts on the trade-off between themes and core code
Giving themes lots of flexibility is really great, but there is a down-side.
If every theme can do completely its own thing, then we end up with each theme completely re-inventing the Moodle UI. Where we are now is that we cannot even build one Moodle UI that is free from serious flaws. Just making it possible to build lots of different Moodle UIs is not the answer to that!
We want the out-of-the box Moodle experience to be great. Let us invest most of our effort into the standard Moodle UI.
Of course the theming system should stay. Remember that the vision has always been that there would be a 'base' theme that was very plain - a blank slate available for anyone who really wants to build a Moodle UI from scratch.
Then there should be a higher level theme that makes sure most things are laid out OK and nicely functional. We actually have two of those in core at the moment, more or less, the 'standard' and 'canvas' themes.
Finally, we have sub-themes that put nice visual style on top of the basic structure. That is, most of the new Moodle 2.x themes are based on 'canvas'.
I still think that is the right overall vision. The implementation needs to be improved. In particular, we need to ask "How plain should base theme be?" and "How much should a middle-level theme like canvas do?". Where does the code that implements the look and feel of the filepicker live?
Remember that plugin developers need to be able to ship their plugins so that they slot into Moodle with any theme, and look great.