Giter Club home page Giter Club logo

ng2-dragula's Introduction

Logo

Drag and drop so simple it hurts

Official Angular2 wrapper for dragula.

Demo

Demo

Try out the demo!

Install

You can get it on npm.

npm install ng2-dragula --save

Setup

You'll need to add DragulaService to your components viewProviders and also add Dragula to your components directives.

@Component({
  selector: 'sample',
  directives: [Dragula],
  viewProviders: [DragulaService],
  template:`
  <div>
    <div class='wrapper'>
      <div class='container' [dragula]='"first-bag"'>
        <div>You can move these elements between these two containers</div>
        <div>Moving them anywhere else isn't quite possible</div>
        <div>There's also the possibility of moving elements around in the same container, changing their position</div>
      </div>
      <div class='container' [dragula]='"first-bag"'>
        <div>This is the default use case. You only need to specify the containers you want to use</div>
        <div>More interactive use cases lie ahead</div>
        <div>Make sure to check out the <a href='https://github.com/bevacqua/dragula#readme'>documentation on GitHub!</a></div>
      </div>
    </div>
  </div>
  `
})
class Sample {}

Usage

This package isn't very different from dragula itself. I'll mark the differences here, but please refer to the documentation for dragula if you need to learn more about dragula itself.

Directive

There's a dragula directive that allows you to group containers together. That grouping of containers is called a bag.

<div [dragula]='"bag-one"'></div>
<div [dragula]='"bag-one"'></div>
<div [dragula]='"bag-two"'></div>

dragulaModel

If your ngFor is compiled from array, you may wish to have it synced. For that purpose you need to provide model by setting the dragulaModel attribute on the bag element.

<ul>
  <li *ngFor="#item of items" [dragula]='"bag-one"' [dragulaModel]='items'></li>
</ul>

The standard drop event is fired before the model is synced. For that purpose you need to use the dropModel. The same behavior exists in the remove event. Therefore is the removeModel event. Further details are available under Events

drake options

If you need to configure the drake (there's only one drake per bag), you can use the DragulaService.

class ConfigExample {
  constructor(private dragulaService: DragulaService) {
    dragulaService.setOptions('third-bag', {
      removeOnSpill: true
    });
  }
}

Events

Whenever a drake instance is created with the dragula directive, there are several events you can subscribe to via DragulaService. Each event emits an Array where the first item is the name of the bag. The remaining items depend on the event. The sample below illustrates how you can use destructuring to assign the values from the event. Refer to: https://github.com/bevacqua/dragula#drakeon-events

<div [dragula]='"evented-bag"'></div>
export class EventExample {
  
  constructor(private dragulaService: DragulaService) {
    dragulaService.drag.subscribe((value) => {
      console.log(`drag: ${value[0]}`);
      this.onDrag(value.slice(1));
    });
    dragulaService.drop.subscribe((value) => {
      console.log(`drop: ${value[0]}`);
      this.onDrop(value.slice(1));
    });
    dragulaService.over.subscribe((value) => {
      console.log(`over: ${value[0]}`);
      this.onOver(value.slice(1));
    });
    dragulaService.out.subscribe((value) => {
      console.log(`out: ${value[0]}`);
      this.onOut(value.slice(1));
    });
  }
  
  private onDrag(args) {
    let [e, el] = args;
    // do something
  }
  
  private onDrop(args) {
    let [e, el] = args;
    // do something
  }
  
  private onOver(args) {
    let [e, el, container] = args;
    // do something
  }
  
  private onOut(args) {
    let [e, el, container] = args;
    // do something
  }
}

Special Events for ng2-dragula

Event Name Listener Arguments Event Description
dropModel bagName, el, target, source same as normal drop, but model was synced, just available with the use of dragulaModel
removeModel bagName, el, container same as normal remove, but model was synced, just available with the use of dragulaModel

DragulaService

This service exposes a few different methods with which you can interact with dragula.

dragulaService.add(name, drake)

Creates a bag identified by name. You should provide the entire drake instance. Typically, the directive takes care of this step.

dragulaService.setOptions(name, options)

Sets the options used to instantiate a drake. Refer to the documentation for dragula to learn more about the options themselves.

dragulaService.find(name)

Returns the bag for a drake instance. Contains the following properties.

  • name is the name that identifies the bag under scope
  • drake is the raw drake instance itself

dragulaService.destroy(name)

Destroys a drake instance named name.

Contributing

Please see the CONTRIBUTING file for guidelines.

License

MIT

Awesome thank you's

Project setup is based on the excellent angular2-seed by Minko Gechev.

ng2-dragula's People

Contributors

nathanwalker avatar valorkin avatar

Watchers

 avatar  avatar

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.