Skip to content

Commit

Permalink
MDL-21400 removed js_object_init, the new js_init_call is a new repla…
Browse files Browse the repository at this point in the history
…cement which encourages our new standard coding style
  • Loading branch information
skodak committed Feb 6, 2010
1 parent 7d54028 commit 781bd8a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 106 deletions.
Expand Up @@ -83,7 +83,7 @@ function get_required_javascript() {
global $CFG;
$this->_initialise_dock();
$this->page->requires->js_module(array('name'=>'core_dock', 'fullpath'=>'/blocks/dock.js', 'requires'=>array('base', 'cookie', 'dom', 'io', 'node', 'event-custom')));
$this->page->requires->js_module(array('name'=>'blocks_navigation', 'fullpath'=>'/blocks/global_navigation_tree/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom')));
$this->page->requires->js_module(array('name'=>'block_navigation', 'fullpath'=>'/blocks/global_navigation_tree/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom')));
user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
}

Expand Down Expand Up @@ -155,8 +155,9 @@ function get_content() {
$this->page->navigation->find_expandable($expandable);

// Initialise the JS tree object
$args = array($this->instance->id, array('expansions'=>$expandable,'instance'=>$this->instance->id, 'candock'=>$this->instance_can_be_docked()));
$this->page->requires->js_object_init("M.block_navigation.treecollection[".$this->instance->id."]", 'M.block_navigation.classes.tree', $args, array('blocks_navigation'));
$module = array('name'=>'block_navigation', 'fullpath'=>'/blocks/global_navigation_tree/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom'));
$arguments = array($this->instance->id, array('expansions'=>$expandable, 'instance'=>$this->instance->id, 'candock'=>$this->instance_can_be_docked()));
$this->page->requires->js_init_call('M.block_navigation.init_add_tree', $arguments, false, $module);

// Grab the items to display
$this->content->items = array($this->page->navigation);
Expand Down
10 changes: 8 additions & 2 deletions blocks/global_navigation_tree/navigation.js
Expand Up @@ -28,7 +28,7 @@
* global navigation and settings.
* @namespace
*/
M.block_navigation = {
M.block_navigation = M.block_navigation || {
/** The number of expandable branches in existence */
expandablebranchcount:0,
/** An array of initialised trees */
Expand All @@ -50,6 +50,12 @@ M.block_navigation = {
// Give the tree class the dock block properties
Y.augment(M.block_navigation.classes.tree, M.core_dock.genericblock);
}
},
/**
* Add new instance of navigation tree to tree collection
*/
init_add_tree:function(Y, id, properties) {
M.block_navigation.treecollection[id] = new M.block_navigation.classes.tree(Y, id, properties);
}
};

Expand Down Expand Up @@ -341,4 +347,4 @@ M.block_navigation.classes.branch.prototype.inject_into_dom = function(element)
* NOTE: Never convert the second argument to a function reference...
* doing so causes scoping issues
*/
YUI.add('blocks_navigation', function(Y){M.block_navigation.init(Y);}, '0.0.0.1', M.yui.loader.modules.blocks_navigation.requires);
YUI.add('block_navigation', function(Y){M.block_navigation.init(Y);}, '0.0.0.1', M.yui.loader.modules.block_navigation.requires);
Expand Up @@ -80,10 +80,9 @@ function instance_allow_config() {
function get_required_javascript() {
global $CFG;
$this->_initialise_dock();
$this->page->requires->js_module(array('name'=>'core_dock', 'fullpath'=>'/blocks/dock.js', 'requires'=>array('base', 'cookie', 'dom', 'io', 'node', 'event-custom')));
$this->page->requires->js_module(array('name'=>'blocks_navigation', 'fullpath'=>'/blocks/global_navigation_tree/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom')));
$module = array('name'=>'block_navigation', 'fullpath'=>'/blocks/global_navigation_tree/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom'));
$arguments = array($this->instance->id, array('instance'=>$this->instance->id, 'candock'=>$this->instance_can_be_docked()));
$this->page->requires->js_object_init("M.block_navigation.treecollection[".$this->instance->id."]", 'M.block_navigation.classes.tree', $arguments, array('blocks_navigation'));
$this->page->requires->js_init_call('M.block_navigation.init_add_tree', $arguments, false, $module);
user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
}

Expand Down
101 changes: 3 additions & 98 deletions lib/ajax/ajaxlib.php
Expand Up @@ -149,6 +149,7 @@ public function __construct() {
$this->M_yui_loader->modules = array();
$this->add_yui2_modules(); // adds loading info for YUI2
$this->js_module($this->find_module('core_filepicker'));
$this->js_module($this->find_module('core_dock'));

// YUI3 init code
$libs = array('cssreset', 'cssbase', 'cssfonts', 'cssgrids', 'node', 'loader'); // full CSS reset + basic libs
Expand Down Expand Up @@ -352,6 +353,8 @@ protected function find_module($name) {
$module = array('name'=>'core_message', 'fullpath'=>'/message/module.js');
} else if ($name === 'core_flashdetect') {
$module = array('name'=>'core_flashdetect', 'fullpath'=>'/lib/flashdetect/flashdetect.js', 'requires'=>array('io'));
} else if ($name === 'core_dock') {
$module = array('name'=>'core_dock', 'fullpath'=>'/blocks/dock.js', 'requires'=>array('base', 'cookie', 'dom', 'io', 'node', 'event-custom'));
}
} else {
if ($dir = get_component_directory($name, false)) {
Expand Down Expand Up @@ -547,21 +550,6 @@ public function js_init_code($jscode, $ondomready = false, array $module = null)
$this->jsinitcode[] = $jscode;
}

/**
* Adds a required JavaScript object initialisation to the page.
*
* @param string|null $var If null the object is not assigned to any variable
* @param string $class
* @param array $arguments
* @param array $requirements
* @return required_js_object_init
*/
public function js_object_init($var, $class, array $arguments = null, array $requirements = null) {
$requirement = new required_js_object_init($this, $var, $class, $arguments, $requirements);
$this->requiredjscode[] = $requirement;
return $requirement;
}

/**
* Make a language string available to JavaScript.
*
Expand Down Expand Up @@ -1137,89 +1125,6 @@ public function in_head() {
}
}

/**
* This class is used to manage an object initialisation in JavaScript.
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class required_js_object_init extends required_js_code {
/**
* The variable to assign the object to or null for none
* @var string|null
*/
protected $var;
/**
* The class of the object to initialise
* @var string
*/
protected $class;
/**
* Arguments to pass in to the constructor
*/
protected $arguments;
/**
* Required YUI modules
*/
protected $requirements;
protected $delay = 0;

/**
* 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_object_init()}.
*
* @param page_requirements_manager $manager the page_requirements_manager we are associated with.
* @param string|null $var
* @param string $class
* @param array|null $arguments
* @param array|null $requirements
*/
public function __construct(page_requirements_manager $manager, $var, $class, array $arguments = null, array $requirements = null) {
parent::__construct($manager);
$this->when = page_requirements_manager::WHEN_IN_YUI;
$this->var = $var;
$this->class = $class;
$this->arguments = $arguments;
$this->requirements = $requirements;
}

/**
* Gets the actual JavaScript code for the required object initialisation
* @return string
*/
public function get_js_code() {
return js_writer::object_init($this->var, $this->class, $this->arguments, $this->requirements, $this->delay);
}

/**
* Indicate that this initalisation should be called in YUI's onDomReady event.
*
* Thisis needed mostly for buggy IE browsers because they have problems
* when JS starts modifying DOM structure before the DOM is ready.
*/
public function on_dom_ready() {
if ($this->is_done() || $this->when < page_requirements_manager::WHEN_IN_YUI) {
return;
}
$this->when = page_requirements_manager::WHEN_ON_DOM_READY;
}

/**
* Indicate that this function should be called a certain number of seconds
* after the page has finished loading. (More exactly, a number of seconds
* after the onDomReady event fires.)
*
* @param integer $seconds the number of seconds delay.
*/
public function after_delay($seconds) {
if ($seconds) {
$this->on_dom_ready();
}
$this->delay = $seconds;
}
}

/**
* This class represents a JavaScript function that must be called from the HTML
Expand Down

0 comments on commit 781bd8a

Please sign in to comment.