Skip to content

Commit

Permalink
navigation MDL-21366 Converted dock and navigation to YUI3 modules an…
Browse files Browse the repository at this point in the history
…d added some supporting structures to outputcomponents and ajaxlib
  • Loading branch information
Sam Hemelryk committed Jan 21, 2010
1 parent fc9d196 commit 1ce15fd
Show file tree
Hide file tree
Showing 9 changed files with 897 additions and 770 deletions.
700 changes: 700 additions & 0 deletions blocks/dock.js

Large diffs are not rendered by default.

Expand Up @@ -80,8 +80,9 @@ function instance_allow_config() {
}

function get_required_javascript() {
global $CFG;
$this->_initialise_dock();
$this->page->requires->js('/blocks/global_navigation_tree/navigation.js');
$this->page->requires->js_module('blocks_navigation', array('fullpath'=>$CFG->wwwroot.'/blocks/global_navigation_tree/navigation.js', 'requires'=>array('blocks_dock', 'io', 'node', 'dom', 'event-custom')));
user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
}

Expand Down Expand Up @@ -153,8 +154,8 @@ 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_function_call('blocks.navigation.setup_new_tree', $args)->on_dom_ready();
$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.blocks.navigation.treecollection[".$this->instance->id."]", 'M.blocks.navigation.classes.tree', $args, array('blocks_navigation'));

// Grab the items to display
$this->content->items = array($this->page->navigation);
Expand Down
64 changes: 30 additions & 34 deletions blocks/global_navigation_tree/navigation.js
Expand Up @@ -34,7 +34,7 @@ var blocks = blocks || {};
* global navigation and settings.
* @namespace
*/
blocks.navigation = {
M.blocks.navigation = {
/** The number of expandable branches in existence */
expandablebranchcount:0,
/** An array of initialised trees */
Expand All @@ -45,35 +45,36 @@ blocks.navigation = {
*/
classes:{},
/**
* This function gets called when the module is first loaded as required by
* the YUI.add statement at the bottom of the page.
*
* NOTE: This will only be executed ONCE
* @function
* @static
* @param {int} uid The id of the block within the page
* @param {object} properties
*/
setup_new_tree:function(uid, properties) {
Y.use('base','dom','io','node', function() {
properties = properties || {'instance':uid};
blocks.navigation.treecollection[uid] = new blocks.navigation.classes.tree(uid, uid, properties);
});
init:function() {
if (M.blocks.genericblock) {
// Give the tree class the dock block properties
Y.augment(M.blocks.navigation.classes.tree, M.blocks.genericblock);
}
}
};

/**
* @class tree
* @constructor
* @base blocks.dock.abstractblock
* @base M.blocks.dock.abstractblock
* @param {string} id The name of the tree
* @param {int} key The internal id within the tree store
* @param {object} properties Object containing tree properties
*/
blocks.navigation.classes.tree = function(id, key, properties) {
M.blocks.navigation.classes.tree = function(id, properties) {
this.id = id;
this.key = key;
this.type = 'blocks.navigation.classes.tree';
this.key = id;
this.type = 'M.blocks.navigation.classes.tree';
this.errorlog = [];
this.ajaxbranches = 0;
this.expansions = [];
this.instance = null;
this.instance = id;
this.cachedcontentnode = null;
this.cachedfooter = null;
this.position = 'block';
Expand Down Expand Up @@ -102,7 +103,7 @@ blocks.navigation.classes.tree = function(id, key, properties) {
// Attache events to expand by AJAX
for (var i in this.expansions) {
Y.one('#'+this.expansions[i].id).on('ajaxload|click', this.init_load_ajax, this, this.expansions[i]);
blocks.navigation.expandablebranchcount++;
M.blocks.navigation.expandablebranchcount++;
}

if (node.hasClass('block_js_expansion')) {
Expand All @@ -121,7 +122,7 @@ blocks.navigation.classes.tree = function(id, key, properties) {
* @param {event} e The event object
* @param {object} branch A branch to load via ajax
*/
blocks.navigation.classes.tree.prototype.init_load_ajax = function(e, branch) {
M.blocks.navigation.classes.tree.prototype.init_load_ajax = function(e, branch) {
e.stopPropagation();
if (e.target.get('nodeName').toUpperCase() != 'P') {
return true;
Expand Down Expand Up @@ -152,14 +153,14 @@ blocks.navigation.classes.tree.prototype.init_load_ajax = function(e, branch) {
* @param {mixed} args
* @return bool
*/
blocks.navigation.classes.tree.prototype.load_ajax = function(tid, outcome, args) {
M.blocks.navigation.classes.tree.prototype.load_ajax = function(tid, outcome, args) {
// Check the status
if (outcome.status!=0 && outcome.responseXML!=null) {
var branch = outcome.responseXML.documentElement;
if (branch!=null && this.add_branch(branch, args.target.ancestor('LI') ,1)) {
// If we get here everything worked perfectly
if (this.candock) {
blocks.dock.resize();
M.blocks.dock.resize();
}
return true;
}
Expand All @@ -176,10 +177,10 @@ blocks.navigation.classes.tree.prototype.load_ajax = function(tid, outcome, args
* @param {int} depth
* @return bool
*/
blocks.navigation.classes.tree.prototype.add_branch = function(branchxml, target, depth) {
M.blocks.navigation.classes.tree.prototype.add_branch = function(branchxml, target, depth) {

// Make the new branch into an object
var branch = new blocks.navigation.classes.branch(this, branchxml);
var branch = new M.blocks.navigation.classes.branch(this, branchxml);

var childrenul = false;
if (depth === 1) {
Expand All @@ -203,7 +204,7 @@ blocks.navigation.classes.tree.prototype.add_branch = function(branchxml, target
* Toggle a branch as expanded or collapsed
* @param {Event} e
*/
blocks.navigation.classes.tree.prototype.toggleexpansion = function(e) {
M.blocks.navigation.classes.tree.prototype.toggleexpansion = function(e) {
// First check if they managed to click on the li iteslf, then find the closest
// LI ancestor and use that
if (e.target.get('nodeName').toUpperCase() == 'LI') {
Expand All @@ -212,18 +213,18 @@ blocks.navigation.classes.tree.prototype.toggleexpansion = function(e) {
e.target.ancestor('LI').toggleClass('collapsed');
}
if (this.candock) {
blocks.dock.resize();
M.blocks.dock.resize();
}
}

/**
* This class represents a branch for a tree
* @class branch
* @constructor
* @param {blocks.navigation.classes.tree} tree
* @param {M.blocks.navigation.classes.tree} tree
* @param {xmldoc|null} xml
*/
blocks.navigation.classes.branch = function(tree, xml) {
M.blocks.navigation.classes.branch = function(tree, xml) {
this.tree = tree;
this.name = null;
this.title = null;
Expand All @@ -247,7 +248,7 @@ blocks.navigation.classes.branch = function(tree, xml) {
* Constructs a branch from XML
* @param {xmldoc} xml
*/
blocks.navigation.classes.branch.prototype.construct_from_xml = function(xml) {
M.blocks.navigation.classes.branch.prototype.construct_from_xml = function(xml) {
// Get required attributes
this.title = xml.getAttribute('title');
this.classname = xml.getAttribute('class');
Expand All @@ -264,8 +265,8 @@ blocks.navigation.classes.branch.prototype.construct_from_xml = function(xml) {

if (this.id && this.id.match(/^expandable_branch_\d+$/)) {
// Assign a new unique id for this new expandable branch
blocks.navigation.expandablebranchcount++;
this.id = 'expandable_branch_'+blocks.navigation.expandablebranchcount;
M.blocks.navigation.expandablebranchcount++;
this.id = 'expandable_branch_'+M.blocks.navigation.expandablebranchcount;
}

// Retrieve any additional information
Expand All @@ -284,7 +285,7 @@ blocks.navigation.classes.branch.prototype.construct_from_xml = function(xml) {
* Injects a branch into the tree at the given location
* @param {element} element
*/
blocks.navigation.classes.branch.prototype.inject_into_dom = function(element) {
M.blocks.navigation.classes.branch.prototype.inject_into_dom = function(element) {

var branchli = Y.Node.create('<li></li>');
var branchp = Y.Node.create('<p class="tree_item"></p>');
Expand Down Expand Up @@ -338,9 +339,4 @@ blocks.navigation.classes.branch.prototype.inject_into_dom = function(element) {
}
}

YUI(yui3loader).use('event-custom', 'node', function(Y){
if (blocks.genericblock) {
// Give the tree class the dock block properties
Y.augment(blocks.navigation.classes.tree, blocks.genericblock);
}
});
YUI.add('blocks_navigation', M.blocks.navigation.init, '0.0.0.1', yui3loader.modules.blocks_navigation.requires);
12 changes: 6 additions & 6 deletions blocks/moodleblock.class.php
Expand Up @@ -578,7 +578,8 @@ function _load_instance($instance, $page) {
function get_required_javascript() {
$this->_initialise_dock();
if ($this->instance_can_be_docked()) {
$this->page->requires->js_function_call('blocks.setup_generic_block', array($this->instance->id))->on_dom_ready();
$this->page->requires->js('/blocks/dock.js');
$this->page->requires->js_object_init(null, 'M.blocks.genericblock', array($this->instance->id), array('blocks_dock'));
user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
}
}
Expand Down Expand Up @@ -750,11 +751,10 @@ public function instance_can_be_docked() {
}

public function _initialise_dock() {
global $CFG;
if (!self::$dockinitialised) {
$this->page->requires->js_function_call('blocks.dock.init')->on_dom_ready();
$this->page->requires->data_for_js('blocks.dock.strings.addtodock', get_string('addtodock', 'block'));
$this->page->requires->data_for_js('blocks.dock.strings.undockitem', get_string('undockitem', 'block'));
$this->page->requires->data_for_js('blocks.dock.strings.undockall', get_string('undockall', 'block'));
$this->page->requires->js_module('blocks_dock', array('fullpath'=>$CFG->wwwroot.'/blocks/dock.js', 'requires'=>array('base','dom','io','node', 'event-custom')));
$this->page->requires->strings_for_js(array('addtodock','undockitem','undockall'), 'block');
self::$dockinitialised = true;
}
}
Expand Down Expand Up @@ -861,7 +861,7 @@ protected function formatted_contents($output) {
$this->content->items = array();
}
$this->get_content();
$content = $output->tree_block_contents($this->content->items,array('class'=>'block_tree'));
$content = $output->tree_block_contents($this->content->items,array('class'=>'block_tree list'));
if (isset($this->id) && !is_numeric($this->id)) {
$content = $output->box($content, 'block_tree_box', $this->id);
}
Expand Down
Expand Up @@ -78,11 +78,12 @@ function instance_allow_config() {
}

function get_required_javascript() {
global $CFG;
$this->_initialise_dock();
$this->page->requires->js('/blocks/global_navigation_tree/navigation.js');
$args = array($this->instance->id, array('instance'=>$this->instance->id, 'candock'=>$this->instance_can_be_docked()));
$this->page->requires->js_function_call('blocks.navigation.setup_new_tree', $args)->on_dom_ready();
user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
$this->page->requires->js_module('blocks_navigation', array('fullpath'=>$CFG->wwwroot.'/blocks/global_navigation_tree/navigation.js', 'requires'=>array('blocks_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.blocks.navigation.treecollection[".$this->instance->id."]", 'M.blocks.navigation.classes.tree', $arguments, array('blocks_navigation'));
user_preference_allow_ajax_update('M.docked_block_instance_'.$this->instance->id, PARAM_INT);
}

/**
Expand Down

0 comments on commit 1ce15fd

Please sign in to comment.