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.

90 lines
2.6 KiB

  1. // Class for a Sugar select box
  2. enyo.kind({
  3. name: "Sugar.SelectBox",
  4. kind: enyo.Control,
  5. classes: "selectbox-border",
  6. published: { selected: -1, items: [], parentMargin: null },
  7. events: { onIndexChanged: "" },
  8. components: [
  9. {components: [
  10. {name: "icon", kind: "Sugar.Icon", size: 20, x: 6, y: 6, classes: "selectbox-icon"},
  11. {name: "text", content: "xxx", classes: "selectbox-text"},
  12. {name: "selectpopup", kind: "Sugar.Popup", classes: "selectbox-popup", showing: false}
  13. ], ontap:"showPopup"}
  14. ],
  15. // Constructor
  16. create: function() {
  17. this.inherited(arguments);
  18. this.timer = null;
  19. this.itemsChanged();
  20. this.selectedChanged();
  21. this.parentMarginChanged();
  22. },
  23. // Property changed
  24. itemsChanged: function() {
  25. if (this.items.length == 0)
  26. return;
  27. this.selected = 0;
  28. this.selectedChanged();
  29. },
  30. selectedChanged: function() {
  31. if (this.selected > this.items.length || this.selected < 0)
  32. return;
  33. this.$.icon.setIcon(this.items[this.selected].icon);
  34. this.$.text.setContent(this.items[this.selected].name);
  35. },
  36. parentMarginChanged: function() {
  37. },
  38. // Popup menu for item selection
  39. showPopup: function() {
  40. // Create popup
  41. if (this.items.length == 0)
  42. return;
  43. var clientrects = this.hasNode().getClientRects();
  44. var ctrlpos = clientrects[clientrects.length-1];
  45. if (this.parentMargin != null) {
  46. var nodepos = enyo.dom.calcNodePosition(this.parentMargin.hasNode());
  47. ctrlpos = { left: ctrlpos.left-nodepos.left, top: ctrlpos.top-nodepos.top };
  48. }
  49. this.$.selectpopup.setMargin({left: ctrlpos.left-mouse.position.x+5, top: ctrlpos.top-mouse.position.y-3});
  50. this.$.selectpopup.setHeader({
  51. icon: this.items[0].icon,
  52. name: this.items[0].name,
  53. action: enyo.bind(this, "setSelection"),
  54. data: [0]
  55. });
  56. var items = [];
  57. for (var i = 1 ; i < this.items.length ; i++)
  58. items.push({
  59. icon: this.items[i].icon,
  60. name: this.items[i].name,
  61. action: enyo.bind(this, "setSelection"),
  62. data: [i]
  63. });
  64. this.$.selectpopup.setItems(items);
  65. this.$.selectpopup.showPopup();
  66. this.$.selectpopup.showContent();
  67. this.timer = window.setInterval(enyo.bind(this, "hidePopup"), constant.timerPopupDuration);
  68. },
  69. hidePopup: function() {
  70. // Hide popup
  71. if (this.$.selectpopup.cursorIsInside())
  72. return false;
  73. window.clearInterval(this.timer);
  74. this.timer = null;
  75. this.$.selectpopup.hidePopup();
  76. return true;
  77. },
  78. // Set selection
  79. setSelection: function(index) {
  80. this.selected = index;
  81. this.selectedChanged();
  82. window.clearInterval(this.timer);
  83. this.timer = null;
  84. this.$.selectpopup.hidePopup();
  85. this.doIndexChanged();
  86. }
  87. });