0

How to handle mouse wheel event?

asked 2010-02-02 08:37:50 +0800

enixser gravatar image enixser
185 3

Hi,

I created my own widget by deriving from Groupbox class and creating some subsequent components in the onCreate method. Everything works fine so far. The only thing I did not get working is to get my widget notified that the user scrolled with the mouse wheel (as in Google Maps or Google Calendar).

I tried to define a new event with addClientEvent for "onMouseWheel" and then add an event listener for this new event, but I do not know what I have to do on the client side.

Are there any examples how to get user defined events working?

Best regards,
Ralf.

delete flag offensive retag edit

8 Replies

Sort by ยป oldest newest

answered 2010-02-02 20:21:13 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

updated 2010-02-02 21:17:49 +0800

Hi, enixser
ZK is not support onMouseWheel

I write a sample component

Myimg.js

mycomp.Myimg = zk.$extends(zul.Widget, {

    $define: {
        src: function(v){
            var n = this.$n();
            if (n) 
                n.src = v || '';
        }
    },
    
    bind_: function(){// after compose
        this.$supers('bind_', arguments);
        
        var node = this.$n();
        if (node.addEventListener) 
            node.addEventListener('DOMMouseScroll', this.proxy(this.wheel), false);
        
        node.onmousewheel = this.proxy(this.wheel);
    },
    
    domAttrs_: function(no){
        var attr = this.$supers('domAttrs_', arguments);
        if (!no || !no.content) 
            attr += ' src="' + (this._src || '') + '"';
        return attr;
    },
    
    redraw: function(out){
        out.push('<img', this.domAttrs_(), '/>');
    },
    
     zoom: function(range){
        var node = jq(this.$n()),
              w = node.width(),
              h = node.height();

        node.width(w * range);
        node.height(h * range);
        this.fire("onMouseWheel",{data: });
    },
	
	
    handle: function(delta){		
        if (delta < 0) 
            this.zoom(0.7)
        else 
            this.zoom(1.2)
    },
    
    wheel: function(event){
        var delta = 0;
        if (!event) /* For IE. */ 
            event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
            delta = event.wheelDelta / 120;
            if (window.opera) 
                delta = -delta;
        }
        else 
            if (event.detail) {
                delta = -event.detail / 3;
            }
        if (delta) 
            this.handle(delta);
        if (event.preventDefault) 
            event.preventDefault();
        event.returnValue = false;
    }    
});

MyImg.java

public class MyImg extends XulElement{
	
	private String _src;
	
	
	static {		
		addClientEvent(MyImg.class, "onMouseWheel", CE_REPEAT_IGNORE);
	}
		
	public String getSrc() {
		return _src;
	}

	public void setSrc(String src) {
		if (src != null && src.length() == 0)
			src = null;

		_src = src;
		smartUpdate("src", new EncodedURL());
	}

	private class EncodedURL implements org.zkoss.zk.ui.util.DeferredValue {
		public Object getValue() {
			return getEncodedURL();
		}
	}
	
	private String getEncodedURL() {
		return getDesktop().getExecution().encodeURL(_src);
	}
	
	public void service(org.zkoss.zk.au.AuRequest request, boolean everError) {
		final String cmd = request.getCommand();		

		if (cmd.equals("onMouseWheel")) {
			final JSONArray data = (JSONArray) request.getData().get("data");
			Events.postEvent(new Event(cmd, this, data.get(0)));
			
		} else super.service(request, everError);
	}
	
	protected void renderProperties(org.zkoss.zk.ui.sys.ContentRenderer renderer)
			throws IOException {
		super.renderProperties(renderer);					
		renderer.render("src", getEncodedURL());		
	}
 }

test.zul

<zk>
	<myimg src="item1.jpg" onMouseWheel='alert("zoom = " + event.data);'/>
</zk>

link publish delete flag offensive edit

answered 2010-02-03 00:34:10 +0800

enixser gravatar image enixser
185 3

Hi as1225,

where do I specify where to find the JS file? I added the following code to the lang-addon.xml file:

<component>
<extends>groupbox</extends>
<component-name>mycomp</component-name>
<component-class>de.efgapp.zk.components.MyComp</component-class>
</component>

After that I can use the new component in a ZUL file like this:

<mycomp id="c1" title="Test Box" />

Where do I have to put the JS file and how does ZK (or my new component) knows of its existence?

link publish delete flag offensive edit

answered 2010-02-03 01:06:09 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

You can refer to here

link publish delete flag offensive edit

answered 2010-02-03 01:28:32 +0800

enixser gravatar image enixser
185 3

The example is not clear to me. The example specifies a JS file in the <mold-uri> tag, but the description speaks about a JSP file. So, I guess it's a typo in the example.
The description says that <widget-class> specifies a JavaScript class. The example seems more to be a Java class.

It seems that I'm missing a small piece of information to get it working :-(

If I have the component definition as shown above and I put your JS code (changed to fit my component name) in a file named mycomp.js in the directory /WebContent/scripts in my Eclipse project, how do I have to tell ZK where to find this JS file and that it belongs to my component?

link publish delete flag offensive edit

answered 2010-02-04 19:50:30 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

What version of ZK do you used ?

link publish delete flag offensive edit

answered 2010-02-08 23:46:19 +0800

enixser gravatar image enixser
185 3

I use ZK 5.0-RC2.

link publish delete flag offensive edit

answered 2010-02-09 18:51:55 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

ZK is not support onMouseWheel
My sample is write a new component
You can refer to ZK5: Component Development Guide
or you can post a feature request

link publish delete flag offensive edit

answered 2015-11-25 11:27:01 +0800

WilliamB gravatar image WilliamB
1609 1 6

updated 2015-11-25 14:30:46 +0800

Not sure how to post a feature request on the new bug tracker, seems I can only consult.

So the event is still not supported ?

Found this explained on stackoverflow : http://stackoverflow.com/questions/9158411/handle-mousescroll-in-zk

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-02-02 08:37:50 +0800

Seen: 893 times

Last updated: Nov 25 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More