row_ctrl.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import config from 'app/core/config';
  4. import {coreModule} 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. /** @ngInject */
  97. function dashRowDirective($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. /** @ngInject */
  126. function panelWidthDirective($rootScope) {
  127. return function(scope, element) {
  128. var fullscreen = false;
  129. function updateWidth() {
  130. if (!fullscreen) {
  131. element[0].style.width = ((scope.panel.span / 1.2) * 10) + '%';
  132. }
  133. }
  134. $rootScope.onAppEvent('panel-fullscreen-enter', function(evt, info) {
  135. fullscreen = true;
  136. if (scope.panel.id !== info.panelId) {
  137. element.hide();
  138. } else {
  139. element[0].style.width = '100%';
  140. }
  141. }, scope);
  142. $rootScope.onAppEvent('panel-fullscreen-exit', function(evt, info) {
  143. fullscreen = false;
  144. if (scope.panel.id !== info.panelId) {
  145. element.show();
  146. }
  147. updateWidth();
  148. }, scope);
  149. scope.$watch('panel.span', updateWidth);
  150. if (fullscreen) {
  151. element.hide();
  152. }
  153. };
  154. }
  155. /** @ngInject */
  156. function panelDropZoneDirective($timeout) {
  157. return function(scope, element) {
  158. var row = scope.ctrl.row;
  159. var indrag = false;
  160. var textEl = element.find('.panel-drop-zone-text');
  161. function showPanel(span, text) {
  162. element.find('.panel-container').css('height', row.height);
  163. element[0].style.width = ((span / 1.2) * 10) + '%';
  164. textEl.text(text);
  165. element.show();
  166. }
  167. function hidePanel() {
  168. element.hide();
  169. }
  170. function updateState() {
  171. if (row.panels.length === 0 && indrag === false) {
  172. return showPanel(12, 'Empty Space');
  173. }
  174. var dropZoneSpan = 12 - row.span;
  175. if (dropZoneSpan > 0) {
  176. if (indrag) {
  177. return showPanel(dropZoneSpan, 'Drop Here');
  178. } else {
  179. return showPanel(dropZoneSpan, 'Empty Space');
  180. }
  181. }
  182. if (indrag === true) {
  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. }
  201. coreModule.directive('dashRow', dashRowDirective);
  202. coreModule.directive('panelWidth', panelWidthDirective);
  203. coreModule.directive('panelDropZone', panelDropZoneDirective);