Commit a7287d28 authored by Evan Goldenberg's avatar Evan Goldenberg Committed by Richard Mansfield
Browse files

Add plugin specific javascript capability.

Blocktype plugins can now specify javascript files
to be called at the top of the view. The files should
be specified relative to lib.php, and must be instance
independent. Each file will be called once per blocktype
in a view, not once per block instance.

Blocktypes can also specify javascript to be loaded when
instances of the blocktype are being configured.
parent b56cdf14
......@@ -83,6 +83,15 @@ abstract class PluginBlocktype extends Plugin {
public static abstract function render_instance(BlockInstance $instance, $editing=false);
/**
* This function must be implemented in the subclass if it requires
* javascript. It returns an array of javascript files, either local
* or remote.
*/
public static function get_instance_javascript(BlockInstance $instance) {
return array();
}
/**
* If this blocktype contains artefacts, and uses the artefactchooser
* Pieform element to choose them, this method must return the definition
......@@ -116,6 +125,15 @@ abstract class PluginBlocktype extends Plugin {
throw new SystemException(get_string('blocktypemissingconfigform', 'error', $instance->get('blocktype')));
}
/**
* Thus function must be implemented in the subclass is it has an
* instance config form that requires javascript. It returns an
* array of javascript files, either local or remote.
*/
public static function get_instance_config_javascript(BlockInstance $instance) {
return array();
}
/**
* Blocktype plugins can implement this to perform custom pieform
* validation, should they need it
......@@ -712,6 +730,7 @@ class BlockInstance {
$title = $this->get('title');
}
$elements = array_merge(
array(
'title' => array(
......@@ -815,6 +834,24 @@ class BlockInstance {
}
}
$configjs = call_static_method($blocktypeclass, 'get_instance_config_javascript', $this);
foreach($configjs as &$jsfile) {
if(strpos($jsfile, 'http://') === false) {
if($this->artefactplugin) {
$jsfile = 'artefact/' . $this->artefactplugin . '/blocktype/' .
$this->blocktype . '/' . $jsfile;
}
else {
$jsfile = 'blocktype/' . $this->blocktype . '/' . $jsfile;
}
$jsfile = '$j.getScript("' . get_config('wwwroot') . $jsfile . '");';
}
else {
$jsfile = '$j.getScript("' . $jsfile . '");';
}
}
$js .= implode('', $configjs);
$renderedform = array('html' => $html, 'javascript' => $js);
return $renderedform;
}
......
......@@ -1382,6 +1382,38 @@ class View {
db_commit();
}
/**
* Returns a list of required javascript files, based on
* the blockinstances present in the view.
*/
public function get_blocktype_javascript() {
$view_data = $this->get_column_datastructure();
$javascript = array();
foreach($view_data as $column) {
foreach($column['blockinstances'] as $blockinstance) {
$pluginname = $blockinstance->get('blocktype');
safe_require('blocktype', $pluginname);
$instancejs = call_static_method(
generate_class_name('blocktype', $pluginname),
'get_instance_javascript',
$blockinstance
);
foreach($instancejs as &$jsfile) {
if(strpos($jsfile, 'http://') === false) {
if($artefactplugin = get_field('blocktype_installed', 'artefactplugin', 'name', $pluginname)) {
$jsfile = 'artefact/' . $artefactplugin . '/blocktype/' .
$pluginname . '/' . $jsfile;
}
else {
$jsfile = 'blocktype/' . $blockinstance->get('blocktype') . '/' . $jsfile;
}
}
}
$javascript = array_merge($javascript, $instancejs);
}
}
return array_unique($javascript);
}
private $blockinstance_currently_being_configured = 0;
......
......@@ -260,8 +260,8 @@ EOF;
}
}
}
else {
// A .js file with a fully specified path
else if (strpos($jsfile, 'http://') === false) {
// A local .js file with a fully specified path
$javascript_array[] = $wwwroot . $jsfile;
// If $jsfile is from a plugin (i.e. plugintype/pluginname/js/foo.js)
// Then get js strings from static function jsstrings in plugintype/pluginname/lib.php
......@@ -295,6 +295,10 @@ EOF;
}
}
}
else {
// A remote .js file
$javascript_array[] = $jsfile;
}
}
$javascript_array[] = $jsroot . 'mahara.js';
......
......@@ -70,6 +70,9 @@ if (!$view || !can_view_view($viewid)) {
throw new AccessDeniedException(get_string('youcannotviewthisusersprofile', 'error'));
}
$javascript = array('paginator', 'lib/pieforms/static/core/pieforms.js', 'artefact/resume/resumeshowhide.js');
$javascript = array_merge($view->get_blocktype_javascript());
// Set up theme
$viewtheme = $view->get('theme');
if ($viewtheme && $THEME->basename != $viewtheme) {
......@@ -80,7 +83,7 @@ $stylesheets = array('<link rel="stylesheet" type="text/css" href="' . get_confi
$name = display_name($user);
define('TITLE', $name);
$smarty = smarty(
array('paginator', 'lib/pieforms/static/core/pieforms.js', 'artefact/resume/resumeshowhide.js'),
$javascript,
$stylesheets,
array(),
array(
......
......@@ -162,7 +162,10 @@ if ($viewtheme && !isset($allowedthemes[$viewtheme])) {
exit;
}
$smarty = smarty(array('views', 'tinymce', 'paginator', 'tablerenderer', 'artefact/file/js/filebrowser.js', 'lib/pieforms/static/core/pieforms.js', 'blocktype/creativecommons/js/creativecommons.js'), $stylesheets, false, $extraconfig);
$javascript = array('views', 'tinymce', 'paginator', 'tablerenderer', 'artefact/file/js/filebrowser.js', 'lib/pieforms/static/core/pieforms.js', 'blocktype/creativecommons/js/creativecommons.js');
$javascript = array_merge($javascript, $view->get_blocktype_javascript());
$smarty = smarty($javascript, $stylesheets, false, $extraconfig);
// The list of categories for the tabbed interface
$smarty->assign('category_list', $view->build_category_list($category, $new));
......
......@@ -154,6 +154,9 @@ $viewbeingwatched = (int)record_exists('usr_watchlist_view', 'usr', $USER->get('
$feedback = ArtefactTypeComment::get_comments($limit, $offset, $showcomment, $view);
$javascript = array('paginator', 'viewmenu', 'artefact/resume/resumeshowhide.js');
$javascript = array_merge($javascript, $view->get_blocktype_javascript());
// Set up theme
$viewtheme = $view->get('theme');
if ($viewtheme && $THEME->basename != $viewtheme) {
......@@ -164,7 +167,7 @@ $stylesheets = array('<link rel="stylesheet" type="text/css" href="' . get_confi
$can_edit = $USER->can_edit_view($view) && !$submittedgroup && !$view->is_submitted();
$smarty = smarty(
array('paginator', 'viewmenu', 'artefact/resume/resumeshowhide.js'),
$javascript,
$stylesheets,
array(),
array(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment