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.

114 lines
2.8 KiB

  1. /**
  2. A control that looks like a switch with labels for two states. Each time a ToggleButton is tapped,
  3. it switches its value and fires an onChange event.
  4. {kind: "onyx.ToggleButton", onContent: "foo", offContent: "bar", onChange: "buttonToggle"}
  5. buttonToggle: function(inSender, inEvent) {
  6. this.log("Toggled to value " + inEvent.value);
  7. }
  8. To find out the value of the button, use getValue:
  9. queryToggleValue: function() {
  10. return this.$.toggleButton.getValue();
  11. }
  12. The color of the toggle button can be customized by applying a background color:
  13. {kind: "onyx.ToggleButton", style: "background-color: #35A8EE;"}
  14. */
  15. enyo.kind({
  16. name: "onyx.ToggleButton",
  17. classes: "onyx-toggle-button",
  18. published: {
  19. active: false,
  20. value: false,
  21. onContent: "On",
  22. offContent: "Off",
  23. disabled: false
  24. },
  25. events: {
  26. /**
  27. The onChange event fires when the user changes the value of the toggle button,
  28. but not when the value is changed programmatically.
  29. */
  30. onChange: ""
  31. },
  32. //* @protected
  33. handlers: {
  34. ondragstart: "dragstart",
  35. ondrag: "drag",
  36. ondragfinish: "dragfinish"
  37. },
  38. components: [
  39. {name: "contentOn", classes: "onyx-toggle-content on"},
  40. {name: "contentOff", classes: "onyx-toggle-content off"},
  41. {classes: "onyx-toggle-button-knob"}
  42. ],
  43. create: function() {
  44. this.inherited(arguments);
  45. this.value = Boolean(this.value || this.active);
  46. this.onContentChanged();
  47. this.offContentChanged();
  48. this.disabledChanged();
  49. },
  50. rendered: function() {
  51. this.inherited(arguments);
  52. this.valueChanged();
  53. },
  54. valueChanged: function() {
  55. this.addRemoveClass("off", !this.value);
  56. this.$.contentOn.setShowing(this.value);
  57. this.$.contentOff.setShowing(!this.value);
  58. this.setActive(this.value);
  59. },
  60. activeChanged: function() {
  61. this.setValue(this.active);
  62. this.bubble("onActivate");
  63. },
  64. onContentChanged: function() {
  65. this.$.contentOn.setContent(this.onContent || "");
  66. this.$.contentOn.addRemoveClass("empty", !this.onContent);
  67. },
  68. offContentChanged: function() {
  69. this.$.contentOff.setContent(this.offContent || "");
  70. this.$.contentOff.addRemoveClass("empty", !this.onContent);
  71. },
  72. disabledChanged: function() {
  73. this.addRemoveClass("disabled", this.disabled);
  74. },
  75. updateValue: function(inValue) {
  76. if (!this.disabled) {
  77. this.setValue(inValue);
  78. this.doChange({value: this.value});
  79. }
  80. },
  81. tap: function() {
  82. this.updateValue(!this.value);
  83. },
  84. dragstart: function(inSender, inEvent) {
  85. if (inEvent.horizontal) {
  86. inEvent.preventDefault();
  87. this.dragging = true;
  88. this.dragged = false;
  89. return true;
  90. }
  91. },
  92. drag: function(inSender, inEvent) {
  93. if (this.dragging) {
  94. var d = inEvent.dx;
  95. if (Math.abs(d) > 10) {
  96. this.updateValue(d > 0);
  97. this.dragged = true;
  98. }
  99. return true;
  100. }
  101. },
  102. dragfinish: function(inSender, inEvent) {
  103. this.dragging = false;
  104. if (this.dragged) {
  105. inEvent.preventTap();
  106. }
  107. }
  108. })