row_ctrl.ts 5.9 KB

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