-
Notifications
You must be signed in to change notification settings - Fork 297
/
WidgetModel.js
112 lines (90 loc) · 3.31 KB
/
WidgetModel.js
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
/*
* Copyright (c) 2014 DataTorrent, Inc. 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.
*/
'use strict';
angular.module('ui.dashboard')
.factory('WidgetModel', function ($log) {
function defaults() {
return {
title: 'Widget',
style: {},
size: { width: '33%' },
enableVerticalResize: true,
containerStyle: { width: '33%' }, // default width
contentStyle: {}
};
};
// constructor for widget model instances
function WidgetModel(widgetDefinition, overrides) {
// Extend this with the widget definition object with overrides merged in (deep extended).
angular.extend(this, defaults(), _.merge(angular.copy(widgetDefinition), overrides));
this.updateContainerStyle(this.style);
if (!this.templateUrl && !this.template && !this.directive) {
this.directive = widgetDefinition.name;
}
if (this.size && _.has(this.size, 'height')) {
this.setHeight(this.size.height);
}
if (this.style && _.has(this.style, 'width')) { //TODO deprecate style attribute
this.setWidth(this.style.width);
}
if (this.size && _.has(this.size, 'width')) {
this.setWidth(this.size.width);
}
}
WidgetModel.prototype = {
// sets the width (and widthUnits)
setWidth: function (width, units) {
width = width.toString();
units = units || width.replace(/^[-\.\d]+/, '') || '%';
this.widthUnits = units;
width = parseFloat(width);
// check with min width if set, unit refer to width's unit
if (this.size && _.has(this.size, 'minWidth') && _.endsWith(this.size.minWidth, units)) {
width = _.max([parseFloat(this.size.minWidth), width]);
}
if (width < 0 || isNaN(width)) {
$log.warn('malhar-angular-dashboard: setWidth was called when width was ' + width);
return;
}
if (units === '%') {
width = Math.min(100, width);
width = Math.max(0, width);
}
this.containerStyle.width = width + '' + units;
this.updateSize(this.containerStyle);
return width + units;
},
setHeight: function (height) {
this.contentStyle.height = height;
this.updateSize(this.contentStyle);
return height + 'px';
},
setStyle: function (style) {
this.style = style;
this.updateContainerStyle(style);
},
updateSize: function (size) {
angular.extend(this.size, size);
},
updateContainerStyle: function (style) {
angular.extend(this.containerStyle, style);
},
serialize: function() {
return _.pick(this, ['title', 'name', 'style', 'size', 'dataModelOptions', 'attrs', 'storageHash']);
}
};
return WidgetModel;
});