row.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import angular from 'angular';
  5. import config from 'app/core/config';
  6. import {coreModule} from 'app/core/core';
  7. import './options';
  8. import './add_panel';
  9. export class DashRowCtrl {
  10. dashboard: any;
  11. row: any;
  12. dropView: number;
  13. /** @ngInject */
  14. constructor(private $scope, private $rootScope, private $timeout, private uiSegmentSrv, private $q) {
  15. this.row.title = this.row.title || 'Row title';
  16. if (this.dashboard.meta.isNew) {
  17. this.dropView = 1;
  18. delete this.row.isNew;
  19. }
  20. }
  21. onDrop(panelId, dropTarget) {
  22. var info = this.dashboard.getPanelInfoById(panelId);
  23. if (dropTarget) {
  24. var dropInfo = this.dashboard.getPanelInfoById(dropTarget.id);
  25. dropInfo.row.panels[dropInfo.index] = info.panel;
  26. info.row.panels[info.index] = dropTarget;
  27. var dragSpan = info.panel.span;
  28. info.panel.span = dropTarget.span;
  29. dropTarget.span = dragSpan;
  30. } else {
  31. info.row.panels.splice(info.index, 1);
  32. info.panel.span = 12 - this.dashboard.rowSpan(this.row);
  33. this.row.panels.push(info.panel);
  34. }
  35. this.$rootScope.$broadcast('render');
  36. }
  37. setHeight(height) {
  38. this.row.height = height;
  39. this.$scope.$broadcast('render');
  40. }
  41. moveRow(direction) {
  42. var rowsList = this.dashboard.rows;
  43. var rowIndex = _.indexOf(rowsList, this.row);
  44. var newIndex = rowIndex;
  45. switch (direction) {
  46. case 'up': {
  47. newIndex = rowIndex - 1;
  48. break;
  49. }
  50. case 'down': {
  51. newIndex = rowIndex + 1;
  52. break;
  53. }
  54. case 'top': {
  55. newIndex = 0;
  56. break;
  57. }
  58. case 'bottom': {
  59. newIndex = rowsList.length - 1;
  60. break;
  61. }
  62. default: {
  63. newIndex = rowIndex;
  64. }
  65. }
  66. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  67. _.move(rowsList, rowIndex, newIndex);
  68. }
  69. }
  70. toggleCollapse() {
  71. this.dropView = 0;
  72. this.row.collapse = !this.row.collapse;
  73. }
  74. showAddPanel() {
  75. this.dropView = this.dropView === 1 ? 0 : 1;
  76. }
  77. showRowOptions() {
  78. this.dropView = this.dropView === 2 ? 0 : 2;
  79. }
  80. }
  81. export function rowDirective($rootScope) {
  82. return {
  83. restrict: 'E',
  84. templateUrl: 'public/app/features/dashboard/row/row.html',
  85. controller: DashRowCtrl,
  86. bindToController: true,
  87. controllerAs: 'ctrl',
  88. scope: {
  89. dashboard: "=",
  90. row: "=",
  91. },
  92. link: function(scope, element) {
  93. scope.$watchGroup(['ctrl.row.collapse', 'ctrl.row.height', 'ctrl.row.showTitle', 'ctrl.dropView'], function() {
  94. element.css({minHeight: scope.ctrl.row.collapse ? '5px' : scope.ctrl.row.height});
  95. element.toggleClass('dash-row-show-title', scope.ctrl.row.showTitle === true);
  96. element.toggleClass('dash-row-show-options', scope.ctrl.dropView === 2);
  97. element.toggleClass('dash-row-show-add-panel', scope.ctrl.dropView === 1);
  98. });
  99. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  100. var hasPanel = _.find(scope.ctrl.row.panels, {id: info.panelId});
  101. if (!hasPanel) {
  102. element.hide();
  103. }
  104. }, scope);
  105. $rootScope.onAppEvent('panel-fullscreen-exit', function() {
  106. element.show();
  107. }, scope);
  108. }
  109. };
  110. }
  111. coreModule.directive('dashRow', rowDirective);
  112. coreModule.directive('panelWidth', function($rootScope) {
  113. return function(scope, element) {
  114. var fullscreen = false;
  115. function updateWidth() {
  116. if (!fullscreen) {
  117. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  118. }
  119. }
  120. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  121. fullscreen = true;
  122. if (scope.panel.id !== info.panelId) {
  123. element.hide();
  124. } else {
  125. element[0].style.width = '100%';
  126. }
  127. }, scope);
  128. $rootScope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  129. fullscreen = false;
  130. if (scope.panel.id !== info.panelId) {
  131. element.show();
  132. }
  133. updateWidth();
  134. }, scope);
  135. scope.$watch('panel.span', updateWidth);
  136. if (fullscreen) {
  137. element.hide();
  138. }
  139. };
  140. });
  141. coreModule.directive('panelDropZone', function($timeout) {
  142. return function(scope, element) {
  143. scope.$on("ANGULAR_DRAG_START", function() {
  144. $timeout(function() {
  145. var dropZoneSpan = 12 - scope.ctrl.dashboard.rowSpan(scope.ctrl.row);
  146. if (dropZoneSpan > 0) {
  147. element.find('.panel-container').css('height', scope.ctrl.row.height);
  148. element[0].style.width = ((dropZoneSpan / 1.2) * 10) + '%';
  149. element.show();
  150. }
  151. });
  152. });
  153. scope.$on("ANGULAR_DRAG_END", function() {
  154. element.hide();
  155. });
  156. };
  157. });