row_ctrl.ts 6.2 KB

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