TemplateContainer.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2015, 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['jquery', './Absolute'], function ($, BaseLayout) {
  8. var PageLayout = null;
  9. /**
  10. * Top level container that contains a template.
  11. */
  12. PageLayout = BaseLayout.extend({
  13. init: function init() {
  14. PageLayout.inherited('init', this, arguments);
  15. this.scrollPosition = {
  16. top: this.domNode.scrollTop,
  17. left: this.domNode.scrollLeft
  18. };
  19. this.$el.on('scroll.' + this.id, this._onScroll.bind(this));
  20. },
  21. destroy: function destroy() {
  22. PageLayout.inherited('destroy', this, arguments);
  23. this.$el.off('scroll.' + this.id);
  24. },
  25. // Prevent adding widgets outside of the page boundaries
  26. createDropZone: function createDropZone() {
  27. // No-op, override the inherited behaviour.
  28. },
  29. _onScroll: function _onScroll() {
  30. var scrollTop = this.domNode.scrollTop;
  31. var scrollLeft = this.domNode.scrollLeft;
  32. this._dndManager.scrollChildrenCandidateDropTargets(this.domNode, this.scrollPosition.top - scrollTop, this.scrollPosition.left - scrollLeft);
  33. this.scrollPosition.top = scrollTop;
  34. this.scrollPosition.left = scrollLeft;
  35. },
  36. _getTemplateLayoutView: function _getTemplateLayoutView() {
  37. var templateLayout = null;
  38. if (this.model.items && this.model.items.length > 0) {
  39. var model = this.model.items[0];
  40. templateLayout = $('#' + this.layoutController.modelIdToNodeId(model.id), this.layoutController.$el)[0]._layout;
  41. }
  42. return templateLayout;
  43. },
  44. _addWidget: function _addWidget(widgetSpec, isTouch) {
  45. var templateView = this._getTemplateLayoutView();
  46. if (templateView) {
  47. // Set new parent id to the template id.
  48. widgetSpec.parentId = templateView.model.id;
  49. // Calculate new position
  50. var top = parseInt(widgetSpec.layoutProperties.style.top, 10);
  51. var left = parseInt(widgetSpec.layoutProperties.style.left, 10);
  52. // Calculate new top and left
  53. var position = templateView.$el.position();
  54. var newTop = top - position.top - this.domNode.scrollTop;
  55. var newLeft = left - position.left - this.domNode.scrollLeft;
  56. // Take into account the minimum top/left supported by the templateView that we are adding to.
  57. widgetSpec.layoutProperties.style.top = Math.round(Math.max(newTop, templateView.getMinimumTop())) + 'px';
  58. widgetSpec.layoutProperties.style.left = Math.round(Math.max(newLeft, templateView.getMinimumLeft())) + 'px';
  59. // Add the widget the template layout
  60. templateView.authorHelper._addWidget(widgetSpec, isTouch);
  61. }
  62. },
  63. setPreferredLocation: function setPreferredLocation(options) {
  64. var view = this._getTemplateLayoutView();
  65. if (view && view.setPreferredLocation) {
  66. view.setPreferredLocation(options);
  67. }
  68. }
  69. });
  70. return PageLayout;
  71. });
  72. //# sourceMappingURL=TemplateContainer.js.map