row_ctrl.ts 5.9 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) {
  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.toggleClass('dash-row--collapse', scope.ctrl.row.collapse);
  111. element.find('.panels-wrapper').css({minHeight: scope.ctrl.row.collapse ? '5px' : scope.ctrl.row.height});
  112. });
  113. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  114. var hasPanel = _.find(scope.ctrl.row.panels, {id: info.panelId});
  115. if (!hasPanel) {
  116. element.hide();
  117. }
  118. }, scope);
  119. $rootScope.onAppEvent('panel-fullscreen-exit', function() {
  120. element.show();
  121. }, scope);
  122. }
  123. };
  124. });
  125. coreModule.directive('panelWidth', function($rootScope) {
  126. return function(scope, element) {
  127. var fullscreen = false;
  128. function updateWidth() {
  129. if (!fullscreen) {
  130. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  131. }
  132. }
  133. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  134. fullscreen = true;
  135. if (scope.panel.id !== info.panelId) {
  136. element.hide();
  137. } else {
  138. element[0].style.width = '100%';
  139. }
  140. }, scope);
  141. $rootScope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  142. fullscreen = false;
  143. if (scope.panel.id !== info.panelId) {
  144. element.show();
  145. }
  146. updateWidth();
  147. }, scope);
  148. scope.$watch('panel.span', updateWidth);
  149. if (fullscreen) {
  150. element.hide();
  151. }
  152. };
  153. });
  154. coreModule.directive('panelDropZone', function($timeout) {
  155. return function(scope, element) {
  156. var row = scope.ctrl.row;
  157. var dashboard = scope.ctrl.dashboard;
  158. var indrag = false;
  159. var textEl = element.find('.panel-drop-zone-text');
  160. function showPanel(span, text) {
  161. element.find('.panel-container').css('height', row.height);
  162. element[0].style.width = ((span / 1.2) * 10) + '%';
  163. textEl.text(text);
  164. element.show();
  165. }
  166. function hidePanel() {
  167. element.hide();
  168. }
  169. function updateState() {
  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. if (indrag === true) {
  182. var dropZoneSpan = 12 - row.span;
  183. if (dropZoneSpan > 1) {
  184. return showPanel(dropZoneSpan, 'Drop Here');
  185. }
  186. }
  187. hidePanel();
  188. }
  189. row.events.on('span-changed', updateState, scope);
  190. scope.$on("ANGULAR_DRAG_START", function() {
  191. indrag = true;
  192. updateState();
  193. });
  194. scope.$on("ANGULAR_DRAG_END", function() {
  195. indrag = false;
  196. updateState();
  197. });
  198. updateState();
  199. };
  200. });