Skip to content

Commit

Permalink
MDL-21532 ->in_head() now really exceptional (after overrlib removal …
Browse files Browse the repository at this point in the history
…there will be just a few of them)
  • Loading branch information
skodak committed Feb 6, 2010
1 parent 781bd8a commit 60409fe
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 108 deletions.
4 changes: 2 additions & 2 deletions grade/report/grader/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@
$PAGE->requires->yui2_lib('container');
$PAGE->requires->js('/grade/report/grader/functions.js');
$PAGE->requires->js('/grade/report/grader/grader.js');
$PAGE->requires->js('/lib/overlib/overlib.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib.js', true);
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js', true);

if ($report->get_pref('enableajax')) {
$report = new grade_report_grader_ajax($courseid, $gpr, $context, $page, $sortitemid);
Expand Down
4 changes: 2 additions & 2 deletions group/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@
$rs->close();
}

$PAGE->requires->js('/lib/overlib/overlib.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib.js', true);
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js', true);

$PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
$PAGE->navbar->add($strgroups);
Expand Down
103 changes: 19 additions & 84 deletions lib/ajax/ajaxlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* <pre>
* $PAGE->requires->css('/mod/mymod/userstyles.php?id='.$id); // not overriddable via themes!
* $PAGE->requires->js('/mod/mymod/script.js');
* $PAGE->requires->js('/mod/mymod/small_but_urgent.js')->in_head();
* $PAGE->requires->js('/mod/mymod/small_but_urgent.js', true);
* $PAGE->requires->js_function_call('init_mymod', array($data))->on_dom_ready();
* </pre>
*
Expand All @@ -53,13 +53,14 @@ class page_requirements_manager {
const WHEN_IN_YUI = 20;
const WHEN_ON_DOM_READY = 30;

protected $linkedrequirements = array();
protected $requiredjscode = array();

/** List of string available from JS */
protected $stringsforjs = array();
/** List of JS variables to be initialised */
protected $jsinitvariables = array('head'=>array(), 'footer'=>array());
/** Included JS scripts */
protected $jsincludes = array('head'=>array(), 'footer'=>array());
/**
* List of skip links, those are needed for accessibility reasons
* @var array
Expand Down Expand Up @@ -266,15 +267,13 @@ protected function init_requirements_data(moodle_page $page, core_renderer $rend
*
* @param string|moodle_url $url The path to the .js file, relative to $CFG->dirroot / $CFG->wwwroot.
* For example '/mod/mymod/customscripts.js'; use moodle_url for external scripts
* @return required_js The required_js object. This allows you to control when the
* link to the script is output by calling methods like {@link required_js::in_head()}.
* @param bool $inhead initialise in head
* @return void
*/
public function js($url) {
public function js($url, $inhead=false) {
$url = $this->js_fix_url($url);
if (!isset($this->linkedrequirements[$url->out()])) {
$this->linkedrequirements[$url->out(false)] = new required_js($this, $url->out(false));
}
return $this->linkedrequirements[$url->out(false)];
$where = $inhead ? 'head' : 'footer';
$this->jsincludes[$where][$url->out()] = $url;
}

/**
Expand Down Expand Up @@ -667,22 +666,6 @@ protected function get_event_handler_code() {
return $output;
}

/**
* Get the code for the linked resources that need to appear in a particular place.
* @param $when one of the WHEN_... constants.
* @return string the HTML that should be output in that place.
*/
protected function get_linked_resources_code($when) {
$output = '';
foreach ($this->linkedrequirements as $requirement) {
if (!$requirement->is_done() && $requirement->get_when() == $when) {
$output .= $requirement->get_html();
$requirement->mark_done();
}
}
return $output;
}

/**
* Get the inline JavaScript code that need to appear in a particular place.
* @param $when one of the WHEN_... constants.
Expand Down Expand Up @@ -841,8 +824,11 @@ public function get_head_code(moodle_page $page, core_renderer $renderer) {
}

// all the other linked things from HEAD - there should be as few as possible
// because we need to minimise number of http requests,
$output .= $this->get_linked_resources_code(self::WHEN_IN_HEAD);
if ($this->jsincludes['head']) {
foreach ($this->jsincludes['head'] as $url) {
$output .= html_writer::script('', $url);
}
}

// finally all JS that should go directly into head tag
$output .= html_writer::script($this->get_javascript_code(self::WHEN_IN_HEAD));
Expand Down Expand Up @@ -893,8 +879,12 @@ public function get_end_code() {
// add missing YUI2 YUI - to be removed once we convert everything to YUI3!
$output .= $this->get_yui2lib_code();

// now print all the stuff that was added through ->requires
$output .= $this->get_linked_resources_code(self::WHEN_AT_END);
// all the other linked scripts - there should be as few as possible
if ($this->jsincludes['footer']) {
foreach ($this->jsincludes['footer'] as $url) {
$output .= html_writer::script('', $url);
}
}

// add all needed strings
if (!empty($this->stringsforjs)) {
Expand Down Expand Up @@ -1031,61 +1021,6 @@ abstract public function get_html();
}


/**
* A subclass of {@link linked_requirement} to represent a requried JavaScript file.
*
* You should not create instances of this class directly. Instead you should
* work with a {@link page_requirements_manager} - and probably the only
* page_requirements_manager you will ever need is the one at $PAGE->requires.
*
* The methods {@link in_head()}
* are indented to be used as a fluid API, so you can say things like
* $PAGE->requires->js('/mod/mymod/script.js')->in_head();
*
* However, by default JavaScript files are included at the end of the HTML.
* This is recommended practice because it means that the web browser will only
* start loading the javascript files after the rest of the page is loaded, and
* that gives the best performance for users.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class required_js extends linked_requirement {
/**
* Constructor. Normally instances of this class should not be created
* directly. Client code should create them via the page_requirements_manager
* method {@link page_requirements_manager::js()}.
*
* @param page_requirements_manager $manager the page_requirements_manager we are associated with.
* @param string $url The URL of the JavaScript file we are linking to.
*/
public function __construct(page_requirements_manager $manager, $url) {
parent::__construct($manager, $url);
$this->when = page_requirements_manager::WHEN_AT_END;
}

public function get_html() {
return html_writer::script('', $this->url);
}

/**
* Indicate that the link to this JavaScript file should be output in the
* <head> section of the HTML. If it too late for this request to be
* satisfied, an exception is thrown.
*/
public function in_head() {
if ($this->is_done() || $this->when <= page_requirements_manager::WHEN_IN_HEAD) {
return;
}
if ($this->manager->is_head_done()) {
throw new coding_exception('Too late to ask for a JavaScript file to be linked to from &lt;head>.');
}
$this->when = page_requirements_manager::WHEN_IN_HEAD;
}
}


/**
* This is the base class for requirements that are JavaScript code.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function standard_head_html() {

// Get the theme javascript head and footer
$jsurl = $this->page->theme->javascript_url();
$this->page->requires->js($jsurl)->in_head();
$this->page->requires->js($jsurl, true);
$jsurl = $this->page->theme->javascript_url(true);
$this->page->requires->js($jsurl);

Expand Down
2 changes: 1 addition & 1 deletion mod/chat/gui_header_js/chatinput.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

//Setup course, lang and theme
$PAGE->set_course($course);
$PAGE->requires->js('/mod/chat/gui_header_js/chat_gui_header.js')->in_head();
$PAGE->requires->js('/mod/chat/gui_header_js/chat_gui_header.js', true);
$PAGE->set_pagelayout('embedded');
$PAGE->set_focuscontrol('input_chat_message');
$PAGE->set_cacheable(false);
Expand Down
2 changes: 1 addition & 1 deletion mod/chat/gui_sockets/chatinput.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

//Setup course, lang and theme
$PAGE->set_course($DB->get_record('course', array('id' => $chatuser->course)));
$PAGE->requires->js('/mod/chat/gui_sockets/chat_gui_sockets.js')->in_head();
$PAGE->requires->js('/mod/chat/gui_sockets/chat_gui_sockets.js', true);
$PAGE->requires->js_function_call('setfocus');
$PAGE->set_focuscontrol('chat_message');
$PAGE->set_cacheable(false);
Expand Down
2 changes: 1 addition & 1 deletion mod/data/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
$PAGE->requires->css('/mod/data/css.php?d='.$data->id);
}
if ($data->jstemplate) {
$PAGE->requires->js('/mod/data/js.php?d='.$data->id)->in_head();
$PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
}


Expand Down
2 changes: 1 addition & 1 deletion mod/data/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
$PAGE->requires->css('/mod/data/css.php?d='.$data->id);
}
if ($data->jstemplate) {
$PAGE->requires->js('/mod/data/js.php?d='.$data->id)->in_head();
$PAGE->requires->js('/mod/data/js.php?d='.$data->id, true);
}

/// Print the page header
Expand Down
4 changes: 2 additions & 2 deletions mod/hotpot/review.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
$strmodulename = get_string("modulename", "hotpot");
// print header

$PAGE->requires->js('/lib/overlib/overlib.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib.js', true);
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js', true);
$PAGE->set_title(format_string($course->shortname) . ": $hotpot->name");
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
Expand Down
2 changes: 1 addition & 1 deletion mod/imscp/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
$PAGE->requires->yui2_lib('container');
$PAGE->requires->yui2_lib('dragdrop');
$PAGE->requires->yui2_lib('resize');
$PAGE->requires->js('/mod/imscp/functions.js')->in_head();
$PAGE->requires->js('/mod/imscp/functions.js', true);

$PAGE->requires->string_for_js('navigation', 'imscp');
$PAGE->requires->string_for_js('toc', 'imscp');
Expand Down
4 changes: 2 additions & 2 deletions mod/quiz/attempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
$attemptobj->load_question_states($questionids);

/// Print the quiz page ////////////////////////////////////////////////////////
$PAGE->requires->js('/lib/overlib/overlib.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib.js', true);
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js', true);

// Arrange for the navigation to be displayed.
$navbc = $attemptobj->get_navigation_panel('quiz_attempt_nav_panel', $page);
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/attemptlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ public function get_html_head_contributions($page = 'all') {
// as seen as possible, particularly if the page is loading slowly.
$PAGE->requires->yui2_lib('dom');
$PAGE->requires->yui2_lib('event');
$PAGE->requires->js('/mod/quiz/quiz.js')->in_head();
$PAGE->requires->js('/mod/quiz/quiz.js', true);
get_html_head_contributions($this->get_question_ids($page), $this->questions, $this->states);
}

Expand Down
4 changes: 2 additions & 2 deletions mod/quiz/review.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
$firstregion = reset($PAGE->blocks->get_regions());
$PAGE->blocks->add_pretend_block($navbc, $firstregion);

$PAGE->requires->js('/lib/overlib/overlib.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib.js', true);
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js', true);

/// Print the page header
$headtags = $attemptobj->get_html_head_contributions($page);
Expand Down
4 changes: 2 additions & 2 deletions mod/quiz/reviewquestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
($stateid ? '&state=' . $stateid : ''),
$attemptobj->get_quizid(), $attemptobj->get_cmid());

$PAGE->requires->js('/lib/overlib/overlib.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js')->in_head();
$PAGE->requires->js('/lib/overlib/overlib.js', true);
$PAGE->requires->js('/lib/overlib/overlib_cssstyle.js', true);

/// Print the page header
$attemptobj->get_question_html_head_contributions($questionid);
Expand Down
8 changes: 4 additions & 4 deletions mod/scorm/player.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@
$PAGE->set_button($exitlink);

$PAGE->requires->data_for_js('scormplayerdata', Array('cwidth'=>$scorm->width,'cheight'=>$scorm->height), true);
$PAGE->requires->js('/mod/scorm/request.js')->in_head();
$PAGE->requires->js('/lib/cookies.js')->in_head();
$PAGE->requires->js('/mod/scorm/loaddatamodel.php?id='.$cm->id.$scoidstr.$modestr.$attemptstr)->in_head();
$PAGE->requires->js('/mod/scorm/rd.js')->in_head();
$PAGE->requires->js('/mod/scorm/request.js', true);
$PAGE->requires->js('/lib/cookies.js', true);
$PAGE->requires->js('/mod/scorm/loaddatamodel.php?id='.$cm->id.$scoidstr.$modestr.$attemptstr, true);
$PAGE->requires->js('/mod/scorm/rd.js', true);

echo $OUTPUT->header();

Expand Down
2 changes: 1 addition & 1 deletion mod/url/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ function url_display_embed($url, $cm, $course) {
// anything else - just try object tag enlarged as much as possible
$code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype);
$PAGE->requires->yui2_lib('dom');
$PAGE->requires->js('/mod/url/functions.js')->in_head();
$PAGE->requires->js('/mod/url/functions.js', true);
$PAGE->requires->js_function_call('url_init_object');
}

Expand Down

0 comments on commit 60409fe

Please sign in to comment.