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.

151 lines
3.6 KiB

  1. var _displayedPalette = null;
  2. // Class for a Sugar palette
  3. enyo.kind({
  4. name: "Sugar.Palette",
  5. kind: enyo.Control,
  6. published: {
  7. icon: null,
  8. text: null,
  9. contents: null,
  10. contentsClasses: "",
  11. header: null
  12. },
  13. classes: "palette-sugarizer palette-down",
  14. components: [
  15. { name: "icon", kind: "Sugar.Icon", classes: "palette-icon", size: 20, x: 6, y: 6 },
  16. { name: "text", classes: "palette-text" }
  17. ],
  18. // Constructor
  19. create: function() {
  20. this.inherited(arguments);
  21. this.iconChanged();
  22. this.textChanged();
  23. },
  24. rendered: function() {
  25. this.inherited(arguments);
  26. },
  27. // Property changed
  28. iconChanged: function() {
  29. this.$.icon.setIcon(this.icon);
  30. },
  31. textChanged: function() {
  32. this.$.text.setContent(this.text);
  33. },
  34. displayPopup: function(view) {
  35. if (_displayedPalette) {
  36. _displayedPalette.switchPalette(view);
  37. _displayedPalette = null;
  38. }
  39. if (this.contents == null) {
  40. return;
  41. }
  42. var id = this.hasNode().id+"_content";
  43. this.palette = view.createComponent({
  44. name: id,
  45. classes: "palette-content "+this.contentsClasses
  46. });
  47. if (this.header) {
  48. this.palette.createComponent({content: this.header, classes: "palette-header"}, {owner: this.palette});
  49. this.palette.createComponent({classes: "palette-hr"}, {owner: this.palette});
  50. }
  51. this.paletteEvents = [];
  52. for (var i = 0 ; i < this.contents.length ; i++) {
  53. var newObject = this.palette.createComponent(this.contents[i], {owner: this.palette});
  54. var tapMethod = this.contents[i].ontap;
  55. if (tapMethod) {
  56. this.palette[tapMethod] = enyo.bind(this, "bindedFunction");
  57. this.paletteEvents.push({"event": "tap", "object": newObject, "name": tapMethod})
  58. }
  59. }
  60. this.palette.render();
  61. _updatePosition(this);
  62. _displayedPalette = this;
  63. },
  64. setItems: function(items) {
  65. this.contents = items;
  66. },
  67. bindedFunction: function(o, e) {
  68. for (var i = 0 ; i < this.paletteEvents.length ; i++) {
  69. if (this.paletteEvents[i].event == e.type && this.paletteEvents[i].object == o) {
  70. enyo.call(this.parent, this.paletteEvents[i].name, [o, e]);
  71. }
  72. }
  73. },
  74. hidePopup: function(view) {
  75. var that = this;
  76. var toDestroy = null;
  77. enyo.forEach(view.getControls(), function(item) {
  78. if (item.getAttribute("id") == that.palette.getAttribute("id")) {
  79. toDestroy = item;
  80. }
  81. });
  82. if (toDestroy) {
  83. toDestroy.destroy();
  84. }
  85. _displayedPalette = null;
  86. },
  87. switchPalette: function(view) {
  88. var isOpen = this.isOpen();
  89. this.addRemoveClass("palette-down", isOpen);
  90. this.addRemoveClass("palette-up", !isOpen);
  91. if (!isOpen) {
  92. this.displayPopup(view);
  93. } else {
  94. this.hidePopup(view);
  95. }
  96. },
  97. isOpen: function() {
  98. return this.hasClass("palette-up");
  99. }
  100. });
  101. // Internal functions adapted from sugar-web/palette.js
  102. function _getOffset(elem, paletteElem) {
  103. // Ugly hack to consider the palette margin.
  104. var style = elem.currentStyle || window.getComputedStyle(elem, '');
  105. // Remove 'px' from the strings.
  106. var x = -2 * style.marginLeft.slice(0, -2);
  107. var y = -1 * style.marginTop.slice(0, -2);
  108. var rect = elem.getBoundingClientRect();
  109. x = rect.left;
  110. y += rect.top;
  111. // Shift to the right if overflow screen
  112. var delta = x + paletteElem.offsetWidth - document.getElementById("canvas").offsetWidth;
  113. if (delta > 0) {
  114. x -= delta;
  115. }
  116. return {
  117. top: y,
  118. left: x,
  119. width: rect.width,
  120. height: rect.height
  121. };
  122. }
  123. function _updatePosition(that) {
  124. var invokerOffset = _getOffset(that.hasNode(), that.palette.hasNode());
  125. var paletteX = invokerOffset.left;
  126. var paletteY = invokerOffset.top;
  127. var paletteElem = document.getElementById(that.palette.getAttribute("id"));
  128. paletteElem.style.left = paletteX + "px";
  129. paletteElem.style.top = paletteY + "px";
  130. }