Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. import DragDropController from '../../base/dragDropController';
  2.  
  3. /**
  4. * row directive
  5. */
  6. function row() {
  7. let directive = {
  8. template: require('./row.directive.html'),
  9. restrict: 'EA',
  10. controller: ['$scope', '$element', '$injector', RowController],
  11. controllerAs: 'ctrl',
  12. scope: true
  13. };
  14.  
  15. return directive;
  16. }
  17.  
  18. /**
  19. * Class initializing the controller to add drop functionality
  20. */
  21. class RowController extends DragDropController {
  22.  
  23. /**
  24. * Attach drop functionality to directive, while also managing the columns and placeholders
  25. */
  26. constructor($scope, $element, $injector) {
  27. super($scope, $element, $injector, 'structure');
  28. var vm = this;
  29. let styleOptionFactory = $injector.get('styleOptionFactory', this);
  30. const drop_container = angular.element($element).find('div')[0];
  31.  
  32. // attach drop functionality
  33. this.attachDrop(drop_container, {
  34. drop: function(dragged_controller) {
  35. let el;
  36.  
  37. if (dragged_controller.recompile === false) {
  38. el = angular.element(dragged_controller.baseElement())[0];
  39. } else {
  40. el = angular.element(vm.$compile(dragged_controller.baseElement())($scope))[0];
  41. }
  42.  
  43. vm.addColumn(el);
  44. vm.removePlaceholder();
  45. },
  46. dragenter: function(dragged_controller) {
  47. vm.addPlaceholder();
  48. vm.deleteIfEmpty();
  49. },
  50. dragleave: function(dragged_controller) {
  51. vm.removePlaceholder();
  52. vm.deleteIfEmpty();
  53. },
  54. dragover: function(dragged_controller, e) {
  55. vm.movePlaceholder(e);
  56. }
  57. });
  58.  
  59. // attach drop global drop listener
  60. var dragDropListener = $scope.$on('drag.drop', function(event, el) {
  61. vm.$timeout(function() {
  62. vm.removePlaceholder();
  63. vm.deleteIfEmpty(dragDropListener);
  64. })
  65. });
  66.  
  67. this.container = angular.element($element).find('div')[0];
  68. this.options = styleOptionFactory.getOptions([
  69. 'margin',
  70. 'padding',
  71. 'height',
  72. 'background-color',
  73. ]);
  74.  
  75. this.options.margin.value['margin-left'] = -15;
  76. this.options.margin.value['margin-right'] = -15;
  77. this.options.height.value['min-height'] = 30;
  78.  
  79. this.vm = vm;
  80.  
  81. var deleteListener = $scope.$on('pb.elementDeleted', function() {
  82. vm.setChildrenWidth();
  83. vm.deleteIfEmpty(deleteListener);
  84. });
  85.  
  86. var dragStartListener = $scope.$on('drag.start', function(event, el) {
  87. vm.setChildrenWidth();
  88. vm.deleteIfEmpty(dragStartListener);
  89. })
  90. }
  91.  
  92. /**
  93. * add the given element as a column to this row
  94. * @param el
  95. */
  96. addColumn(el) {
  97. let container = angular.element(this.container);
  98. el = angular.element(el)
  99. let _class = container.find('placeholder').prop('class');
  100. el.addClass(_class);
  101.  
  102. container.find('placeholder').replaceWith(el);
  103. this.setChildrenWidth();
  104. }
  105.  
  106. /**
  107. * Add a placeholder to this row
  108. */
  109. addPlaceholder() {
  110. // make sure there are a maximum of 4 children including placeholders for this row
  111. if (this.getColumnCount() > 3)
  112. return;
  113.  
  114. // don't add another placeholder if there is a placeholder present
  115. /* istanbul ignore next - hard to test, also it's a fail-safe */
  116. if (angular.element(this.container).find('placeholder').length > 0) {
  117. this.setChildrenWidth();
  118. return;
  119. }
  120.  
  121. this.container.append(angular.element(`<placeholder class="col-sm-12"></placeholder>`)[0]);
  122. this.setChildrenWidth();
  123. }
  124.  
  125. /**
  126. * remove the placeholder from this row
  127. */
  128. removePlaceholder() {
  129. let placeholder = angular.element(this.container).find('placeholder');
  130. placeholder.remove();
  131. this.setChildrenWidth();
  132. }
  133.  
  134. /**
  135. * delete a row if it is empty and finalize the listener
  136. * @param {function()} listener
  137. */
  138. deleteIfEmpty(listener) {
  139. if (this.$element.children()[0].children.length < 1) {
  140. this.$element.next().remove();
  141. this.$element.remove();
  142.  
  143. if (listener)
  144. listener();
  145. }
  146. }
  147.  
  148. /**
  149. * move the placeholder to the correct position when hovering over a row
  150. * @param {MouseEvent} e
  151. */
  152. movePlaceholder(e) {
  153. let offset = e.target.offsetLeft; // get the offset
  154. let width = e.target.clientWidth; // get the width of this row
  155. let x = e.x - offset; // normalize the x value for this row
  156. let column_percentile_width = 1.0 / this.getColumnCount(); // set the percentile width
  157. let column_px_width = width * column_percentile_width; // set the px width
  158. let index = Math.floor(x / column_px_width); // get the index of the normalized x position
  159. let container = angular.element(this.container); // make sure the container is of type JQLite
  160.  
  161. if (index > 0) {
  162. let el = angular.element(Array.from(container.children()).filter(function(n) {
  163. return n.tagName != 'PLACEHOLDER'
  164. })[index - 1]);
  165. el.after(container.find('placeholder'));
  166. } else {
  167. container.prepend(container.find('placeholder'));
  168. }
  169. }
  170.  
  171. /**
  172. * Set the proper width for the children following the count of said children
  173. */
  174. setChildrenWidth() {
  175. let width = 12 / this.getColumnCount();
  176. if (!isFinite(width))
  177. width = 12;
  178.  
  179. for (let child of this.container.children) {
  180. child = angular.element(child);
  181. child.removeClass('col-sm-12');
  182. child.removeClass('col-sm-6');
  183. child.removeClass('col-sm-4');
  184. child.removeClass('col-sm-3');
  185. child.addClass(`col-sm-${width}`);
  186. }
  187. }
  188.  
  189. /**
  190. * get the current column count (e.g. the count of children)
  191. * @returns {number} column count
  192. */
  193. getColumnCount() {
  194. return this.container.children.length;
  195. }
  196. }
  197.  
  198. export default row;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement