diagram-js_lib_features_context-pad_ContextPad.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import {
  2. import_css
  3. } from "./chunk-PGYCW7PW.js";
  4. import {
  5. attr,
  6. classes,
  7. delegate,
  8. domify$1,
  9. event,
  10. query
  11. } from "./chunk-XZ2PVW7M.js";
  12. import {
  13. assign,
  14. every,
  15. forEach,
  16. isArray,
  17. isDefined,
  18. isFunction,
  19. some
  20. } from "./chunk-4AK4GF4H.js";
  21. import "./chunk-2LSFTFF7.js";
  22. // node_modules/.pnpm/diagram-js@11.9.1/node_modules/diagram-js/lib/util/Elements.js
  23. function getBBox(elements, stopRecursion) {
  24. stopRecursion = !!stopRecursion;
  25. if (!isArray(elements)) {
  26. elements = [elements];
  27. }
  28. var minX, minY, maxX, maxY;
  29. forEach(elements, function(element) {
  30. var bbox = element;
  31. if (element.waypoints && !stopRecursion) {
  32. bbox = getBBox(element.waypoints, true);
  33. }
  34. var x = bbox.x, y = bbox.y, height = bbox.height || 0, width = bbox.width || 0;
  35. if (x < minX || minX === void 0) {
  36. minX = x;
  37. }
  38. if (y < minY || minY === void 0) {
  39. minY = y;
  40. }
  41. if (x + width > maxX || maxX === void 0) {
  42. maxX = x + width;
  43. }
  44. if (y + height > maxY || maxY === void 0) {
  45. maxY = y + height;
  46. }
  47. });
  48. return {
  49. x: minX,
  50. y: minY,
  51. height: maxY - minY,
  52. width: maxX - minX
  53. };
  54. }
  55. // node_modules/.pnpm/diagram-js@11.9.1/node_modules/diagram-js/lib/features/context-pad/ContextPad.js
  56. var entrySelector = ".entry";
  57. var DEFAULT_PRIORITY = 1e3;
  58. var CONTEXT_PAD_PADDING = 12;
  59. function ContextPad(canvas, config, eventBus, overlays) {
  60. this._canvas = canvas;
  61. this._eventBus = eventBus;
  62. this._overlays = overlays;
  63. var scale = isDefined(config && config.scale) ? config.scale : {
  64. min: 1,
  65. max: 1.5
  66. };
  67. this._overlaysConfig = {
  68. scale
  69. };
  70. this._current = null;
  71. this._init();
  72. }
  73. ContextPad.$inject = [
  74. "canvas",
  75. "config.contextPad",
  76. "eventBus",
  77. "overlays"
  78. ];
  79. ContextPad.prototype._init = function() {
  80. var self = this;
  81. this._eventBus.on("selection.changed", function(event2) {
  82. var selection = event2.newSelection;
  83. var target = selection.length ? selection.length === 1 ? selection[0] : selection : null;
  84. if (target) {
  85. self.open(target, true);
  86. } else {
  87. self.close();
  88. }
  89. });
  90. this._eventBus.on("elements.changed", function(event2) {
  91. var elements = event2.elements, current = self._current;
  92. if (!current) {
  93. return;
  94. }
  95. var currentTarget = current.target;
  96. var currentChanged = some(
  97. isArray(currentTarget) ? currentTarget : [currentTarget],
  98. function(element) {
  99. return includes(elements, element);
  100. }
  101. );
  102. if (currentChanged) {
  103. self.open(currentTarget, true);
  104. }
  105. });
  106. };
  107. ContextPad.prototype.registerProvider = function(priority, provider) {
  108. if (!provider) {
  109. provider = priority;
  110. priority = DEFAULT_PRIORITY;
  111. }
  112. this._eventBus.on("contextPad.getProviders", priority, function(event2) {
  113. event2.providers.push(provider);
  114. });
  115. };
  116. ContextPad.prototype.getEntries = function(target) {
  117. var providers = this._getProviders();
  118. var provideFn = isArray(target) ? "getMultiElementContextPadEntries" : "getContextPadEntries";
  119. var entries = {};
  120. forEach(providers, function(provider) {
  121. if (!isFunction(provider[provideFn])) {
  122. return;
  123. }
  124. var entriesOrUpdater = provider[provideFn](target);
  125. if (isFunction(entriesOrUpdater)) {
  126. entries = entriesOrUpdater(entries);
  127. } else {
  128. forEach(entriesOrUpdater, function(entry, id) {
  129. entries[id] = entry;
  130. });
  131. }
  132. });
  133. return entries;
  134. };
  135. ContextPad.prototype.trigger = function(action, event2, autoActivate) {
  136. var entry, originalEvent, button = event2.delegateTarget || event2.target;
  137. if (!button) {
  138. return event2.preventDefault();
  139. }
  140. entry = attr(button, "data-action");
  141. originalEvent = event2.originalEvent || event2;
  142. return this.triggerEntry(entry, action, originalEvent, autoActivate);
  143. };
  144. ContextPad.prototype.triggerEntry = function(entryId, action, event2, autoActivate) {
  145. if (!this.isShown()) {
  146. return;
  147. }
  148. var target = this._current.target, entries = this._current.entries;
  149. var entry = entries[entryId];
  150. if (!entry) {
  151. return;
  152. }
  153. var handler = entry.action;
  154. if (isFunction(handler)) {
  155. if (action === "click") {
  156. return handler(event2, target, autoActivate);
  157. }
  158. } else {
  159. if (handler[action]) {
  160. return handler[action](event2, target, autoActivate);
  161. }
  162. }
  163. event2.preventDefault();
  164. };
  165. ContextPad.prototype.open = function(target, force) {
  166. if (!force && this.isOpen(target)) {
  167. return;
  168. }
  169. this.close();
  170. this._updateAndOpen(target);
  171. };
  172. ContextPad.prototype._getProviders = function() {
  173. var event2 = this._eventBus.createEvent({
  174. type: "contextPad.getProviders",
  175. providers: []
  176. });
  177. this._eventBus.fire(event2);
  178. return event2.providers;
  179. };
  180. ContextPad.prototype._updateAndOpen = function(target) {
  181. var entries = this.getEntries(target), pad = this.getPad(target), html = pad.html, image;
  182. forEach(entries, function(entry, id) {
  183. var grouping = entry.group || "default", control = domify$1(entry.html || '<div class="entry" draggable="true"></div>'), container;
  184. attr(control, "data-action", id);
  185. container = query("[data-group=" + (0, import_css.default)(grouping) + "]", html);
  186. if (!container) {
  187. container = domify$1('<div class="group"></div>');
  188. attr(container, "data-group", grouping);
  189. html.appendChild(container);
  190. }
  191. container.appendChild(control);
  192. if (entry.className) {
  193. addClasses(control, entry.className);
  194. }
  195. if (entry.title) {
  196. attr(control, "title", entry.title);
  197. }
  198. if (entry.imageUrl) {
  199. image = domify$1("<img>");
  200. attr(image, "src", entry.imageUrl);
  201. image.style.width = "100%";
  202. image.style.height = "100%";
  203. control.appendChild(image);
  204. }
  205. });
  206. classes(html).add("open");
  207. this._current = {
  208. target,
  209. entries,
  210. pad
  211. };
  212. this._eventBus.fire("contextPad.open", { current: this._current });
  213. };
  214. ContextPad.prototype.getPad = function(target) {
  215. if (this.isOpen()) {
  216. return this._current.pad;
  217. }
  218. var self = this;
  219. var overlays = this._overlays;
  220. var html = domify$1('<div class="djs-context-pad"></div>');
  221. var position = this._getPosition(target);
  222. var overlaysConfig = assign({
  223. html
  224. }, this._overlaysConfig, position);
  225. delegate.bind(html, entrySelector, "click", function(event2) {
  226. self.trigger("click", event2);
  227. });
  228. delegate.bind(html, entrySelector, "dragstart", function(event2) {
  229. self.trigger("dragstart", event2);
  230. });
  231. event.bind(html, "mousedown", function(event2) {
  232. event2.stopPropagation();
  233. });
  234. var activeRootElement = this._canvas.getRootElement();
  235. this._overlayId = overlays.add(activeRootElement, "context-pad", overlaysConfig);
  236. var pad = overlays.get(this._overlayId);
  237. this._eventBus.fire("contextPad.create", {
  238. target,
  239. pad
  240. });
  241. return pad;
  242. };
  243. ContextPad.prototype.close = function() {
  244. if (!this.isOpen()) {
  245. return;
  246. }
  247. this._overlays.remove(this._overlayId);
  248. this._overlayId = null;
  249. this._eventBus.fire("contextPad.close", { current: this._current });
  250. this._current = null;
  251. };
  252. ContextPad.prototype.isOpen = function(target) {
  253. var current = this._current;
  254. if (!current) {
  255. return false;
  256. }
  257. if (!target) {
  258. return true;
  259. }
  260. var currentTarget = current.target;
  261. if (isArray(target) !== isArray(currentTarget)) {
  262. return false;
  263. }
  264. if (isArray(target)) {
  265. return target.length === currentTarget.length && every(target, function(element) {
  266. return includes(currentTarget, element);
  267. });
  268. } else {
  269. return currentTarget === target;
  270. }
  271. };
  272. ContextPad.prototype.isShown = function() {
  273. return this.isOpen() && this._overlays.isShown();
  274. };
  275. ContextPad.prototype._getPosition = function(target) {
  276. var elements = isArray(target) ? target : [target];
  277. var bBox = getBBox(elements);
  278. return {
  279. position: {
  280. left: bBox.x + bBox.width + CONTEXT_PAD_PADDING,
  281. top: bBox.y - CONTEXT_PAD_PADDING / 2
  282. }
  283. };
  284. };
  285. function addClasses(element, classNames) {
  286. var classes2 = classes(element);
  287. classNames = isArray(classNames) ? classNames : classNames.split(/\s+/g);
  288. classNames.forEach(function(cls) {
  289. classes2.add(cls);
  290. });
  291. }
  292. function includes(array, item) {
  293. return array.indexOf(item) !== -1;
  294. }
  295. export {
  296. ContextPad as default
  297. };
  298. //# sourceMappingURL=diagram-js_lib_features_context-pad_ContextPad.js.map