This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
/
MaterialModel.dart
122 lines (93 loc) · 4.19 KB
/
MaterialModel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
part of mdldirective;
/// Store strings for class names defined by this component that are used in
/// Dart. This allows us to simply change it in one place should we
/// decide to modify at a later date.
class _MaterialModelCssClasses {
final String IS_UPGRADED = 'is-upgraded';
const _MaterialModelCssClasses(); }
/// Store constants in one place so they can be updated easily.
class _MaterialModelConstant {
static const String WIDGET_SELECTOR = "mdl-model";
const _MaterialModelConstant();
}
class MaterialModel extends MdlComponent implements RefreshableComponent {
final Logger _logger = new Logger('mdldirective.MaterialModel');
//static const _MaterialModelConstant _constant = const _MaterialModelConstant();
static const _MaterialModelCssClasses _cssClasses = const _MaterialModelCssClasses();
Scope _scope;
final ModelObserverFactory _observerFactory;
MaterialModel.fromElement(final dom.HtmlElement element,final di.Injector injector)
: _observerFactory = injector.get(ModelObserverFactory), super(element,injector) {
}
@override
void attached() {
_scope = new Scope(this, mdlParentScope(this));
try {
_init();
} on NoSuchMethodError catch(e,stacktrace) {
// It's possible that at this moment the requested fieldname is not yet available
// MaterialDialog is a candidate for this.
// If MaterialDialog pops up attached() is called but only then the parent-scope is set
// via dialogComponent.parentScope = this;
if(!_scope.parentContext is HasDynamicParentScope) {
_logger.shout(e.toString(),e,stacktrace);
}
}
}
@override
/// Called in [_MaterialDialogComponent] if parent changes ([MaterialDialog] sets itself as parent!!!)
void refresh() {
_logger.fine("MaterialModel - refresh");
// Most important part - check if there is a new parent
_scope = new Scope(this, mdlParentScope(this));
// Remove previously registered Streams!
downgrade();
// Re-Init
_setupObserver();
}
//- private -----------------------------------------------------------------------------------
void _init() {
_logger.fine("MaterialModel - init");
/// Recommended - add SELECTOR as class
element.classes.add(_MaterialModelConstant.WIDGET_SELECTOR);
_setupObserver();
element.classes.add(_cssClasses.IS_UPGRADED);
}
/// Scope-Context is always the next (up the tree) ScopeAware parent
void _setupObserver() {
_scope.context = _scope.parentContext;
final ModelObserver observer = _observerFactory.createFor(element);
eventStreams.addAll( observer.observe( _scope ,_fieldname));
}
String get _fieldname => element.attributes[_MaterialModelConstant.WIDGET_SELECTOR].trim();
}
/// registration-Helper
void registerMaterialModel() {
final MdlConfig config = new MdlConfig<MaterialModel>(
_MaterialModelConstant.WIDGET_SELECTOR,
(final dom.HtmlElement element,final di.Injector injector) => new MaterialModel.fromElement(element,injector)
);
// If you want <mdl-model></mdl-model> set selectorType to SelectorType.TAG.
// If you want <div mdl-model></div> set selectorType to SelectorType.ATTRIBUTE.
// By default it's used as a class name. (<div class="mdl-model"></div>)
config.selectorType = SelectorType.ATTRIBUTE;
componentHandler().register(config);
}