SlideShare a Scribd company logo
1 of 24
Making stuff draggable in
Plunker (and other places)
Creating a drag and drop directive for
Angular.js
CodeMentor – 9 March 2015
Geoff Goodman (@filearts)
Who is this guy?
Hi, I’m Geoff Goodman
Twitter: @filearts
Github: @ggoodman
During the day, I value businesses at
EY (Ernst & Young)
At night, I build Plunker which is an in-browser
editor for front-end code.
• Like I said, Plunker is an in-browser code
editor
• A code editor is only half done if you can’t
drag things around…
FAIL
Prior art
• https://github.com/codef0rmer/angular-
dragdrop - Wraps jQuery UI
• https://github.com/a5hik/ng-sortable -
Comprehensive, dependency-free list
reordering and transferral
• https://github.com/logicbomb/lvlDragDrop -
Also low-level, dependency-free drag and
drop based on html5 api
Figuring out what I needed
• I know that I have things that I want to drag
• I know that there are only certain places to
drop stuff
• I know that only certain things can be dropped
in certain places
Making this in Angular.js
• Angular lets me define how I want certain
things to behave straight in the markup
– These are called directives
– They are not the easiest thing to learn and
understand
– But they are crazy powerful
• HTML5 also defines a great API for drag and
drop (Spec)
Overcome API limitations
• The drag and drop pioneer is now a black
sheep
• IE only allows Text and URL as the data types
• Using our own layer on top, we can support
any data
Angular internals
• Three directives:
1. drag-container
2. drop-container
3. drop-area
• And one service to share drag state between
the directives: $dragging.
What I came up with:
<div drag-container="model" mime-type="text/whatever">
<!-- Container that is draggable -->
</div>
<div drop-container accepts="['text/x-plunker-file']">
<div drop-target="left" on-drop=“dropL(data)"></div>
<div drop-target="right" on-drop=“dropR(data)"></div>
<!-- You can put any markup here -->
</div>
drag-container
• Determines the data being dragged (drag-
container attribute) and the type of that data
(mime-type attribute)
• Call preventDefault() on the dragstart
event and the browser will start dragging! In
the event handler, I set the drag data.
<div drag-container="model" mime-type="text/whatever">
<!-- Container that is draggable -->
</div>
drop-container
• Defines a region that will accept any drop that
passes the ‘accept’ test.
• Accepts event handlers, figures out
appropriate target and delegates event.
<div drop-container accepts="['text/x-plunker-file']">
<div drop-target="left" on-drop=“dropL(data)"></div>
<div drop-target="right" on-drop=“dropR(data)"></div>
<!-- You can put any markup here -->
</div>
Delegating to targets
• Create a virtual point for each target
• Calculate minimum distance b/w mouse and
point
top top-right
right
bottom-rightbottombottom-left
left
top-left
center
drop-target
• Defines a potential region of the drop-
container that can respond to drag and
drop events
• Can by styled by css to give drop hints
<div drop-container accepts="['text/x-plunker-file']">
<div drop-target="left" on-drop=“dropL(data)"></div>
<div drop-target="right" on-drop=“dropR(data)"></div>
<!-- You can put any markup here -->
</div>
Linking the directives up
• drop-targets require an instance of a
drop-container in their DDO:
.directive("dropTarget", ["$parse", function ($parse) {
return {
restrict: "A",
require: ["^dropContainer", "dropTarget"],
controller: "DropTargetController",
controllerAs: "dropTarget",
link: function ($scope, $element, $attrs, ctrls) {
var dropContainer = ctrls[0];
var dropTarget = ctrls[1];
var anchor = $attrs.dropTarget || "center";
dropContainer.addDropTarget(anchor, dropTarget);
drop-targets are just attachment
points
• They are not designed to have content
• When active, an -active css class is added
so that they can: e.g. be shown or given
dashed borders
• Creating them gives you regions of the drop-
container that can respond to events that
relate only to that region
Event handling
• HTML5 gives us a bunch of events. I chose a
few to expose:
– on-drag-start, on-drag-end
– on-drag-enter, on-drag-leave
– on-drag-over
– on-drop
• Angular can inject special objects exposed to
those handlers, like $event and data
<div drop-container
accepts="['text/x-dragular-piece']"
on-drag-enter="game.swap($index, data)">
What this let’s you do
• Trello-style storyboards lists: DEMO
• Re-orderable lists by:
– Having a leading and trailing drop-target for each
list item
– Splicing before or after the target element on-
drag-enter
• Sliding puzzle: DEMO
• More complex behaviours: DEMO
Caveats
• Determining if something is draggable must
be synchronous
• This means zip-building
But what about the code?
• The code is MIT-licensed on github:
https://github.com/ggoodman/angular-drag-
drop
• Feel free to ping me if you have questions on
Twitter: @filearts or @plnkrco
One more thing…
• With a hack here and some duct tape there…
DEMO
THANKS FOR LISTENING!

More Related Content

Viewers also liked

membuat grafis dengan PHP
membuat grafis dengan PHPmembuat grafis dengan PHP
membuat grafis dengan PHPdetal 31392
 
Writing Reusable Web Components with jQuery and jQuery UI
Writing Reusable Web Components with jQuery and jQuery UIWriting Reusable Web Components with jQuery and jQuery UI
Writing Reusable Web Components with jQuery and jQuery UIYnon Perek
 
Cyberdrama and digital drama
Cyberdrama and digital dramaCyberdrama and digital drama
Cyberdrama and digital dramaSue Davis
 
Developing R Graphical User Interfaces
Developing R Graphical User InterfacesDeveloping R Graphical User Interfaces
Developing R Graphical User InterfacesSetia Pramana
 
5 tips for your HTML5 games
5 tips for your HTML5 games5 tips for your HTML5 games
5 tips for your HTML5 gamesErnesto Jiménez
 
Node js presentation
Node js presentationNode js presentation
Node js presentationmartincabrera
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS ArchitectureEyal Vardi
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsVikash Singh
 
A Product Manager's Job
A Product Manager's JobA Product Manager's Job
A Product Manager's Jobjoshelman
 
The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017LinkedIn
 
TEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of WorkTEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of WorkVolker Hirsch
 

Viewers also liked (13)

membuat grafis dengan PHP
membuat grafis dengan PHPmembuat grafis dengan PHP
membuat grafis dengan PHP
 
Writing Reusable Web Components with jQuery and jQuery UI
Writing Reusable Web Components with jQuery and jQuery UIWriting Reusable Web Components with jQuery and jQuery UI
Writing Reusable Web Components with jQuery and jQuery UI
 
Cyberdrama and digital drama
Cyberdrama and digital dramaCyberdrama and digital drama
Cyberdrama and digital drama
 
Developing R Graphical User Interfaces
Developing R Graphical User InterfacesDeveloping R Graphical User Interfaces
Developing R Graphical User Interfaces
 
5 tips for your HTML5 games
5 tips for your HTML5 games5 tips for your HTML5 games
5 tips for your HTML5 games
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
 
Protractor: Tips & Tricks
Protractor: Tips & TricksProtractor: Tips & Tricks
Protractor: Tips & Tricks
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
reveal.js 3.0.0
reveal.js 3.0.0reveal.js 3.0.0
reveal.js 3.0.0
 
A Product Manager's Job
A Product Manager's JobA Product Manager's Job
A Product Manager's Job
 
The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017
 
TEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of WorkTEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of Work
 

More from Arc & Codementor

Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcRemote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcArc & Codementor
 
Introduction to Python for Data Science
Introduction to Python for Data ScienceIntroduction to Python for Data Science
Introduction to Python for Data ScienceArc & Codementor
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementorArc & Codementor
 
20 iOS developer interview questions
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questionsArc & Codementor
 
29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview QuestionsArc & Codementor
 
37 Java Interview Questions
37 Java Interview Questions37 Java Interview Questions
37 Java Interview QuestionsArc & Codementor
 
21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview QuestionsArc & Codementor
 
Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015Arc & Codementor
 
How to Build Your App from Scratch
How to Build Your App from ScratchHow to Build Your App from Scratch
How to Build Your App from ScratchArc & Codementor
 
Angular meteor for angular devs
Angular meteor for angular devsAngular meteor for angular devs
Angular meteor for angular devsArc & Codementor
 
Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1Arc & Codementor
 
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...Arc & Codementor
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Arc & Codementor
 
Python Internals Optimization Choices Made - Codementors Office Hours with St...
Python Internals Optimization Choices Made - Codementors Office Hours with St...Python Internals Optimization Choices Made - Codementors Office Hours with St...
Python Internals Optimization Choices Made - Codementors Office Hours with St...Arc & Codementor
 

More from Arc & Codementor (15)

Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcRemote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
 
Introduction to Python for Data Science
Introduction to Python for Data ScienceIntroduction to Python for Data Science
Introduction to Python for Data Science
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementor
 
20 iOS developer interview questions
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questions
 
29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions
 
37 Java Interview Questions
37 Java Interview Questions37 Java Interview Questions
37 Java Interview Questions
 
21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions
 
Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015
 
How to Build Your App from Scratch
How to Build Your App from ScratchHow to Build Your App from Scratch
How to Build Your App from Scratch
 
Angular meteor for angular devs
Angular meteor for angular devsAngular meteor for angular devs
Angular meteor for angular devs
 
Tmux tips and_tricks
Tmux tips and_tricksTmux tips and_tricks
Tmux tips and_tricks
 
Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1
 
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
 
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
 
Python Internals Optimization Choices Made - Codementors Office Hours with St...
Python Internals Optimization Choices Made - Codementors Office Hours with St...Python Internals Optimization Choices Made - Codementors Office Hours with St...
Python Internals Optimization Choices Made - Codementors Office Hours with St...
 

Recently uploaded

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 

Recently uploaded (20)

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 

Codementor Plunker AngularJS Office Hours - Making things draggable in plunker

  • 1. Making stuff draggable in Plunker (and other places) Creating a drag and drop directive for Angular.js CodeMentor – 9 March 2015 Geoff Goodman (@filearts)
  • 2. Who is this guy? Hi, I’m Geoff Goodman Twitter: @filearts Github: @ggoodman During the day, I value businesses at EY (Ernst & Young) At night, I build Plunker which is an in-browser editor for front-end code.
  • 3. • Like I said, Plunker is an in-browser code editor • A code editor is only half done if you can’t drag things around…
  • 4.
  • 6.
  • 7. Prior art • https://github.com/codef0rmer/angular- dragdrop - Wraps jQuery UI • https://github.com/a5hik/ng-sortable - Comprehensive, dependency-free list reordering and transferral • https://github.com/logicbomb/lvlDragDrop - Also low-level, dependency-free drag and drop based on html5 api
  • 8. Figuring out what I needed • I know that I have things that I want to drag • I know that there are only certain places to drop stuff • I know that only certain things can be dropped in certain places
  • 9. Making this in Angular.js • Angular lets me define how I want certain things to behave straight in the markup – These are called directives – They are not the easiest thing to learn and understand – But they are crazy powerful • HTML5 also defines a great API for drag and drop (Spec)
  • 10. Overcome API limitations • The drag and drop pioneer is now a black sheep • IE only allows Text and URL as the data types • Using our own layer on top, we can support any data
  • 11. Angular internals • Three directives: 1. drag-container 2. drop-container 3. drop-area • And one service to share drag state between the directives: $dragging.
  • 12. What I came up with: <div drag-container="model" mime-type="text/whatever"> <!-- Container that is draggable --> </div> <div drop-container accepts="['text/x-plunker-file']"> <div drop-target="left" on-drop=“dropL(data)"></div> <div drop-target="right" on-drop=“dropR(data)"></div> <!-- You can put any markup here --> </div>
  • 13. drag-container • Determines the data being dragged (drag- container attribute) and the type of that data (mime-type attribute) • Call preventDefault() on the dragstart event and the browser will start dragging! In the event handler, I set the drag data. <div drag-container="model" mime-type="text/whatever"> <!-- Container that is draggable --> </div>
  • 14. drop-container • Defines a region that will accept any drop that passes the ‘accept’ test. • Accepts event handlers, figures out appropriate target and delegates event. <div drop-container accepts="['text/x-plunker-file']"> <div drop-target="left" on-drop=“dropL(data)"></div> <div drop-target="right" on-drop=“dropR(data)"></div> <!-- You can put any markup here --> </div>
  • 15. Delegating to targets • Create a virtual point for each target • Calculate minimum distance b/w mouse and point top top-right right bottom-rightbottombottom-left left top-left center
  • 16. drop-target • Defines a potential region of the drop- container that can respond to drag and drop events • Can by styled by css to give drop hints <div drop-container accepts="['text/x-plunker-file']"> <div drop-target="left" on-drop=“dropL(data)"></div> <div drop-target="right" on-drop=“dropR(data)"></div> <!-- You can put any markup here --> </div>
  • 17. Linking the directives up • drop-targets require an instance of a drop-container in their DDO: .directive("dropTarget", ["$parse", function ($parse) { return { restrict: "A", require: ["^dropContainer", "dropTarget"], controller: "DropTargetController", controllerAs: "dropTarget", link: function ($scope, $element, $attrs, ctrls) { var dropContainer = ctrls[0]; var dropTarget = ctrls[1]; var anchor = $attrs.dropTarget || "center"; dropContainer.addDropTarget(anchor, dropTarget);
  • 18. drop-targets are just attachment points • They are not designed to have content • When active, an -active css class is added so that they can: e.g. be shown or given dashed borders • Creating them gives you regions of the drop- container that can respond to events that relate only to that region
  • 19. Event handling • HTML5 gives us a bunch of events. I chose a few to expose: – on-drag-start, on-drag-end – on-drag-enter, on-drag-leave – on-drag-over – on-drop • Angular can inject special objects exposed to those handlers, like $event and data <div drop-container accepts="['text/x-dragular-piece']" on-drag-enter="game.swap($index, data)">
  • 20. What this let’s you do • Trello-style storyboards lists: DEMO • Re-orderable lists by: – Having a leading and trailing drop-target for each list item – Splicing before or after the target element on- drag-enter • Sliding puzzle: DEMO • More complex behaviours: DEMO
  • 21. Caveats • Determining if something is draggable must be synchronous • This means zip-building
  • 22. But what about the code? • The code is MIT-licensed on github: https://github.com/ggoodman/angular-drag- drop • Feel free to ping me if you have questions on Twitter: @filearts or @plnkrco
  • 23. One more thing… • With a hack here and some duct tape there… DEMO

Editor's Notes

  1. I’m not really good with Powerpoint. Bear with my crappy black and white presentation and gif animations
  2. In case it wasn’t obvious, instead of dragging files, some useless selection range happened.
  3. In a modern editor, you should be able to drag files around in a file tree. (Show video) Also, wouldn’t it be handy if you could re-organize your workspace however you wanted? (Show video)
  4. Things to drag: files, directories, panes I also thought it would be great if you could drag files to and from the desktop But you can’t drag a folder into a file, you can’t drag a folder into a child folder etc.. You need to be able to define what can be dragged where
  5. Little segway into Angular (this won’t be a deep dive into Angular, sorry!) The HTML5 api is a little unintuitive to begin with, but is really powerful and actually quite simple to use! - Need to cancel events to opt-in
  6. A service is an instance of an object that can be injected into other bits of an angular application. A service is a singleton instance so injecting the $dragging service will always give me the same instance. I use this to store a reference to the object identified as being dragged. I can also set the full mime-type of the data being dragged to work around a quirk of IE’s.
  7. I also need to call some methods of the dataTransfer object to tell the browser what the mime-type of my data is and pass the browser the data itself. Even though IE pioneered this api, it has moved forward since then. In IE, you cannot set the type to anything but ‘text’ and ‘url’. Fortunately, by having a service instance
  8. So for the workspace of the next version of Plunker, I wanted people to be able to reorganize their workspace in a simple and intuitive way. A tiling window manager is an appropriate example of what I wanted. You can drag a file or pane into the workspace, split an existing pane horizontally, vertically or simply replace the pane depending on where you drop within the overall pane’s surface.
  9. For the plunker workspace, I have top, right, bottom and left targets that will trigger splits. I have a center target that will trigger a pane replace.
  10. The dr
  11. In Angular, you can use the $parse service to parse the value of your element’s attributes into a function that can be run on any scope-like object and another ‘locals’ object and will run the angular expression as if it was in the context defined by the $scope and locals. The injectable $event and data object are made available via this ‘locals’ concept. The library uses this mechanism for the events listed on this slide. Note that ‘on-drag-start’ and ‘on-drag-end’ are only available to ‘drag-containers’ whereas all the other events are only available on ‘drop-container’ and ‘drop-target’s.