ui.diagram.toolbar.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /**
  2. * DevExtreme (ui/diagram/ui.diagram.toolbar.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 _diagram = require("./diagram.panel");
  21. var _diagram2 = _interopRequireDefault(_diagram);
  22. var _toolbar = require("../toolbar");
  23. var _toolbar2 = _interopRequireDefault(_toolbar);
  24. var _context_menu = require("../context_menu");
  25. var _context_menu2 = _interopRequireDefault(_context_menu);
  26. var _uiDiagram = require("./ui.diagram.commands");
  27. var _uiDiagram2 = _interopRequireDefault(_uiDiagram);
  28. var _diagram_bar = require("./diagram_bar");
  29. var _diagram_bar2 = _interopRequireDefault(_diagram_bar);
  30. var _extend = require("../../core/utils/extend");
  31. require("../select_box");
  32. require("../color_box");
  33. require("../check_box");
  34. function _interopRequireDefault(obj) {
  35. return obj && obj.__esModule ? obj : {
  36. "default": obj
  37. }
  38. }
  39. function _classCallCheck(instance, Constructor) {
  40. if (!(instance instanceof Constructor)) {
  41. throw new TypeError("Cannot call a class as a function")
  42. }
  43. }
  44. function _defineProperties(target, props) {
  45. for (var i = 0; i < props.length; i++) {
  46. var descriptor = props[i];
  47. descriptor.enumerable = descriptor.enumerable || false;
  48. descriptor.configurable = true;
  49. if ("value" in descriptor) {
  50. descriptor.writable = true
  51. }
  52. Object.defineProperty(target, descriptor.key, descriptor)
  53. }
  54. }
  55. function _createClass(Constructor, protoProps, staticProps) {
  56. if (protoProps) {
  57. _defineProperties(Constructor.prototype, protoProps)
  58. }
  59. if (staticProps) {
  60. _defineProperties(Constructor, staticProps)
  61. }
  62. Object.defineProperty(Constructor, "prototype", {
  63. writable: false
  64. });
  65. return Constructor
  66. }
  67. function _get() {
  68. if ("undefined" !== typeof Reflect && Reflect.get) {
  69. _get = Reflect.get.bind()
  70. } else {
  71. _get = function(target, property, receiver) {
  72. var base = _superPropBase(target, property);
  73. if (!base) {
  74. return
  75. }
  76. var desc = Object.getOwnPropertyDescriptor(base, property);
  77. if (desc.get) {
  78. return desc.get.call(arguments.length < 3 ? target : receiver)
  79. }
  80. return desc.value
  81. }
  82. }
  83. return _get.apply(this, arguments)
  84. }
  85. function _superPropBase(object, property) {
  86. while (!Object.prototype.hasOwnProperty.call(object, property)) {
  87. object = _getPrototypeOf(object);
  88. if (null === object) {
  89. break
  90. }
  91. }
  92. return object
  93. }
  94. function _inherits(subClass, superClass) {
  95. if ("function" !== typeof superClass && null !== superClass) {
  96. throw new TypeError("Super expression must either be null or a function")
  97. }
  98. subClass.prototype = Object.create(superClass && superClass.prototype, {
  99. constructor: {
  100. value: subClass,
  101. writable: true,
  102. configurable: true
  103. }
  104. });
  105. Object.defineProperty(subClass, "prototype", {
  106. writable: false
  107. });
  108. if (superClass) {
  109. _setPrototypeOf(subClass, superClass)
  110. }
  111. }
  112. function _setPrototypeOf(o, p) {
  113. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  114. o.__proto__ = p;
  115. return o
  116. };
  117. return _setPrototypeOf(o, p)
  118. }
  119. function _createSuper(Derived) {
  120. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  121. return function() {
  122. var result, Super = _getPrototypeOf(Derived);
  123. if (hasNativeReflectConstruct) {
  124. var NewTarget = _getPrototypeOf(this).constructor;
  125. result = Reflect.construct(Super, arguments, NewTarget)
  126. } else {
  127. result = Super.apply(this, arguments)
  128. }
  129. return _possibleConstructorReturn(this, result)
  130. }
  131. }
  132. function _possibleConstructorReturn(self, call) {
  133. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  134. return call
  135. } else {
  136. if (void 0 !== call) {
  137. throw new TypeError("Derived constructors may only return object or undefined")
  138. }
  139. }
  140. return _assertThisInitialized(self)
  141. }
  142. function _assertThisInitialized(self) {
  143. if (void 0 === self) {
  144. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  145. }
  146. return self
  147. }
  148. function _isNativeReflectConstruct() {
  149. if ("undefined" === typeof Reflect || !Reflect.construct) {
  150. return false
  151. }
  152. if (Reflect.construct.sham) {
  153. return false
  154. }
  155. if ("function" === typeof Proxy) {
  156. return true
  157. }
  158. try {
  159. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  160. return true
  161. } catch (e) {
  162. return false
  163. }
  164. }
  165. function _getPrototypeOf(o) {
  166. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  167. return o.__proto__ || Object.getPrototypeOf(o)
  168. };
  169. return _getPrototypeOf(o)
  170. }
  171. var ACTIVE_FORMAT_CLASS = "dx-format-active";
  172. var TOOLBAR_CLASS = "dx-diagram-toolbar";
  173. var WIDGET_COMMANDS = [{
  174. command: "options",
  175. icon: "preferences",
  176. hint: "Show Properties",
  177. text: "Properties"
  178. }];
  179. var TOOLBAR_SEPARATOR_CLASS = "dx-diagram-toolbar-separator";
  180. var TOOLBAR_MENU_SEPARATOR_CLASS = "dx-diagram-toolbar-menu-separator";
  181. var DiagramToolbar = function(_DiagramPanel) {
  182. _inherits(DiagramToolbar, _DiagramPanel);
  183. var _super = _createSuper(DiagramToolbar);
  184. function DiagramToolbar() {
  185. _classCallCheck(this, DiagramToolbar);
  186. return _super.apply(this, arguments)
  187. }
  188. _createClass(DiagramToolbar, [{
  189. key: "_init",
  190. value: function() {
  191. this.bar = new ToolbarDiagramBar(this);
  192. this._itemHelpers = {};
  193. this._contextMenus = [];
  194. this._createOnWidgetCommand();
  195. _get(_getPrototypeOf(DiagramToolbar.prototype), "_init", this).call(this)
  196. }
  197. }, {
  198. key: "_initMarkup",
  199. value: function() {
  200. _get(_getPrototypeOf(DiagramToolbar.prototype), "_initMarkup", this).call(this);
  201. var $toolbar = (0, _renderer2.default)("<div>").addClass(TOOLBAR_CLASS).appendTo(this._$element);
  202. this._renderToolbar($toolbar)
  203. }
  204. }, {
  205. key: "_renderToolbar",
  206. value: function($toolbar) {
  207. var dataSource = this._prepareToolbarItems(_uiDiagram2.default.getToolbar(), "before", this._execDiagramCommand);
  208. dataSource = dataSource.concat(this._prepareToolbarItems(WIDGET_COMMANDS, "after", this._execWidgetCommand));
  209. this._toolbarInstance = this._createComponent($toolbar, _toolbar2.default, {
  210. dataSource: dataSource
  211. })
  212. }
  213. }, {
  214. key: "_prepareToolbarItems",
  215. value: function(items, location, actionHandler) {
  216. var _this = this;
  217. return items.map(function(item) {
  218. return (0, _extend.extend)(true, {
  219. location: location,
  220. locateInMenu: "auto"
  221. }, _this._createItem(item, location, actionHandler), _this._createItemOptions(item), _this._createItemActionOptions(item, actionHandler))
  222. })
  223. }
  224. }, {
  225. key: "_createItem",
  226. value: function(item, location, actionHandler) {
  227. var _this2 = this;
  228. if ("separator" === item.widget) {
  229. return {
  230. template: function(data, index, element) {
  231. (0, _renderer2.default)(element).addClass(TOOLBAR_SEPARATOR_CLASS)
  232. },
  233. menuItemTemplate: function(data, index, element) {
  234. (0, _renderer2.default)(element).addClass(TOOLBAR_MENU_SEPARATOR_CLASS)
  235. }
  236. }
  237. }
  238. return {
  239. widget: item.widget || "dxButton",
  240. cssClass: item.cssClass,
  241. options: {
  242. stylingMode: "text",
  243. text: item.text,
  244. hint: item.hint,
  245. icon: item.icon,
  246. onInitialized: function(e) {
  247. return _this2._onItemInitialized(e.component, item)
  248. },
  249. onContentReady: function(e) {
  250. return _this2._onItemContentReady(e.component, item, actionHandler)
  251. }
  252. }
  253. }
  254. }
  255. }, {
  256. key: "_createItemOptions",
  257. value: function(_ref) {
  258. var widget = _ref.widget,
  259. items = _ref.items,
  260. valueExpr = _ref.valueExpr,
  261. displayExpr = _ref.displayExpr,
  262. showText = _ref.showText,
  263. hint = _ref.hint,
  264. icon = _ref.icon;
  265. if ("dxSelectBox" === widget) {
  266. return this._createSelectBoxItemOptions(hint, items, valueExpr, displayExpr)
  267. } else {
  268. if ("dxColorBox" === widget) {
  269. return this._createColorBoxItemOptions(hint, icon)
  270. } else {
  271. if (!widget || "dxButton" === widget) {
  272. return {
  273. showText: showText || "inMenu"
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }, {
  280. key: "_createSelectBoxItemOptions",
  281. value: function(hint, items, valueExpr, displayExpr) {
  282. var options = this._createSelectBoxBaseItemOptions(hint);
  283. options = (0, _extend.extend)(true, options, {
  284. options: {
  285. items: items,
  286. valueExpr: valueExpr,
  287. displayExpr: displayExpr
  288. }
  289. });
  290. var isSelectButton = items.every(function(i) {
  291. return void 0 !== i.icon
  292. });
  293. if (isSelectButton) {
  294. options = (0, _extend.extend)(true, options, {
  295. options: {
  296. fieldTemplate: function(data, container) {
  297. (0, _renderer2.default)("<i>").addClass(data && data.icon).appendTo(container);
  298. (0, _renderer2.default)("<div>").dxTextBox({
  299. readOnly: true,
  300. stylingMode: "outlined"
  301. }).appendTo(container)
  302. },
  303. itemTemplate: function(data) {
  304. return '<i class="'.concat(data.icon, '"').concat(data.hint && ' title="'.concat(data.hint), '"}></i>')
  305. }
  306. }
  307. })
  308. }
  309. return options
  310. }
  311. }, {
  312. key: "_createColorBoxItemOptions",
  313. value: function(hint, icon) {
  314. var options = this._createSelectBoxBaseItemOptions(hint);
  315. if (icon) {
  316. options = (0, _extend.extend)(true, options, {
  317. options: {
  318. openOnFieldClick: true,
  319. fieldTemplate: function(data, container) {
  320. (0, _renderer2.default)("<i>").addClass(icon).css("borderBottomColor", data).appendTo(container);
  321. (0, _renderer2.default)("<div>").dxTextBox({
  322. readOnly: true,
  323. stylingMode: "outlined"
  324. }).appendTo(container)
  325. }
  326. }
  327. })
  328. }
  329. return options
  330. }
  331. }, {
  332. key: "_createSelectBoxBaseItemOptions",
  333. value: function(hint) {
  334. return {
  335. options: {
  336. stylingMode: "filled",
  337. hint: hint
  338. }
  339. }
  340. }
  341. }, {
  342. key: "_createItemActionOptions",
  343. value: function(item, handler) {
  344. var _this3 = this;
  345. switch (item.widget) {
  346. case "dxSelectBox":
  347. case "dxColorBox":
  348. return {
  349. options: {
  350. onValueChanged: function(e) {
  351. var parameter = _this3._getExecCommandParameter(item, e.component.option("value"));
  352. handler.call(_this3, item.command, parameter)
  353. }
  354. }
  355. };
  356. default:
  357. if (!item.items) {
  358. return {
  359. options: {
  360. onClick: function(e) {
  361. var parameter = _this3._getExecCommandParameter(item);
  362. handler.call(_this3, item.command, parameter)
  363. }
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }, {
  370. key: "_getExecCommandParameter",
  371. value: function(item, widgetValue) {
  372. if (item.getParameter) {
  373. return item.getParameter(this, widgetValue)
  374. }
  375. return widgetValue
  376. }
  377. }, {
  378. key: "_onItemInitialized",
  379. value: function(widget, item) {
  380. if (void 0 !== item.command) {
  381. this._itemHelpers[item.command] = new ToolbarItemHelper(widget)
  382. }
  383. }
  384. }, {
  385. key: "_onItemContentReady",
  386. value: function(widget, item, actionHandler) {
  387. var _this4 = this;
  388. if ("dxButton" === widget.NAME && item.items) {
  389. var $menuContainer = (0, _renderer2.default)("<div>").appendTo(this.$element());
  390. this._createComponent($menuContainer, _context_menu2.default, {
  391. dataSource: item.items,
  392. displayExpr: "text",
  393. valueExpr: "command",
  394. target: widget.$element(),
  395. showEvent: "dxclick",
  396. position: {
  397. at: "left bottom"
  398. },
  399. onItemClick: function(_ref2) {
  400. var itemData = _ref2.itemData;
  401. if (void 0 !== itemData.command) {
  402. var parameter = _this4._getExecCommandParameter(itemData);
  403. actionHandler.call(_this4, itemData.command, parameter)
  404. }
  405. },
  406. onInitialized: function(_ref3) {
  407. var component = _ref3.component;
  408. return _this4._onContextMenuInitialized(component, item)
  409. },
  410. onDisposing: function(_ref4) {
  411. var component = _ref4.component;
  412. return _this4._onContextMenuDisposing(component, item)
  413. }
  414. })
  415. }
  416. }
  417. }, {
  418. key: "_onContextMenuInitialized",
  419. value: function(widget, item) {
  420. var _this5 = this;
  421. this._contextMenus.push(widget);
  422. item.items.forEach(function(item, index) {
  423. _this5._itemHelpers[item.command] = new ContextMenuItemHelper(widget, index)
  424. })
  425. }
  426. }, {
  427. key: "_onContextMenuDisposing",
  428. value: function(widget, item) {
  429. this._contextMenus = this._contextMenus.filter(function(cm) {
  430. return cm !== widget
  431. })
  432. }
  433. }, {
  434. key: "_execDiagramCommand",
  435. value: function(command, value) {
  436. if (!this._updateLocked) {
  437. this.bar.raiseBarCommandExecuted(command, value)
  438. }
  439. }
  440. }, {
  441. key: "_execWidgetCommand",
  442. value: function(command) {
  443. if (!this._updateLocked) {
  444. this._onWidgetCommandAction({
  445. name: command
  446. })
  447. }
  448. }
  449. }, {
  450. key: "_createOnWidgetCommand",
  451. value: function() {
  452. this._onWidgetCommandAction = this._createActionByOption("onWidgetCommand")
  453. }
  454. }, {
  455. key: "_setItemEnabled",
  456. value: function(command, enabled) {
  457. if (command in this._itemHelpers) {
  458. this._itemHelpers[command].setEnabled(enabled)
  459. }
  460. }
  461. }, {
  462. key: "_setEnabled",
  463. value: function(enabled) {
  464. this._toolbarInstance.option("disabled", !enabled);
  465. this._contextMenus.forEach(function(cm) {
  466. return cm.option("disabled", !enabled)
  467. })
  468. }
  469. }, {
  470. key: "_setItemValue",
  471. value: function(command, value) {
  472. try {
  473. this._updateLocked = true;
  474. if (command in this._itemHelpers) {
  475. this._itemHelpers[command].setValue(value)
  476. }
  477. } finally {
  478. this._updateLocked = false
  479. }
  480. }
  481. }, {
  482. key: "_optionChanged",
  483. value: function(args) {
  484. switch (args.name) {
  485. case "onWidgetCommand":
  486. this._createOnWidgetCommand();
  487. break;
  488. case "export":
  489. break;
  490. default:
  491. _get(_getPrototypeOf(DiagramToolbar.prototype), "_optionChanged", this).call(this, args)
  492. }
  493. }
  494. }, {
  495. key: "_getDefaultOptions",
  496. value: function() {
  497. return (0, _extend.extend)(_get(_getPrototypeOf(DiagramToolbar.prototype), "_getDefaultOptions", this).call(this), {
  498. "export": {
  499. fileName: "Diagram",
  500. proxyUrl: void 0
  501. }
  502. })
  503. }
  504. }]);
  505. return DiagramToolbar
  506. }(_diagram2.default);
  507. var ToolbarDiagramBar = function(_DiagramBar) {
  508. _inherits(ToolbarDiagramBar, _DiagramBar);
  509. var _super2 = _createSuper(ToolbarDiagramBar);
  510. function ToolbarDiagramBar() {
  511. _classCallCheck(this, ToolbarDiagramBar);
  512. return _super2.apply(this, arguments)
  513. }
  514. _createClass(ToolbarDiagramBar, [{
  515. key: "getCommandKeys",
  516. value: function() {
  517. return _uiDiagram2.default.getToolbar().reduce(function(commands, i) {
  518. if (void 0 !== i.command) {
  519. commands.push(i.command)
  520. }
  521. return i.items ? commands.concat(i.items.filter(function(ci) {
  522. return void 0 !== ci.command
  523. }).map(function(ci) {
  524. return ci.command
  525. })) : commands
  526. }, [])
  527. }
  528. }, {
  529. key: "setItemValue",
  530. value: function(key, value) {
  531. this._owner._setItemValue(key, value)
  532. }
  533. }, {
  534. key: "setItemEnabled",
  535. value: function(key, enabled) {
  536. this._owner._setItemEnabled(key, enabled)
  537. }
  538. }, {
  539. key: "setEnabled",
  540. value: function(enabled) {
  541. this._owner._setEnabled(enabled)
  542. }
  543. }]);
  544. return ToolbarDiagramBar
  545. }(_diagram_bar2.default);
  546. var ToolbarItemHelper = function() {
  547. function ToolbarItemHelper(widget) {
  548. _classCallCheck(this, ToolbarItemHelper);
  549. this._widget = widget
  550. }
  551. _createClass(ToolbarItemHelper, [{
  552. key: "setEnabled",
  553. value: function(enabled) {
  554. this._widget.option("disabled", !enabled)
  555. }
  556. }, {
  557. key: "setValue",
  558. value: function(value) {
  559. if ("value" in this._widget.option()) {
  560. this._widget.option("value", value)
  561. } else {
  562. if (void 0 !== value) {
  563. this._widget.$element().toggleClass(ACTIVE_FORMAT_CLASS, value)
  564. }
  565. }
  566. }
  567. }]);
  568. return ToolbarItemHelper
  569. }();
  570. var ContextMenuItemHelper = function(_ToolbarItemHelper) {
  571. _inherits(ContextMenuItemHelper, _ToolbarItemHelper);
  572. var _super3 = _createSuper(ContextMenuItemHelper);
  573. function ContextMenuItemHelper(widget, index) {
  574. var _this6;
  575. _classCallCheck(this, ContextMenuItemHelper);
  576. _this6 = _super3.call(this, widget);
  577. _this6._index = index;
  578. return _this6
  579. }
  580. _createClass(ContextMenuItemHelper, [{
  581. key: "setEnabled",
  582. value: function(enabled) {
  583. this._widget.option("items[".concat(this._index, "].disabled"), !enabled)
  584. }
  585. }, {
  586. key: "setValue",
  587. value: function(value) {}
  588. }]);
  589. return ContextMenuItemHelper
  590. }(ToolbarItemHelper);
  591. module.exports = DiagramToolbar;