bbf107a811c7a67e0f1d61c025d050e6869905cf
[portal/sdk.git] /
1 /*
2  * Copyright (c) 2014 DataTorrent, Inc. ALL Rights Reserved.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 'use strict';
18
19 angular.module('ui.dashboard')
20   .directive('dashboardLayouts', ['LayoutStorage', '$timeout', '$uibModal',
21     function(LayoutStorage, $timeout, $uibModal) {
22       return {
23         scope: true,
24         templateUrl: function(element, attr) {
25           return attr.templateUrl ? attr.templateUrl : 'app/fusion/scripts/view-models/reportdashboard-page/src/components/directives/dashboardLayouts/dashboardLayouts.html';
26         },
27         link: function(scope, element, attrs) {
28
29           scope.options = scope.$eval(attrs.dashboardLayouts);
30
31           var layoutStorage = new LayoutStorage(scope.options);
32
33           scope.layouts = layoutStorage.layouts;
34
35           scope.createNewLayout = function() {
36             var newLayout = {
37               title: 'Custom',
38               defaultWidgets: scope.options.defaultWidgets || []
39             };
40             layoutStorage.add(newLayout);
41             scope.makeLayoutActive(newLayout);
42             layoutStorage.save();
43             return newLayout;
44           };
45
46           scope.removeLayout = function(layout) {
47             layoutStorage.remove(layout);
48             layoutStorage.save();
49           };
50
51           scope.makeLayoutActive = function(layout) {
52
53             var current = layoutStorage.getActiveLayout();
54
55             if (current && current.dashboard.unsavedChangeCount) {
56               var modalInstance = $uibModal.open({
57                 templateUrl: 'template/SaveChangesModal.html',
58                 resolve: {
59                   layout: function() {
60                     return layout;
61                   }
62                 },
63                 controller: 'SaveChangesModalCtrl'
64               });
65
66               // Set resolve and reject callbacks for the result promise
67               modalInstance.result.then(
68                 function() {
69                   current.dashboard.saveDashboard();
70                   scope._makeLayoutActive(layout);
71                 },
72                 function() {
73                   scope._makeLayoutActive(layout);
74                 }
75               );
76             } else {
77               scope._makeLayoutActive(layout);
78             }
79
80           };
81
82           scope._makeLayoutActive = function(layout) {
83             angular.forEach(scope.layouts, function(l) {
84               if (l !== layout) {
85                 l.active = false;
86               } else {
87                 l.active = true;
88               }
89             });
90             layoutStorage.save();
91           };
92
93           scope.isActive = function(layout) {
94             return !!layout.active;
95           };
96
97           scope.editTitle = function(layout) {
98             if (layout.locked) {
99               return;
100             }
101
102             var input = element.find('input[data-layout="' + layout.id + '"]');
103             layout.editingTitle = true;
104
105             $timeout(function() {
106               input.focus()[0].setSelectionRange(0, 9999);
107             });
108           };
109
110           // saves whatever is in the title input as the new title
111           scope.saveTitleEdit = function(layout) {
112             layout.editingTitle = false;
113             layoutStorage.save();
114           };
115
116           scope.options.saveLayouts = function() {
117             layoutStorage.save(true);
118           };
119           scope.options.addWidget = function() {
120             var layout = layoutStorage.getActiveLayout();
121             if (layout) {
122               layout.dashboard.addWidget.apply(layout.dashboard, arguments);
123             }
124           };
125           scope.options.loadWidgets = function() {
126             var layout = layoutStorage.getActiveLayout();
127             if (layout) {
128               layout.dashboard.loadWidgets.apply(layout.dashboard, arguments);
129             }
130           };
131           scope.options.saveDashboard = function() {
132                   console.log("================= saveDashboard called =================")
133             var layout = layoutStorage.getActiveLayout();
134                   console.log("===================== layout ===========================");
135                   console.log(layout);
136             if (layout) {
137               layout.dashboard.saveDashboard.apply(layout.dashboard, arguments);
138             }
139           };
140
141           var sortableDefaults = {
142             stop: function() {
143               scope.options.saveLayouts();
144             },
145             distance: 5
146           };
147           scope.sortableOptions = angular.extend({}, sortableDefaults, scope.options.sortableOptions || {});
148         }
149       };
150     }
151   ]);