diff --git a/filter/glossary/filter.php b/filter/glossary/filter.php index 20879d9f363..259c668cc12 100644 --- a/filter/glossary/filter.php +++ b/filter/glossary/filter.php @@ -35,14 +35,25 @@ defined('MOODLE_INTERNAL') || die(); */ class filter_glossary extends moodle_text_filter { + public function setup($page, $context) { + // This only requires execution once per request. + static $jsinitialised = false; + if (empty($jsinitialised)) { + $page->requires->yui_module( + 'moodle-filter_glossary-autolinker', + 'M.filter_glossary.init_filter_autolinking', + array(array('courseid' => 0))); + $jsinitialised = true; + } + } + public function filter($text, array $options = array()) { - global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS, $PAGE; + global $CFG, $DB, $GLOSSARY_EXCLUDECONCEPTS; // Trivial-cache - keyed on $cachedcontextid static $cachedcontextid; static $conceptlist; - static $jsinitialised; // To control unique js init static $nothingtodo; // To avoid processing if no glossaries / concepts are found // Try to get current course @@ -185,16 +196,6 @@ class filter_glossary extends moodle_text_filter { } $conceptlist = filter_remove_duplicates($conceptlist); - - if (empty($jsinitialised)) { - // Add a JavaScript event to open popup's here. This only ever need to be - // added once! - $PAGE->requires->yui_module( - 'moodle-filter_glossary-autolinker', - 'M.filter_glossary.init_filter_autolinking', - array(array('courseid' => $courseid))); - $jsinitialised = true; - } } if (!empty($GLOSSARY_EXCLUDECONCEPTS)) { diff --git a/filter/glossary/yui/autolinker/autolinker.js b/filter/glossary/yui/autolinker/autolinker.js index e9af017e0ad..18677afe85f 100644 --- a/filter/glossary/yui/autolinker/autolinker.js +++ b/filter/glossary/yui/autolinker/autolinker.js @@ -5,7 +5,6 @@ YUI.add('moodle-filter_glossary-autolinker', function(Y) { POPUPNAME = 'name', POPUPOPTIONS = 'options', TITLE = 'title', - COURSEID = 'courseid', WIDTH = 'width', HEIGHT = 'height', MENUBAR = 'menubar', @@ -122,8 +121,7 @@ YUI.add('moodle-filter_glossary-autolinker', function(Y) { status : {value : true}, directories : {value : false}, fullscreen : {value : false}, - dependent : {value : true}, - courseid : {value : 1} + dependent : {value : true} } }); diff --git a/filter/upgrade.txt b/filter/upgrade.txt index bb0522ecc4b..cf94aced19e 100644 --- a/filter/upgrade.txt +++ b/filter/upgrade.txt @@ -1,6 +1,13 @@ This file describes API changes in core filter API and plugins, information provided here is intended especially for developers. +=== 2.3 === + +* new setup() method added to moodle_text_filter, invoked before + filtering happens, used to add all the requirements to the page + (js, css...) and/or other init tasks. See filter/glossary for + an example using the API (and MDL-32279 for its justification). + === 2.2 === * legacy filters and legacy locations have been deprecated, so any diff --git a/lib/filterlib.php b/lib/filterlib.php index 62e68c12f43..ca75b0d1cb6 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -222,6 +222,29 @@ class filter_manager { } return implode('-', $hashes); } + + /** + * Setup page with filters requirements and other prepare stuff. + * + * This method is used by {@see format_text()} and {@see format_string()} + * in order to allow filters to setup any page requirement (js, css...) + * or perform any action needed to get them prepared before filtering itself + * happens by calling to each every active setup() method. + * + * Note it's executed for each piece of text filtered, so filter implementations + * are responsible of controlling the cardinality of the executions that may + * be different depending of the stuff to prepare. + * + * @param moodle_page $page the page we are going to add requirements to. + * @param context $context the context which contents are going to be filtered. + * @since 2.3 + */ + public function setup_page_for_filters($page, $context) { + $filters = $this->get_text_filters($context); + foreach ($filters as $filter) { + $filter->setup($page, $context); + } + } } /** @@ -357,6 +380,24 @@ abstract class moodle_text_filter { return __CLASS__; } + /** + * Setup page with filter requirements and other prepare stuff. + * + * Override this method if the filter needs to setup page + * requirements or needs other stuff to be executed. + * + * Note this method is invoked from {@see setup_page_for_filters()} + * for each piece of text being filtered, so it is responsible + * for controlling its own execution cardinality. + * + * @param moodle_page $page the page we are going to add requirements to. + * @param context $context the context which contents are going to be filtered. + * @since 2.3 + */ + public function setup($page, $context) { + // Override me, if needed. + } + /** * Override this function to actually implement the filtering. * diff --git a/lib/weblib.php b/lib/weblib.php index f68f3d59406..d84d16d2018 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1090,6 +1090,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_ if ($options['filter']) { $filtermanager = filter_manager::instance(); + $filtermanager->setup_page_for_filters($PAGE, $context); // Setup global stuff filters may have. } else { $filtermanager = new null_filter_manager(); } @@ -1302,6 +1303,7 @@ function format_string($string, $striplinks = true, $options = NULL) { if (!empty($CFG->filterall)) { $string = filter_manager::instance()->filter_string($string, $options['context']); + $filtermanager->setup_page_for_filters($PAGE, $options['context']); // Setup global stuff filters may have. } // If the site requires it, strip ALL tags from this string