0

I am using Openlayers on Drupal 7 and I'm trying to remove the key control so the person won't be able to use the key - and + to zoom.

for (map in Drupal.settings.openlayers.maps) {
    data = jQuery('#' + map).data('openlayers').openlayers;
    jQuery(data.controls).each(function(){
        data.removeControl((this));
    });
}

So with this code I delete all the controls, but in the end it's still working !

On the file openlayers_behavior_keyboarddefaults.js if I comment the line Drupal.openlayers.addControl(data.openlayers, 'KeyboardDefaults');

The keyboard zoom is not working !

In drupal there is an option witch can do that, but that not what I'm looking for.

If I don't find any solution I still can change the value in the DataBase but I would like not using this method, (I'd prefere to learn OL instead :))

Do you have any ideas ?

Thanks !

0

I finally found the solution after spending a lot of time in OpenLayers library. Basically the solution is to create a new behavior which will override the function OpenLayers.Control.KeyboardDefaults, after that you will have to comment the lines which use the - and + code keys, and in the end force to render the behavior.

This Solution works for Drupal 7

Create a new module and don't forget to add these 2 lines in the info file:

dependencies[] = openlayers
dependencies[] = geofield

The new behavior

function mymodule_openlayers_behaviors(){
  return array(
    'openlayers_behavior_OKC' => array(
      'title' => t('Override key control'),
      'description' => t('Remove the - and + zoom key'),
      'type' => 'behaviors',
      // 'file' => 'includes/openlayers_behavior_yop.inc',
      'behavior' => array(
        'class' => 'openlayers_behavior_OKC',
        'parent' => 'openlayers_behavior',
        'path' => drupal_get_path('module', 'mymodule') . '/includes',
        'file' => 'openlayers_behavior_OKC.inc',

      ),
    ),
  );
}

the file includes/openlayers_behavior_OKC.inc will call the js to override the function.

class openlayers_behavior_OKC extends openlayers_behavior {
  /**
   * Provide initial values for options.
   */
  function options_init() {
    return array(
      'keyboarddefaults' => '',
    );
  }

  // /**
  //  * Render.
  //  */

  function render(&$map) {
      drupal_add_js(drupal_get_path('module', 'mymodule').'/js/openlayers_behavior_OKC.js');
      return $this->options;
    }

}

after that you just need to rewrite the function OpenLayers.Control.KeyboardDefaults (I've comment the lines related to the + and - but you can choose or add new keys if you want to) Here is the file js/openlayers_behavior_OKC.js

/**
 * @file
 * JS Implementation of OpenLayers behavior.
 */

/**
 * Keyboard Defaults Behavior.  Implements the KeyboardDefaults OpenLayers
 * Control.
 */
OpenLayers.Control.KeyboardDefaults = OpenLayers.Class(OpenLayers.Control, {

    /**
     * APIProperty: autoActivate
     * {Boolean} Activate the control when it is added to a map.  Default is
     *     true.
     */
    autoActivate: true,

    /**
     * APIProperty: slideFactor
     * Pixels to slide by.
     */
    slideFactor: 75,

    /**
     * APIProperty: observeElement
     * {DOMelement|String} The DOM element to handle keys for. You
     *     can use the map div here, to have the navigation keys
     *     work when the map div has the focus. If undefined the
     *     document is used.
     */
    observeElement: null,

    /**
     * Constructor: OpenLayers.Control.KeyboardDefaults
     */

    /**
     * Method: draw
     * Create handler.
     */
    draw: function() {
        var observeElement = this.observeElement || document;
        this.handler = new OpenLayers.Handler.Keyboard( this,
                {"keydown": this.defaultKeyPress},
                {observeElement: observeElement}
        );
    },

    /**
     * Method: defaultKeyPress
     * When handling the key event, we only use evt.keyCode. This holds 
     * some drawbacks, though we get around them below. When interpretting
     * the keycodes below (including the comments associated with them),
     * consult the URL below. For instance, the Safari browser returns
     * "IE keycodes", and so is supported by any keycode labeled "IE".
     * 
     * Very informative URL:
     *    http://unixpapa.com/js/key.html
     *
     * Parameters:
     * evt - {Event} 
     */
    defaultKeyPress: function (evt) {
        var size, handled = true;
        switch(evt.keyCode) {
            case OpenLayers.Event.KEY_LEFT:
                this.map.pan(-this.slideFactor, 0);
                break;
            case OpenLayers.Event.KEY_RIGHT: 
                this.map.pan(this.slideFactor, 0);
                break;
            case OpenLayers.Event.KEY_UP:
                this.map.pan(0, -this.slideFactor);
                break;
            case OpenLayers.Event.KEY_DOWN:
                this.map.pan(0, this.slideFactor);
                break;

            case 33: // Page Up. Same in all browsers.
                size = this.map.getSize();
                this.map.pan(0, -0.75*size.h);
                break;
            case 34: // Page Down. Same in all browsers.
                size = this.map.getSize();
                this.map.pan(0, 0.75*size.h);
                break; 
            case 35: // End. Same in all browsers.
                size = this.map.getSize();
                this.map.pan(0.75*size.w, 0);
                break; 
            case 36: // Home. Same in all browsers.
                size = this.map.getSize();
                this.map.pan(-0.75*size.w, 0);
                break; 

/*            case 43:  // +/= (ASCII), keypad + (ASCII, Opera)
            case 61:  // +/= (Mozilla, Opera, some ASCII)
            case 187: // +/= (IE)
            case 107: // keypad + (IE, Mozilla)
                this.map.zoomIn();
                break; 
            case 45:  // -/_ (ASCII, Opera), keypad - (ASCII, Opera)
            case 109: // -/_ (Mozilla), keypad - (Mozilla, IE)
            case 189: // -/_ (IE)
            case 95:  // -/_ (some ASCII)
                this.map.zoomOut();
                break; */
            default:
                handled = false;
        }
        if (handled) {
            // prevent browser default not to move the page
            // when moving the page with the keyboard
            OpenLayers.Event.stop(evt);
        }
    },

    CLASS_NAME: "OpenLayers.Control.KeyboardDefaults"
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.