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 .directive('dashboardLayouts', ['LayoutStorage', '$timeout', '$uibModal',
21 function(LayoutStorage, $timeout, $uibModal) {
24 templateUrl: function(element, attr) {
25 return attr.templateUrl ? attr.templateUrl : 'app/fusion/scripts/view-models/reportdashboard-page/src/components/directives/dashboardLayouts/dashboardLayouts.html';
27 link: function(scope, element, attrs) {
29 scope.options = scope.$eval(attrs.dashboardLayouts);
31 var layoutStorage = new LayoutStorage(scope.options);
33 scope.layouts = layoutStorage.layouts;
35 scope.createNewLayout = function() {
38 defaultWidgets: scope.options.defaultWidgets || []
40 layoutStorage.add(newLayout);
41 scope.makeLayoutActive(newLayout);
46 scope.removeLayout = function(layout) {
47 layoutStorage.remove(layout);
51 scope.makeLayoutActive = function(layout) {
53 var current = layoutStorage.getActiveLayout();
55 if (current && current.dashboard.unsavedChangeCount) {
56 var modalInstance = $uibModal.open({
57 templateUrl: 'template/SaveChangesModal.html',
63 controller: 'SaveChangesModalCtrl'
66 // Set resolve and reject callbacks for the result promise
67 modalInstance.result.then(
69 current.dashboard.saveDashboard();
70 scope._makeLayoutActive(layout);
73 scope._makeLayoutActive(layout);
77 scope._makeLayoutActive(layout);
82 scope._makeLayoutActive = function(layout) {
83 angular.forEach(scope.layouts, function(l) {
93 scope.isActive = function(layout) {
94 return !!layout.active;
97 scope.editTitle = function(layout) {
102 var input = element.find('input[data-layout="' + layout.id + '"]');
103 layout.editingTitle = true;
105 $timeout(function() {
106 input.focus()[0].setSelectionRange(0, 9999);
110 // saves whatever is in the title input as the new title
111 scope.saveTitleEdit = function(layout) {
112 layout.editingTitle = false;
113 layoutStorage.save();
116 scope.options.saveLayouts = function() {
117 layoutStorage.save(true);
119 scope.options.addWidget = function() {
120 var layout = layoutStorage.getActiveLayout();
122 layout.dashboard.addWidget.apply(layout.dashboard, arguments);
125 scope.options.loadWidgets = function() {
126 var layout = layoutStorage.getActiveLayout();
128 layout.dashboard.loadWidgets.apply(layout.dashboard, arguments);
131 scope.options.saveDashboard = function() {
132 console.log("================= saveDashboard called =================")
133 var layout = layoutStorage.getActiveLayout();
134 console.log("===================== layout ===========================");
137 layout.dashboard.saveDashboard.apply(layout.dashboard, arguments);
141 var sortableDefaults = {
143 scope.options.saveLayouts();
147 scope.sortableOptions = angular.extend({}, sortableDefaults, scope.options.sortableOptions || {});