開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
Head First JavaScript ―頭とからだで覚えるJavaScriptの基本( Michael Morrison (著), 豊福 剛 (翻訳)、オライリージャパン)の10章(カスタムオブジェクトを作成する)、自分で考えてみよう(p.477-479)をDartで考えてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
自分で考えてみよう(p.477-479)
コード
sample.dart
import 'dart:html'; void main () { run.onClick.listen((MouseEvent event) => Blog.showBlog()); clear.onClick.listen((MouseEvent event) => div.innerHtml = ''); } ButtonElement run = querySelector('#run_dart'); ButtonElement clear = querySelector('#clear'); DivElement div = querySelector('#d0'); List<zBlog> blog = [new Blog(null, null, 'https://lh5.googleusercontent.com/' + 'rljVgO2PsdWyf5TVbjD3xefkzEDGpXR5QVeAKaPnJlI=s222-p-no'), new Blog(new DateTime(2008, 8, 14), '注文していた…'), new Blog(new DateTime(2008, 8, 19), '新しいキューブは…'), new Blog(new DateTime(2008, 8, 16), '新しいキューブと…'), new Blog(new DateTime(2008, 8, 21), 'ネットで…')]; String shortFormat(DateTime dt){ return '${dt.month}/${dt.day}/${dt.year}'; } class Blog implements Comparable{ static const SIGNATURE = 'This blog created by Puzzler Ruby'; static void showBlog([int n]) { var blog_html = ''; if (n == null){ n = blog.length; } blog.sort((x, y) => x.compareTo(y)); bool highlight = true; blog.forEach((Blog entry){ div.append(entry.toHtml(highlight)); highlight = !highlight; }); } DateTime date_time; String body; String image; Blog([DateTime date_time, String body, String image]){ print('$date_time, $body, $image'); this.date_time = date_time != null ? date_time : new DateTime.now(); this.body = body != null ? body : 'Nothing going on today'; this.image = image; } ParagraphElement toHtml(bool highlight){ ParagraphElement p = new ParagraphElement(); p.appendHtml('<zstrong>${shortFormat(date_time)}<z/strong><zbr />'); if (highlight) { p.style.backgroundColor = '#EEEEEE'; } if (image != null) { TableElement table = new TableElement(); TableRowElement row = new TableRowElement(); TableCellElement cell1 = new TableCellElement(); TableCellElement cell2 = new TableCellElement(); ImageElement img = new ImageElement(src:image); cell2.style.verticalAlign = 'top'; Text text = new Text(body); cell1.append(img); cell2.append(text); row ..append(cell1) ..append(cell2); table.append(row); p.append(table); } else { p.appendText(body); } p.appendHtml('<zbr /><zem>$SIGNATURE<z/em>'); return p; } int compareTo(Blog other) => other.date_time.compareTo(date_time); } void showBlog([int n]) { if (n == null) { n = blog.length; } String blog_text = ''; int i; for (i = 0; i <z n; i += 1) { Blog entry = blog[i]; DateTime date = entry.date_time; blog_text += '<zstrong>${date.month}/${date.day}/${date.year}<z/strong>' + '<zbr />${entry.body}<zbr />'; } div.innerHtml = blog_text; }
0 コメント:
コメントを投稿