This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
MaterialDraggable.dart
147 lines (105 loc) · 4.39 KB
/
MaterialDraggable.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* 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 mdldnd;
/// Singleton - used by MaterialDraggable and MaterialDroppable
class DragInfo {
static DragInfo _draginfo;
var data;
List<String> allowedDropZones = [];
factory DragInfo () {
if(_draginfo == null) {
_draginfo = new DragInfo._internal();
}
return _draginfo;
}
DragInfo._internal();
}
/// 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 _MaterialDraggableCssClasses {
final String IS_UPGRADED = 'is-upgraded';
final String DRAGGABLE = 'mdl-draggable';
final String DND_DRAGGABLE = 'dnd-draggable';
const _MaterialDraggableCssClasses(); }
/// Store constants in one place so they can be updated easily.
class _MaterialDraggableConstant {
static const String WIDGET_SELECTOR = "mdl-draggable";
final String DROP_ZONE = "drop-zone";
const _MaterialDraggableConstant();
}
class MaterialDraggable extends MdlComponent implements MdlDataConsumer {
final Logger _logger = new Logger('mdldnd.MaterialDraggable');
static const _MaterialDraggableCssClasses _cssClasses = const _MaterialDraggableCssClasses();
static const _MaterialDraggableConstant _constants = const _MaterialDraggableConstant();
final DragInfo _dragInfo = new DragInfo();
Draggable _draggable;
var _consumedData;
MaterialDraggable.fromElement(final dom.HtmlElement element,final di.Injector injector)
: super(element,injector) {
_init();
}
static MaterialDraggable widget(final dom.HtmlElement element) => mdlComponent(element,MaterialDraggable) as MaterialDraggable;
// Central Element - by default this is where mdldraggable can be found (element)
// html.Element get hub => inputElement;
get _isDisabled => ElementProperties.isDisabled(element);
void consume(final data) {
//_logger.info("Consumed: $data");
_consumedData = data;
}
//- private -----------------------------------------------------------------------------------
void _init() {
_logger.fine("MaterialDraggable - init");
/// Recommended - add SELECTOR as class
element.classes.add(_MaterialDraggableConstant.WIDGET_SELECTOR);
element.classes.add(_cssClasses.DRAGGABLE);
element.classes.add(_cssClasses.DND_DRAGGABLE);
_draggable = new Draggable(element, avatarHandler: new AvatarHandler.clone());
_draggable.onDragStart.listen(_onDragStart);
_draggable.onDragEnd.listen(_onDragEnd);
element.classes.add(_cssClasses.IS_UPGRADED);
}
void _onDragStart(final DraggableEvent event) {
_logger.fine("_onDragStart ${event}");
if (_isDisabled) {
return;
}
List<String> _allowedDropZones() {
if(element.attributes.containsKey(_constants.DROP_ZONE)) {
return element.attributes[_constants.DROP_ZONE].split(",");
}
return new List<String>();
}
_dragInfo.allowedDropZones = _allowedDropZones();
_dragInfo.data = _consumedData;
}
void _onDragEnd(final DraggableEvent event) {
_logger.fine("_onDragEnd ${event}");
_dragInfo.data = null;
}
}
/// registration-Helper
void registerMaterialDraggable() {
final MdlConfig config = new MdlConfig<MaterialDraggable>(
_MaterialDraggableConstant.WIDGET_SELECTOR,
(final dom.HtmlElement element,final di.Injector injector) => new MaterialDraggable.fromElement(element,injector)
);
config.selectorType = SelectorType.TAG;
componentHandler().register(config);
}