not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.6 KiB

  1. define(function () {
  2. 'use strict';
  3. var palettesGroup = [];
  4. function getOffset(elem) {
  5. // Ugly hack to consider the palette margin.
  6. var style = elem.currentStyle || window.getComputedStyle(elem, '');
  7. // Remove 'px' from the strings.
  8. var x = -2 * style.marginLeft.slice(0, -2);
  9. var y = -1 * style.marginTop.slice(0, -2);
  10. var rect = elem.getBoundingClientRect();
  11. x += rect.left;
  12. y += rect.top;
  13. return {
  14. top: y,
  15. left: x,
  16. width: rect.width,
  17. height: rect.height
  18. };
  19. }
  20. var palette = {};
  21. palette.Palette = function (invoker, primaryText) {
  22. this.invoker = invoker;
  23. if (this.invoker.classList.contains("toolbutton")) {
  24. this.invoker.classList.add("invoker");
  25. }
  26. this.primaryText = primaryText;
  27. var paletteElem;
  28. var wrapperElem;
  29. var headerElem;
  30. var headerSeparatorElem;
  31. var containerElem;
  32. var that = this;
  33. palettesGroup.push(this);
  34. invoker.addEventListener('click', function (event) {
  35. if (!that.invoker.classList.contains("toolbutton")) {
  36. updatePosition(event.x, event.y);
  37. }
  38. that.toggle();
  39. });
  40. function updatePosition(clickX, clickY) {
  41. var paletteX;
  42. var paletteY;
  43. if (typeof (clickX) !== 'undefined' &&
  44. typeof (clickY) !== 'undefined') {
  45. paletteX = clickX;
  46. paletteY = clickY;
  47. } else {
  48. var invokerOffset = getOffset(that.invoker);
  49. paletteX = invokerOffset.left;
  50. paletteY = invokerOffset.top;
  51. }
  52. paletteElem.style.left = paletteX + "px";
  53. paletteElem.style.top = paletteY + "px";
  54. }
  55. // A palette element can have a header, content, one or both.
  56. function createPaletteElement() {
  57. if (paletteElem !== undefined) {
  58. return;
  59. }
  60. paletteElem = document.createElement('div');
  61. paletteElem.className = "palette";
  62. paletteElem.style.visibility = "hidden";
  63. document.body.appendChild(paletteElem);
  64. if (that.invoker.classList.contains("toolbutton")) {
  65. var invokerElem = document.createElement('div');
  66. invokerElem.className = "palette-invoker";
  67. var style = that.invoker.currentStyle ||
  68. window.getComputedStyle(that.invoker, '');
  69. invokerElem.style.backgroundImage = style.backgroundImage;
  70. invokerElem.addEventListener('click', function (e) {
  71. that.toggle();
  72. });
  73. paletteElem.appendChild(invokerElem);
  74. }
  75. wrapperElem = document.createElement('div');
  76. wrapperElem.className = "wrapper";
  77. paletteElem.appendChild(wrapperElem);
  78. if (that.primaryText !== undefined) {
  79. headerElem = document.createElement('div');
  80. headerElem.className = "header";
  81. headerElem.innerText = that.primaryText;
  82. wrapperElem.appendChild(headerElem);
  83. }
  84. headerSeparatorElem = document.createElement('hr');
  85. headerSeparatorElem.className = "header-separator";
  86. headerSeparatorElem.style.display = "none";
  87. wrapperElem.appendChild(headerSeparatorElem);
  88. containerElem = document.createElement('div');
  89. containerElem.className = "container";
  90. wrapperElem.appendChild(containerElem);
  91. updatePosition();
  92. }
  93. this.getPalette = function () {
  94. if (paletteElem === undefined) {
  95. createPaletteElement();
  96. }
  97. return paletteElem;
  98. };
  99. this.setContent = function (elementsList) {
  100. if (paletteElem === undefined) {
  101. createPaletteElement();
  102. }
  103. (function removePreviousContent() {
  104. for (var i = 0; i < containerElem.children.length; i++) {
  105. var child = containerElem.children[i];
  106. containerElem.removeChild(child);
  107. }
  108. }());
  109. (function addNewContent() {
  110. for (var i = 0; i < elementsList.length; i++) {
  111. var child = elementsList[i];
  112. containerElem.appendChild(child);
  113. }
  114. }());
  115. // The header separator will be visible only if there are
  116. // both, header and content.
  117. if (elementsList.length > 0 && this.primaryText !== undefined) {
  118. headerSeparatorElem.style.display = "block";
  119. } else {
  120. headerSeparatorElem.style.display = "none";
  121. }
  122. };
  123. this.isDown = function () {
  124. return paletteElem === undefined ||
  125. paletteElem.style.visibility == "hidden";
  126. };
  127. };
  128. palette.Palette.prototype.popUp = function () {
  129. for (var i = 0; i < palettesGroup.length; i++) {
  130. var otherPalette = palettesGroup[i];
  131. if (otherPalette != this) {
  132. otherPalette.popDown();
  133. }
  134. }
  135. this.getPalette().style.visibility = "visible";
  136. };
  137. palette.Palette.prototype.popDown = function () {
  138. this.getPalette().style.visibility = "hidden";
  139. };
  140. palette.Palette.prototype.toggle = function () {
  141. if (this.isDown()) {
  142. this.popUp();
  143. } else {
  144. this.popDown();
  145. }
  146. };
  147. return palette;
  148. });