row_ctrl.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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) {
  13. this.row.title = this.row.title || 'Row title';
  14. if (this.row.isNew) {
  15. this.dropView = 1;
  16. }
  17. }
  18. onDrop(panelId, dropTarget) {
  19. var dragObject;
  20. // if string it's a panel type
  21. if (_.isString(panelId)) {
  22. // setup new panel
  23. dragObject = {
  24. row: this.row,
  25. panel: {
  26. title: config.new_panel_title,
  27. type: panelId,
  28. id: this.dashboard.getNextPanelId(),
  29. isNew: true,
  30. },
  31. };
  32. } else {
  33. dragObject = this.dashboard.getPanelInfoById(panelId);
  34. }
  35. if (dropTarget) {
  36. dropTarget = this.dashboard.getPanelInfoById(dropTarget.id);
  37. // if draging new panel onto existing panel split it
  38. if (dragObject.panel.isNew) {
  39. dragObject.panel.span = dropTarget.panel.span = dropTarget.panel.span/2;
  40. // insert after
  41. dropTarget.row.panels.splice(dropTarget.index+1, 0, dragObject.panel);
  42. } else if (this.row === dragObject.row) {
  43. // just move element
  44. this.row.movePanel(dragObject.index, dropTarget.index);
  45. } else {
  46. // split drop target space
  47. dragObject.panel.span = dropTarget.panel.span = dropTarget.panel.span/2;
  48. // insert after
  49. dropTarget.row.panels.splice(dropTarget.index+1, 0, dragObject.panel);
  50. // remove from source row
  51. dragObject.row.removePanel(dragObject.panel, false);
  52. }
  53. } else {
  54. dragObject.panel.span = 12 - this.row.span;
  55. this.row.panels.push(dragObject.panel);
  56. // if not new remove from source row
  57. if (!dragObject.panel.isNew) {
  58. dragObject.row.removePanel(dragObject.panel, false);
  59. }
  60. }
  61. this.dropView = 0;
  62. this.row.panelSpanChanged();
  63. this.$timeout(() => {
  64. this.$rootScope.$broadcast('render');
  65. });
  66. }
  67. setHeight(height) {
  68. this.row.height = height;
  69. this.$scope.$broadcast('render');
  70. }
  71. moveRow(direction) {
  72. var rowsList = this.dashboard.rows;
  73. var rowIndex = _.indexOf(rowsList, this.row);
  74. var newIndex = rowIndex + direction;
  75. if (newIndex >= 0 && newIndex <= (rowsList.length - 1)) {
  76. _.move(rowsList, rowIndex, newIndex);
  77. }
  78. }
  79. toggleCollapse() {
  80. this.closeDropView();
  81. this.row.collapse = !this.row.collapse;
  82. }
  83. onMenuAddPanel() {
  84. this.dropView = 1;
  85. }
  86. onMenuRowOptions() {
  87. this.dropView = 2;
  88. }
  89. closeDropView() {
  90. this.dropView = 0;
  91. }
  92. onMenuDeleteRow() {
  93. this.dashboard.removeRow(this.row);
  94. }
  95. }
  96. coreModule.directive('dashRow', function($rootScope) {
  97. return {
  98. restrict: 'E',
  99. templateUrl: 'public/app/features/dashboard/row/row.html',
  100. controller: DashRowCtrl,
  101. bindToController: true,
  102. controllerAs: 'ctrl',
  103. scope: {
  104. dashboard: "=",
  105. row: "=",
  106. },
  107. link: function(scope, element) {
  108. scope.$watchGroup(['ctrl.row.collapse', 'ctrl.row.height'], function() {
  109. element.toggleClass('dash-row--collapse', scope.ctrl.row.collapse);
  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 (row.panels.length === 0 && indrag === false) {
  170. return showPanel(12, 'Empty Space');
  171. }
  172. var dropZoneSpan = 12 - row.span;
  173. if (dropZoneSpan > 0) {
  174. if (indrag) {
  175. return showPanel(dropZoneSpan, 'Drop Here');
  176. } else {
  177. return showPanel(dropZoneSpan, 'Empty Space');
  178. }
  179. }
  180. if (indrag === true) {
  181. var dropZoneSpan = 12 - row.span;
  182. if (dropZoneSpan > 1) {
  183. return showPanel(dropZoneSpan, 'Drop Here');
  184. }
  185. }
  186. hidePanel();
  187. }
  188. row.events.on('span-changed', updateState, scope);
  189. scope.$on("ANGULAR_DRAG_START", function() {
  190. indrag = true;
  191. updateState();
  192. });
  193. scope.$on("ANGULAR_DRAG_END", function() {
  194. indrag = false;
  195. updateState();
  196. });
  197. updateState();
  198. };
  199. });