RadioCheckBase.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. /**
  3.  * Licensed Materials - Property of IBM
  4.  * IBM Cognos Products: Cognos Analytics
  5.  * Copyright IBM Corp. 2015, 2016
  6.  * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7.  */
  8. define(['jquery', 'bi/admin/common/ui/WidgetView'], function ($, WidgetView) {
  9. 'use strict'; //NOSONAR: meant to be strict
  10. var RadioCheckBase = WidgetView.extend({
  11. widgetName: $.noop,
  12. inputType: $.noop,
  13. options: {
  14. value: ''
  15. },
  16. init: function init(options) {
  17. RadioCheckBase.inherited('init', this, arguments);
  18. var query = "input[type='" + this.inputType + "']";
  19. if (this.element.is(query)) {
  20. this._create();
  21. this.refresh();
  22. }
  23. },
  24. _create: function _create() {
  25. var cls = "bi-" + this.inputType;
  26. var wrapperCls = cls + "-wrapper";
  27. var wrapper = $('<div></div>');
  28. wrapper.addClass(wrapperCls);
  29. wrapper.attr('role', this.inputType);
  30. var labelEl = this.element.parent().find("label");
  31. var labId = labelEl.attr("local_id");
  32. labelEl.attr("id", labId);
  33. this.element.parent().attr("aria-labelledby", labId);
  34. this.element.wrap(wrapper);
  35. this.element.addClass(cls);
  36. var label = '<span></span>';
  37. this.element.after(label);
  38. wrapper = this.element.parent();
  39. wrapper.on('primaryaction', function () {
  40. this.toggleCheck();
  41. }.bind(this));
  42. this._wrapper = wrapper;
  43. },
  44. toggleCheck: function toggleCheck() {
  45. this.option('checked', !this.options.checked);
  46. },
  47. check: function check() {
  48. this.option('checked', true);
  49. },
  50. uncheck: function uncheck() {
  51. this.option('checked', false);
  52. },
  53. isChecked: function isChecked() {
  54. return !!this.element.is(':checked');
  55. },
  56. isDisabled: function isDisabled() {
  57. return !!this.element.is(':disabled');
  58. },
  59. refresh: function refresh() {
  60. var checked = this.isChecked();
  61. var disabled = this.isDisabled();
  62. this.options.checked = checked;
  63. this.options.value = this.element.val() || "";
  64. this.element.attr('aria-checked', checked);
  65. this._wrapper.toggleClass('checked', checked);
  66. this._wrapper.attr('aria-checked', checked);
  67. this._wrapper.toggleClass('disabled', disabled);
  68. this._wrapper.attr('aria-disabled', disabled);
  69. if (!disabled) {
  70. this._wrapper.attr('tabindex', '0');
  71. }
  72. },
  73. _cleanGroup: function _cleanGroup() {},
  74. _setChecked: function _setChecked(checked) {
  75. if (this.isChecked() !== checked) {
  76. if (checked) {
  77. this._cleanGroup();
  78. this._wrapper.addClass('checked');
  79. this.element.prop('checked', true);
  80. this.element.attr('aria-checked', true);
  81. this._wrapper.attr('aria-checked', true);
  82. } else {
  83. this._wrapper.removeClass('checked');
  84. this.element.prop('checked', false);
  85. this.element.attr('aria-checked', false);
  86. this._wrapper.attr('aria-checked', false);
  87. }
  88. }
  89. },
  90. _setOption: function _setOption(key, value) {
  91. var self = this;
  92. var oldVal = this.options[key];
  93. switch (key) {
  94. case "value":
  95. this.element.val(value);
  96. break;
  97. case "checked":
  98. this._setChecked(value);
  99. break;
  100. case "disabled":
  101. this._wrapper.toggleClass("disabled", !!value);
  102. break;
  103. default:
  104. break;
  105. }
  106. WidgetView.prototype._setOption.apply(self, arguments);
  107. switch (key) {
  108. case "checked":
  109. if (oldVal !== value) {
  110. this.trigger('change');
  111. this.element.trigger('change');
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. });
  119. return RadioCheckBase;
  120. });