2 * Copyright (c) 2014 DataTorrent, Inc. ALL Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 angular.module('ui.dashboard')
20 .factory('WidgetModel', function ($log) {
27 enableVerticalResize: true,
28 containerStyle: { width: '33%' }, // default width
33 // constructor for widget model instances
34 function WidgetModel(widgetDefinition, overrides) {
36 // Extend this with the widget definition object with overrides merged in (deep extended).
37 angular.extend(this, defaults(), _.merge(angular.copy(widgetDefinition), overrides));
39 this.updateContainerStyle(this.style);
41 if (!this.templateUrl && !this.template && !this.directive) {
42 this.directive = widgetDefinition.name;
45 if (this.size && _.has(this.size, 'height')) {
46 this.setHeight(this.size.height);
49 if (this.style && _.has(this.style, 'width')) { //TODO deprecate style attribute
50 this.setWidth(this.style.width);
53 if (this.size && _.has(this.size, 'width')) {
54 this.setWidth(this.size.width);
58 WidgetModel.prototype = {
59 // sets the width (and widthUnits)
60 setWidth: function (width, units) {
61 width = width.toString();
62 units = units || width.replace(/^[-\.\d]+/, '') || '%';
64 this.widthUnits = units;
65 width = parseFloat(width);
67 // check with min width if set, unit refer to width's unit
68 if (this.size && _.has(this.size, 'minWidth')) {
69 width = _.max([parseFloat(this.size.minWidth), width]);
72 if (width < 0 || isNaN(width)) {
73 $log.warn('malhar-angular-dashboard: setWidth was called when width was ' + width);
78 width = Math.min(100, width);
79 width = Math.max(0, width);
82 this.containerStyle.width = width + '' + units;
84 this.updateSize(this.containerStyle);
89 setHeight: function (height) {
90 this.contentStyle.height = height;
91 this.updateSize(this.contentStyle);
94 setStyle: function (style) {
96 this.updateContainerStyle(style);
99 updateSize: function (size) {
100 angular.extend(this.size, size);
103 updateContainerStyle: function (style) {
104 angular.extend(this.containerStyle, style);
106 serialize: function() {
107 return _.pick(this, ['title', 'name', 'report_id', 'hideGrid', 'showChart' ,'rcloud_url','reportData','style', 'size', 'dataModelOptions', 'attrs', 'storageHash']);