123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- "use strict";
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: Cognos Analytics
- * Copyright IBM Corp. 2015, 2016
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- define(['jquery', 'bi/admin/common/ui/WidgetView'], function ($, WidgetView) {
- 'use strict'; //NOSONAR: meant to be strict
- var RadioCheckBase = WidgetView.extend({
- widgetName: $.noop,
- inputType: $.noop,
- options: {
- value: ''
- },
- init: function init(options) {
- RadioCheckBase.inherited('init', this, arguments);
- var query = "input[type='" + this.inputType + "']";
- if (this.element.is(query)) {
- this._create();
- this.refresh();
- }
- },
- _create: function _create() {
- var cls = "bi-" + this.inputType;
- var wrapperCls = cls + "-wrapper";
- var wrapper = $('<div></div>');
- wrapper.addClass(wrapperCls);
- wrapper.attr('role', this.inputType);
- var labelEl = this.element.parent().find("label");
- var labId = labelEl.attr("local_id");
- labelEl.attr("id", labId);
- this.element.parent().attr("aria-labelledby", labId);
- this.element.wrap(wrapper);
- this.element.addClass(cls);
- var label = '<span></span>';
- this.element.after(label);
- wrapper = this.element.parent();
- wrapper.on('primaryaction', function () {
- this.toggleCheck();
- }.bind(this));
- this._wrapper = wrapper;
- },
- toggleCheck: function toggleCheck() {
- this.option('checked', !this.options.checked);
- },
- check: function check() {
- this.option('checked', true);
- },
- uncheck: function uncheck() {
- this.option('checked', false);
- },
- isChecked: function isChecked() {
- return !!this.element.is(':checked');
- },
- isDisabled: function isDisabled() {
- return !!this.element.is(':disabled');
- },
- refresh: function refresh() {
- var checked = this.isChecked();
- var disabled = this.isDisabled();
- this.options.checked = checked;
- this.options.value = this.element.val() || "";
- this.element.attr('aria-checked', checked);
- this._wrapper.toggleClass('checked', checked);
- this._wrapper.attr('aria-checked', checked);
- this._wrapper.toggleClass('disabled', disabled);
- this._wrapper.attr('aria-disabled', disabled);
- if (!disabled) {
- this._wrapper.attr('tabindex', '0');
- }
- },
- _cleanGroup: function _cleanGroup() {},
- _setChecked: function _setChecked(checked) {
- if (this.isChecked() !== checked) {
- if (checked) {
- this._cleanGroup();
- this._wrapper.addClass('checked');
- this.element.prop('checked', true);
- this.element.attr('aria-checked', true);
- this._wrapper.attr('aria-checked', true);
- } else {
- this._wrapper.removeClass('checked');
- this.element.prop('checked', false);
- this.element.attr('aria-checked', false);
- this._wrapper.attr('aria-checked', false);
- }
- }
- },
- _setOption: function _setOption(key, value) {
- var self = this;
- var oldVal = this.options[key];
- switch (key) {
- case "value":
- this.element.val(value);
- break;
- case "checked":
- this._setChecked(value);
- break;
- case "disabled":
- this._wrapper.toggleClass("disabled", !!value);
- break;
- default:
- break;
- }
- WidgetView.prototype._setOption.apply(self, arguments);
- switch (key) {
- case "checked":
- if (oldVal !== value) {
- this.trigger('change');
- this.element.trigger('change');
- }
- break;
- default:
- break;
- }
- }
- });
- return RadioCheckBase;
- });
|