Code Monkey home page Code Monkey logo

propagating-hammerjs's Introduction

propagating-hammerjs

Extend hammer.js (v2) with event propagation.

We use the @egjs/hammerjs fork because hammer.js is not maintained anymore.

Features

  • Events emitted by hammer will propagate in order from child to parent elements.
  • Events are extended with a function event.stopPropagation() to stop propagation to parent elements.
  • Events are extended with a property event.firstTarget containing the element where a gesture started.
  • Supports changing and rearranging the HTML DOM on the fly.
  • Load via commonjs, AMD, or as a plain old JavaScript file.

Install

npm install @egjs/hammerjs propagating-hammerjs

Load

Browser

<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/@egjs/hammerjs@latest/dist/hammer.js"></script>
  <script src="https://unpkg.com/propagating-hammerjs@latest/propagating.js"></script>
  <script>
    function init() {
      var hammer = propagating(new Hammer(element));
    }
  </script>
</head>
<body>
</body>
</html>

Commonjs (e.g. Node.js, Browserify)

var Hammer = require('@egjs/hammerjs');
var propagating = require('propagating-hammerjs');

function init() {
  var hammer = propagating(new Hammer(element));
}

ESM (e.g. ES6, typescript)

import Hammer from '@egjs/hammerjs';
import propagating from 'propagating-hammerjs';

function init() {
  const hammer = propagating(new Hammer(element));
}

Use

To extend individual hammer.js instances with event propagation:

var hammer = propagating(new Hammer(element));

To extend the global hammer.js constructor

Hammer = propagating(Hammer);

Examples

Here a basic usage example. More examples are available in the folder /examples.

<!DOCTYPE html>
<html>
<head>
  <script src="node_modules/@egjs/hammerjs/dist/hammer.js"></script>
  <script src="node_muludes/propagating-hammerjs/propagating.js"></script>
  <style>
    div     {border: 1px solid black;}
    #parent {width: 400px; height: 400px; background: lightgreen;}
    #child  {width: 200px; height: 200px; background: yellow; margin: 10px;}
  </style>
</head>
<body>

<div id="parent">
  parent
  <div id="child">
    child
  </div>
</div>

<script>
  var parent = document.getElementById('parent');
  var hammer1 = propagating(new Hammer(parent))
      .on('tap', function (event) {
        alert('tap on parent');
      });

  var child = document.getElementById('child');
  var hammer2 = propagating(new Hammer(child))
      .on('tap', function (event) {
        alert('tap on child');

        // stop propagation from child to parent
        event.stopPropagation();
      });
</script>
</body>
</html>

API

Construction:

propagating(hammer: Hammer.Manager, options?: {
  preventDefault?: true | 'mouse' | 'touch' | 'pen'
}): Hammer.Manager

parameters

  • hammer: Hammer.Manager An hammer instance or the global hammer constructor.

  • options: Object An optional object with options. Available options:

    • preventDefault: true | 'mouse' | 'touch' | 'pen'. Optional. Enforce preventing the default browser behavior. Cannot be set to false.

returns

Returns the same hammer instance with extended functionality.

events

The emitted hammer.js events are extended with:

  • event.stopPropagation()

    If called, the event will not further propagate the elements parents.

  • event.firstTarget

    Contains the HTML element where a gesture started (where as event.target contains the element where the pointer is right now).

Develop

To generate the UMD bundle for commonjs and browser, run:

npm run build

License

MIT

propagating-hammerjs's People

Contributors

josdejong avatar mojoaxel avatar sgparrish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

propagating-hammerjs's Issues

Anyway to run client side with Meteor

Hi,

as I understand it I can't run NPM modules client-side in Meteor. As this code has to run client-side I am a bit stuck.

Any hints (hacks) on how I could run this to help stop "press" events propagating back up the DOM, pls?

ps. I did try just including propagating.js in the client side folders but get an error propagating is not defined

any help appreciated, pls

Doesn't work when using the jquery.hammer plugin

How can I get this to work with the jquery plugin for hammer 2.0?

If I use the hammer plugin propagation doesn't work. If I don't use the hammer plugin hammer propagation does work with your propagation.js script

Propagation is not stopped for dynamically added items

Hi, I'm working on react app (in fact preact but doesn't matter so much) and I add points inside of some plane(<div />). Points and the plane can be dragged independently with mouse pointer. So I have to stopPropagation on a point to prevent pan event propagate up to the plane and prevent it to be shifted too. So if I add a new point dynamically - stopPropagation doesn't work on newly added point. After refresh page - it works just fine.

I would happy to make live example, please just let me know if you still maintain this repo and I'll provide you reproduced issue.

panmove is not work when swipe first time in device of android

I am facing issue with "panmove". when i swipe bottom to top and top to bottom its not work first time. I have used Hammerjs with backbonejs hybrid mobile app. I have make a bottom penal when it swipe we do some more option. but its not work properly. when i swipe second time it work fine. at first time i am not getting panmove event same thing i am getting in ios device.

this.$el.html("#div")
this.$el.hammer();
this.$el.data('hammer').get('pan').set({ threshold: 5 });
this.initHeight = -(this.$el.outerHeight() - this.$('.swiper-container').outerHeight());
this.estimateTopPosition = this.$el.position().top;

//This function call on panup, pandown, panstart, panmove, panend, pancancel, panright, panleft, tap, press event
fareContainerSwipe: function (e) {
	var swipeType = e.type;
	var speed = 500;
	console.log(e.type + ', ' + e.gesture.offsetDirection + ', ' + e.gesture.direction + ', ' + e.gesture.distance + ', deltaY: ' + e.gesture.deltaY);
	var distance = e.gesture.distance;
	var nav = this.$el;
	var transInitHeight = this.transInitHeight;
	var currentPosition = this.$el.position();
	
	
	var mDistance;

	if (swipeType == 'panmove' && (e.gesture.direction == 16)) { //Pan down

	}else if (swipeType == 'panmove' && e.gesture.direction == 8) {    //Pan up

	}
} 

Multitouch does not work

With original hammerjs, i can pan multi elements at same time.
But with propagating() multi touch does not work, can pan only one element at a time

Use apply() in stopPropagation wrapper

Hello,

I am using vis.js (and consequently propagating-hammerjs) in conjunction with https://github.com/Pomax/react-onclickoutside. I am getting the following error for click events.

Chrome: Uncaught TypeError: Illegal invocation
Firefox: TypeError: 'stopPropagation' called on an object that does not implement interface Event.

I believe the following line should be changed to

srcStop.apply(event.srcEvent);

I didn't bother creating a PR since this is a very minor change.

needs domEvents:true? but this causes issues

Hi,

is the domEvents setting required to be set to true? i have been unable to get propagating to work without this setting (no events being triggered at all..)

but once I set this setting to true other issues arise from the fact that I hammer() nested elements and if an event gets triggered on a child and a parent is also hammer()ed, the event gets triggered twice..

perhaps you have any experience and suggestions with getting propagating to work without domEvents:true? I see in your examples that this is not needed but as soon as I plug it into our code no events are being triggered until I set domEvents to true..

Thanks so much!
Anna

Way to emit events in test code that is compatible with propagating-hammerjs

I started using propagating-hammerjs which works fine for my app but broke my tests. I realized I could fix my failing tests by emitting an additional event before each simulated event with something like the following:

hammerInstance.emit('hammer.input', {target: event.target, isFirst: true})
hammerInstance.emit(eventType, event)

I'm curious if this is the recommended way to simulate events in a propagating-hammerjs compatible way? Or is there a better way to simulate events that will work out of the box? or is there a way that propagating-hammerjs could be improved in such a way that the requisite setup for simulated events will have happened without manually needing to do it?

export also as ESM module

We get the following waning in an Angular project using keychartm (for vis-timeline):

WARNING in node_modules/vis-timeline/esnext/esm/vis-timeline-graph2d.js depends on 'propagating-hammerjs'.
CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

It would be helpfull to also export a ESM module.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.