SlideShare a Scribd company logo
1 of 23
AngularJS 
for Interactive Application Behavior 
Brent Goldstein 
brent@kubilateam.com 
Practical Directives
Back Story 
● Building an app and want rich behavior 
○ Don’t limit goals because “it’s a web app” 
● Need an interactive Spreadsheet-like Grid 
○ Hierarchical data, specialized data entry needs 
○ Similarities to Google Sheets, some differences 
● Yikes, this seemed daunting at first 
○ Hierarchical Data Binding 
○ Navigation 
○ Complex element/range selection
Requirements - (be a product mgr) 
● Hierarchical Data Set 
o 3 Row Levels - Person, Projects, Tasks 
o Columns are calendar weeks 
● Navigate and Select like a Spreadsheet 
o Move w/ arrows, tab, shift-tab; mouse-click 
o Select single cell or range of cells 
● Specialized Editing 
o Business logic applied to keystrokes before display
How to Render Complex Data? 
● Resist thinking like an ‘engineer’ 
● Visual representation must be intuitive 
● Even more important that ‘being’ intuitive 
→ will someone actually use it? 
● Really must pass the dog food test
Digression - Dogfooding? 
● OK, this is one of many overused tech industry terms 
● Seems to put a rather unappetizing spin on one of the 
MOST IMPORTANT practices in application design 
● It does not matter what you think or say, or what others 
think or say about using your app 
● The application must draw you in (yes, YOU) 
● Otherwise, it’s just neat tech, and neat is not a product
Grid Hierarchy Rendering 
● Go “Flat” -- flat visual data modeling that is 
● Basically, de-normalize where it makes sense 
→ use grouping to organize 
● Greatly simplifies UI construction and interpretation 
● Still possible to “drill down”, but using filters and 
selection to show more/fewer rows 
● Avoid “nested grids”; hard to render, hard to read
So how to build it? 
<div grid-editor> 
<div class=’grid-person’> 
div div div div div div ….. div 
….. 
….. 
<div> 
….. 
<div> 
<div class=’grid-person’> 
<div> 
- Custom Directive “grid-editor” provides special behaviors 
- Grid-Editor looks for child DOM elements with special classes 
Note, grid constructed from divs, not tables, to support custom layout
DOM Location and Navigation 
Let’s consider one use case, Find the Next cell to the RIGHT 
<div grid-editor> 
<div> 
<div class=’grid-person’> 
<div> 
... 
<div class=’grid-project’> 
<div> 
<div class=’grid-project’> 
<div> 
1) At last cell, but not last in row 
2) At last cell in row 
3) At last cell/row in grid-project 
Actually requires handling several sub-cases...
Getting down to Business -- jqLite 
Assuming we know the selected cell, we can find the next like this: 
<div grid-editor> 
<div class=’grid-person’> 
<div class=’grid-project’> 
1) At last cell, but not last in row 
col=’1’ col=’2’ col=’3’ col=’4’ 
col=’5’ ... 
2) At last cell in row 
Case 1) Look for next sibling element, use attribute to be selective 
var newElement = currEle.next(‘[col]’); 
Case 2) Look for the FIRST element in the NEXT row (requires some maneuvering) 
var newElement = currEle.closest(‘grid-project’).next(‘grid-project’).find(‘.first’) 
Similar ‘traverse up, look for next’ can be applied to higher levels in hierarchy 
col=’6’ 
applied applied to first cell div: class=’first’ to every cell div: class=’cell’
Going down, same column, new row 
What about other cases, like the down arrow key? 
Need the next row, but column needs to remain the same. Now that’s why the 
“col=n” attribute is useful…. 
Look for next sibling row, use attribute to find the correct column 
var newElement = currEle.closest(‘grid-project’).next(‘grid-project’) 
.find(‘.cell[col=’ + currEle.attr(‘col’) + ‘]’); 
<div grid-editor> 
<div class=’grid-person’> 
<div class=’grid-project’> 
col=’1’ col=’2’ col=’3’ col=’4’ 
col=’5’ col=’6’ 
col=’4’
How to capture the arrow keys? 
Can be useful to step outside of Angular for event handling 
element.bind('keydown keypress', function (e){ 
if(!scope.editActive) return; // track the editing state 
var navArrow = function(e){ 
var arrowKeys = { 37:'left', 38:'up', 39:'right', 40:'down' }; 
return arrowKeys[e.which]; 
}; 
var navTab = function(e){ // tab key 
return (e.which === 9) ? (e.shiftKey?"left":"right") : null; 
}; 
var doNav = navFromArrow(e) || navFromTab(e); 
if(doNav){ 
e.preventDefault(); // prevent the browser default action 
theFunctionThatNavigatesToNextCell(doNav) 
} 
}; 
The ‘element’ above could be the $document root, or a sub-document, 
depending on the desired behavior. If a sub-doc, the keys will only be 
captured when that element or children have focus
Rendering the Selection Band 
To indicate selected cell, a “rectangle select band” 
is typically used to surround the cells 
How to accomplish this in DOM? 
A couple of options: 
1) Identify and alter the display the underlying 
DIVs 
2) Add new elements that Overlay the native grid
Draw band using actual grid cells? 
Altering the display of underlying cells can be tricky. 
How to actually make the highlight band? 
- Alter the div/span border(s) 
Not ideal. Positioning is limited to element border 
Also harder to make a multi-cell rectangle
Overlay Rendering 
What about creating a new element/set of 
elements that display ON TOP of the grid? 
Let’s timewarp back to 1998 when graphics 
were rendered into framebuffers. 
Remember the Overlay Framebuffer? 
Let’s apply the same logical concept
The Magic Select Rectangle Band 
How to render the select rectangle in DOM? 
We’ll construct with separate banding elements for max control 
It will simplify hover/select options since we can know exactly which side of the band is 
under the mouse 
Top DIV 
Bottom DIV 
Left Div 
Right Div 
With this approach we can also add other visual elements, like a cursor select target for 
dragging, etc.
Dynamic Element Creation 
The select band is managed by the GridEditor Directive: 
- Create: When the directive is instantiated 
- Move: When the arrow/tab keys are captured 
- Grow/Shrink: Based on shift-click 
This function is called inside the directive link() function to instantiate the band elements: 
var _installBand = function(){ 
var html = 
"<div id='magic-square'>"+ 
"<div class='band-horiz top'></div>" + 
"<div class='band-horiz bottom'></div>"+ 
"<div class='band-vert left'></div>"+ 
"<div class='band-vert right'></div>" + 
"<div class='band-grabbox'></div>" + 
"</div>"; 
_band = angular.element(html); // this creates the dom element 
_gridEditorElement.prepend(_band); // attach to directive element 
return _band; // to adjust properties and location later 
};
Dynamically alter DOM properties 
Properties are updated to adjust the rectangle band size 
when needed 
Note, _unitWidth is already set with the pixel width of a single cell 
var _setBandWidth = function(){ 
var rangeWidth = _range(); // gets the desired #cells to cover 
// update the top/bottom horizontal div widths 
_band.children(".band-horiz").width((_unitWidth-1) * rangeWidth); 
// update the right vertical div position 
var rightVert = _magicSquare.children(".band-vert.right"); 
var posLeft = (_unitWidth-1) * rangeWidth -1; 
rightVert.css({left: posLeft}); 
};
Let’s take a look - Initial View 
Top-level view, summary data 
User sees this view when page is loaded 
● Threshold coloring (red means too much) 
● Click on row to expand 
● Expand All/Collapse All buttons
Let’s take a look - Expanded View 
Details for each person visible when expanded 
● Project-task rows at same indent level 
● Grouping by project 
● Optional project sub-total rows
Let’s take a look - Editing 
Behaviors to streamline complex editing needs 
● Single Select and Range select 
● Smart Navigation across projects/people 
● Shortcut keys for pre-defined operations
Coding tidbit - $emit for messaging 
Both $scope.$broadcast() and $scope.$emit() can be used 
to communicate between directives and controllers 
What’s the difference? Well, direction: 
$emit goes UP, $broadcast goes DOWN 
As a result, $emit is more efficient bubbling only to parents 
$broadcast fans out to all child scopes 
Maybe you really need $broadcast... but often not 
$emit can be applied in a message-bus pattern to achieve 
efficient dedicated communication across an app
$emit for messaging 
Message bus approach can often replace $broadcast 
1) Bubble Up 2) Message Bus 
Higher level controller 
or directive 
$scope.$on(‘hello.world’, 
function(event){//dosomething)); 
$scope.$emit(‘hello.world’); 
Originating Controller or 
Directive 
RootScope actiing as 
message Bus 
$rootScope.$on(‘hello.world’, 
function(event){//dosomething)); 
$rootScope.$emit(‘hello.world’); 
Originating Controller or 
Directive 
Listening Directive or 
Controller
thanks! 
contact: brent@kubilateam.com

More Related Content

What's hot

Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Christian Lilley
 

What's hot (20)

Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
 
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
 
Angularjs architecture
Angularjs architectureAngularjs architecture
Angularjs architecture
 
Building a Startup Stack with AngularJS
Building a Startup Stack with AngularJSBuilding a Startup Stack with AngularJS
Building a Startup Stack with AngularJS
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Ext JS Architecture Best Practices - Mitchell Simeons
Ext JS Architecture Best Practices - Mitchell SimeonsExt JS Architecture Best Practices - Mitchell Simeons
Ext JS Architecture Best Practices - Mitchell Simeons
 
AngularJS best-practices
AngularJS best-practicesAngularJS best-practices
AngularJS best-practices
 
Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Introduction to VueJS & Vuex
Introduction to VueJS & VuexIntroduction to VueJS & Vuex
Introduction to VueJS & Vuex
 
AngularJs
AngularJsAngularJs
AngularJs
 
Building AngularJS Custom Directives
Building AngularJS Custom DirectivesBuilding AngularJS Custom Directives
Building AngularJS Custom Directives
 
AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS - What is it & Why is it awesome ? (with demos)AngularJS - What is it & Why is it awesome ? (with demos)
AngularJS - What is it & Why is it awesome ? (with demos)
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 
Why angular js Framework
Why angular js Framework Why angular js Framework
Why angular js Framework
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
 
Intro to AngularJs
Intro to AngularJsIntro to AngularJs
Intro to AngularJs
 

Similar to Angular.js Directives for Interactive Web Applications

elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
Spiros
 
Js info vis_toolkit
Js info vis_toolkitJs info vis_toolkit
Js info vis_toolkit
nikhilyagnic
 

Similar to Angular.js Directives for Interactive Web Applications (20)

The Future State of Layout
The Future State of LayoutThe Future State of Layout
The Future State of Layout
 
Designing and implementing_android_uis_for_phones_and_tablets
Designing and implementing_android_uis_for_phones_and_tabletsDesigning and implementing_android_uis_for_phones_and_tablets
Designing and implementing_android_uis_for_phones_and_tablets
 
Performance #3 layout&amp;animation
Performance #3  layout&amp;animationPerformance #3  layout&amp;animation
Performance #3 layout&amp;animation
 
Additional 3174 mp3174 - navisworks 2014 - quick reference guide
Additional 3174 mp3174 - navisworks 2014 - quick reference guideAdditional 3174 mp3174 - navisworks 2014 - quick reference guide
Additional 3174 mp3174 - navisworks 2014 - quick reference guide
 
Grids of Tomorrow: CSS Grid Layout
Grids of Tomorrow: CSS Grid LayoutGrids of Tomorrow: CSS Grid Layout
Grids of Tomorrow: CSS Grid Layout
 
D3.JS Tips & Tricks (export to svg, crossfilter, maps etc.)
D3.JS Tips & Tricks (export to svg, crossfilter, maps etc.)D3.JS Tips & Tricks (export to svg, crossfilter, maps etc.)
D3.JS Tips & Tricks (export to svg, crossfilter, maps etc.)
 
But what about old browsers?
But what about old browsers?But what about old browsers?
But what about old browsers?
 
What you need to know about FEMAP v11.3
What you need to know about FEMAP v11.3What you need to know about FEMAP v11.3
What you need to know about FEMAP v11.3
 
Start Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJSStart Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJS
 
Visualizing large data sets with wijmo enterprise webinar
Visualizing large data sets with wijmo enterprise webinarVisualizing large data sets with wijmo enterprise webinar
Visualizing large data sets with wijmo enterprise webinar
 
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)elm-d3 @ NYC D3.js Meetup (30 June, 2014)
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
 
CSS- Smacss Design Rule
CSS- Smacss Design RuleCSS- Smacss Design Rule
CSS- Smacss Design Rule
 
Sign in
Sign inSign in
Sign in
 
Goodbye Nightmare : Tops and Tricks for creating Layouts
Goodbye Nightmare : Tops and Tricks for creating LayoutsGoodbye Nightmare : Tops and Tricks for creating Layouts
Goodbye Nightmare : Tops and Tricks for creating Layouts
 
Goodbye Nightmare: Tips and Tricks for Creating Complex Layouts with Oracle A...
Goodbye Nightmare: Tips and Tricks for Creating Complex Layouts with Oracle A...Goodbye Nightmare: Tips and Tricks for Creating Complex Layouts with Oracle A...
Goodbye Nightmare: Tips and Tricks for Creating Complex Layouts with Oracle A...
 
Supervised embedding techniques in search ranking system
Supervised embedding techniques in search ranking systemSupervised embedding techniques in search ranking system
Supervised embedding techniques in search ranking system
 
INTRODUCTIONS OF CSS PART 2
INTRODUCTIONS OF CSS PART 2INTRODUCTIONS OF CSS PART 2
INTRODUCTIONS OF CSS PART 2
 
Bootstrap 4 ppt
Bootstrap 4 pptBootstrap 4 ppt
Bootstrap 4 ppt
 
Hello Android
Hello AndroidHello Android
Hello Android
 
Js info vis_toolkit
Js info vis_toolkitJs info vis_toolkit
Js info vis_toolkit
 

Recently uploaded

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 

Angular.js Directives for Interactive Web Applications

  • 1. AngularJS for Interactive Application Behavior Brent Goldstein brent@kubilateam.com Practical Directives
  • 2. Back Story ● Building an app and want rich behavior ○ Don’t limit goals because “it’s a web app” ● Need an interactive Spreadsheet-like Grid ○ Hierarchical data, specialized data entry needs ○ Similarities to Google Sheets, some differences ● Yikes, this seemed daunting at first ○ Hierarchical Data Binding ○ Navigation ○ Complex element/range selection
  • 3. Requirements - (be a product mgr) ● Hierarchical Data Set o 3 Row Levels - Person, Projects, Tasks o Columns are calendar weeks ● Navigate and Select like a Spreadsheet o Move w/ arrows, tab, shift-tab; mouse-click o Select single cell or range of cells ● Specialized Editing o Business logic applied to keystrokes before display
  • 4. How to Render Complex Data? ● Resist thinking like an ‘engineer’ ● Visual representation must be intuitive ● Even more important that ‘being’ intuitive → will someone actually use it? ● Really must pass the dog food test
  • 5. Digression - Dogfooding? ● OK, this is one of many overused tech industry terms ● Seems to put a rather unappetizing spin on one of the MOST IMPORTANT practices in application design ● It does not matter what you think or say, or what others think or say about using your app ● The application must draw you in (yes, YOU) ● Otherwise, it’s just neat tech, and neat is not a product
  • 6. Grid Hierarchy Rendering ● Go “Flat” -- flat visual data modeling that is ● Basically, de-normalize where it makes sense → use grouping to organize ● Greatly simplifies UI construction and interpretation ● Still possible to “drill down”, but using filters and selection to show more/fewer rows ● Avoid “nested grids”; hard to render, hard to read
  • 7. So how to build it? <div grid-editor> <div class=’grid-person’> div div div div div div ….. div ….. ….. <div> ….. <div> <div class=’grid-person’> <div> - Custom Directive “grid-editor” provides special behaviors - Grid-Editor looks for child DOM elements with special classes Note, grid constructed from divs, not tables, to support custom layout
  • 8. DOM Location and Navigation Let’s consider one use case, Find the Next cell to the RIGHT <div grid-editor> <div> <div class=’grid-person’> <div> ... <div class=’grid-project’> <div> <div class=’grid-project’> <div> 1) At last cell, but not last in row 2) At last cell in row 3) At last cell/row in grid-project Actually requires handling several sub-cases...
  • 9. Getting down to Business -- jqLite Assuming we know the selected cell, we can find the next like this: <div grid-editor> <div class=’grid-person’> <div class=’grid-project’> 1) At last cell, but not last in row col=’1’ col=’2’ col=’3’ col=’4’ col=’5’ ... 2) At last cell in row Case 1) Look for next sibling element, use attribute to be selective var newElement = currEle.next(‘[col]’); Case 2) Look for the FIRST element in the NEXT row (requires some maneuvering) var newElement = currEle.closest(‘grid-project’).next(‘grid-project’).find(‘.first’) Similar ‘traverse up, look for next’ can be applied to higher levels in hierarchy col=’6’ applied applied to first cell div: class=’first’ to every cell div: class=’cell’
  • 10. Going down, same column, new row What about other cases, like the down arrow key? Need the next row, but column needs to remain the same. Now that’s why the “col=n” attribute is useful…. Look for next sibling row, use attribute to find the correct column var newElement = currEle.closest(‘grid-project’).next(‘grid-project’) .find(‘.cell[col=’ + currEle.attr(‘col’) + ‘]’); <div grid-editor> <div class=’grid-person’> <div class=’grid-project’> col=’1’ col=’2’ col=’3’ col=’4’ col=’5’ col=’6’ col=’4’
  • 11. How to capture the arrow keys? Can be useful to step outside of Angular for event handling element.bind('keydown keypress', function (e){ if(!scope.editActive) return; // track the editing state var navArrow = function(e){ var arrowKeys = { 37:'left', 38:'up', 39:'right', 40:'down' }; return arrowKeys[e.which]; }; var navTab = function(e){ // tab key return (e.which === 9) ? (e.shiftKey?"left":"right") : null; }; var doNav = navFromArrow(e) || navFromTab(e); if(doNav){ e.preventDefault(); // prevent the browser default action theFunctionThatNavigatesToNextCell(doNav) } }; The ‘element’ above could be the $document root, or a sub-document, depending on the desired behavior. If a sub-doc, the keys will only be captured when that element or children have focus
  • 12. Rendering the Selection Band To indicate selected cell, a “rectangle select band” is typically used to surround the cells How to accomplish this in DOM? A couple of options: 1) Identify and alter the display the underlying DIVs 2) Add new elements that Overlay the native grid
  • 13. Draw band using actual grid cells? Altering the display of underlying cells can be tricky. How to actually make the highlight band? - Alter the div/span border(s) Not ideal. Positioning is limited to element border Also harder to make a multi-cell rectangle
  • 14. Overlay Rendering What about creating a new element/set of elements that display ON TOP of the grid? Let’s timewarp back to 1998 when graphics were rendered into framebuffers. Remember the Overlay Framebuffer? Let’s apply the same logical concept
  • 15. The Magic Select Rectangle Band How to render the select rectangle in DOM? We’ll construct with separate banding elements for max control It will simplify hover/select options since we can know exactly which side of the band is under the mouse Top DIV Bottom DIV Left Div Right Div With this approach we can also add other visual elements, like a cursor select target for dragging, etc.
  • 16. Dynamic Element Creation The select band is managed by the GridEditor Directive: - Create: When the directive is instantiated - Move: When the arrow/tab keys are captured - Grow/Shrink: Based on shift-click This function is called inside the directive link() function to instantiate the band elements: var _installBand = function(){ var html = "<div id='magic-square'>"+ "<div class='band-horiz top'></div>" + "<div class='band-horiz bottom'></div>"+ "<div class='band-vert left'></div>"+ "<div class='band-vert right'></div>" + "<div class='band-grabbox'></div>" + "</div>"; _band = angular.element(html); // this creates the dom element _gridEditorElement.prepend(_band); // attach to directive element return _band; // to adjust properties and location later };
  • 17. Dynamically alter DOM properties Properties are updated to adjust the rectangle band size when needed Note, _unitWidth is already set with the pixel width of a single cell var _setBandWidth = function(){ var rangeWidth = _range(); // gets the desired #cells to cover // update the top/bottom horizontal div widths _band.children(".band-horiz").width((_unitWidth-1) * rangeWidth); // update the right vertical div position var rightVert = _magicSquare.children(".band-vert.right"); var posLeft = (_unitWidth-1) * rangeWidth -1; rightVert.css({left: posLeft}); };
  • 18. Let’s take a look - Initial View Top-level view, summary data User sees this view when page is loaded ● Threshold coloring (red means too much) ● Click on row to expand ● Expand All/Collapse All buttons
  • 19. Let’s take a look - Expanded View Details for each person visible when expanded ● Project-task rows at same indent level ● Grouping by project ● Optional project sub-total rows
  • 20. Let’s take a look - Editing Behaviors to streamline complex editing needs ● Single Select and Range select ● Smart Navigation across projects/people ● Shortcut keys for pre-defined operations
  • 21. Coding tidbit - $emit for messaging Both $scope.$broadcast() and $scope.$emit() can be used to communicate between directives and controllers What’s the difference? Well, direction: $emit goes UP, $broadcast goes DOWN As a result, $emit is more efficient bubbling only to parents $broadcast fans out to all child scopes Maybe you really need $broadcast... but often not $emit can be applied in a message-bus pattern to achieve efficient dedicated communication across an app
  • 22. $emit for messaging Message bus approach can often replace $broadcast 1) Bubble Up 2) Message Bus Higher level controller or directive $scope.$on(‘hello.world’, function(event){//dosomething)); $scope.$emit(‘hello.world’); Originating Controller or Directive RootScope actiing as message Bus $rootScope.$on(‘hello.world’, function(event){//dosomething)); $rootScope.$emit(‘hello.world’); Originating Controller or Directive Listening Directive or Controller