? 710908-style_handler-41.patch ? 710908-style_plugin-47.patch ? includes/layer_types/vector.inc ? modules/openlayers_views/views/openlayers-views-data.tpl.php Index: openlayers.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/openlayers/openlayers.module,v retrieving revision 1.69.2.75 diff -u -p -r1.69.2.75 openlayers.module --- openlayers.module 10 Jun 2010 11:06:50 -0000 1.69.2.75 +++ openlayers.module 27 Jun 2010 21:29:47 -0000 @@ -432,6 +432,21 @@ function openlayers_behaviors($reset = F } /** + * Get all style plugins. + * + * @ingroup openlayers_api + * + * @param $reset + * Boolean whether to reset cache or not. + * @return + * Array of style handler info. + */ +function openlayers_style_plugins($reset = FALSE) { + ctools_include('plugins'); + return ctools_get_plugins('openlayers', 'style_plugin'); +} + +/** * Get all openlayers styles. * * @ingroup openlayers_api @@ -885,6 +900,87 @@ class openlayers_layer_type { } /** + * Base class for style plugins + * + * We define base classes in the core module. + * All other parent classes can be autoloaded through ctools. + */ +class openlayers_style_plugin { + var $options; + + /** + * Constructor + * + * Takes options sent in and adds them to the + * class. + * + * @param $options + * Array of options. + */ + function __construct($options = array()) { + $this->options = $options + $this->options_init(); + } + + /** + * Initial default options. + * + * @return + * Array of default options. + */ + function options_init() { + return array(); + } + + /** + * Options form. + * + * @param $defaults + * Array of default values for the form. + * @return + * Array of Drupal form elements. + */ + function options_form($defaults = array()) { + return array(); + } + + /** + * Render the style. + * + * @param $style + * The style passed in to be handled. + */ + function render(&$style) { + // Render style. + } +} + +/** + * Implementation of hook_ctools_plugin_directory + */ +function openlayers_ctools_plugin_directory($module, $plugin) { + // The format of plugin includes should be the + // following: + // modulename_plugin_name.inc + // + // For example: + // openlayers_style_plugin_name.inc + + // If this module needed to supply style plugins. + /* + if ($module == 'openlayers' && $plugin == 'style_plugin') { + return 'plugins/style_plugin'; + } + */ + + // This should change to the following when converted: + /* + if ($module == 'openlayers') { + return 'plugins/' . $plugin; + } + */ +} + +/** * Implementation of hook_ctools_plugin */ function openlayers_ctools_plugin_behaviors() { Index: includes/openlayers.render.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/openlayers/includes/openlayers.render.inc,v retrieving revision 1.3.2.16 diff -u -p -r1.3.2.16 openlayers.render.inc --- includes/openlayers.render.inc 27 Apr 2010 15:50:15 -0000 1.3.2.16 +++ includes/openlayers.render.inc 27 Jun 2010 21:29:47 -0000 @@ -68,9 +68,31 @@ function _openlayers_behaviors_render($b */ function _openlayers_styles_process($styles = array(), $layer_styles = array(), &$map = array()) { + ctools_include('plugins'); // Get styles info array $styles_info = openlayers_styles(); + + // Process with handler if available. + $style_plugins = openlayers_style_plugins(); + foreach ($styles_info as $i => $style) { + // Check for plugins. + if (isset($style->data['plugins']) && + is_array($style->data['plugins'])) { + foreach ($style->data['plugins'] as $p => $options) { + // Ensure that the plugin exists + if (($style_plugins[$p])) { + $plugin_class = ctools_plugin_get_class($style_plugins[$p], + 'style_plugin'); + if (isset($plugin_class)) { + $plugin = new $plugin_class; + // Render the plugin with the style. + $plugin->render($styles_info[$i]); + } + } + } + } + } // Go through styles $processed = array(); Index: js/openlayers.js =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/openlayers/js/openlayers.js,v retrieving revision 1.47.2.34 diff -u -p -r1.47.2.34 openlayers.js --- js/openlayers.js 27 Jun 2010 14:32:09 -0000 1.47.2.34 +++ js/openlayers.js 27 Jun 2010 21:29:47 -0000 @@ -277,8 +277,19 @@ Drupal.openlayers = { // Implement layer-specific styles. if (map.layer_styles !== undefined && map.layer_styles[layername]) { var style = map.layer_styles[layername]; - stylesAdded['default'] = new OpenLayers.Style(map.styles[style]); + // Check for context + if (typeof map.styles[style].context_object != 'undefined' && + typeof Drupal.openlayers.styleContext[map.styles[style].context_object] != 'undefined') { + stylesAdded['default'] = new OpenLayers.Style( + map.styles[style], + { context: Drupal.openlayers.styleContext[map.styles[style].context_object] } + ); + } + else { + stylesAdded['default'] = new OpenLayers.Style(map.styles[style]); + } } + return new OpenLayers.StyleMap(stylesAdded); } // Default styles @@ -309,3 +320,4 @@ Drupal.openlayers = { }; Drupal.openlayers.layer = {}; +Drupal.openlayers.styleContext = {}; Index: modules/openlayers_ui/includes/openlayers_ui.styles.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/openlayers/modules/openlayers_ui/includes/Attic/openlayers_ui.styles.inc,v retrieving revision 1.1.2.7 diff -u -p -r1.1.2.7 openlayers_ui.styles.inc --- modules/openlayers_ui/includes/openlayers_ui.styles.inc 25 Jun 2010 15:47:04 -0000 1.1.2.7 +++ modules/openlayers_ui/includes/openlayers_ui.styles.inc 27 Jun 2010 21:29:47 -0000 @@ -154,6 +154,9 @@ function openlayers_ui_styles_form(&$for $form['info'] = array( '#type' => 'fieldset', '#tree' => FALSE, + '#title' => t('Basic Information'), + '#description' => t('The basic information for the style, used to refer to and describe the style.'), + '#collapsible' => TRUE, ); $form['info']['name'] = array( '#title' => t('Name'), @@ -174,8 +177,16 @@ function openlayers_ui_styles_form(&$for ); // OpenLayers style properties - $form['data'] = array('#type' => 'fieldset', '#tree' => TRUE); - + $form['data'] = array( + '#type' => 'fieldset', + '#tree' => TRUE, + '#title' => t('Style Properties and Plugins'), + '#description' => t('Style properties are properties as + defined by the OpenLayers library. Plugins are dynamically + process the layer at render time; plugins may override the + values that you have set for style properies.'), + '#collapsible' => TRUE, + ); foreach ($properties as $key => $prop) { $form['data'][$key] = array( '#type' => !isset($prop['options']) ? 'textfield' : 'select', @@ -190,6 +201,10 @@ function openlayers_ui_styles_form(&$for $form['data'][$key]['#options'] = $prop['options']; } } + + // Add style plugins + $form['data']['plugins'] = openlayers_ui_get_style_plugin_options( + isset($style->data) ? $style->data : array()); $form['submit'] = array( '#type' => 'submit', @@ -202,12 +217,28 @@ function openlayers_ui_styles_form(&$for * Submit handler for layers. */ function openlayers_ui_styles_form_submit(&$form, &$form_state) { + $data = $form_state['values']['data']; + + // Process style plugins. This allows us to have simpler + // arrays for plugins. + foreach ($data['plugins'] as $plugin => $settings) { + if ($settings['enabled']) { + $data['plugins'][$plugin] = isset($data['options']) ? + $data['options'] : array(); + } + else { + unset($data['plugins'][$plugin]); + } + } + + // Create style object $style = new stdClass(); $style->name = $form_state['values']['name']; $style->title = $form_state['values']['title']; $style->description = $form_state['values']['description']; - $style->data = $form_state['values']['data']; + $style->data = $data; + // Save values $success = openlayers_style_save($style); // Redirect to edit page @@ -220,6 +251,45 @@ function openlayers_ui_styles_form_submi } } +/** + * Get style plugin options. + */ +function openlayers_ui_get_style_plugin_options($defaults) { + $form = array(); + + // Get all available style plugins. + foreach (openlayers_style_plugins() as $key => $plugin) { + $plugin_class = ctools_plugin_get_class($plugin, 'style_plugin'); + if (!empty($plugin_class)) { + // Create options for oprion form. + $options = isset($defaults['plugins'][$key]) ? + $defaults['plugins'][$key] : array(); + + // Create object + $style_plugin = new $plugin_class($options); + // Put together fieldset of options. This should + // be improved so that it looks better. + $form[$key] = array( + '#tree' => TRUE, + '#type' => 'fieldset', + '#title' => $plugin['title'], + '#description' => $plugin['description'], + 'enabled' => array( + '#type' => 'checkbox', + '#title' => t('Enabled'), + '#default_value' => isset($defaults['plugins'][$key]['enabled']) + ? $defaults['plugins'][$key]['enabled'] : FALSE, + ), + ); + + // Add extra options form. + $form[$key]['options'] = $style_plugin->options_form($options); + } + } + + return $form; +} + // /** // * Import a preset from cut & paste // */ Index: tests/openlayers_test.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/openlayers/tests/Attic/openlayers_test.module,v retrieving revision 1.2.2.15 diff -u -p -r1.2.2.15 openlayers_test.module --- tests/openlayers_test.module 27 Jun 2010 15:57:01 -0000 1.2.2.15 +++ tests/openlayers_test.module 27 Jun 2010 21:29:47 -0000 @@ -30,6 +30,16 @@ function openlayers_test_menu() { } /** + * Implementation of hook_ctools_plugin_directory + */ +function openlayers_test_ctools_plugin_directory($module, $plugin) { + if ($module == 'openlayers' && $plugin == 'style_plugin') { + return 'plugins/style_plugin'; + } +} + + +/** * Implementation of hook_ctools_plugin_api(). */ function openlayers_test_ctools_plugin_api($module, $api) { @@ -38,6 +48,9 @@ function openlayers_test_ctools_plugin_a switch ($api) { case 'openlayers_presets': return array('version' => 1); + + case 'openlayers_styles': + return array('version' => 1); } } @@ -53,6 +66,32 @@ function openlayers_test_views_api() { } /** + * Implementation of hook_openlayers_styles() + */ +function openlayers_test_openlayers_styles() { + $styles = array(); + + $style = new stdClass(); + $style->api_version = 1; + $style->name = 'test_context'; + $style->title = t('Test: Context style'); + $style->description = t('A style to test context styling.'); + $style->data = array( + 'pointRadius' => '6', + 'fillColor' => '#77777', + 'strokeColor' => '#222222', + 'strokeWidth' => '2', + 'strokeOpacity' => '0.8', + 'plugins' => array( + 'openlayers_test_test_plugin' => array(), + ), + ); + $styles[$style->name] = $style; + + return $styles; +} + +/** * Implementation of hook_openlayers_presets(). */ function openlayers_test_openlayers_presets() { Index: tests/plugins/style_plugin/openlayers_test_test_plugin.inc =================================================================== RCS file: tests/plugins/style_plugin/openlayers_test_test_plugin.inc diff -N tests/plugins/style_plugin/openlayers_test_test_plugin.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/plugins/style_plugin/openlayers_test_test_plugin.inc 27 Jun 2010 21:29:47 -0000 @@ -0,0 +1,61 @@ + t('Example Plugin'), + 'description' => t('Example style plugin for context styling. + Sets a random Fill Opacity.'), + 'style_plugin' => array( + 'class' => 'openlayers_style_plugin_test_plugin', + 'parent' => 'openlayers_style_plugin', + ), +); + +/** + * Style Plugin for testing purposes. + */ +class openlayers_style_plugin_test_plugin extends + openlayers_style_plugin { + /** + * Provide initial values for options. + */ + function options_init() { + return array( + 'context_properties' => array( + 'fillOpacity' => 'getFillOpacity', + ), + 'context_object' => 'openlayersExampleContext', + ); + } + + /** + * Options form. + */ + function options_form($defaults = array()) { + return array(); + } + + /** + * Render function + */ + function render(&$style) { + // For context styling, we want to replace the + // style properties with their context value + foreach ($this->options['context_properties'] as $prop => $callback) { + $style->data[$prop] = '${' . $callback . '}'; + } + + // Enable context + $style->data['context_object'] = $this->options['context_object']; + + // Add JS + drupal_add_js(drupal_get_path('module', 'openlayers_test') . + '/plugins/style_plugin/openlayers_test_test_plugin.js'); + } +} \ No newline at end of file Index: tests/plugins/style_plugin/openlayers_test_test_plugin.js =================================================================== RCS file: tests/plugins/style_plugin/openlayers_test_test_plugin.js diff -N tests/plugins/style_plugin/openlayers_test_test_plugin.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/plugins/style_plugin/openlayers_test_test_plugin.js 27 Jun 2010 21:29:47 -0000 @@ -0,0 +1,16 @@ +// $Id$ + +/** + * @file + * File to hold custom context styling + */ + +/** + * Global variable for context styling. + */ +Drupal.openlayers.styleContext.openlayersExampleContext = { + 'getFillOpacity': function(feature) { + // Random fill opacity + return Math.random(); + } +}; \ No newline at end of file