radio_group.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /**
  2. * DevExtreme (ui/radio_group/radio_group.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  13. return typeof obj
  14. } : function(obj) {
  15. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  16. }, _typeof(obj)
  17. }
  18. var _renderer = require("../../core/renderer");
  19. var _renderer2 = _interopRequireDefault(_renderer);
  20. var _extend = require("../../core/utils/extend");
  21. var _devices = require("../../core/devices");
  22. var _devices2 = _interopRequireDefault(_devices);
  23. var _common = require("../../core/utils/common");
  24. var _type = require("../../core/utils/type");
  25. var _utils = require("../widget/utils.ink_ripple");
  26. var _utils2 = _interopRequireDefault(_utils);
  27. var _component_registrator = require("../../core/component_registrator");
  28. var _component_registrator2 = _interopRequireDefault(_component_registrator);
  29. var _uiCollection_widget = require("../collection/ui.collection_widget.edit");
  30. var _uiCollection_widget2 = _interopRequireDefault(_uiCollection_widget);
  31. var _ui = require("../editor/ui.data_expression");
  32. var _ui2 = _interopRequireDefault(_ui);
  33. var _editor = require("../editor/editor");
  34. var _editor2 = _interopRequireDefault(_editor);
  35. var _deferred = require("../../core/utils/deferred");
  36. function _interopRequireDefault(obj) {
  37. return obj && obj.__esModule ? obj : {
  38. "default": obj
  39. }
  40. }
  41. function _classCallCheck(instance, Constructor) {
  42. if (!(instance instanceof Constructor)) {
  43. throw new TypeError("Cannot call a class as a function")
  44. }
  45. }
  46. function _defineProperties(target, props) {
  47. for (var i = 0; i < props.length; i++) {
  48. var descriptor = props[i];
  49. descriptor.enumerable = descriptor.enumerable || false;
  50. descriptor.configurable = true;
  51. if ("value" in descriptor) {
  52. descriptor.writable = true
  53. }
  54. Object.defineProperty(target, descriptor.key, descriptor)
  55. }
  56. }
  57. function _createClass(Constructor, protoProps, staticProps) {
  58. if (protoProps) {
  59. _defineProperties(Constructor.prototype, protoProps)
  60. }
  61. if (staticProps) {
  62. _defineProperties(Constructor, staticProps)
  63. }
  64. Object.defineProperty(Constructor, "prototype", {
  65. writable: false
  66. });
  67. return Constructor
  68. }
  69. function _get() {
  70. if ("undefined" !== typeof Reflect && Reflect.get) {
  71. _get = Reflect.get.bind()
  72. } else {
  73. _get = function(target, property, receiver) {
  74. var base = _superPropBase(target, property);
  75. if (!base) {
  76. return
  77. }
  78. var desc = Object.getOwnPropertyDescriptor(base, property);
  79. if (desc.get) {
  80. return desc.get.call(arguments.length < 3 ? target : receiver)
  81. }
  82. return desc.value
  83. }
  84. }
  85. return _get.apply(this, arguments)
  86. }
  87. function _superPropBase(object, property) {
  88. while (!Object.prototype.hasOwnProperty.call(object, property)) {
  89. object = _getPrototypeOf(object);
  90. if (null === object) {
  91. break
  92. }
  93. }
  94. return object
  95. }
  96. function _inherits(subClass, superClass) {
  97. if ("function" !== typeof superClass && null !== superClass) {
  98. throw new TypeError("Super expression must either be null or a function")
  99. }
  100. subClass.prototype = Object.create(superClass && superClass.prototype, {
  101. constructor: {
  102. value: subClass,
  103. writable: true,
  104. configurable: true
  105. }
  106. });
  107. Object.defineProperty(subClass, "prototype", {
  108. writable: false
  109. });
  110. if (superClass) {
  111. _setPrototypeOf(subClass, superClass)
  112. }
  113. }
  114. function _setPrototypeOf(o, p) {
  115. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  116. o.__proto__ = p;
  117. return o
  118. };
  119. return _setPrototypeOf(o, p)
  120. }
  121. function _createSuper(Derived) {
  122. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  123. return function() {
  124. var result, Super = _getPrototypeOf(Derived);
  125. if (hasNativeReflectConstruct) {
  126. var NewTarget = _getPrototypeOf(this).constructor;
  127. result = Reflect.construct(Super, arguments, NewTarget)
  128. } else {
  129. result = Super.apply(this, arguments)
  130. }
  131. return _possibleConstructorReturn(this, result)
  132. }
  133. }
  134. function _possibleConstructorReturn(self, call) {
  135. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  136. return call
  137. } else {
  138. if (void 0 !== call) {
  139. throw new TypeError("Derived constructors may only return object or undefined")
  140. }
  141. }
  142. return _assertThisInitialized(self)
  143. }
  144. function _assertThisInitialized(self) {
  145. if (void 0 === self) {
  146. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  147. }
  148. return self
  149. }
  150. function _isNativeReflectConstruct() {
  151. if ("undefined" === typeof Reflect || !Reflect.construct) {
  152. return false
  153. }
  154. if (Reflect.construct.sham) {
  155. return false
  156. }
  157. if ("function" === typeof Proxy) {
  158. return true
  159. }
  160. try {
  161. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  162. return true
  163. } catch (e) {
  164. return false
  165. }
  166. }
  167. function _getPrototypeOf(o) {
  168. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  169. return o.__proto__ || Object.getPrototypeOf(o)
  170. };
  171. return _getPrototypeOf(o)
  172. }
  173. var RADIO_BUTTON_CHECKED_CLASS = "dx-radiobutton-checked";
  174. var RADIO_BUTTON_CLASS = "dx-radiobutton";
  175. var RADIO_BUTTON_ICON_CHECKED_CLASS = "dx-radiobutton-icon-checked";
  176. var RADIO_BUTTON_ICON_CLASS = "dx-radiobutton-icon";
  177. var RADIO_BUTTON_ICON_DOT_CLASS = "dx-radiobutton-icon-dot";
  178. var RADIO_GROUP_HORIZONTAL_CLASS = "dx-radiogroup-horizontal";
  179. var RADIO_GROUP_VERTICAL_CLASS = "dx-radiogroup-vertical";
  180. var RADIO_VALUE_CONTAINER_CLASS = "dx-radio-value-container";
  181. var RADIO_GROUP_CLASS = "dx-radiogroup";
  182. var RADIO_FEEDBACK_HIDE_TIMEOUT = 100;
  183. var RadioCollection = function(_CollectionWidget) {
  184. _inherits(RadioCollection, _CollectionWidget);
  185. var _super = _createSuper(RadioCollection);
  186. function RadioCollection() {
  187. _classCallCheck(this, RadioCollection);
  188. return _super.apply(this, arguments)
  189. }
  190. _createClass(RadioCollection, [{
  191. key: "_focusTarget",
  192. value: function() {
  193. return this.$element().parent()
  194. }
  195. }, {
  196. key: "_nullValueSelectionSupported",
  197. value: function() {
  198. return true
  199. }
  200. }, {
  201. key: "_getDefaultOptions",
  202. value: function() {
  203. var defaultOptions = _get(_getPrototypeOf(RadioCollection.prototype), "_getDefaultOptions", this).call(this);
  204. return (0, _extend.extend)(defaultOptions, _ui2.default._dataExpressionDefaultOptions(), {
  205. _itemAttributes: {
  206. role: "radio"
  207. }
  208. })
  209. }
  210. }, {
  211. key: "_initMarkup",
  212. value: function() {
  213. var _this = this;
  214. _get(_getPrototypeOf(RadioCollection.prototype), "_initMarkup", this).call(this);
  215. (0, _common.deferRender)(function() {
  216. _this.itemElements().addClass(RADIO_BUTTON_CLASS)
  217. })
  218. }
  219. }, {
  220. key: "_keyboardEventBindingTarget",
  221. value: function() {
  222. return this._focusTarget()
  223. }
  224. }, {
  225. key: "_postprocessRenderItem",
  226. value: function(args) {
  227. var html = args.itemData.html,
  228. itemElement = args.itemElement;
  229. if (!html) {
  230. var $radio = (0, _renderer2.default)("<div>").addClass(RADIO_BUTTON_ICON_CLASS);
  231. (0, _renderer2.default)("<div>").addClass(RADIO_BUTTON_ICON_DOT_CLASS).appendTo($radio);
  232. var $radioContainer = (0, _renderer2.default)("<div>").append($radio).addClass(RADIO_VALUE_CONTAINER_CLASS);
  233. (0, _renderer2.default)(itemElement).prepend($radioContainer)
  234. }
  235. _get(_getPrototypeOf(RadioCollection.prototype), "_postprocessRenderItem", this).call(this, args)
  236. }
  237. }, {
  238. key: "_processSelectableItem",
  239. value: function($itemElement, isSelected) {
  240. _get(_getPrototypeOf(RadioCollection.prototype), "_processSelectableItem", this).call(this, $itemElement, isSelected);
  241. $itemElement.toggleClass(RADIO_BUTTON_CHECKED_CLASS, isSelected).find(".".concat(RADIO_BUTTON_ICON_CLASS)).first().toggleClass(RADIO_BUTTON_ICON_CHECKED_CLASS, isSelected);
  242. this.setAria("checked", isSelected, $itemElement)
  243. }
  244. }, {
  245. key: "_refreshContent",
  246. value: function() {
  247. this._prepareContent();
  248. this._renderContent()
  249. }
  250. }, {
  251. key: "_supportedKeys",
  252. value: function() {
  253. var parent = _get(_getPrototypeOf(RadioCollection.prototype), "_supportedKeys", this).call(this);
  254. return (0, _extend.extend)({}, parent, {
  255. enter: function(e) {
  256. e.preventDefault();
  257. return parent.enter.apply(this, arguments)
  258. },
  259. space: function(e) {
  260. e.preventDefault();
  261. return parent.space.apply(this, arguments)
  262. }
  263. })
  264. }
  265. }, {
  266. key: "_itemElements",
  267. value: function() {
  268. return this._itemContainer().children(this._itemSelector())
  269. }
  270. }]);
  271. return RadioCollection
  272. }(_uiCollection_widget2.default);
  273. var RadioGroup = function(_Editor) {
  274. _inherits(RadioGroup, _Editor);
  275. var _super2 = _createSuper(RadioGroup);
  276. function RadioGroup() {
  277. _classCallCheck(this, RadioGroup);
  278. return _super2.apply(this, arguments)
  279. }
  280. _createClass(RadioGroup, [{
  281. key: "_clean",
  282. value: function() {
  283. delete this._inkRipple;
  284. _get(_getPrototypeOf(RadioGroup.prototype), "_clean", this).call(this)
  285. }
  286. }, {
  287. key: "_dataSourceOptions",
  288. value: function() {
  289. return {
  290. paginate: false
  291. }
  292. }
  293. }, {
  294. key: "_defaultOptionsRules",
  295. value: function() {
  296. var defaultOptionsRules = _get(_getPrototypeOf(RadioGroup.prototype), "_defaultOptionsRules", this).call(this);
  297. return defaultOptionsRules.concat([{
  298. device: {
  299. tablet: true
  300. },
  301. options: {
  302. layout: "horizontal"
  303. }
  304. }, {
  305. device: function() {
  306. return "desktop" === _devices2.default.real().deviceType && !_devices2.default.isSimulator()
  307. },
  308. options: {
  309. focusStateEnabled: true
  310. }
  311. }])
  312. }
  313. }, {
  314. key: "_fireContentReadyAction",
  315. value: function(force) {
  316. force && _get(_getPrototypeOf(RadioGroup.prototype), "_fireContentReadyAction", this).call(this)
  317. }
  318. }, {
  319. key: "_focusTarget",
  320. value: function() {
  321. return this.$element()
  322. }
  323. }, {
  324. key: "_getAriaTarget",
  325. value: function() {
  326. return this.$element()
  327. }
  328. }, {
  329. key: "_getDefaultOptions",
  330. value: function() {
  331. var defaultOptions = _get(_getPrototypeOf(RadioGroup.prototype), "_getDefaultOptions", this).call(this);
  332. return (0, _extend.extend)(defaultOptions, (0, _extend.extend)(_ui2.default._dataExpressionDefaultOptions(), {
  333. hoverStateEnabled: true,
  334. activeStateEnabled: true,
  335. layout: "vertical",
  336. useInkRipple: false
  337. }))
  338. }
  339. }, {
  340. key: "_getItemValue",
  341. value: function(item) {
  342. return this._valueGetter ? this._valueGetter(item) : item.text
  343. }
  344. }, {
  345. key: "_getSubmitElement",
  346. value: function() {
  347. return this._$submitElement
  348. }
  349. }, {
  350. key: "_init",
  351. value: function() {
  352. _get(_getPrototypeOf(RadioGroup.prototype), "_init", this).call(this);
  353. this._activeStateUnit = ".".concat(RADIO_BUTTON_CLASS);
  354. this._feedbackHideTimeout = RADIO_FEEDBACK_HIDE_TIMEOUT;
  355. this._initDataExpressions()
  356. }
  357. }, {
  358. key: "_initMarkup",
  359. value: function() {
  360. this.$element().addClass(RADIO_GROUP_CLASS);
  361. this._renderSubmitElement();
  362. this.setAria("role", "radiogroup");
  363. this._renderRadios();
  364. this.option("useInkRipple") && this._renderInkRipple();
  365. this._renderLayout();
  366. _get(_getPrototypeOf(RadioGroup.prototype), "_initMarkup", this).call(this)
  367. }
  368. }, {
  369. key: "_itemClickHandler",
  370. value: function(_ref) {
  371. var itemElement = _ref.itemElement,
  372. event = _ref.event,
  373. itemData = _ref.itemData;
  374. if (this.itemElements().is(itemElement)) {
  375. var newValue = this._getItemValue(itemData);
  376. if (newValue !== this.option("value")) {
  377. this._saveValueChangeEvent(event);
  378. this.option("value", newValue)
  379. }
  380. }
  381. }
  382. }, {
  383. key: "_optionChanged",
  384. value: function(args) {
  385. var name = args.name,
  386. value = args.value;
  387. this._dataExpressionOptionChanged(args);
  388. switch (name) {
  389. case "useInkRipple":
  390. this._invalidate();
  391. break;
  392. case "focusStateEnabled":
  393. case "accessKey":
  394. case "tabIndex":
  395. this._setCollectionWidgetOption(name, value);
  396. break;
  397. case "disabled":
  398. _get(_getPrototypeOf(RadioGroup.prototype), "_optionChanged", this).call(this, args);
  399. this._setCollectionWidgetOption(name, value);
  400. break;
  401. case "dataSource":
  402. this._setCollectionWidgetOption("dataSource", this._dataSource);
  403. break;
  404. case "valueExpr":
  405. this._setCollectionWidgetOption("keyExpr", this._getCollectionKeyExpr());
  406. break;
  407. case "value":
  408. this._setCollectionWidgetOption("selectedItemKeys", [value]);
  409. this._setSubmitValue(value);
  410. _get(_getPrototypeOf(RadioGroup.prototype), "_optionChanged", this).call(this, args);
  411. break;
  412. case "items":
  413. case "itemTemplate":
  414. case "displayExpr":
  415. break;
  416. case "layout":
  417. this._renderLayout();
  418. this._updateItemsSize();
  419. break;
  420. default:
  421. _get(_getPrototypeOf(RadioGroup.prototype), "_optionChanged", this).call(this, args)
  422. }
  423. }
  424. }, {
  425. key: "_render",
  426. value: function() {
  427. _get(_getPrototypeOf(RadioGroup.prototype), "_render", this).call(this);
  428. this._updateItemsSize()
  429. }
  430. }, {
  431. key: "_renderInkRipple",
  432. value: function() {
  433. this._inkRipple = _utils2.default.render({
  434. waveSizeCoefficient: 3.3,
  435. useHoldAnimation: false,
  436. isCentered: true
  437. })
  438. }
  439. }, {
  440. key: "_renderLayout",
  441. value: function() {
  442. var layout = this.option("layout");
  443. var $element = this.$element();
  444. $element.toggleClass(RADIO_GROUP_VERTICAL_CLASS, "vertical" === layout);
  445. $element.toggleClass(RADIO_GROUP_HORIZONTAL_CLASS, "horizontal" === layout)
  446. }
  447. }, {
  448. key: "_renderRadios",
  449. value: function() {
  450. var _this2 = this;
  451. this._areRadiosCreated = new _deferred.Deferred;
  452. var $radios = (0, _renderer2.default)("<div>").appendTo(this.$element());
  453. var _this$option = this.option(),
  454. value = _this$option.value,
  455. displayExpr = _this$option.displayExpr,
  456. accessKey = _this$option.accessKey,
  457. focusStateEnabled = _this$option.focusStateEnabled,
  458. itemTemplate = _this$option.itemTemplate,
  459. tabIndex = _this$option.tabIndex,
  460. valueExpr = _this$option.valueExpr;
  461. var isNullSelectable = "this" !== valueExpr;
  462. this._radios = this._createComponent($radios, RadioCollection, {
  463. displayExpr: displayExpr,
  464. accessKey: accessKey,
  465. dataSource: this._dataSource,
  466. focusStateEnabled: focusStateEnabled,
  467. itemTemplate: itemTemplate,
  468. keyExpr: this._getCollectionKeyExpr(),
  469. noDataText: "",
  470. onContentReady: function() {
  471. return _this2._fireContentReadyAction(true)
  472. },
  473. onItemClick: this._itemClickHandler.bind(this),
  474. scrollingEnabled: false,
  475. selectionByClick: false,
  476. selectionMode: "single",
  477. selectedItemKeys: isNullSelectable || (0, _type.isDefined)(value) ? [value] : [],
  478. tabIndex: tabIndex
  479. });
  480. this._areRadiosCreated.resolve()
  481. }
  482. }, {
  483. key: "_renderSubmitElement",
  484. value: function() {
  485. this._$submitElement = (0, _renderer2.default)("<input>").attr("type", "hidden").appendTo(this.$element());
  486. this._setSubmitValue()
  487. }
  488. }, {
  489. key: "_setOptionsByReference",
  490. value: function() {
  491. _get(_getPrototypeOf(RadioGroup.prototype), "_setOptionsByReference", this).call(this);
  492. (0, _extend.extend)(this._optionsByReference, {
  493. value: true
  494. })
  495. }
  496. }, {
  497. key: "_setSubmitValue",
  498. value: function(value) {
  499. value = value || this.option("value");
  500. var submitValue = "this" === this.option("valueExpr") ? this._displayGetter(value) : value;
  501. this._$submitElement.val(submitValue)
  502. }
  503. }, {
  504. key: "_setCollectionWidgetOption",
  505. value: function() {
  506. this._areRadiosCreated.done(this._setWidgetOption.bind(this, "_radios", arguments))
  507. }
  508. }, {
  509. key: "_toggleActiveState",
  510. value: function($element, value, e) {
  511. _get(_getPrototypeOf(RadioGroup.prototype), "_toggleActiveState", this).call(this, $element, value, e);
  512. if (this._inkRipple) {
  513. var event = {
  514. element: $element.find(".".concat(RADIO_BUTTON_ICON_CLASS)),
  515. event: e
  516. };
  517. value ? this._inkRipple.showWave(event) : this._inkRipple.hideWave(event)
  518. }
  519. }
  520. }, {
  521. key: "_updateItemsSize",
  522. value: function() {
  523. if ("horizontal" === this.option("layout")) {
  524. this.itemElements().css("height", "auto")
  525. } else {
  526. var itemsCount = this.option("items").length;
  527. this.itemElements().css("height", 100 / itemsCount + "%")
  528. }
  529. }
  530. }, {
  531. key: "focus",
  532. value: function() {
  533. this._radios && this._radios.focus()
  534. }
  535. }, {
  536. key: "itemElements",
  537. value: function() {
  538. return this._radios.itemElements()
  539. }
  540. }]);
  541. return RadioGroup
  542. }(_editor2.default);
  543. RadioGroup.include(_ui2.default);
  544. (0, _component_registrator2.default)("dxRadioGroup", RadioGroup);
  545. module.exports = RadioGroup;