dashgrid.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import $ from 'jquery';
  3. import coreModule from 'app/core/core_module';
  4. import {DashboardModel, CELL_HEIGHT, CELL_VMARGIN} from '../model';
  5. import 'jquery-ui';
  6. import 'gridstack';
  7. import 'gridstack.jquery-ui';
  8. const template = `
  9. <div class="grid-stack">
  10. <dash-grid-item ng-repeat="panel in ctrl.panels"
  11. class="grid-stack-item"
  12. grid-ctrl="ctrl"
  13. panel="panel">
  14. <plugin-component type="panel" class="grid-stack-item-content">
  15. </plugin-component>
  16. </dash-grid-item>
  17. </div>
  18. `;
  19. export class GridCtrl {
  20. options: any;
  21. dashboard: any;
  22. panels: any;
  23. gridstack: any;
  24. gridElem: any;
  25. /** @ngInject */
  26. constructor(private $rootScope, private $element, private $timeout) {
  27. this.panels = this.dashboard.panels;
  28. }
  29. init() {
  30. this.gridElem = this.$element.find('.grid-stack');
  31. this.gridstack = this.gridElem.gridstack({
  32. animate: true,
  33. cellHeight: CELL_HEIGHT,
  34. verticalMargin: CELL_VMARGIN,
  35. }).data('gridstack');
  36. this.gridElem.on('change', (e, items) => {
  37. this.$timeout(() => this.itemsChanged(items), 50);
  38. });
  39. }
  40. itemsChanged(items) {
  41. for (let item of items) {
  42. var panel = this.dashboard.getPanelById(parseInt(item.id));
  43. panel.x = item.x;
  44. panel.y = item.y;
  45. panel.width = item.width;
  46. panel.height = item.height;
  47. console.log('update panel', panel.id, panel.height);
  48. }
  49. this.$rootScope.$broadcast('render');
  50. console.log('broadcast render');
  51. }
  52. bindItem(element) {
  53. this.gridstack.makeWidget(element);
  54. }
  55. }
  56. /** @ngInject **/
  57. export function dashGrid($timeout) {
  58. return {
  59. restrict: 'E',
  60. template: template,
  61. controller: GridCtrl,
  62. bindToController: true,
  63. controllerAs: 'ctrl',
  64. scope: {
  65. dashboard: "="
  66. },
  67. link: function(scope, elem, attrs, ctrl) {
  68. $timeout(function() {
  69. console.log(elem.html());
  70. ctrl.init();
  71. });
  72. }
  73. };
  74. }
  75. /** @ngInject **/
  76. export function dashGridItem($timeout) {
  77. return {
  78. restrict: "E",
  79. scope: {
  80. panel: '=',
  81. gridCtrl: '='
  82. },
  83. link: function (scope, element, attrs) {
  84. let gridCtrl = scope.gridCtrl;
  85. let panel = scope.panel;
  86. element.attr({
  87. 'data-gs-id': panel.id,
  88. 'data-gs-x': panel.x,
  89. 'data-gs-y': panel.y,
  90. 'data-gs-width': panel.width,
  91. 'data-gs-height': panel.height,
  92. });
  93. //var item = element.data('_gridstack_node');
  94. //console.log('link item', item);
  95. //gridCtrl.bindItem(element);
  96. // element.bind('$destroy', function() {
  97. // var item = element.data('_gridstack_node');
  98. // scope.onItemRemoved({item: item});
  99. // ctrl.removeItem(element);
  100. // });
  101. }
  102. };
  103. }
  104. coreModule.directive('dashGrid', dashGrid);
  105. coreModule.directive('dashGridItem', dashGridItem);