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.

183 lines
5.8 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. //Ensure its under the palette class as well, add other classes so you can tweak them in the CSS
  62. paletteElem.className = "palette widepalette";
  63. paletteElem.style.visibility = "hidden";
  64. document.body.appendChild(paletteElem);
  65. if (that.invoker.classList.contains("toolbutton")) {
  66. var invokerElem = document.createElement('div');
  67. invokerElem.className = "palette-invoker";
  68. var style = that.invoker.currentStyle ||
  69. window.getComputedStyle(that.invoker, '');
  70. invokerElem.style.backgroundImage = style.backgroundImage;
  71. invokerElem.addEventListener('click', function (e) {
  72. that.toggle();
  73. });
  74. paletteElem.appendChild(invokerElem);
  75. }
  76. wrapperElem = document.createElement('div');
  77. wrapperElem.className = "wrapper";
  78. paletteElem.appendChild(wrapperElem);
  79. if (that.primaryText !== undefined) {
  80. headerElem = document.createElement('div');
  81. headerElem.className = "header";
  82. headerElem.innerText = that.primaryText;
  83. wrapperElem.appendChild(headerElem);
  84. }
  85. headerSeparatorElem = document.createElement('hr');
  86. headerSeparatorElem.className = "header-separator";
  87. headerSeparatorElem.style.display = "none";
  88. wrapperElem.appendChild(headerSeparatorElem);
  89. containerElem = document.createElement('div');
  90. containerElem.className = "container";
  91. wrapperElem.appendChild(containerElem);
  92. updatePosition();
  93. }
  94. this.getPalette = function () {
  95. if (paletteElem === undefined) {
  96. createPaletteElement();
  97. }
  98. return paletteElem;
  99. };
  100. this.setContent = function (elementsList) {
  101. if (paletteElem === undefined) {
  102. createPaletteElement();
  103. }
  104. (function removePreviousContent() {
  105. for (var i = 0; i < containerElem.children.length; i++) {
  106. var child = containerElem.children[i];
  107. containerElem.removeChild(child);
  108. }
  109. }());
  110. (function addNewContent() {
  111. for (var i = 0; i < elementsList.length; i++) {
  112. var child = elementsList[i];
  113. containerElem.appendChild(child);
  114. }
  115. }());
  116. // The header separator will be visible only if there are
  117. // both, header and content.
  118. if (elementsList.length > 0 && this.primaryText !== undefined) {
  119. headerSeparatorElem.style.display = "block";
  120. } else {
  121. headerSeparatorElem.style.display = "none";
  122. }
  123. };
  124. this.isDown = function () {
  125. return paletteElem === undefined ||
  126. paletteElem.style.visibility == "hidden";
  127. };
  128. };
  129. palette.Palette.prototype.popUp = function () {
  130. for (var i = 0; i < palettesGroup.length; i++) {
  131. var otherPalette = palettesGroup[i];
  132. if (otherPalette != this) {
  133. otherPalette.popDown();
  134. }
  135. }
  136. this.getPalette().style.visibility = "visible";
  137. };
  138. palette.Palette.prototype.popDown = function () {
  139. this.getPalette().style.visibility = "hidden";
  140. };
  141. palette.Palette.prototype.toggle = function () {
  142. if (this.isDown()) {
  143. this.popUp();
  144. } else {
  145. this.popDown();
  146. }
  147. };
  148. return palette;
  149. });