Use 'pageAdd*' events instead of special plugins methods

This commit is contained in:
Dmitriy Simushev 2013-02-05 08:25:34 +00:00
parent b96c9d0a2c
commit c2f3993fc1

View File

@ -65,78 +65,98 @@ function topage($text)
} }
/** /**
* Load additional CSS files, required by plugins, and build HTML code to include them * Load additional CSS files, required by plugins, and build HTML code to
* include them
*
* Triggers 'pageAddCSS' and pass listeners associative array with
* following keys:
* - 'page': string, name of page to which CSS files will be attached.
* - 'js': array, with CSS files paths. Modify this array to add or remove
* additional CSS files.
* *
* @param string $page_name CSS files load to this page * @param string $page_name CSS files load to this page
* @return string HTML block of 'link' tags * @return string HTML block of 'link' tags
*/ */
function get_additional_css($page_name) { function get_additional_css($page_name) {
$method = $page_name . 'AddCss'; // Prepare event arguments array
$plugins = PluginManager::getAllPlugins(); $args = array(
'page' => $page_name,
'css' => array()
);
// Trigger event
$dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('pageAddCSS', $args);
// Build resulting css list
$result = array(); $result = array();
// Check all plugins foreach($args['css'] as $css) {
foreach ($plugins as $plugin) {
if (is_callable(array($plugin, $method))) {
// Try to invoke '<$page_name>AddCss' method
$css_list = $plugin->$method();
foreach ($css_list as $css) {
// Add script tag for each javascript file
$result[] = '<link rel="stylesheet" type="text/css" href="'.$css.'">'; $result[] = '<link rel="stylesheet" type="text/css" href="'.$css.'">';
} }
}
}
return implode("\n", $result); return implode("\n", $result);
} }
/** /**
* Load additional JavaScript files, required by plugins, and build HTML code to include them * Load additional JavaScript files, required by plugins, and build HTML code
* to include them
*
* Triggers 'pageAddJS' and pass listeners associative array with
* following keys:
* - 'page': string, name of page to which JavaScript files will be attached.
* - 'js': array, with JavaScript files paths. Modify this array to add or
* remove additional JavaScript files.
* *
* @param string $page_name JavaScript files load to this page * @param string $page_name JavaScript files load to this page
* @return string HTML block of 'script' tags * @return string HTML block of 'script' tags
*/ */
function get_additional_js($page_name) { function get_additional_js($page_name) {
$method = $page_name . 'AddJs'; // Prepare event arguments array
$plugins = PluginManager::getAllPlugins(); $args = array(
'page' => $page_name,
'js' => array()
);
// Trigger event
$dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('pageAddJS', $args);
// Build resulting css list
$result = array(); $result = array();
// Check all plugins foreach($args['js'] as $js) {
foreach ($plugins as $plugin) {
if (is_callable(array($plugin, $method))) {
// Try to invoke '<$page_name>AddJs' method
$js_list = $plugin->$method();
foreach ($js_list as $js) {
// Add script tag for each javascript file
$result[] = '<script type="text/javascript" src="'.$js.'"></script>'; $result[] = '<script type="text/javascript" src="'.$js.'"></script>';
} }
}
}
return implode("\n", $result); return implode("\n", $result);
} }
/** /**
* Build Javascript code that contains initializing options for JavaScript plugins * Build Javascript code that contains initializing options for JavaScript
* plugins
*
* Triggers 'pageAddJSPluginOptions' and pass listeners associative array with
* following keys:
* - 'page': string, name of page at which plugins will work.
* - 'plugins': associative array, whose keys are plugins names and values are
* plugins options. Modify this array to add or change plugins options
* *
* @param string $page_name Plugins initialize at this page * @param string $page_name Plugins initialize at this page
* @return string JavaScript options block * @return string JavaScript options block
*/ */
function get_js_plugin_options($page_name) { function get_js_plugin_options($page_name) {
$method = $page_name . 'AddJsPluginOptions'; // Prepare event arguments array
$plugins = PluginManager::getAllPlugins(); $args = array(
$result = array(); 'page' => $page_name,
// Check all plugins 'plugins' => array()
foreach ($plugins as $plugin) { );
if (is_callable(array($plugin, $method))) {
// Try to invoke '<$page_name>AddJsPluginOptions' method // Trigger event
$js_plugins = $plugin->$method(); $dispatcher = EventDispatcher::getInstance();
foreach ($js_plugins as $js_plugin => $js_options) { $dispatcher->triggerEvent('pageAddJSPluginOptions', $args);
// Add plugin's initialization options code
if (empty($result[$js_plugin])) { // Return encoded options
$result[$js_plugin] = array(); return json_encode($args['plugins']);
}
$result[$js_plugin] = array_merge($result[$js_plugin], $js_options);
}
}
}
return json_encode($result);
} }
function no_field($key) function no_field($key)