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.

68 lines
1.8 KiB

  1. (function($K)
  2. {
  3. $K.add('module', 'selector', {
  4. init: function(app, context)
  5. {
  6. this.app = app;
  7. // context
  8. this.context = context;
  9. this.$element = context.getElement();
  10. },
  11. // public
  12. start: function()
  13. {
  14. this.$selector = this._buildSelector();
  15. this.$selector.on('change.kube.selector', this._toggle.bind(this));
  16. },
  17. stop: function()
  18. {
  19. this.$selector.off('.kube.selector');
  20. },
  21. // private
  22. _isSelect: function()
  23. {
  24. return (this.$element.get().tagName === 'SELECT');
  25. },
  26. _isHashValue: function(value)
  27. {
  28. return (value.search(/^#/) === 0);
  29. },
  30. _buildSelector: function()
  31. {
  32. return (this._isSelect()) ? this.$element : this.$element.find('input[type="radio"]');
  33. },
  34. _getValue: function()
  35. {
  36. return (this._isSelect()) ? this.$selector.val() : this.$selector.filter(':checked').val();
  37. },
  38. _getBoxes: function()
  39. {
  40. var $boxes = $K.dom([]);
  41. var $targets = (this._isSelect()) ? this.$selector.find('option') : this.$selector;
  42. $targets.each(function(node)
  43. {
  44. if (this._isHashValue(node.value))
  45. {
  46. $boxes.add($K.dom(node.value));
  47. }
  48. }.bind(this));
  49. return $boxes;
  50. },
  51. _toggle: function()
  52. {
  53. var value = this._getValue();
  54. var $boxes = this._getBoxes();
  55. var $box = $K.dom(value);
  56. $boxes.addClass('is-hidden');
  57. $box.removeClass('is-hidden');
  58. this.app.broadcast('selector.opened', this, $box);
  59. }
  60. });
  61. })(Kube);