OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2014 Google Inc. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style | |
5 * license that can be found in the LICENSE file or at | |
6 * https://developers.google.com/open-source/licenses/bsd | |
7 */ | |
8 | |
9 part of charted.selection; | |
10 | |
11 /** SelectionScope represents a scope for all the data and DOM operations. */ | |
12 class SelectionScope { | |
13 Expando _associations = new Expando(); | |
14 Expando<Map<String, Pair<Function, bool>>> _listeners = new Expando(); | |
15 Element _root; | |
16 | |
17 /** Creates a new selection scope with document as the root. */ | |
18 SelectionScope() { | |
19 _root = document.documentElement; | |
20 } | |
21 | |
22 /** | |
23 * Creates a new selection scope with the first element matching | |
24 * [selector] as the root. | |
25 */ | |
26 SelectionScope.selector(String selector) { | |
27 if (selector == null || selector.isEmpty ){ | |
28 throw new ArgumentError('Selector cannot be empty'); | |
29 } | |
30 _root = document.querySelector(selector); | |
31 } | |
32 | |
33 /** | |
34 * Creates a new selection scope with the passed [element] as [root]. | |
35 * Charted assumes that the element is already part of the DOM. | |
36 */ | |
37 SelectionScope.element(Element element) { | |
38 if (element == null) { | |
39 throw new ArgumentError('Root element for SelectionScope cannot be null'); | |
40 } | |
41 _root = element; | |
42 } | |
43 | |
44 /** | |
45 * Returns the [root] element for this scope. | |
46 * All [Selection]s and elements created using the Charted API, | |
47 * are created as descendants of root. | |
48 */ | |
49 Element get root => _root; | |
50 | |
51 /* | |
52 * Current event for which a callback is being called. | |
53 */ | |
54 Event event; | |
55 | |
56 /** Returns the stored for the given [element]. */ | |
57 datum(Object element) => element == null ? null : _associations[element]; | |
58 | |
59 /** Associates data to the given [element]. */ | |
60 associate(Object element, datum) => | |
61 datum != null ? _associations[element] = datum : null; | |
62 | |
63 /** | |
64 * Creates a new [Selection] containing the first element matching | |
65 * [selector]. If no element matches, the resulting selection will | |
66 * have a null element. | |
67 */ | |
68 Selection select(String selector) => | |
69 new _SelectionImpl.single(selector: selector, scope: this); | |
70 | |
71 /** | |
72 * Creates a new [Selection] containing all elements matching [selector]. | |
73 * If no element matches, the resulting selection will not have any | |
74 * elements in it. | |
75 */ | |
76 Selection selectAll(String selector) => | |
77 new _SelectionImpl.all(selector:selector, scope:this); | |
78 | |
79 /** | |
80 * Creates a new [Selection] containing [elements]. Assumes that | |
81 * all the given elements are descendants of [root] in DOM. | |
82 */ | |
83 Selection selectElements(List<Element> elements) => | |
84 new _SelectionImpl.elements(elements, this); | |
85 | |
86 /** | |
87 * Appends a new element to [root] and creates a selection containing | |
88 * the newly added element. | |
89 */ | |
90 Selection append(String tag) { | |
91 var element = Namespace.createChildElement(tag, _root); | |
92 root.children.add(element); | |
93 | |
94 return selectElements([element]); | |
95 } | |
96 } | |
OLD | NEW |