Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-29224 navigation: Navigation now respects a modules onclick event…

… when preparing navigation nodes
  • Loading branch information...
commit dee1a0fd7ae035402e8cf79d3712c0c8f5905e19 1 parent 228d24f
@samhemelryk samhemelryk authored
Showing with 31 additions and 1 deletion.
  1. +31 −1 lib/navigationlib.php
View
32 lib/navigationlib.php
@@ -1608,6 +1608,7 @@ protected function generate_sections_and_activities(stdClass $course) {
$activity->hidden = (!$cm->visible);
$activity->modname = $cm->modname;
$activity->nodetype = navigation_node::NODETYPE_LEAF;
+ $activity->onclick = $cm->get_on_click();
$url = $cm->get_url();
if (!$url) {
$activity->url = null;
@@ -1706,6 +1707,8 @@ public function load_generic_course_sections(stdClass $course, navigation_node $
* @return array Array of activity nodes
*/
protected function load_section_activities(navigation_node $sectionnode, $sectionnumber, $activities) {
+ // A static counter for JS function naming
+ static $legacyonclickcounter = 0;
if ($activities instanceof course_modinfo) {
debugging('global_navigation::load_section_activities argument 3 should now recieve an array of activites. See that method for an example.', DEBUG_DEVELOPER);
@@ -1722,7 +1725,34 @@ protected function load_section_activities(navigation_node $sectionnode, $sectio
} else {
$icon = new pix_icon('icon', get_string('modulename', $activity->modname), $activity->modname);
}
- $activitynode = $sectionnode->add(format_string($activity->name), $activity->url, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon);
+
+ // Prepare the default name and url for the node
+ $activityname = format_string($activity->name, true, array('context' => get_context_instance(CONTEXT_MODULE, $activity->id)));
+ $action = new moodle_url($activity->url);
+
+ // Check if the onclick property is set (puke!)
+ if (!empty($activity->onclick)) {
+ // Increment the counter so that we have a unique number.
+ $legacyonclickcounter++;
+ // Generate the function name we will use
+ $functionname = 'legacy_activity_onclick_handler_'.$legacyonclickcounter;
+ $propogrationhandler = '';
+ // Check if we need to cancel propogation. Remember inline onclick
+ // events would return false if they wanted to prevent propogation and the
+ // default action.
+ if (strpos($activity->onclick, 'return false')) {
+ $propogrationhandler = 'e.halt();';
+ }
+ // Decode the onclick - it has already been encoded for display (puke)
+ $onclick = htmlspecialchars_decode($activity->onclick);
+ // Build the JS function the click event will call
+ $jscode = "function {$functionname}(e) { $propogrationhandler $onclick }";
+ $this->page->requires->js_init_code($jscode);
+ // Override the default url with the new action link
+ $action = new action_link($action, $activityname, new component_action('click', $functionname));
+ }
+
+ $activitynode = $sectionnode->add($activityname, $action, navigation_node::TYPE_ACTIVITY, null, $activity->id, $icon);
$activitynode->title(get_string('modulename', $activity->modname));
$activitynode->hidden = $activity->hidden;
$activitynode->display = $activity->display;
Please sign in to comment.
Something went wrong with that request. Please try again.