MDL-21240 adding new level to requirements manager, now the function calls and events have access to "Y."

This commit is contained in:
Petr Skoda 2010-01-19 22:01:12 +00:00
parent 8f29813160
commit d96d8f03b4
2 changed files with 28 additions and 26 deletions

View file

@ -51,6 +51,7 @@ class page_requirements_manager {
const WHEN_IN_HEAD = 0; const WHEN_IN_HEAD = 0;
const WHEN_TOP_OF_BODY = 10; const WHEN_TOP_OF_BODY = 10;
const WHEN_AT_END = 20; const WHEN_AT_END = 20;
const WHEN_IN_YUI = 25;
const WHEN_ON_DOM_READY = 30; const WHEN_ON_DOM_READY = 30;
protected $linkedrequirements = array(); protected $linkedrequirements = array();
@ -554,16 +555,15 @@ class page_requirements_manager {
/** /**
* Creates a YUI event handler. * Creates a YUI event handler.
* *
* @param string $id The id of the DOM element that will be listening for the event * @param mixed $selector standard YUI selector for elemnts, may be array or string, element id is in the form "#idvalue"
* @param string $event A valid DOM event (click, mousedown, change etc.) * @param string $event A valid DOM event (click, mousedown, change etc.)
* @param string $function The name of the function to call * @param string $function The name of the function to call
* @param array $arguments An optional array of argument parameters to pass to the function * @param array $arguments An optional array of argument parameters to pass to the function
* @return required_event_handler The event_handler object * @return required_event_handler The event_handler object
*/ */
public function event_handler($id, $event, $function, $arguments=array()) { public function event_handler($selector, $event, $function, array $arguments = null) {
$requirement = new required_event_handler($this, $id, $event, $function, $arguments); $requirement = new required_event_handler($this, $selector, $event, $function, $arguments);
$this->requiredjscode[] = $requirement; $this->requiredjscode[] = $requirement;
$this->yui2_lib('event');
return $requirement; return $requirement;
} }
@ -753,18 +753,18 @@ class page_requirements_manager {
$js = $this->get_javascript_code(self::WHEN_AT_END); $js = $this->get_javascript_code(self::WHEN_AT_END);
$inyuijs = $this->get_javascript_code(self::WHEN_IN_YUI, ' ');
$ondomreadyjs = $this->get_javascript_code(self::WHEN_ON_DOM_READY, ' '); $ondomreadyjs = $this->get_javascript_code(self::WHEN_ON_DOM_READY, ' ');
//TODO: do we really need the global "Y" defined in javasecript-static.js? //TODO: do we really need the global "Y" defined in javasecript-static.js?
// The problem is that we can not rely on it to be fully initialised // The problem is that we can not rely on it to be fully initialised
$js .= <<<EOD $js .= <<<EOD
Y = YUI(yui3loader).use('node-base', function(Y) {
Y = YUI(yui3loader).use('node-base', function(Y) { $inyuijs ;
Y.on('domready', function() { Y.on('domready', function() {
$ondomreadyjs $ondomreadyjs
});
// TODO: call user js functions from here so that they have the proper initialised Y
}); });
});
EOD; EOD;
$output .= html_writer::script($js); $output .= html_writer::script($js);
@ -1118,8 +1118,8 @@ abstract class required_js_code extends requirement_base {
/** /**
* This class represents a JavaScript function that must be called from the HTML * This class represents a JavaScript function that must be called from the HTML
* page. By default the call will be made at the end of the page, but you can * page. By default the call will be made at the end of the page when YUI initialised,
* chage that using the {@link asap()}, {@link in_head()}, etc. methods. * but you can chage that using the {@link asap()}, {@link in_head()}, etc. methods.
* *
* @copyright 2009 Tim Hunt * @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@ -1137,13 +1137,14 @@ class required_js_function_call extends required_js_code {
* *
* @param page_requirements_manager $manager the page_requirements_manager we are associated with. * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
* @param string $function the name of the JavaScritp function to call. * @param string $function the name of the JavaScritp function to call.
* Can be a compound name like 'YAHOO.util.Event.addListener'. * Can be a compound name like 'Y.on'. Do not use old YUI2 YAHOO. function names.
* @param array $arguments and array of arguments to be passed to the function. * @param array $arguments and array of arguments to be passed to the function.
* When generating the function call, this will be escaped using json_encode, * When generating the function call, this will be escaped using json_encode,
* so passing objects and arrays should work. * so passing objects and arrays should work.
*/ */
public function __construct(page_requirements_manager $manager, $function, $arguments) { public function __construct(page_requirements_manager $manager, $function, $arguments) {
parent::__construct($manager); parent::__construct($manager);
$this->when = page_requirements_manager::WHEN_IN_YUI;
$this->function = $function; $this->function = $function;
$this->arguments = $arguments; $this->arguments = $arguments;
} }
@ -1163,14 +1164,13 @@ class required_js_function_call extends required_js_code {
/** /**
* Indicate that this function should be called in YUI's onDomReady event. * Indicate that this function should be called in YUI's onDomReady event.
* *
* Not that this is probably not necessary most of the time. Just having the * Thisis needed mostly for buggy IE browsers because they have problems
* function call at the end of the HTML should normally be sufficient. * when JS starts modifying DOM structure before the DOM is ready.
*/ */
public function on_dom_ready() { public function on_dom_ready() {
if ($this->is_done() || $this->when < page_requirements_manager::WHEN_AT_END) { if ($this->is_done() || $this->when < page_requirements_manager::WHEN_IN_YUI) {
return; return;
} }
$this->manager->yui2_lib('event');
$this->when = page_requirements_manager::WHEN_ON_DOM_READY; $this->when = page_requirements_manager::WHEN_ON_DOM_READY;
} }
@ -1244,7 +1244,7 @@ class required_data_for_js extends required_js_code {
* @since Moodle 2.0 * @since Moodle 2.0
*/ */
class required_event_handler extends required_js_code { class required_event_handler extends required_js_code {
protected $id; protected $selector;
protected $event; protected $event;
protected $function; protected $function;
protected $args = array(); protected $args = array();
@ -1255,21 +1255,23 @@ class required_event_handler extends required_js_code {
* method {@link page_requirements_manager::data_for_js()}. * method {@link page_requirements_manager::data_for_js()}.
* *
* @param page_requirements_manager $manager the page_requirements_manager we are associated with. * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
* @param string $id The id of the DOM element that will be listening for the event * @param mixed $selector standard YUI selector for elemnts, may be array or string, element id is in the form "#idvalue"
* @param string $event A valid DOM event (click, mousedown, change etc.) * @param string $event A valid DOM event (click, mousedown, change etc.)
* @param string $function The name of the function to call * @param string $function The name of the function to call
* @param array $arguments An optional array of argument parameters to pass to the function * @param array $arguments An optional array of argument parameters to pass to the function
*/ */
public function __construct(page_requirements_manager $manager, $id, $event, $function, $args=array()) { public function __construct(page_requirements_manager $manager, $selector, $event, $function, array $args = null) {
parent::__construct($manager); parent::__construct($manager);
$this->id = $id; $this->when = page_requirements_manager::WHEN_IN_YUI;
$this->selector = $selector;
$this->event = $event; $this->event = $event;
$this->function = $function; $this->function = $function;
$this->args = $args; $this->args = $args;
} }
public function get_js_code() { public function get_js_code() {
$output = "YAHOO.util.Event.addListener('$this->id', '$this->event', $this->function"; $selector = json_encode($this->selector);
$output = "Y.on('$this->event', $this->function, $selector, null";
if (!empty($this->args)) { if (!empty($this->args)) {
$output .= ', ' . json_encode($this->args); $output .= ', ' . json_encode($this->args);
} }

View file

@ -76,7 +76,7 @@ class renderer_base {
* @return void * @return void
*/ */
public function add_action_handler($id, component_action $action) { public function add_action_handler($id, component_action $action) {
$this->page->requires->event_handler($id, $action->event, $action->jsfunction, $action->jsfunctionargs); $this->page->requires->event_handler("#$id", $action->event, $action->jsfunction, $action->jsfunctionargs);
} }
/** /**
@ -140,7 +140,7 @@ class renderer_base {
if (!empty($actions) && is_array($actions) && $actions[0] instanceof component_action) { if (!empty($actions) && is_array($actions) && $actions[0] instanceof component_action) {
foreach ($actions as $action) { foreach ($actions as $action) {
if (!empty($action->jsfunction)) { if (!empty($action->jsfunction)) {
$this->page->requires->event_handler($component->id, $action->event, $action->jsfunction, $action->jsfunctionargs); $this->page->requires->event_handler("#$component->id", $action->event, $action->jsfunction, $action->jsfunctionargs);
} }
} }
} }