row_ctrl.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import {coreModule, appEvents} from 'app/core/core';
  5. import './options';
  6. import './add_panel';
  7. export class DashRowCtrl {
  8. dashboard: any;
  9. row: any;
  10. dropView: number;
  11. /** @ngInject */
  12. constructor(private $scope, private $rootScope, private $timeout, private uiSegmentSrv, private $q) {
  13. this.row.title = this.row.title || 'Row title';
  14. if (this.row.isNew) {
  15. this.dropView = 1;
  16. delete this.row.isNew;
  17. }
  18. }
  19. onDrop(panelId, dropTarget) {
  20. var dragObject;
  21. // if string it's a panel type
  22. if (_.isString(panelId)) {
  23. // setup new panel
  24. dragObject = {
  25. row: this.row,
  26. panel: {
  27. title: config.new_panel_title,
  28. type: panelId,
  29. id: this.dashboard.getNextPanelId(),
  30. },
  31. isNew: true,
  32. };
  33. } else {
  34. dragObject = this.dashboard.getPanelInfoById(panelId);
  35. }
  36. if (dropTarget) {
  37. dropTarget = this.dashboard.getPanelInfoById(dropTarget.id);
  38. // if draging new panel onto existing panel split it
  39. if (dragObject.isNew) {
  40. dragObject.panel.span = dropTarget.panel.span = dropTarget.panel.span/2;
  41. // insert after
  42. dropTarget.row.panels.splice(dropTarget.index+1, 0, dragObject.panel);
  43. } else if (this.row === dragObject.row) {
  44. // just move element
  45. this.row.movePanel(dragObject.index, dropTarget.index);
  46. } else {
  47. // split drop target space
  48. dragObject.panel.span = dropTarget.panel.span = dropTarget.panel.span/2;
  49. // insert after
  50. dropTarget.row.panels.splice(dropTarget.index+1, 0, dragObject.panel);
  51. // remove from source row
  52. dragObject.row.removePanel(dragObject.panel, false);
  53. }
  54. } else {
  55. dragObject.panel.span = 12 - this.row.span;
  56. this.row.panels.push(dragObject.panel);
  57. // if not new remove from source row
  58. if (!dragObject.isNew) {
  59. dragObject.row.removePanel(dragObject.panel, false);
  60. }
  61. }
  62. this.dropView = 0;
  63. this.row.panelSpanChanged();
  64. this.$timeout(() => {
  65. this.$rootScope.$broadcast('render');
  66. });
  67. }
  68. setHeight(height) {
  69. this.row.height = height;
  70. this.$scope.$broadcast('render');
  71. }
  72. moveRow(direction) {
  73. var rowsList = this.dashboard.rows;
  74. var rowIndex = _.indexOf(rowsList, this.row);
  75. var newIndex = rowIndex + direction;
  76. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  77. _.move(rowsList, rowIndex, newIndex);
  78. }
  79. }
  80. toggleCollapse() {
  81. this.closeDropView();
  82. this.row.collapse = !this.row.collapse;
  83. }
  84. onMenuAddPanel() {
  85. this.dropView = 1;
  86. }
  87. onMenuRowOptions() {
  88. this.dropView = 2;
  89. }
  90. closeDropView() {
  91. this.dropView = 0;
  92. }
  93. onMenuDeleteRow() {
  94. this.dashboard.removeRow(this.row);
  95. }
  96. }
  97. coreModule.directive('dashRow', function($rootScope) {
  98. return {
  99. restrict: 'E',
  100. templateUrl: 'public/app/features/dashboard/row/row.html',
  101. controller: DashRowCtrl,
  102. bindToController: true,
  103. controllerAs: 'ctrl',
  104. scope: {
  105. dashboard: "=",
  106. row: "=",
  107. },
  108. link: function(scope, element) {
  109. scope.$watchGroup(['ctrl.row.collapse', 'ctrl.row.height'], function() {
  110. element.find('.panels-wrapper').css({minHeight: scope.ctrl.row.collapse ? '5px' : scope.ctrl.row.height});
  111. });
  112. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  113. var hasPanel = _.find(scope.ctrl.row.panels, {id: info.panelId});
  114. if (!hasPanel) {
  115. element.hide();
  116. }
  117. }, scope);
  118. $rootScope.onAppEvent('panel-fullscreen-exit', function() {
  119. element.show();
  120. }, scope);
  121. }
  122. };
  123. });
  124. coreModule.directive('panelWidth', function($rootScope) {
  125. return function(scope, element) {
  126. var fullscreen = false;
  127. function updateWidth() {
  128. if (!fullscreen) {
  129. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  130. }
  131. }
  132. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  133. fullscreen = true;
  134. if (scope.panel.id !== info.panelId) {
  135. element.hide();
  136. } else {
  137. element[0].style.width = '100%';
  138. }
  139. }, scope);
  140. $rootScope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  141. fullscreen = false;
  142. if (scope.panel.id !== info.panelId) {
  143. element.show();
  144. }
  145. updateWidth();
  146. }, scope);
  147. scope.$watch('panel.span', updateWidth);
  148. if (fullscreen) {
  149. element.hide();
  150. }
  151. };
  152. });
  153. coreModule.directive('panelDropZone', function($timeout) {
  154. return function(scope, element) {
  155. var row = scope.ctrl.row;
  156. var dashboard = scope.ctrl.dashboard;
  157. var indrag = false;
  158. var textEl = element.find('.panel-drop-zone-text');
  159. function showPanel(span, text) {
  160. element.find('.panel-container').css('height', row.height);
  161. element[0].style.width = ((span / 1.2) * 10) + '%';
  162. textEl.text(text);
  163. element.show();
  164. }
  165. function hidePanel() {
  166. element.hide();
  167. }
  168. function updateState() {
  169. if (scope.ctrl.dashboard.editMode) {
  170. if (row.panels.length === 0 && indrag === false) {
  171. return showPanel(12, 'Empty Space');
  172. }
  173. var dropZoneSpan = 12 - row.span;
  174. if (dropZoneSpan > 0) {
  175. if (indrag) {
  176. return showPanel(dropZoneSpan, 'Drop Here');
  177. } else {
  178. return showPanel(dropZoneSpan, 'Empty Space');
  179. }
  180. }
  181. }
  182. if (indrag === true) {
  183. var dropZoneSpan = 12 - row.span;
  184. if (dropZoneSpan > 1) {
  185. return showPanel(dropZoneSpan, 'Drop Here');
  186. }
  187. }
  188. hidePanel();
  189. }
  190. row.events.on('span-changed', updateState, scope);
  191. dashboard.events.emit('edit-mode-changed', updateState, scope);
  192. scope.$on("ANGULAR_DRAG_START", function() {
  193. indrag = true;
  194. updateState();
  195. });
  196. scope.$on("ANGULAR_DRAG_END", function() {
  197. indrag = false;
  198. updateState();
  199. });
  200. };
  201. });