| OLD | NEW |
| 1 --- | 1 --- |
| 2 layout: default | 2 layout: default |
| 3 title: "Connect Dart & HTML" | 3 title: "Connect Dart & HTML" |
| 4 description: "Shows basic scaffolding of a Dart web app" | 4 description: "Shows basic scaffolding of a Dart web app" |
| 5 has-permalinks: true | 5 has-permalinks: true |
| 6 tutorial: | 6 tutorial: |
| 7 id: connect-dart-html | 7 id: connect-dart-html |
| 8 next: add-elements/ | 8 next: add-elements/ |
| 9 next-title: "Add Elements to the DOM" | 9 next-title: "Add Elements to the DOM" |
| 10 prev: get-started/ | 10 prev: get-started/ |
| 11 prev-title: "Get Started" | 11 prev-title: "Get started" |
| 12 --- | 12 --- |
| 13 | 13 |
| 14 {% capture whats_the_point %} | 14 {% capture whats_the_point %} |
| 15 | 15 |
| 16 * The DOM models a browser page in a tree/node structure. | 16 * The DOM models a browser page in a tree/node structure. |
| 17 * An HTML file hosts your Dart code in a browser page. | 17 * An HTML file hosts your Dart code in a browser page. |
| 18 * Use query() with an ID to get an element from the DOM. | 18 * Use query() with an ID to get an element from the DOM. |
| 19 * Control run-time configurations in Dart Editor with named launches. | |
| 20 * Compile to JavaScript to run in any modern browser. | 19 * Compile to JavaScript to run in any modern browser. |
| 21 * CSS selectors are patterns used to select matching elements in the DOM. | 20 * CSS selectors are patterns used to select matching elements in the DOM. |
| 22 * Use CSS rules to style elements. | 21 * Use CSS rules to style elements. |
| 23 | 22 |
| 24 {% endcapture %} | 23 {% endcapture %} |
| 25 | 24 |
| 26 {% capture sample_links %} | 25 {% capture sample_links %} |
| 27 | 26 |
| 28 <p> | 27 <p> |
| 29 Get the source code for the samples featured in this target:</p> | 28 Get the source code for the samples featured in this tutorial:</p> |
| 30 | 29 |
| 31 <ul> | 30 <ul> |
| 32 <li> | 31 <li> |
| 33 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/target02/mini" | 32 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/connect-dart-html/mini" |
| 34 target="_blank">mini</a> | 33 target="_blank">mini</a> |
| 35 </li> | 34 </li> |
| 36 <li> | 35 <li> |
| 37 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/target02/mini_with_style" | 36 <a href="https://github.com/dart-lang/dart-tutorials-samples/tree/master/web
/connect-dart-html/mini_with_style" |
| 38 target="_blank">mini_with_style</a> | 37 target="_blank">mini_with_style</a> |
| 39 </li> | 38 </li> |
| 40 </ul> | 39 </ul> |
| 41 | 40 |
| 42 {% endcapture %} | 41 {% endcapture %} |
| 43 | 42 |
| 44 {% capture content %} | 43 {% capture content %} |
| 45 | 44 |
| 46 <div class="tute-target-title"> | 45 <div class="tute-target-title"> |
| 47 <h1>{{page.title}}</h1> | 46 <h1>{{page.title}}</h1> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 66 Though simple, | 65 Though simple, |
| 67 this example shows you how to connect a Dart | 66 this example shows you how to connect a Dart |
| 68 app to an HTML page and | 67 app to an HTML page and |
| 69 one way that a Dart app can interact with items on the page. | 68 one way that a Dart app can interact with items on the page. |
| 70 These concepts | 69 These concepts |
| 71 provide the foundation | 70 provide the foundation |
| 72 for more interesting and useful web apps. | 71 for more interesting and useful web apps. |
| 73 | 72 |
| 74 * [About the DOM](#dom-intro) | 73 * [About the DOM](#dom-intro) |
| 75 * [Create a new Dart app](#create-dart-app) | 74 * [Create a new Dart app](#create-dart-app) |
| 75 * [Edit the HTML file](#create-html) |
| 76 * [About the HTML source code](#about-html-code) |
| 76 * [Edit Dart source code](#dart-editor-happiness) | 77 * [Edit Dart source code](#dart-editor-happiness) |
| 77 * [About the Dart source code](#about-dart-code) | 78 * [About the Dart source code](#about-dart-code) |
| 78 * [Edit the HTML file](#create-html) | 79 * [Summary of HTML and Dart connections](#summary) |
| 79 * [About the HTML source code](#about-html-code) | |
| 80 * [Run the mini web app](#run-mini) | 80 * [Run the mini web app](#run-mini) |
| 81 * [Create a JavaScript launch](#mini-with-js) | |
| 82 * [Give the app some style with CSS](#add-css) | 81 * [Give the app some style with CSS](#add-css) |
| 83 * [About CSS selectors](#about-css-selectors) | 82 * [About CSS selectors](#about-css-selectors) |
| 84 * [Other resources](#other-resources) | 83 * [Other resources](#other-resources) |
| 84 * [What next?](#what-next) |
| 85 | 85 |
| 86 ##About the DOM {#dom-intro} | 86 ##About the DOM {#dom-intro} |
| 87 | 87 |
| 88 The Document Object Model (DOM) | 88 The Document Object Model (DOM) |
| 89 represents the structure of a web document as a tree of nodes. | 89 represents the structure of a web document as a tree of nodes. |
| 90 When an HTML file is loaded into a browser, | 90 When an HTML file is loaded into a browser, |
| 91 the browser interprets the HTML | 91 the browser interprets the HTML |
| 92 and displays the document in a window. | 92 and displays the document in a window. |
| 93 The following diagram shows a simple HTML file and | 93 The following diagram shows a simple HTML file and |
| 94 the resulting web browser page in Chrome. | 94 the resulting web browser page in Chrome. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 alt="A Dart program can dynamically change the DOM"> | 138 alt="A Dart program can dynamically change the DOM"> |
| 139 | 139 |
| 140 The diagram shows a small Dart program that makes | 140 The diagram shows a small Dart program that makes |
| 141 a modest change to the DOM by dynamically | 141 a modest change to the DOM by dynamically |
| 142 changing a paragraph's text. | 142 changing a paragraph's text. |
| 143 A program could add and delete nodes, | 143 A program could add and delete nodes, |
| 144 or even insert an entire sub-tree of nodes. | 144 or even insert an entire sub-tree of nodes. |
| 145 | 145 |
| 146 ## Create a new Dart app {#create-dart-app} | 146 ## Create a new Dart app {#create-dart-app} |
| 147 | 147 |
| 148 The application you write in this target will be a web application. | 148 The application you write in this tutorial is a web application. |
| 149 Web applications use code from the browser package to run inside of a browser, | 149 Web applications use code from the `browser` package |
| 150 to run inside of a browser, |
| 150 so you need to start with the supporting files and packages | 151 so you need to start with the supporting files and packages |
| 151 even for the smallest web app. | 152 even for the smallest web app. |
| 152 | 153 |
| 153 In Dart Editor, create a new application called mini. | 154 In Dart Editor, create a new web application |
| 154 Select **Generate sample content** | 155 like you did in the previous tutorial, |
| 155 and **Web application**. | 156 [Get Started](/docs/tutorials/get-started/). |
| 157 Name the app `mini`. |
| 156 | 158 |
| 157 <img src="images/new-app-mini.png" | 159 <img src="images/new-app-mini.png" |
| 158 alt="New Application dialog window"> | 160 alt="New Application dialog window"> |
| 159 | 161 |
| 160 Dart Editor creates a directory called mini and, within it, | 162 ##Edit the HTML file {#create-html} |
| 161 the files and directories needed for a web application. | |
| 162 | 163 |
| 163 <img src="images/new-app-files.png" | 164 When Dart Editor created the application, |
| 164 alt="After new application created"> | 165 it created boilerplate HTML code. |
| 166 Double click `mini.html` to open it. |
| 167 Then replace the code with the following, |
| 168 simplied HTML. |
| 165 | 169 |
| 166 * The top-level directory is named after the application. | 170 {% prettify html %} |
| 171 <!DOCTYPE html> |
| 167 | 172 |
| 168 * The pubspec.yaml file is used by applications | 173 <html> |
| 169 that rely on external packages of Dart code. | 174 <head> |
| 170 [Target 5: Install Shared Packages](/docs/tutorials/shared-pkgs/) | 175 <title>A Minimalist App</title> |
| 171 provides more information. | 176 </head> |
| 172 Web applications use a package called `browser`. | 177 |
| 173 This dependency is declared in the pubspec.yaml file. | 178 <body> |
| 179 <p id="RipVanWinkle"> |
| 180 RipVanWinkle paragraph. |
| 181 </p> |
| 182 <script type="application/dart" src="mini.dart"></script> |
| 183 <script src="packages/browser/dart.js"></script> |
| 184 </body> |
| 185 </html> |
| 186 {% endprettify %} |
| 174 | 187 |
| 175 * The web directory contains the Dart, HTML, and CSS code for the web app. | 188 Finish your edits and save the file with **File > Save**. |
| 176 | 189 |
| 177 * The main source file, mini.dart in this sample, | 190 ##About the HTML source code {#about-html-code} |
| 178 contains boilerplate code for the clickme app | 191 |
| 179 you saw in the previous target. | 192 This HTML code is similar to the simple HTML code in the |
| 180 You will edit the code in this file and | 193 various diagrams earlier in this tutorial. |
| 181 in mini.html to create a bare-bones app. | 194 Again, you can see the use of the \<head>, \<title>, \<body>, |
| 195 and \<p> tags. |
| 196 And there, in the paragraph tag, |
| 197 is an identifier "RipVanWinkle". |
| 198 The Dart code you create in the next step uses this ID |
| 199 to get the paragraph element. |
| 200 |
| 201 <img src="images/html-id.png" |
| 202 alt="Using an ID in HTML"> |
| 203 |
| 204 Notice the use of the `script` tag. |
| 205 The source for a script is provided by a client-side file. |
| 206 The HTML code above has two scripts. |
| 207 |
| 208 <img src="images/script-tags.png" |
| 209 alt="Including Dart apps in HTML"> |
| 210 |
| 211 The first script includes your mini app. |
| 212 The `type` attribute specifies that the script has |
| 213 the type `application/dart`, |
| 214 which is a new type created by the Dart team. |
| 215 Currently, only the Dartium build of Chromium supports |
| 216 `application/dart`. |
| 217 The `src` attribute provides the URL to the source file of the script. |
| 218 In this case, it is the Dart source file `mini.dart`, |
| 219 which you provide in the next step. |
| 220 The Dart file should be in the same directory as its host HTML file. |
| 221 |
| 222 The second `script` tag is |
| 223 a bootstrap script that takes care of turning on the Dart VM, |
| 224 as well as compatibility with non-Dart browsers. |
| 182 | 225 |
| 183 ##Edit Dart source code {#dart-editor-happiness} | 226 ##Edit Dart source code {#dart-editor-happiness} |
| 184 | 227 |
| 185 Use Dart Editor | 228 Open `mini.dart` and modify the source code |
| 186 to modify the source code | |
| 187 to look like this: | 229 to look like this: |
| 188 | 230 |
| 189 {% prettify dart %} | 231 {% prettify dart %} |
| 190 import 'dart:html'; | 232 import 'dart:html'; |
| 191 void main() { | 233 void main() { |
| 192 query('#RipVanWinkle').text = 'Wake up, sleepy head!'; | 234 query('#RipVanWinkle').text = 'Wake up, sleepy head!'; |
| 193 } | 235 } |
| 194 {% endprettify %} | 236 {% endprettify %} |
| 195 | 237 |
| 196 <i class="icon-info-sign"> </i> | |
| 197 <a href="errors-warnings.html">What is Dart Editor trying to tell me?</a> | |
| 198 | |
| 199 ##About the Dart source code {#about-dart-code} | 238 ##About the Dart source code {#about-dart-code} |
| 200 | 239 |
| 201 Let's step through the code. | 240 Let's step through the code. |
| 202 | 241 |
| 203 ###Importing libraries | 242 ###Importing libraries |
| 204 | 243 |
| 205 The import directive imports the specified library, | 244 The import directive imports the specified library, |
| 206 making all of the classes and functions | 245 making all of the classes and functions |
| 207 in that library | 246 in that library |
| 208 available to your program. | 247 available to your program. |
| 209 | 248 |
| 210 <img src="images/0-mini-code-walk-through.png" | 249 <img src="images/0-mini-code-walk-through.png" |
| 211 alt="Import Dart HTML library"> | 250 alt="Import Dart HTML library"> |
| 212 | 251 |
| 213 This program imports Dart's HTML library, | 252 This program imports Dart's HTML library, |
| 214 which contains the classes and functions for programming the DOM. | 253 which contains key classes and functions for programming the DOM. |
| 215 Generally speaking, all Dart web apps need the Dart HTML library. | |
| 216 Key classes include: | 254 Key classes include: |
| 217 | 255 |
| 218 | Dart class | Description | | 256 | Dart class | Description | |
| 219 |---|---| | 257 |---|---| |
| 220 | <a href="https://api.dartlang.org/dart_html/Node.html" target="_blank">Node</a
> | Implements a Dart Node. | | 258 | <a href="https://api.dartlang.org/dart_html/Node.html" target="_blank">Node</a
> | Implements a Dart Node. | |
| 221 | <a href="https://api.dartlang.org/dart_html/Element.html" target="_blank">Elem
ent</a> | A subclass of Node, implements a web page element. | | 259 | <a href="https://api.dartlang.org/dart_html/Element.html" target="_blank">Elem
ent</a> | A subclass of Node, implements a web page element. | |
| 222 | <a href="https://api.dartlang.org/dart_html/Document.html" target="_blank">Doc
ument</a> | Another subclass of Node. Implements the document object. | | 260 | <a href="https://api.dartlang.org/dart_html/Document.html" target="_blank">Doc
ument</a> | Another subclass of Node. Implements the document object. | |
| 223 {: .table} | 261 {: .table} |
| 224 | 262 |
| 225 The Dart core library contains another useful class, | 263 The Dart core library contains another useful class, |
| 226 <a href="https://api.dartlang.org/dart_core/List.html" target="_blank">List</a>, | 264 <a href="https://api.dartlang.org/dart_core/List.html" target="_blank">List</a>, |
| 227 a parameterized class that can specify the type of its members. | 265 a parameterized class that can specify the type of its members. |
| 228 The Element class keeps its list of child Elements | 266 An instance of Element keeps its list of child Elements |
| 229 in a List\<Element>, a list that can contain only Element objects. | 267 in a List\<Element>. |
| 230 | 268 |
| 231 ###Using the query() function | 269 ###Using the query() function |
| 232 | 270 |
| 233 This app's main() function contains a single | 271 This app's main() function contains a single |
| 234 line of code that is a little like a run-on sentence | 272 line of code that is a little like a run-on sentence |
| 235 with multiple things happening one after another. | 273 with multiple things happening one after another. |
| 236 Let's deconstruct it. | 274 Let's deconstruct it. |
| 237 | 275 |
| 238 query() is a top-level function provided by the Dart HTML library | 276 query() is a top-level function provided by the Dart HTML library |
| 239 that gets an Element object from the DOM. | 277 that gets an Element object from the DOM. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 you can simply use the Element `text` property, | 318 you can simply use the Element `text` property, |
| 281 which has a getter | 319 which has a getter |
| 282 that walks the subtree of nodes for you and extracts their text. | 320 that walks the subtree of nodes for you and extracts their text. |
| 283 | 321 |
| 284 <img src="images/4-mini-code-walk-through.png" | 322 <img src="images/4-mini-code-walk-through.png" |
| 285 alt="Text node child of the paragraph"> | 323 alt="Text node child of the paragraph"> |
| 286 | 324 |
| 287 However, if the text node has styles (and thus a subtree), | 325 However, if the text node has styles (and thus a subtree), |
| 288 getting text and then setting it immediately is likely | 326 getting text and then setting it immediately is likely |
| 289 to change the DOM, as a result of losing subtree information. | 327 to change the DOM, as a result of losing subtree information. |
| 290 | |
| 291 Often, as with our RipVanWinkle example, | 328 Often, as with our RipVanWinkle example, |
| 292 this simplification has no adverse effects. | 329 this simplification has no adverse effects. |
| 293 Do know, however, that with more complex situations | |
| 294 getting text and then setting it immediately | |
| 295 will likely change the text. | |
| 296 | 330 |
| 297 The assignment operator (=) sets the text | 331 The assignment operator (=) sets the text |
| 298 of the Element returned by the query() function | 332 of the Element returned by the query() function |
| 299 to the string "Wake up, sleepy head!". | 333 to the string "Wake up, sleepy head!". |
| 300 | 334 |
| 301 <img src="images/5-mini-code-walk-through.png" | 335 <img src="images/5-mini-code-walk-through.png" |
| 302 alt="Set the text node, thereby changing the text on the page"> | 336 alt="Set the text node, thereby changing the text on the page"> |
| 303 | 337 |
| 304 This causes the browser to immediately re-render | 338 This causes the browser to immediately re-render |
| 305 the browser page containing this app | 339 the browser page containing this app, thus |
| 306 dynamically displaying the text on the browser page. | 340 dynamically displaying the text on the browser page. |
| 307 | 341 |
| 308 ##Edit the HTML file {#create-html} | 342 ##Summary of HTML and Dart connections {#summary} |
| 309 | 343 |
| 310 When Dart Editor created the application, | 344 This diagram summarizes the connections |
| 311 it created boilerplate HTML code that worked with the clickme app. | |
| 312 Now that you've modified the Dart code, | |
| 313 you will need to change the HTML code | |
| 314 so that it contains the expected RipVanWinkle paragraph. | |
| 315 | |
| 316 In Dart Editor, double click `mini.html`. | |
| 317 The code appears in the editor pane. | |
| 318 Replace the default contents with the following, | |
| 319 simplified HTML. | |
| 320 | |
| 321 {% prettify html %} | |
| 322 <!DOCTYPE html> | |
| 323 | |
| 324 <html> | |
| 325 <head> | |
| 326 <title>A Minimalist App</title> | |
| 327 </head> | |
| 328 | |
| 329 <body> | |
| 330 <p id="RipVanWinkle"></p> | |
| 331 <script type="application/dart" src="mini.dart"></script> | |
| 332 <script src="packages/browser/dart.js"></script> | |
| 333 </body> | |
| 334 </html> | |
| 335 {% endprettify %} | |
| 336 | |
| 337 Finish your edits and save the file with **File > Save**. | |
| 338 | |
| 339 ##About the HTML source code {#about-html-code} | |
| 340 | |
| 341 This HTML code is similar to the simple HTML code in the | |
| 342 various diagrams earlier in this target. | |
| 343 Again, you can see the use of the \<head>, \<title>, \<body>, | |
| 344 and \<p> tags. | |
| 345 And there, in the paragraph tag, | |
| 346 is the identifier "RipVanWinkle" | |
| 347 that the Dart code you created in the previous step uses | |
| 348 as an argument to query() to get the paragraph element. | |
| 349 | |
| 350 New here is the use of the `script` tag. | |
| 351 A script's content is defined by a client-side script. | |
| 352 The HTML code above has two scripts. | |
| 353 | |
| 354 <img src="images/script-tags.png" | |
| 355 alt="Including Dart apps in HTML"> | |
| 356 | |
| 357 The first includes your mini app. | |
| 358 It uses two attributes: `type` indicates the type of the script. | |
| 359 `application/dart` is a new type created by the Dart team, | |
| 360 which is currently supported by Dartium. | |
| 361 The `src` attribute provides the URL to the source file of the script. | |
| 362 In this case, it is the Dart source file `mini.dart` that you created earlier. | |
| 363 The Dart file should be in the same directory as its host HTML file. | |
| 364 | |
| 365 The second `script` tag is | |
| 366 a bootstrap script that takes care of turning on the Dart VM, | |
| 367 as well as compatibility with non-Dart browsers. | |
| 368 | |
| 369 This diagram summarizes the connection | |
| 370 between `mini.dart` and `mini.html`. | 345 between `mini.dart` and `mini.html`. |
| 371 | 346 |
| 372 <img src="images/dart-html-connect.png" | 347 <img src="images/dart-html-connect.png" |
| 373 alt="The connection between the HTML file and the Dart file"> | 348 alt="The connection between the HTML file and the Dart file"> |
| 374 | 349 |
| 375 ##Run the mini web app {#run-mini} | 350 ##Run the mini web app {#run-mini} |
| 376 | 351 |
| 377 In Dart Editor, | 352 In Dart Editor, |
| 378 make sure that one of `mini`'s files or the directory is selected, | 353 select `mini.html` and then click the Run button |
| 379 and then click the Run button | |
| 380 <img src="images/run.png" width="16" height="16" | 354 <img src="images/run.png" width="16" height="16" |
| 381 alt="Run button">. | 355 alt="Run button">. |
| 382 Dart Editor invokes Dartium | 356 Dart Editor invokes Dartium |
| 383 and loads `mini.html` in it. | 357 and loads `mini.html` in it. |
| 384 Below is mini app running in a frame. | 358 Below is mini app running in a frame. |
| 385 The app just displays a line of text. | 359 The app displays the text provided by Dart code, |
| 360 namely "Wake up, sleepy head!", |
| 361 not the text provided in the HTML file. |
| 386 | 362 |
| 387 <iframe class="running-app-frame" | 363 <iframe class="running-app-frame" |
| 388 style="height:150px;width:300px;" | 364 style="height:150px;width:300px;" |
| 389 src="http://dart-lang.github.io/dart-tutorials-samples/web/target02/mini
/web/mini.html"> | 365 src="examples/mini/mini.html"> |
| 390 </iframe> | 366 </iframe> |
| 391 | 367 |
| 392 The Dart web app changed | 368 The Dart web app changed |
| 393 the text in the browser window dynamically at runtime. | 369 the text in the browser window dynamically at runtime. |
| 394 Of course, placing static text on a browser page | 370 Of course, placing text on a browser page |
| 395 and doing nothing else | 371 and doing nothing else |
| 396 could be accomplished with straight HTML. | 372 could be accomplished with straight HTML. |
| 397 This little app only shows you how to make a connection | 373 This little app only shows you how to make a connection |
| 398 from a Dart app to a browser page. | 374 from a Dart app to a browser page. |
| 399 | 375 |
| 400 ##Create a JavaScript launch {#mini-with-js} | |
| 401 | |
| 402 You can create various runtime scenarios for your Dart app | |
| 403 using *launches* in Dart Editor. | |
| 404 A launch specifies a runtime configuration, | |
| 405 such as whether to compile to JavaScript | |
| 406 or what browser to use. | |
| 407 | |
| 408 In this section, | |
| 409 you will create a launch for mini that first compiles the app to | |
| 410 JavaScript and then runs it in the default system browser. | |
| 411 | |
| 412 <ol> | |
| 413 <li markdown="1"> | |
| 414 In Dart Editor, | |
| 415 click the wee arrow to the right of the run button | |
| 416 <img src="images/run.png" width="16" height="16" | |
| 417 alt="Run button">. | |
| 418 A menu appears: | |
| 419 | |
| 420 <img src="images/manage-launches-menu.png" | |
| 421 alt="Manage launches menu"> | |
| 422 | |
| 423 You have been unwittingly using launches when running | |
| 424 the samples so far. | |
| 425 The menu item `mini.html from mini` is a launch that Dart Editor | |
| 426 automatically created | |
| 427 when you ran the application for the first time | |
| 428 in the previous section. | |
| 429 </li> | |
| 430 | |
| 431 <li markdown="1"> | |
| 432 Select **Manage launches** from the menu | |
| 433 to bring up the **Manage Launches** dialog. | |
| 434 Using the small icons, | |
| 435 you can create launches for different purposes: | |
| 436 | |
| 437 <img src="images/new-launch-menu.png" alt="Launch options"> | |
| 438 | |
| 439 By default, | |
| 440 Dart Editor automatically creates a Dartium launch for web apps. | |
| 441 You must explicitly create a Dart JavaScript launch | |
| 442 to create a runtime configuration that compiles | |
| 443 your web app to JavaScript and then runs it in a browser. | |
| 444 </li> | |
| 445 | |
| 446 <li markdown="1"> | |
| 447 Click the JavaScript launch icon | |
| 448 <img src="images/dart2js-icon.png" width="18" height="17" | |
| 449 alt="JavaScript launch icon">. | |
| 450 The dialog window adjusts to present the appropriate options | |
| 451 for your selection. | |
| 452 </li> | |
| 453 | |
| 454 <li markdown="1"> | |
| 455 Name the launch by typing `mini-with-js` in the first text box. | |
| 456 | |
| 457 <img src="images/new-js-launch.png" | |
| 458 alt="Create a launch that compiles to JavaScript"> | |
| 459 | |
| 460 It is recommended that the launch name reflect | |
| 461 the important aspects of its configuration. | |
| 462 In this case, the launch name specifies the app name | |
| 463 and that it's a JavaScript launch. | |
| 464 If the launch specified a browser, such as FireFox, | |
| 465 you might name the launch `mini-with-js-in-firefox`. | |
| 466 </li> | |
| 467 | |
| 468 <li markdown="1"> | |
| 469 The launch target is the HTML file `mini.html`. | |
| 470 Browse to the file. | |
| 471 </li> | |
| 472 | |
| 473 <li markdown="1"> | |
| 474 Click **Apply**. | |
| 475 The named launch should now appear | |
| 476 in the list to the left of the window. | |
| 477 | |
| 478 <img src="images/mini-with-js-item.png" | |
| 479 alt="New named launch in the list of launches"> | |
| 480 | |
| 481 </li> | |
| 482 | |
| 483 <li markdown="1"> | |
| 484 To run the launch, | |
| 485 click the Run button at the bottom right of the window. | |
| 486 Or select it from the Run menu on the main Dart Editor window. | |
| 487 | |
| 488 This time, Dart Editor | |
| 489 creates a JavaScript file, | |
| 490 invokes the default system browser | |
| 491 and loads the JavaScript file into it. | |
| 492 The output should look the same. | |
| 493 </li> | |
| 494 | |
| 495 </ol> | |
| 496 | |
| 497 ##Give the app some style with CSS {#add-css} | 376 ##Give the app some style with CSS {#add-css} |
| 498 | 377 |
| 499 Most HTML uses cascading style sheets (CSS) to define _styles_ | 378 Most HTML uses cascading style sheets (CSS) to define _styles_ |
| 500 that control the appearance of page elements. | 379 that control the appearance of page elements. |
| 501 Let's customize the CSS for the mini app. | 380 Let's customize the CSS for the mini app. |
| 502 | 381 |
| 503 In Dart Editor, edit the file named mini.css | 382 In Dart Editor, edit the file named `mini.css` |
| 504 and replace the contents of the file with | 383 and replace the contents of the file with |
| 505 the following CSS code: | 384 the following CSS code: |
| 506 | 385 |
| 507 {% prettify dart %} | 386 {% prettify dart %} |
| 508 #RipVanWinkle { | 387 #RipVanWinkle { |
| 509 font-size: 20px; | 388 font-size: 20px; |
| 510 font-family: 'Open Sans', sans-serif; | 389 font-family: 'Open Sans', sans-serif; |
| 511 text-align: center; | 390 text-align: center; |
| 512 margin-top: 20px; | 391 margin-top: 20px; |
| 513 background-color: SlateBlue; | 392 background-color: SlateBlue; |
| 514 color: Yellow; | 393 color: Yellow; |
| 515 } | 394 } |
| 516 {% endprettify %} | 395 {% endprettify %} |
| 517 | 396 |
| 518 This defines a style | 397 This defines a style |
| 519 for the page element | 398 for the page element |
| 520 with the ID RipVanWinkle. | 399 with the ID RipVanWinkle. |
| 521 To use this style sheet, | 400 To use this style sheet, |
| 522 edit mini.html and add the line shown in bold below: | 401 edit `mini.html` and add the line shown in bold below: |
| 523 | 402 |
| 524 <img src="images/html-with-css.png" | 403 <img src="images/html-with-css.png" |
| 525 alt="HTML with CSS"> | 404 alt="HTML with CSS"> |
| 526 | 405 |
| 527 Save your files and run the app again. | 406 Save your files and run the app again. |
| 528 | 407 |
| 529 Below is the revised mini app, | 408 Below is the revised mini app, |
| 530 which is slightly more colorful | 409 which is slightly more colorful |
| 531 but still neither interactive nor interesting. | 410 but still neither interactive nor interesting. |
| 532 | 411 |
| 533 <iframe class="running-app-frame" | 412 <iframe class="running-app-frame" |
| 534 style="height:150px;width:300px;" | 413 style="height:150px;width:300px;" |
| 535 src="http://dart-lang.github.io/dart-tutorials-samples/web/target02/mini
_with_style/web/mini_with_style.html"> | 414 src="examples/mini_with_style/mini_with_style.html"> |
| 536 </iframe> | 415 </iframe> |
| 537 | 416 |
| 538 ##About CSS selectors {#about-css-selectors} | 417 ##About CSS selectors {#about-css-selectors} |
| 539 | 418 |
| 540 IDs, classes, and other information about elements | 419 IDs, classes, and other information about elements |
| 541 are established in HTML. | 420 are established in HTML. |
| 542 Your Dart code can use this information | 421 Your Dart code can use this information |
| 543 to get elements using a CSS selector—a pattern | 422 to get elements using a CSS selector—a pattern |
| 544 used to select matching elements in the DOM. | 423 used to select matching elements in the DOM. |
| 545 CSS selectors allow the CSS, HTML, and Dart code | 424 CSS selectors allow the CSS, HTML, and Dart code |
| (...skipping 10 matching lines...) Expand all Loading... |
| 556 and class selectors with queryAll(). | 435 and class selectors with queryAll(). |
| 557 | 436 |
| 558 Here are some examples of CSS selectors: | 437 Here are some examples of CSS selectors: |
| 559 | 438 |
| 560 | Selector type | Example | Description | | 439 | Selector type | Example | Description | |
| 561 |---|---| | 440 |---|---| |
| 562 | ID selector | #RipVanWinkle | Matches a single, unique element | | 441 | ID selector | #RipVanWinkle | Matches a single, unique element | |
| 563 | HTML element | p | Matches all paragraphs | | 442 | HTML element | p | Matches all paragraphs | |
| 564 | HTML element | h1 | Matches all level-one headers | | 443 | HTML element | h1 | Matches all level-one headers | |
| 565 | Class | _.classname_ | Matches all items with the class _classname_ | | 444 | Class | _.classname_ | Matches all items with the class _classname_ | |
| 566 | Asterisk | * | Using the asterisk matches all elements | | 445 | Asterisk | * | Matches all elements | |
| 567 | Attribute | input[type="button"] | Matches all button input elements | | 446 | Attribute | input[type="button"] | Matches all button input elements | |
| 568 {: .table} | 447 {: .table} |
| 569 | 448 |
| 570 <aside class="alert"> | 449 <aside class="alert"> |
| 571 <strong>Tip:</strong> | 450 <strong>Tip:</strong> |
| 572 As you saw, | 451 As you saw, |
| 573 mini app used a CSS selector, | 452 mini app used a CSS selector, |
| 574 the ID #RipVanWinkle, | 453 the ID #RipVanWinkle, |
| 575 even when there was no CSS file. | 454 even when there was no CSS file. |
| 576 You do not need a CSS file for a Dart program. | 455 You do not need a CSS file for a Dart program. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 alt="A declaration specifies an attribute and its value"> | 489 alt="A declaration specifies an attribute and its value"> |
| 611 | 490 |
| 612 The CSS rule for the RipVanWinkle paragraph | 491 The CSS rule for the RipVanWinkle paragraph |
| 613 specifies several properties; | 492 specifies several properties; |
| 614 for example, it sets the text color to Yellow. | 493 for example, it sets the text color to Yellow. |
| 615 | 494 |
| 616 ##Other resources | 495 ##Other resources |
| 617 | 496 |
| 618 <ul> | 497 <ul> |
| 619 <li> | 498 <li> |
| 620 <a href="/docs/dart-up-and-running/">Dart Up and Running</a> | 499 <a href="/docs/dart-up-and-running/">Dart: Up and Running</a> |
| 621 provides thorough coverage of the Dart language, libraries, and tools. | 500 provides thorough coverage of the Dart language, libraries, and tools. |
| 622 If a topic is not covered explicitly here, | 501 If a topic is not covered explicitly here, |
| 623 you can find the information you need there. | 502 you can find the information you need there. |
| 624 </li> | 503 </li> |
| 625 <li> | 504 <li> |
| 626 <a href="/docs/dart-up-and-running/contents/ch04-tools-editor.html">Dart Edi
tor</a>, | 505 <a href="/docs/dart-up-and-running/contents/ch04-tools-editor.html">Dart Edi
tor</a>, |
| 627 an excerpt from <em>Dart Up and Running</em>, | 506 an excerpt from <em>Dart: Up and Running</em>, |
| 628 provides details about using Dart Editor. | 507 provides details about using Dart Editor. |
| 629 The excerpt includes, for example, | 508 The excerpt includes, for example, |
| 630 how to use Dart Editor's power features such as autocompletion and refactori
ng, | 509 how to use Dart Editor's power features such as autocompletion and refactori
ng, |
| 631 how to set up different run-time environments, and so on. | 510 how to set up different run-time environments, and so on. |
| 632 </li> | 511 </li> |
| 633 <li> | 512 <li> |
| 634 Also, check out | 513 Also, check out |
| 635 <a href="/docs/cookbook/"> | 514 <a href="/docs/cookbook/"> |
| 636 <i class="icon-food"> </i> Dart Cookbook</a>, | 515 <i class="icon-food"> </i> Dart Cookbook</a>, |
| 637 where you'll find many recipes about | 516 where you'll find many recipes about |
| 638 manipulating the DOM and using CSS. | 517 manipulating the DOM and using CSS. |
| 639 The cookbook also has recipes about basic Dart data types, | 518 The cookbook also has recipes about basic Dart data types, |
| 640 such strings, lists, maps, and numbers. | 519 such strings, lists, maps, and numbers. |
| 641 </li> | 520 </li> |
| 642 </ul> | 521 </ul> |
| 643 | 522 |
| 523 ##What next? |
| 524 |
| 525 The next tutorial, [Add Elements to the DOM](/docs/tutorials/add-elements/), |
| 526 shows you how to dynamically change the HTML page |
| 527 by adding elements to the DOM. |
| 528 |
| 644 {% endcapture %} | 529 {% endcapture %} |
| 645 | 530 |
| 646 {% include tutorial.html %} | 531 {% include tutorial.html %} |
| OLD | NEW |