? 710908-style_plugin-47.patch
? 710908-style_plugin-70.patch
Index: openlayers.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openlayers/openlayers.module,v
retrieving revision 1.69.2.77
diff -u -p -r1.69.2.77 openlayers.module
--- openlayers.module	4 Jul 2010 19:23:04 -0000	1.69.2.77
+++ openlayers.module	4 Jul 2010 23:47:01 -0000
@@ -447,6 +447,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
@@ -900,6 +915,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: docs/openlayers.api.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openlayers/docs/Attic/openlayers.api.php,v
retrieving revision 1.2.2.5
diff -u -p -r1.2.2.5 openlayers.api.php
--- docs/openlayers.api.php	2 Jun 2010 23:13:40 -0000	1.2.2.5
+++ docs/openlayers.api.php	4 Jul 2010 23:47:01 -0000
@@ -282,3 +282,33 @@ function hook_openlayers_presets() {
   );
   return array('default' => $default);
 }
+
+/**
+ * CTools Registration Hook (Style Plugins)
+ *
+ * IMPORTANT:
+ *
+ * In order to support style plugins, the first step is to
+ * tell CTools where to find the plugin.
+ *
+ * This function is just an example implementation of 
+ * hook_ctools_plugin_directory() and should be alter according to
+ * your module's name.
+ *
+ * For an example, please see the openlayers_test.module
+ *
+ * @param $module
+ *   Name of a module that supports CTools exportables.
+ * @param $plugin
+ *   Name of the kind of plugin supported.
+ * @return
+ *  If $module is 'openlayers', and $api is a type of exportable that
+ *  your module provides, and you are using Openlayers 2.x, then
+ *  return the directory relative to a module to look for this
+ *  particular plugin.
+ */
+function openlayers_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'openlayers' && $plugin == 'style_plugin') {
+    return 'plugins/style_plugin';
+  }
+}
\ No newline at end of file
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	4 Jul 2010 23:47:01 -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	4 Jul 2010 23:47:02 -0000
@@ -277,8 +277,33 @@ 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') {
+          // Clone object as parameters can change per style
+          var newContext = new
+            Drupal.openlayers.styleContext[map.styles[style].context_object](contextParams);
+          // Define parameters from plugin, if available
+          if (map.styles[style].context_plugin !== undefined) {
+            var contextParams = 
+              map.styles[style]['plugins'][map.styles[style].context_plugin];
+          }
+          else {
+            var contextParams = {};
+          }
+      
+          // Put together style
+          stylesAdded['default'] = new OpenLayers.Style(
+            map.styles[style],
+            { context: newContext }
+          );
+        }
+        else {
+          stylesAdded['default'] = new OpenLayers.Style(map.styles[style]);
+        }        
       }
+      
       return new OpenLayers.StyleMap(stylesAdded);
     }
     // Default styles
@@ -309,3 +334,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.8
diff -u -p -r1.1.2.8 openlayers_ui.styles.inc
--- modules/openlayers_ui/includes/openlayers_ui.styles.inc	29 Jun 2010 18:55:34 -0000	1.1.2.8
+++ modules/openlayers_ui/includes/openlayers_ui.styles.inc	4 Jul 2010 23:47:02 -0000
@@ -169,6 +169,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'),
@@ -189,8 +192,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',
@@ -205,6 +216,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',
@@ -218,10 +233,11 @@ function openlayers_ui_styles_form(&$for
  */
 function openlayers_ui_styles_form_submit(&$form, &$form_state) {
   $style_data = $form_state['values']['style_data'];
-  
+  $data = $form_state['values']['data'];
+
   // Cast and unset values so JS can handle them better
-  foreach ($form_state['values']['data'] as $key => $value) {
-    if ($form_state['values']['data'][$key] === '') {
+  foreach ($data as $key => $value) {
+    if ($data[$key] === '') {
       unset($form_state['values']['data'][$key]);
     }
     elseif (isset($style_data[$key]['type'])) {
@@ -236,6 +252,18 @@ function openlayers_ui_styles_form_submi
     }
   }
   
+  // Process style plugins.  This allows us to have simpler
+  // arrays for plugins.  
+  foreach ($data['plugins'] as $plugin => $settings) {
+    if ($settings['enabled']) {
+      $form_state['values']['data']['plugins'][$plugin] = 
+        isset($settings['options']) ? $settings['options'] : array();
+    }
+    else {
+      unset($form_state['values']['data']['plugins'][$plugin]);
+    }
+  }
+  
   $style = new stdClass();
   $style->name = $form_state['values']['name'];
   $style->title = $form_state['values']['title'];
@@ -255,6 +283,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])
+            ? $defaults['plugins'][$key] : 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	4 Jul 2010 23:47:02 -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: /cvs/drupal-contrib/contributions/modules/openlayers/tests/plugins/style_plugin/Attic/openlayers_test_test_plugin.inc,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 openlayers_test_test_plugin.inc
--- tests/plugins/style_plugin/openlayers_test_test_plugin.inc	4 Jul 2010 18:25:11 -0000	1.1.2.1
+++ tests/plugins/style_plugin/openlayers_test_test_plugin.inc	4 Jul 2010 23:47:02 -0000
@@ -29,8 +29,11 @@ class openlayers_style_plugin_test_plugi
     return array(
       'context_properties' => array(
         'fillOpacity' => 'getFillOpacity',
+        'pointRadius' => 'getpointRadius',
       ),
       'context_object' => 'openlayersExampleContext',
+      'point_radius_low' => 2,
+      'point_radius_high' => 10,
     );
   }
   
@@ -38,7 +41,26 @@ class openlayers_style_plugin_test_plugi
    * Options form.
    */
   function options_form($defaults = array()) {
-    return array();
+    $form = array();
+    
+    // Allow use to pick the high and low for random
+    // point radius
+    $form['point_radius_low'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Low Value'),
+      '#description' => t('Low value for the random point radius.'),
+      '#default_value' => isset($defaults['point_radius_low']) ?
+        $defaults['point_radius_low'] : 2,
+    );
+    $form['point_radius_high'] = array(
+      '#type' => 'textfield',
+      '#title' => t('High Value'),
+      '#description' => t('High value for the random point radius.'),
+      '#default_value' => isset($defaults['point_radius_high']) ?
+        $defaults['point_radius_high'] : 10,
+    );
+    
+    return $form;
   }
 
   /**
@@ -51,8 +73,9 @@ class openlayers_style_plugin_test_plugi
       $style->data[$prop] = '${' . $callback . '}';
     }
     
-    // Enable context
+    // Enable context and tell context about our plugin
     $style->data['context_object'] = $this->options['context_object'];
+    $style->data['context_plugin'] = 'openlayers_style_plugin_test_plugin';
     
     // Add JS
     drupal_add_js(drupal_get_path('module', 'openlayers_test') .
Index: tests/plugins/style_plugin/openlayers_test_test_plugin.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openlayers/tests/plugins/style_plugin/Attic/openlayers_test_test_plugin.js,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 openlayers_test_test_plugin.js
--- tests/plugins/style_plugin/openlayers_test_test_plugin.js	4 Jul 2010 18:25:11 -0000	1.1.2.1
+++ tests/plugins/style_plugin/openlayers_test_test_plugin.js	4 Jul 2010 23:47:02 -0000
@@ -8,9 +8,21 @@
 /**
  * Global variable for context styling.
  */
-Drupal.openlayers.styleContext.openlayersExampleContext = {
-  'getFillOpacity': function(feature) {
+Drupal.openlayers.styleContext.openlayersExampleContext = function(parameters) {
+  this.parameters = parameters;
+
+  // Fill opacity context.  Sets random fill opacity.
+  this.getFillOpacity = function(feature) {
     // Random fill opacity
     return Math.random();
-  }
+  };
+  
+  // Point radius context.  Given paramters, gets a random
+  // pointRadius.
+  this.getpointRadius = function(feature) {
+    // Not working
+    // var high = this.params.point_radius_high;
+    // var low = this.params.point_radius_low;
+    return Math.floor((Math.random() * 12) + 5);
+  };
 };
\ No newline at end of file
