https://github.com/firefirestyle/ebook_windstyle_markdown/tree/master/angular/tiny-dynamicblock
隙間なく、四角形を詰めていくデモです。
コンストラクターで ElementRef を取得できる事がわかれば
簡単に書けますね!!
コード
main.dartimport 'package:angular2/platform/browser.dart'; import 'package:angular2/core.dart'; import 'dart:async'; import 'dart:html' as html; import 'dynablock.dart' as dyna; import 'dart:math'; main() { bootstrap(AppComponent); } class DynamicItemParam { int w; int h; DynamicItemParam() { Random rnd = new Random(); w = rnd.nextInt(100) + 50; h = rnd.nextInt(200) + 100; } } @Component( selector: 'dynamicitem', template: """ xxx """, ) class DynamicItem implements OnInit { @Input() DynamicComponent parent; @Input() int width = 100; @Input() int height = 200; final ElementRef element; DynamicItem(this.element) { (element.nativeElement as html.Element).style.width = "${width}px"; (element.nativeElement as html.Element).style.height = "${height}px"; (element.nativeElement as html.Element).style.color = "#FF00"; (element.nativeElement as html.Element).style.padding = "0px"; (element.nativeElement as html.Element).style.margin = "0px"; (element.nativeElement as html.Element).style.backgroundColor = 'yellow'; (element.nativeElement as html.Element).style.display = 'inline-block'; (element.nativeElement as html.Element).style.border = "medium solid #333333"; } ngOnInit() { print("xx1 ${element.nativeElement}"); (element.nativeElement as html.Element).style.width = "${width}px"; (element.nativeElement as html.Element).style.height = "${height}px"; if (parent != null) { parent.append(this); } } } @Component( selector: 'markdown-component', directives: const [DynamicItem], template: """ <div #markdowncontent> <dynamicitem *ngFor='let c of contents' [parent]='own' [width]='c.w' [height]='c.h' ></dynamicitem> </div> """, ) class DynamicComponent implements OnInit { @Input() List<DynamicItemParam> contents; List<DynamicItem> items = []; html.Element _contentElement; dyna.DynaBlockCore dynaCore = new dyna.DynaBlockCore(); final ElementRef element; DynamicComponent own; DynamicComponent(this.element) { var elm = element.nativeElement; elm.style.position = "absolute"; own = this; } @ViewChild('markdowncontent') set main(ElementRef elementRef) { _contentElement = elementRef.nativeElement; print("${elementRef} ${elementRef.nativeElement}"); _contentElement.onChange.listen((e) { print("-a"); }); } append(DynamicItem ap) { print("XXS"); var elm = ap.element.nativeElement; int margineW = 10; int margineH = 10; dyna.FreeSpaceInfo info = dynaCore.addBlock( ap.width + margineW, ap.height + margineH); elm.style.position = "absolute"; elm.style.left = "${info.xs}px"; elm.style.top = "${info.y}px"; } ngOnInit() { } } @Component( selector: 'my-app', directives: const [DynamicComponent], template: """ <markdown-component [contents]='contents'></markdown-component> """ ) class AppComponent implements OnInit { List<DynamicItemParam> contents = [ new DynamicItemParam(), new DynamicItemParam(), new DynamicItemParam(), new DynamicItemParam(), ]; ngOnInit() async { for (int i = 0; i < 10; i++) { contents.add(new DynamicItemParam()); await new Future.delayed(new Duration(milliseconds: 500)); } } }
0 件のコメント:
コメントを投稿