row_ctrl.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.row.collapse = false;
  76. this.dropView = this.dropView === 1 ? 0 : 1;
  77. }
  78. showRowOptions() {
  79. this.dropView = this.dropView === 2 ? 0 : 2;
  80. }
  81. }
  82. export function rowDirective($rootScope) {
  83. return {
  84. restrict: 'E',
  85. templateUrl: 'public/app/features/dashboard/row/row.html',
  86. controller: DashRowCtrl,
  87. bindToController: true,
  88. controllerAs: 'ctrl',
  89. scope: {
  90. dashboard: "=",
  91. row: "=",
  92. },
  93. link: function(scope, element) {
  94. scope.$watchGroup(['ctrl.row.collapse', 'ctrl.row.height'], function() {
  95. element.find('.panels-wrapper').css({minHeight: scope.ctrl.row.collapse ? '5px' : scope.ctrl.row.height});
  96. });
  97. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  98. var hasPanel = _.find(scope.ctrl.row.panels, {id: info.panelId});
  99. if (!hasPanel) {
  100. element.hide();
  101. }
  102. }, scope);
  103. $rootScope.onAppEvent('panel-fullscreen-exit', function() {
  104. element.show();
  105. }, scope);
  106. }
  107. };
  108. }
  109. coreModule.directive('dashRow', rowDirective);
  110. coreModule.directive('panelWidth', function($rootScope) {
  111. return function(scope, element) {
  112. var fullscreen = false;
  113. function updateWidth() {
  114. if (!fullscreen) {
  115. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  116. }
  117. }
  118. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  119. fullscreen = true;
  120. if (scope.panel.id !== info.panelId) {
  121. element.hide();
  122. } else {
  123. element[0].style.width = '100%';
  124. }
  125. }, scope);
  126. $rootScope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  127. fullscreen = false;
  128. if (scope.panel.id !== info.panelId) {
  129. element.show();
  130. }
  131. updateWidth();
  132. }, scope);
  133. scope.$watch('panel.span', updateWidth);
  134. if (fullscreen) {
  135. element.hide();
  136. }
  137. };
  138. });
  139. coreModule.directive('panelDropZone', function($timeout) {
  140. return function(scope, element) {
  141. var row = scope.ctrl.row;
  142. var indrag = false;
  143. var textEl = element.find('.panel-drop-zone-text');
  144. function showPanel(span, text) {
  145. element.find('.panel-container').css('height', row.height);
  146. element[0].style.width = ((span / 1.2) * 10) + '%';
  147. textEl.text(text);
  148. element.show();
  149. }
  150. function hidePanel() {
  151. element.hide();
  152. // element.removeClass('panel-drop-zone--empty');
  153. }
  154. function updateState() {
  155. if (scope.ctrl.dashboard.editMode) {
  156. if (row.panels.length === 0 && indrag === false) {
  157. return showPanel(12, 'Empty Space');
  158. }
  159. var dropZoneSpan = 12 - scope.ctrl.dashboard.rowSpan(scope.ctrl.row);
  160. if (dropZoneSpan > 1) {
  161. if (indrag) {
  162. return showPanel(dropZoneSpan, 'Drop Here');
  163. } else {
  164. return showPanel(dropZoneSpan, 'Empty Space');
  165. }
  166. }
  167. }
  168. if (indrag === true) {
  169. return showPanel(dropZoneSpan, 'Drop Here');
  170. }
  171. hidePanel();
  172. }
  173. scope.row.events.on('panel-added', updateState);
  174. scope.row.events.on('span-changed', updateState);
  175. scope.$watchGroup(['ctrl.row.panels.length', 'ctrl.dashboard.editMode', 'ctrl.row.span'], updateState);
  176. scope.$on("ANGULAR_DRAG_START", function() {
  177. indrag = true;
  178. updateState();
  179. // $timeout(function() {
  180. // var dropZoneSpan = 12 - scope.ctrl.dashboard.rowSpan(scope.ctrl.row);
  181. // if (dropZoneSpan > 0) {
  182. // showPanel(dropZoneSpan, 'Panel Drop Zone');
  183. // }
  184. // });
  185. });
  186. scope.$on("ANGULAR_DRAG_END", function() {
  187. indrag = false;
  188. updateState();
  189. });
  190. };
  191. });