action_sheet.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * DevExtreme (ui/action_sheet.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. var $ = require("../core/renderer");
  11. var window = require("../core/utils/window").getWindow();
  12. var noop = require("../core/utils/common").noop;
  13. var messageLocalization = require("../localization/message");
  14. var registerComponent = require("../core/component_registrator");
  15. var extend = require("../core/utils/extend").extend;
  16. var Button = require("./button");
  17. var CollectionWidget = require("./collection/ui.collection_widget.edit");
  18. var Popup = require("./popup");
  19. var Popover = require("./popover");
  20. var BindableTemplate = require("./widget/bindable_template");
  21. var Deferred = require("../core/utils/deferred").Deferred;
  22. var ACTION_SHEET_CLASS = "dx-actionsheet";
  23. var ACTION_SHEET_CONTAINER_CLASS = "dx-actionsheet-container";
  24. var ACTION_SHEET_POPUP_WRAPPER_CLASS = "dx-actionsheet-popup-wrapper";
  25. var ACTION_SHEET_POPOVER_WRAPPER_CLASS = "dx-actionsheet-popover-wrapper";
  26. var ACTION_SHEET_CANCEL_BUTTON_CLASS = "dx-actionsheet-cancel";
  27. var ACTION_SHEET_ITEM_CLASS = "dx-actionsheet-item";
  28. var ACTION_SHEET_ITEM_DATA_KEY = "dxActionSheetItemData";
  29. var ACTION_SHEET_WITHOUT_TITLE_CLASS = "dx-actionsheet-without-title";
  30. var ActionSheet = CollectionWidget.inherit({
  31. _getDefaultOptions: function() {
  32. return extend(this.callBase(), {
  33. usePopover: false,
  34. target: null,
  35. title: "",
  36. showTitle: true,
  37. showCancelButton: true,
  38. cancelText: messageLocalization.format("Cancel"),
  39. onCancelClick: null,
  40. visible: false,
  41. noDataText: "",
  42. focusStateEnabled: false,
  43. selectionByClick: false
  44. })
  45. },
  46. _defaultOptionsRules: function() {
  47. return this.callBase().concat([{
  48. device: {
  49. platform: "ios",
  50. tablet: true
  51. },
  52. options: {
  53. usePopover: true
  54. }
  55. }])
  56. },
  57. _initTemplates: function() {
  58. this.callBase();
  59. this._defaultTemplates.item = new BindableTemplate(function($container, data) {
  60. var button = new Button($("<div>"), extend({
  61. onClick: data && data.click
  62. }, data));
  63. $container.append(button.$element())
  64. }, ["disabled", "icon", "text", "type", "onClick", "click"], this.option("integrationOptions.watchMethod"))
  65. },
  66. _itemContainer: function() {
  67. return this._$itemContainer
  68. },
  69. _itemClass: function() {
  70. return ACTION_SHEET_ITEM_CLASS
  71. },
  72. _itemDataKey: function() {
  73. return ACTION_SHEET_ITEM_DATA_KEY
  74. },
  75. _toggleVisibility: noop,
  76. _renderDimensions: noop,
  77. _initMarkup: function() {
  78. this.callBase();
  79. this.$element().addClass(ACTION_SHEET_CLASS);
  80. this._createItemContainer()
  81. },
  82. _render: function() {
  83. this._renderPopup()
  84. },
  85. _createItemContainer: function() {
  86. this._$itemContainer = $("<div>").addClass(ACTION_SHEET_CONTAINER_CLASS);
  87. this._renderDisabled()
  88. },
  89. _renderDisabled: function() {
  90. this._$itemContainer.toggleClass("dx-state-disabled", this.option("disabled"))
  91. },
  92. _renderPopup: function() {
  93. this._$popup = $("<div>").appendTo(this.$element());
  94. this._isPopoverMode() ? this._createPopover() : this._createPopup();
  95. this._renderPopupTitle();
  96. this._mapPopupOption("visible")
  97. },
  98. _mapPopupOption: function(optionName) {
  99. this._popup && this._popup.option(optionName, this.option(optionName))
  100. },
  101. _isPopoverMode: function() {
  102. return this.option("usePopover") && this.option("target")
  103. },
  104. _renderPopupTitle: function() {
  105. this._mapPopupOption("showTitle");
  106. this._popup && this._popup._wrapper().toggleClass(ACTION_SHEET_WITHOUT_TITLE_CLASS, !this.option("showTitle"))
  107. },
  108. _clean: function() {
  109. if (this._$popup) {
  110. this._$popup.remove()
  111. }
  112. this.callBase()
  113. },
  114. _overlayConfig: function() {
  115. return {
  116. onInitialized: function(args) {
  117. this._popup = args.component
  118. }.bind(this),
  119. disabled: false,
  120. showTitle: true,
  121. title: this.option("title"),
  122. deferRendering: !window.angular,
  123. onContentReady: this._popupContentReadyAction.bind(this),
  124. onHidden: this.hide.bind(this)
  125. }
  126. },
  127. _createPopover: function() {
  128. this._createComponent(this._$popup, Popover, extend(this._overlayConfig(), {
  129. width: this.option("width") || 200,
  130. height: this.option("height") || "auto",
  131. target: this.option("target")
  132. }));
  133. this._popup._wrapper().addClass(ACTION_SHEET_POPOVER_WRAPPER_CLASS)
  134. },
  135. _createPopup: function() {
  136. this._createComponent(this._$popup, Popup, extend(this._overlayConfig(), {
  137. dragEnabled: false,
  138. width: this.option("width") || "100%",
  139. height: this.option("height") || "auto",
  140. showCloseButton: false,
  141. position: {
  142. my: "bottom",
  143. at: "bottom",
  144. of: window
  145. },
  146. animation: {
  147. show: {
  148. type: "slide",
  149. duration: 400,
  150. from: {
  151. position: {
  152. my: "top",
  153. at: "bottom",
  154. of: window
  155. }
  156. },
  157. to: {
  158. position: {
  159. my: "bottom",
  160. at: "bottom",
  161. of: window
  162. }
  163. }
  164. },
  165. hide: {
  166. type: "slide",
  167. duration: 400,
  168. from: {
  169. position: {
  170. my: "bottom",
  171. at: "bottom",
  172. of: window
  173. }
  174. },
  175. to: {
  176. position: {
  177. my: "top",
  178. at: "bottom",
  179. of: window
  180. }
  181. }
  182. }
  183. }
  184. }));
  185. this._popup._wrapper().addClass(ACTION_SHEET_POPUP_WRAPPER_CLASS)
  186. },
  187. _popupContentReadyAction: function() {
  188. this._popup.$content().append(this._$itemContainer);
  189. this._attachClickEvent();
  190. this._attachHoldEvent();
  191. this._prepareContent();
  192. this._renderContent();
  193. this._renderCancelButton()
  194. },
  195. _renderCancelButton: function() {
  196. if (this._isPopoverMode()) {
  197. return
  198. }
  199. if (this._$cancelButton) {
  200. this._$cancelButton.remove()
  201. }
  202. if (this.option("showCancelButton")) {
  203. var cancelClickAction = this._createActionByOption("onCancelClick") || noop;
  204. var that = this;
  205. this._$cancelButton = $("<div>").addClass(ACTION_SHEET_CANCEL_BUTTON_CLASS).appendTo(this._popup && this._popup.$content());
  206. this._createComponent(this._$cancelButton, Button, {
  207. disabled: false,
  208. text: this.option("cancelText"),
  209. onClick: function(e) {
  210. var hidingArgs = {
  211. event: e,
  212. cancel: false
  213. };
  214. cancelClickAction(hidingArgs);
  215. if (!hidingArgs.cancel) {
  216. that.hide()
  217. }
  218. },
  219. integrationOptions: {}
  220. })
  221. }
  222. },
  223. _attachItemClickEvent: noop,
  224. _itemClickHandler: function(e) {
  225. this.callBase(e);
  226. if (!$(e.target).is(".dx-state-disabled, .dx-state-disabled *")) {
  227. this.hide()
  228. }
  229. },
  230. _itemHoldHandler: function(e) {
  231. this.callBase(e);
  232. if (!$(e.target).is(".dx-state-disabled, .dx-state-disabled *")) {
  233. this.hide()
  234. }
  235. },
  236. _optionChanged: function(args) {
  237. switch (args.name) {
  238. case "width":
  239. case "height":
  240. case "visible":
  241. case "title":
  242. this._mapPopupOption(args.name);
  243. break;
  244. case "disabled":
  245. this._renderDisabled();
  246. break;
  247. case "showTitle":
  248. this._renderPopupTitle();
  249. break;
  250. case "showCancelButton":
  251. case "onCancelClick":
  252. case "cancelText":
  253. this._renderCancelButton();
  254. break;
  255. case "target":
  256. case "usePopover":
  257. case "items":
  258. this._invalidate();
  259. break;
  260. default:
  261. this.callBase(args)
  262. }
  263. },
  264. toggle: function(showing) {
  265. var that = this;
  266. var d = new Deferred;
  267. that._popup.toggle(showing).done(function() {
  268. that.option("visible", showing);
  269. d.resolveWith(that)
  270. });
  271. return d.promise()
  272. },
  273. show: function() {
  274. return this.toggle(true)
  275. },
  276. hide: function() {
  277. return this.toggle(false)
  278. }
  279. });
  280. registerComponent("dxActionSheet", ActionSheet);
  281. module.exports = ActionSheet;
  282. module.exports.default = module.exports;