Skip to content

Commit

Permalink
navigation MDL-25596 Improvements for the navigation blocks JS
Browse files Browse the repository at this point in the history
The biggest change is that the navigation block has been converted to a proper YUI module.
The following are the other changes made at the same time:
 * A loading icon is displayed when a branch is being loaded by AJAX.
 * Fixed a bug where you could trigger multiple AJAX requests by rapidly clicking an unloaded branch.
 * Fixed a bug where empty branches weren't being marked as such after a successful AJAX load.
 * When docked the width of the blocks dock panel is now inspected an increased if required to try avoid horizontal scrolling.
 * Removed the no longer needed inclusion of the YUI2 dom library from the navigation and settings block.
 * Expandable nodes are now passed as JS data allowing the navigation JS to be initialised through block_navigation::get_required_javascript.
 * AJAX is now focused around the branch in question rather than the tree in general.
 * Expansion of branches is now delegated to the tree rather than being an individual event on all branches.
 * Tidied up the code in general removing unneeded-unused parameters.
  • Loading branch information
Sam Hemelryk committed Dec 23, 2010
1 parent f056cb5 commit 48d8d09
Show file tree
Hide file tree
Showing 7 changed files with 479 additions and 410 deletions.
42 changes: 40 additions & 2 deletions blocks/dock.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,43 @@ M.core_dock.getPanel = function() {
}
return;
};
/**
* Increases the width of the panel to avoid horizontal scrolling
* if possible.
*/
dockpanel.correctWidth = function() {
var bd = this.one('.dockeditempanel_bd');

// Width of content
var w = bd.get('clientWidth');
// Scrollable width of content
var s = bd.get('scrollWidth');
// Width of content container with overflow
var ow = this.get('offsetWidth');
// The new width
var nw = w;
// The max width (80% of screen)
var mw = Math.round(this.get('winWidth') * 0.8);

// If the scrollable width is more than the visible width
if (s > w) {
// Content width
// + the difference
// + any rendering difference (borders, padding)
// + 10px to make it look nice.
nw = w + (s-w) + ((ow-w)*2) + 10;
}

// Make sure its not more then the maxwidth
if (nw > mw) {
nw = mw;
}

// Set the new width if its more than the old width.
if (nw > ow) {
this.setStyle('width', nw+'px');
}
}
// Put the dockpanel in the body
parent.append(dockpanel);
// Return it
Expand Down Expand Up @@ -520,7 +557,7 @@ M.core_dock.init_genericblock = function(Y, id) {
if (!this.initialised) {
this.init(Y);
}
new this.genericblock(id).init(Y, Y.one('#inst'+id));
new this.genericblock(id).initialise_block(Y, Y.one('#inst'+id));
};
/**
* Removes the node at the given index and puts it back into conventional page sturcture
Expand Down Expand Up @@ -700,7 +737,7 @@ M.core_dock.genericblock.prototype = {
* @param {YUI.Node} node The node that contains all of the block's content
* @return {M.core_dock.genericblock}
*/
init : function(Y, node) {
initialise_block : function(Y, node) {
M.core_dock.init(Y);

this.Y = Y;
Expand Down Expand Up @@ -939,6 +976,7 @@ M.core_dock.item.prototype = {
panel.setHeader(this.titlestring, this.commands);
panel.setBody(Y.Node.create('<div class="'+this.blockclass+' block_docked"></div>').append(this.contents));
panel.show();
panel.correctWidth();

this.active = true;
// Add active item class first up
Expand Down
17 changes: 8 additions & 9 deletions blocks/navigation/block_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ function instance_can_be_docked() {
function get_required_javascript() {
global $CFG;
user_preference_allow_ajax_update('docked_block_instance_'.$this->instance->id, PARAM_INT);
$this->page->requires->js_module('core_dock');
$limit = 20;
if (!empty($CFG->navcourselimit)) {
$limit = $CFG->navcourselimit;
}
$arguments = array('id'=>$this->instance->id, 'instance'=>$this->instance->id, 'candock'=>$this->instance_can_be_docked(), 'courselimit'=>$limit);
$this->page->requires->yui_module(array('core_dock', 'moodle-block_navigation-navigation'), 'M.block_navigation.init_add_tree', array($arguments));
}

/**
Expand All @@ -113,7 +120,6 @@ function get_content() {
if ($this->contentgenerated === true) {
return $this->content;
}
$this->page->requires->yui2_lib('dom');
// JS for navigation moved to the standard theme, the code will probably have to depend on the actual page structure
// $this->page->requires->js('/lib/javascript-navigation.js');
// Navcount is used to allow us to have multiple trees although I dont' know why
Expand Down Expand Up @@ -171,14 +177,7 @@ function get_content() {
}
}

// Initialise the JS tree object
$module = array('name'=>'block_navigation', 'fullpath'=>'/blocks/navigation/navigation.js', 'requires'=>array('core_dock', 'io', 'node', 'dom', 'event-custom', 'json-parse'), 'strings'=>array(array('viewallcourses','moodle')));
$limit = 20;
if (!empty($CFG->navcourselimit)) {
$limit = $CFG->navcourselimit;
}
$arguments = array($this->instance->id, array('expansions'=>$expandable, 'instance'=>$this->instance->id, 'candock'=>$this->instance_can_be_docked(), 'courselimit'=>$limit));
$this->page->requires->js_init_call('M.block_navigation.init_add_tree', $arguments, false, $module);
$this->page->requires->data_for_js('navtreeexpansions'.$this->instance->id, $expandable);

$options = array();
$options['linkcategories'] = (!empty($this->config->linkcategories) && $this->config->linkcategories == 'yes');
Expand Down
Loading

0 comments on commit 48d8d09

Please sign in to comment.