jav a sc r ip t and css f o r g eo g raphers · grid layout grid garden a complete guide to grid...

41
JavaScript and CSS for Geographers Patrick Arlt, Allison Davis & Nate Bedortha Slides: http://bit.ly/2PLJft4

Upload: others

Post on 20-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

JavaScript and CSS for GeographersPatrick Arlt, Allison Davis & Nate Bedortha

Slides: http://bit.ly/2PLJft4

Page 2: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

This talk is all fundamentals.

Page 3: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

First, Some NotesLots of supplemental info in these slides.

Designed to help you keep learning beyond this talk.

Pretty much everything is a link.

Slides: http://bit.ly/2PLJft4

Page 4: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Web Development is Hard

It's ok to feel overwhelmed

Good news: you're more equipped than you think!

Scripted with ArcPy?

Scripted with Python?

Con�gured an app?

Used Arcade?

Page 5: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

A quick note on web servers

HTML, CSS, and JS all go in your web server

Install > Terminal/Command Line/Windows Bash/Powershell

For fast prototyping use or

How to set up a local web server

Node

npx http-server .

CodePen StackBlitz

Page 6: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

HTML<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title>Hello!</title> <!-- <link> (CSS) goes here --> </head> <body> <!-- Content (more html) goes here --> <h1>Welcome</h1> <div>Here's some unstyled content.</div> <!-- <script> (JavaScript) goes here --> </body></html>

Try it in CodePenMDN's HTML docs and guides

Page 7: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

CSS

html, body, #map { margin: 0; width: 100%; height: 100%; }

Page 8: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Where does CSS go?

Inside a .css �le that is loaded with a <link> tag.

Inside a <style> tag.

Inside an element’s style attribute. ⚠

<link href="my-css-file.css" rel="stylesheet" />

<style> /* Put CSS here*/</style>

<p style="color:blue;">Blue text!</p>

Page 9: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

What does CSS look like?

html, body, #map { margin: 0; width: 100%; height: 100%; }

Page 10: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

The "C" is for CascadingStyles cascade into the �nal styles for the HTML elements that match their

selectors.

Browser and user styles<link rel="stylesheet"><style> tags

Style attributes <div style="...">

Page 11: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

CSS Speci�cityWhen properties collide speci�city determines which property wins.

1. Rules with !important2. Inline styles <div style="...">3. <style> and <link> tags

4. Selector speci�city1. #id-attribute - <div id="...">

2. .class-attribute - <div class="...">

3. div - <div>

Page 12: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Let's inspect some CSS

Right click on something you want to change click "Inspect Element"

Explore a Storymap

Page 13: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Let's Build an App!

Page 21: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

JavaScript

Page 22: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Where does JavaScript go?

Inside a <script> tag.

Inside a .js �le.

In your browser's Right click > Inspect Element > Console tab

( ) ( )

<script> /* Put JS here*/</script>

<script src="app.js"></script>

DevTools console

Page 23: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

, , & Variables arithmetic comparison logicconst dogName = "Bunsen"; var year = 2020; let skyBlue = true; year++; // 2021 year--; // 2019 "high" + "five"; // 'highfive' // logical 'and'true && skyBlue; // true// 'or'true || false; // true// 'not' !skyBlue; // false

MDN's First Steps JavaScript guideTry it in CodePen

Page 24: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

functionsfunction dogYears(age) { return age * 7; }

dogYears(3); > 21

age => { return age * 7; }; age => age * 7; // these are the same!

Page 25: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

and Arrays[] objects{}var dogs = ["Ginsburg", "Bunsen"]; dogs[0]; // 'Ginsburg' dogs.push("Spot"); dogs.length; // 3 dogs.map(dog => dog.toUpperCase()); // ['GINSBURG', 'BUNSEN', 'SPOT']

let dog = { name: "Ginsburg", age: 4, ageInDogYears: function(age) { return age * 7; } }; dog.name; // 'Ginsburg'

Try it in CodePen

Page 26: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

JavaScript Patterns

Page 27: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

JavaScript is Asynchronous

JavaScript is single threadedRuns one function in its entiretyThen run the next functionThis is the "Event Loop""Callback functions" de�ne thing that happen later

Event Loop and Callbacks Demo

Page 28: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Promises

Promises represent values that will be set in the future.

i.e. I Promise to be a useful value in the future.

function processResponse(response) { return response.json(); } function doSomethingWithUser(user) { console.log(user); // prints a bunch of user info } function anyErrors(error) { console.error("what have you done!", error); } let user = fetch("https://randomuser.me/api/") .then(processResponse) .then(doSomethingWithUser)

catch(anyErrors);

Demo

Page 29: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

The and HTMLJavaScript can interact with your HTML. The HTML on your page is

represented by the DOM (Document Object Model).

Select HTML elementsListen for events & user interactionsChange HTML elements

DOM

Demo

Page 30: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

The future! You will encounter this more often.

JavaScript Modulesimport { something } from 'some-file.js';

Demo

Page 31: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

AMD Modules (JS API)

require is a fancy way of adding <script> tags to load code on demand.

require([ "esri/Map", "esri/views/MapView", ], function (Map, MapView) {

// Map and MapView have been loaded! });

Demo

Page 32: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

~120 lines of CSS, ~30 lines of JS.

Lets �nish our app

Page 34: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Tools & Frameworks

Page 35: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Don't jump into tools

The JS API is MORE then enough for simple mapping appsAdd tools when you KNOW you will bene�t from using themToo many tools === Lots of complexity to manageDon't touch tools until you feel limited

Page 36: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Types of tools

Modules - Formats for splitting up and sharing codeCompilers - Transform code often adding extra featuresBundlers - Combine modules and other assetsFrameworks - Architecture and structure for large apps/teams

Page 37: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Examples of tools

Modules - JS Modules, AMD, CommonJS, CSS ModulesCompilers - Babel, TypeScript, SASS, LESSBundlers - WebPack, Parcel, RollupFrameworks - React, Angular, Vue, Ember, Dojo, Tailwind, Bootstrap

Page 38: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Node JS and NPM

- Run JavaScript on a server or desktop. Build web servers, APIsand CLI tools.

- Package manager and distribution system for JS code. Analogousto Pip or Conda in Python.

Node JS

NPM

Learn Node JS at NodeSchool

Page 39: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Development tools

Set up your local dev environment: Prototype with , or

Do I have a web server running?CodePen JSBin StackBlitz

Visual Studio CodeChrome Developer ToolsFirefox Developer ToolsArcGIS JS CLI

Page 41: Jav a Sc r ip t and CSS f o r G eo g raphers · Grid Layout Grid Garden A Complete Guide to Grid Grid by Example MDN: CSS Grid Layout c Incredibly Easy Layouts with CSS Grid Bonus

Slides at http://bit.ly/2PLJft4