MDL 38508 JavaScript: Split out AJAX and non-AJAX help

We need to keep these two separate as scripts which define AJAX_SCRIPT
before loading config.php will use a different default renderer and will
return appropriate exceptions which can be parsed by M.core.exception and
M.core.ajaxException correctly.

This also addresses an issue whereby a missing heading could break the
tooltip.
This commit is contained in:
Andrew Robert Nicols
2013-04-08 21:01:20 +01:00
committed by Andrew Nicols
parent 422f68fb86
commit 56d465b2d3
8 changed files with 307 additions and 188 deletions
+11 -62
View File
@@ -32,73 +32,22 @@ require_once(dirname(__FILE__) . '/config.php');
$identifier = required_param('identifier', PARAM_STRINGID);
$component = required_param('component', PARAM_COMPONENT);
$lang = required_param('lang', PARAM_LANG); // TODO: maybe split into separate scripts
$ajax = optional_param('ajax', 0, PARAM_BOOL);
$lang = optional_param('lang', 'en', PARAM_LANG);
if (!$lang) {
$lang = 'en';
}
$SESSION->lang = $lang; // does not actually modify session because we do not use cookies here
$sm = get_string_manager();
// We don't actually modify the session here as we have NO_MOODLE_COOKIES set.
$SESSION->lang = $lang;
$PAGE->set_url('/help.php');
$PAGE->set_pagelayout('popup');
$PAGE->set_context(context_system::instance());
if ($ajax) {
@header('Content-Type: text/plain; charset=utf-8');
$data = get_formatted_help_string($identifier, $component, false);
echo $OUTPUT->header();
if (!empty($data->heading)) {
echo $OUTPUT->heading($data->heading, 1, 'helpheading');
}
if (!$sm->string_exists($identifier.'_help', $component)) {
// strings on disk-cache may be dirty - try to rebuild it and check again
$sm->load_component_strings($component, current_language(), true);
}
$data = new stdClass();
if ($sm->string_exists($identifier.'_help', $component)) {
$options = new stdClass();
$options->trusted = false;
$options->noclean = false;
$options->smiley = false;
$options->filter = false;
$options->para = true;
$options->newlines = false;
$options->overflowdiv = !$ajax;
$data->heading = format_string(get_string($identifier, $component));
// Should be simple wiki only MDL-21695
$data->text = format_text(get_string($identifier.'_help', $component), FORMAT_MARKDOWN, $options);
$helplink = $identifier . '_link';
if ($sm->string_exists($helplink, $component)) { // Link to further info in Moodle docs
$link = get_string($helplink, $component);
$linktext = get_string('morehelp');
$data->doclink = new stdClass();
$url = new moodle_url(get_docs_url($link));
$data->doclink->link = $url->out();
$data->doclink->linktext = $linktext;
$data->doclink->class = ($CFG->doctonewwindow) ? 'helplinkpopup' : '';
$completedoclink = html_writer::tag('div', $OUTPUT->doc_link($link, $linktext), array('class' => 'helpdoclink'));
}
} else {
$data->text = html_writer::tag('p',
html_writer::tag('strong', 'TODO') . ": missing help string [{$identifier}_help, {$component}]");
}
if ($ajax) {
echo json_encode($data);
} else {
echo $OUTPUT->header();
if (isset($data->heading)) {
echo $OUTPUT->heading($data->heading, 1, 'helpheading');
}
echo $data->text;
if (isset($completedoclink)) {
echo $completedoclink;
}
echo $OUTPUT->footer();
echo $data->text;
if (isset($data->completedoclink)) {
echo $data->completedoclink;
}
echo $OUTPUT->footer();
+40
View File
@@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Displays help via AJAX call
*
* @copyright 2013 onwards Andrew Nicols
* @package core
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_MOODLE_COOKIES', true);
define('AJAX_SCRIPT', true);
require_once(__DIR__ . '/config.php');
$identifier = required_param('identifier', PARAM_STRINGID);
$component = required_param('component', PARAM_COMPONENT);
$lang = optional_param('lang', 'en', PARAM_LANG);
// We don't actually modify the session here as we have NO_MOODLE_COOKIES set.
$SESSION->lang = $lang;
$PAGE->set_url('/help_ajax.php');
$PAGE->set_context(context_system::instance());
$data = get_formatted_help_string($identifier, $component, true);
echo json_encode($data);
+69
View File
@@ -3463,3 +3463,72 @@ function print_password_policy() {
}
return $message;
}
/**
* Get the value of a help string fully prepared for display in the current language.
*
* @param string $identifier The identifier of the string to search for.
* @param string $component The module the string is associated with.
* @param boolean $ajax Whether this help is called from an AJAX script.
* This is used to influence text formatting and determines
* which format to output the doclink in.
* @return Object An object containing:
* - heading: Any heading that there may be for this help string.
* - text: The wiki-formatted help string.
* - doclink: An object containing a link, the linktext, and any additional
* CSS classes to apply to that link. Only present if $ajax = false.
* - completedoclink: A text representation of the doclink. Only present if $ajax = true.
*/
function get_formatted_help_string($identifier, $component, $ajax = false) {
global $CFG, $OUTPUT;
$sm = get_string_manager();
if (!$sm->string_exists($identifier, $component) ||
!$sm->string_exists($identifier . '_help', $component)) {
// Strings in the on-disk cache may be dirty - try to rebuild it and check again.
$sm->load_component_strings($component, current_language(), true);
}
$data = new stdClass();
if ($sm->string_exists($identifier, $component)) {
$data->heading = format_string(get_string($identifier, $component));
} else {
// Gracefully fall back to an empty string.
$data->heading = '';
}
if ($sm->string_exists($identifier . '_help', $component)) {
$options = new stdClass();
$options->trusted = false;
$options->noclean = false;
$options->smiley = false;
$options->filter = false;
$options->para = true;
$options->newlines = false;
$options->overflowdiv = !$ajax;
// Should be simple wiki only MDL-21695.
$data->text = format_text(get_string($identifier.'_help', $component), FORMAT_MARKDOWN, $options);
$helplink = $identifier . '_link';
if ($sm->string_exists($helplink, $component)) { // Link to further info in Moodle docs
$link = get_string($helplink, $component);
$linktext = get_string('morehelp');
$data->doclink = new stdClass();
$url = new moodle_url(get_docs_url($link));
if ($ajax) {
$data->doclink->link = $url->out();
$data->doclink->linktext = $linktext;
$data->doclink->class = ($CFG->doctonewwindow) ? 'helplinkpopup' : '';
} else {
$data->completedoclink = html_writer::tag('div', $OUTPUT->doc_link($link, $linktext), array('class' => 'helpdoclink'));
}
}
} else {
$data->text = html_writer::tag('p',
html_writer::tag('strong', 'TODO') . ": missing help string [{$identifier}_help, {$component}]");
}
return $data;
}
File diff suppressed because one or more lines are too long
@@ -190,6 +190,19 @@ ATTRS.footerhandler = {
value: null
};
/**
* The function which handles modifying the URL that was clicked on.
*
* The default function rewrites '.php' to '_ajax.php'.
*
* @attribute urlmodifier
* @type Function|String|null
* @default null
*/
ATTRS.urlmodifier = {
value: null
};
/**
* Set the Y.Cache object to use.
*
@@ -247,6 +260,9 @@ Y.extend(TOOLTIP, M.core.dialogue, {
if (!this.get('footerhandler')) {
this.set('footerhandler', function() {});
}
if (!this.get('urlmodifier')) {
this.set('urlmodifier', this.modify_url);
}
// Set up the dialogue with initial content.
this.setAttrs({
@@ -290,7 +306,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
/**
* Display the tooltip for the clicked link.
*
* The anchor for the clicked link is used, additionally appending ajax=1 to the parameters.
* The anchor for the clicked link is used.
*
* @method display_panel
* @param {EventFacade} e The event from the clicked link. This is used to determine the clicked URL.
@@ -332,7 +348,8 @@ Y.extend(TOOLTIP, M.core.dialogue, {
thisevent = this.bb.on('mousedownoutside', this.close_panel, this);
this.listenevents.push(thisevent);
ajaxurl = clickedlink.get('href');
// Modify the URL as required.
ajaxurl = Y.bind(this.get('urlmodifier'), this, clickedlink.get('href'))();
cacheentry = this.get('textcache').retrieve(ajaxurl);
if (cacheentry) {
@@ -344,10 +361,6 @@ Y.extend(TOOLTIP, M.core.dialogue, {
method: 'get',
context: this,
sync: false,
data: {
// We use a slightly different AJAX URL to the one on the anchor to allow non-JS fallback.
ajax: 1
},
on: {
complete: function(tid, response) {
this._set_panel_contents(response.responseText, ajaxurl);
@@ -355,7 +368,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
}
};
Y.io(clickedlink.get('href'), config);
Y.io(ajaxurl, config);
}
},
@@ -371,10 +384,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
}
} catch (error) {
this.close_panel();
return new M.core.exception({
name: error.name,
message: "Unable to retrieve the requested content. The following error was returned: " + error.message
});
return new M.core.exception(error);
}
// Set the contents using various handlers.
@@ -403,6 +413,10 @@ Y.extend(TOOLTIP, M.core.dialogue, {
this.set('bodyContent', bodycontent);
},
modify_url: function(url) {
return url.replace(/\.php\?/, '_ajax.php?');
},
close_panel: function(e) {
// Hide the panel first.
this.hide();
@@ -1 +1 @@
YUI.add("moodle-core-tooltip",function(e,t){function n(e){e||(e={}),typeof e.draggable=="undefined"&&(e.draggable=!0),typeof e.constrain=="undefined"&&(e.constrain=!0),typeof e.lightbox=="undefined"&&(e.lightbox=!1),n.superclass.constructor.apply(this,[e])}var r={CLOSEBUTTON:".closebutton"},i={PANELTEXT:"tooltiptext"},s={WAITICON:{pix:"i/loading_small",component:"moodle"}},o={};n.NAME="moodle-core-tooltip",n.CSS_PREFIX="moodle-dialogue",n.ATTRS=o,o.initialheadertext={value:""},o.initialbodytext={value:"",setter:function(t){var n,r;return n=e.Node.create("<div />").addClass(i.PANELTEXT),r=e.Node.create("<img />").setAttribute("src",M.util.image_url(s.WAITICON.pix,s.WAITICON.component)).addClass("spinner"),t?(n.set("text",t),r.addClass("iconsmall")):n.addClass("content-lightbox"),n.append(r),n}},o.initialfootertext={value:null,setter:function(t){if(t)return e.Node.create("<div />").set("text",t)}},o.headerhandler={value:"set_header_content"},o.bodyhandler={value:"set_body_content"},o.footerhandler={value:null},o.textcache={value:null},o.textcachesize={value:10},e.extend(n,M.core.dialogue,{bb:null,listenevents:[],textcache:null,alignpoints:[e.WidgetPositionAlign.TL,e.WidgetPositionAlign.RC],initializer:function(){return this.get("headerhandler")||this.set("headerhandler",this.set_header_content),this.get("bodyhandler")||this.set("bodyhandler",this.set_body_content),this.get("footerhandler")||this.set("footerhandler",function(){}),this.setAttrs({headerContent:this.get("initialheadertext"),bodyContent:this.get("initialbodytext"),footerContent:this.get("initialfootertext"),zIndex:150}),this.hide(),this.render(),this.bb=this.get("boundingBox"),right_to_left()&&(this.alignpoints=[e.WidgetPositionAlign.TR,e.WidgetPositionAlign.LC]),this.get("textcache")||this.set("textcache",new e.Cache({max:this.get("textcachesize")})),M.cfg.developerdebug&&this.get("textcache").set("max",0),this},display_panel:function(t){var n,i,s,o,u;t.preventDefault(),t.stopPropagation(),this.cancel_events(),n=t.target.ancestor("a",!0),this.align(n,this.alignpoints),this.setAttrs({headerContent:this.get("initialheadertext"),bodyContent:this.get("initialbodytext"),footerContent:this.get("initialfootertext")}),this.show(),i=this.bb.delegate("click",this.close_panel,r.CLOSEBUTTON,this),this.listenevents.push(i),i=e.one("body").on("key",this.close_panel,"esc",this),this.listenevents.push(i),i=this.bb.on("mousedownoutside",this.close_panel,this),this.listenevents.push(i),s=n.get("href"),u=this.get("textcache").retrieve(s),u?this._set_panel_contents(u.response):(o={method:"get",context:this,sync:!1,data:{ajax:1},on:{complete:function(e,t){this._set_panel_contents(t.responseText,s)}}},e.io(n.get("href"),o))},_set_panel_contents:function(t,n){var r;try{r=e.JSON.parse(t);if(r.error)return this.close_panel(),new M.core.ajaxException(r)}catch(i){return this.close_panel(),new M.core.exception({name:i.name,message:"Unable to retrieve the requested content. The following error was returned: "+i.message})}e.bind(this.get("headerhandler"),this,r)(),e.bind(this.get("bodyhandler"),this,r)(),e.bind(this.get("footerhandler"),this,r)(),n&&this.get("textcache").add(n,t),this.get("buttons").header[0].focus()},set_header_content:function(e){this.set("headerContent",e.heading)},set_body_content:function(t){var n=e.Node.create("<div />").set("innerHTML",t.text).setAttribute("role","alert").addClass(i.PANELTEXT);this.set("bodyContent",n)},close_panel:function(e){this.hide(),this.cancel_events(),e&&e.preventDefault()},cancel_events:function(){var e;while(this.listenevents.length)e=this.listenevents.shift(),e.detach()}}),M.core=M.core||{},M.core.tooltip=M.core.tooltip=n},"@VERSION@",{requires:["base","node","io-base","moodle-core-notification","json-parse","widget-position","widget-position-align","event-outside","cache"]});
YUI.add("moodle-core-tooltip",function(e,t){function n(e){e||(e={}),typeof e.draggable=="undefined"&&(e.draggable=!0),typeof e.constrain=="undefined"&&(e.constrain=!0),typeof e.lightbox=="undefined"&&(e.lightbox=!1),n.superclass.constructor.apply(this,[e])}var r={CLOSEBUTTON:".closebutton"},i={PANELTEXT:"tooltiptext"},s={WAITICON:{pix:"i/loading_small",component:"moodle"}},o={};n.NAME="moodle-core-tooltip",n.CSS_PREFIX="moodle-dialogue",n.ATTRS=o,o.initialheadertext={value:""},o.initialbodytext={value:"",setter:function(t){var n,r;return n=e.Node.create("<div />").addClass(i.PANELTEXT),r=e.Node.create("<img />").setAttribute("src",M.util.image_url(s.WAITICON.pix,s.WAITICON.component)).addClass("spinner"),t?(n.set("text",t),r.addClass("iconsmall")):n.addClass("content-lightbox"),n.append(r),n}},o.initialfootertext={value:null,setter:function(t){if(t)return e.Node.create("<div />").set("text",t)}},o.headerhandler={value:"set_header_content"},o.bodyhandler={value:"set_body_content"},o.footerhandler={value:null},o.urlmodifier={value:null},o.textcache={value:null},o.textcachesize={value:10},e.extend(n,M.core.dialogue,{bb:null,listenevents:[],textcache:null,alignpoints:[e.WidgetPositionAlign.TL,e.WidgetPositionAlign.RC],initializer:function(){return this.get("headerhandler")||this.set("headerhandler",this.set_header_content),this.get("bodyhandler")||this.set("bodyhandler",this.set_body_content),this.get("footerhandler")||this.set("footerhandler",function(){}),this.get("urlmodifier")||this.set("urlmodifier",this.modify_url),this.setAttrs({headerContent:this.get("initialheadertext"),bodyContent:this.get("initialbodytext"),footerContent:this.get("initialfootertext"),zIndex:150}),this.hide(),this.render(),this.bb=this.get("boundingBox"),right_to_left()&&(this.alignpoints=[e.WidgetPositionAlign.TR,e.WidgetPositionAlign.LC]),this.get("textcache")||this.set("textcache",new e.Cache({max:this.get("textcachesize")})),M.cfg.developerdebug&&this.get("textcache").set("max",0),this},display_panel:function(t){var n,i,s,o,u;t.preventDefault(),t.stopPropagation(),this.cancel_events(),n=t.target.ancestor("a",!0),this.align(n,this.alignpoints),this.setAttrs({headerContent:this.get("initialheadertext"),bodyContent:this.get("initialbodytext"),footerContent:this.get("initialfootertext")}),this.show(),i=this.bb.delegate("click",this.close_panel,r.CLOSEBUTTON,this),this.listenevents.push(i),i=e.one("body").on("key",this.close_panel,"esc",this),this.listenevents.push(i),i=this.bb.on("mousedownoutside",this.close_panel,this),this.listenevents.push(i),s=e.bind(this.get("urlmodifier"),this,n.get("href"))(),u=this.get("textcache").retrieve(s),u?this._set_panel_contents(u.response):(o={method:"get",context:this,sync:!1,on:{complete:function(e,t){this._set_panel_contents(t.responseText,s)}}},e.io(s,o))},_set_panel_contents:function(t,n){var r;try{r=e.JSON.parse(t);if(r.error)return this.close_panel(),new M.core.ajaxException(r)}catch(i){return this.close_panel(),new M.core.exception(i)}e.bind(this.get("headerhandler"),this,r)(),e.bind(this.get("bodyhandler"),this,r)(),e.bind(this.get("footerhandler"),this,r)(),n&&this.get("textcache").add(n,t),this.get("buttons").header[0].focus()},set_header_content:function(e){this.set("headerContent",e.heading)},set_body_content:function(t){var n=e.Node.create("<div />").set("innerHTML",t.text).setAttribute("role","alert").addClass(i.PANELTEXT);this.set("bodyContent",n)},modify_url:function(e){return e.replace(/\.php\?/,"_ajax.php?")},close_panel:function(e){this.hide(),this.cancel_events(),e&&e.preventDefault()},cancel_events:function(){var e;while(this.listenevents.length)e=this.listenevents.shift(),e.detach()}}),M.core=M.core||{},M.core.tooltip=M.core.tooltip=n},"@VERSION@",{requires:["base","node","io-base","moodle-core-notification","json-parse","widget-position","widget-position-align","event-outside","cache"]});
+25 -11
View File
@@ -190,6 +190,19 @@ ATTRS.footerhandler = {
value: null
};
/**
* The function which handles modifying the URL that was clicked on.
*
* The default function rewrites '.php' to '_ajax.php'.
*
* @attribute urlmodifier
* @type Function|String|null
* @default null
*/
ATTRS.urlmodifier = {
value: null
};
/**
* Set the Y.Cache object to use.
*
@@ -247,6 +260,9 @@ Y.extend(TOOLTIP, M.core.dialogue, {
if (!this.get('footerhandler')) {
this.set('footerhandler', function() {});
}
if (!this.get('urlmodifier')) {
this.set('urlmodifier', this.modify_url);
}
// Set up the dialogue with initial content.
this.setAttrs({
@@ -290,7 +306,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
/**
* Display the tooltip for the clicked link.
*
* The anchor for the clicked link is used, additionally appending ajax=1 to the parameters.
* The anchor for the clicked link is used.
*
* @method display_panel
* @param {EventFacade} e The event from the clicked link. This is used to determine the clicked URL.
@@ -332,7 +348,8 @@ Y.extend(TOOLTIP, M.core.dialogue, {
thisevent = this.bb.on('mousedownoutside', this.close_panel, this);
this.listenevents.push(thisevent);
ajaxurl = clickedlink.get('href');
// Modify the URL as required.
ajaxurl = Y.bind(this.get('urlmodifier'), this, clickedlink.get('href'))();
cacheentry = this.get('textcache').retrieve(ajaxurl);
if (cacheentry) {
@@ -344,10 +361,6 @@ Y.extend(TOOLTIP, M.core.dialogue, {
method: 'get',
context: this,
sync: false,
data: {
// We use a slightly different AJAX URL to the one on the anchor to allow non-JS fallback.
ajax: 1
},
on: {
complete: function(tid, response) {
this._set_panel_contents(response.responseText, ajaxurl);
@@ -355,7 +368,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
}
};
Y.io(clickedlink.get('href'), config);
Y.io(ajaxurl, config);
}
},
@@ -371,10 +384,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
}
} catch (error) {
this.close_panel();
return new M.core.exception({
name: error.name,
message: "Unable to retrieve the requested content. The following error was returned: " + error.message
});
return new M.core.exception(error);
}
// Set the contents using various handlers.
@@ -403,6 +413,10 @@ Y.extend(TOOLTIP, M.core.dialogue, {
this.set('bodyContent', bodycontent);
},
modify_url: function(url) {
return url.replace(/\.php\?/, '_ajax.php?');
},
close_panel: function(e) {
// Hide the panel first.
this.hide();
+25 -11
View File
@@ -188,6 +188,19 @@ ATTRS.footerhandler = {
value: null
};
/**
* The function which handles modifying the URL that was clicked on.
*
* The default function rewrites '.php' to '_ajax.php'.
*
* @attribute urlmodifier
* @type Function|String|null
* @default null
*/
ATTRS.urlmodifier = {
value: null
};
/**
* Set the Y.Cache object to use.
*
@@ -245,6 +258,9 @@ Y.extend(TOOLTIP, M.core.dialogue, {
if (!this.get('footerhandler')) {
this.set('footerhandler', function() {});
}
if (!this.get('urlmodifier')) {
this.set('urlmodifier', this.modify_url);
}
// Set up the dialogue with initial content.
this.setAttrs({
@@ -288,7 +304,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
/**
* Display the tooltip for the clicked link.
*
* The anchor for the clicked link is used, additionally appending ajax=1 to the parameters.
* The anchor for the clicked link is used.
*
* @method display_panel
* @param {EventFacade} e The event from the clicked link. This is used to determine the clicked URL.
@@ -330,7 +346,8 @@ Y.extend(TOOLTIP, M.core.dialogue, {
thisevent = this.bb.on('mousedownoutside', this.close_panel, this);
this.listenevents.push(thisevent);
ajaxurl = clickedlink.get('href');
// Modify the URL as required.
ajaxurl = Y.bind(this.get('urlmodifier'), this, clickedlink.get('href'))();
cacheentry = this.get('textcache').retrieve(ajaxurl);
if (cacheentry) {
@@ -342,10 +359,6 @@ Y.extend(TOOLTIP, M.core.dialogue, {
method: 'get',
context: this,
sync: false,
data: {
// We use a slightly different AJAX URL to the one on the anchor to allow non-JS fallback.
ajax: 1
},
on: {
complete: function(tid, response) {
this._set_panel_contents(response.responseText, ajaxurl);
@@ -353,7 +366,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
}
};
Y.io(clickedlink.get('href'), config);
Y.io(ajaxurl, config);
}
},
@@ -369,10 +382,7 @@ Y.extend(TOOLTIP, M.core.dialogue, {
}
} catch (error) {
this.close_panel();
return new M.core.exception({
name: error.name,
message: "Unable to retrieve the requested content. The following error was returned: " + error.message
});
return new M.core.exception(error);
}
// Set the contents using various handlers.
@@ -401,6 +411,10 @@ Y.extend(TOOLTIP, M.core.dialogue, {
this.set('bodyContent', bodycontent);
},
modify_url: function(url) {
return url.replace(/\.php\?/, '_ajax.php?');
},
close_panel: function(e) {
// Hide the panel first.
this.hide();