selection_box.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __read = (this && this.__read) || function (o, n) {
  16. var m = typeof Symbol === "function" && o[Symbol.iterator];
  17. if (!m) return o;
  18. var i = m.call(o), r, ar = [], e;
  19. try {
  20. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  21. }
  22. catch (error) { e = { error: error }; }
  23. finally {
  24. try {
  25. if (r && !r.done && (m = i["return"])) m.call(i);
  26. }
  27. finally { if (e) throw e.error; }
  28. }
  29. return ar;
  30. };
  31. Object.defineProperty(exports, "__esModule", { value: true });
  32. exports.SelectionBox = exports.SelectionMenu = void 0;
  33. var menu_util_js_1 = require("./menu_util.js");
  34. var html_classes_js_1 = require("./html_classes.js");
  35. var abstract_menu_js_1 = require("./abstract_menu.js");
  36. var info_js_1 = require("./info.js");
  37. var SelectionMenu = (function (_super) {
  38. __extends(SelectionMenu, _super);
  39. function SelectionMenu(anchor) {
  40. var _this = _super.call(this) || this;
  41. _this.anchor = anchor;
  42. _this.className = html_classes_js_1.HtmlClasses['SELECTIONMENU'];
  43. _this.variablePool = _this.anchor.menu.pool;
  44. _this.baseMenu = _this.anchor.menu;
  45. return _this;
  46. }
  47. SelectionMenu.fromJson = function (factory, _a, sb) {
  48. var title = _a.title, values = _a.values, variable = _a.variable;
  49. var selection = new this(sb);
  50. var tit = factory.get('label')(factory, { content: title || '', id: title || 'id' }, selection);
  51. var rul = factory.get('rule')(factory, {}, selection);
  52. var radios = values.map(function (x) { return factory.get('radio')(factory, { content: x, variable: variable, id: x }, selection); });
  53. var items = [tit, rul].concat(radios);
  54. selection.items = items;
  55. return selection;
  56. };
  57. SelectionMenu.prototype.generateHtml = function () {
  58. _super.prototype.generateHtml.call(this);
  59. this.items.forEach(function (item) { return item.html.classList.add(html_classes_js_1.HtmlClasses['SELECTIONITEM']); });
  60. };
  61. SelectionMenu.prototype.display = function () { };
  62. SelectionMenu.prototype.right = function (event) {
  63. this.anchor.right(event);
  64. };
  65. SelectionMenu.prototype.left = function (event) {
  66. this.anchor.left(event);
  67. };
  68. return SelectionMenu;
  69. }(abstract_menu_js_1.AbstractMenu));
  70. exports.SelectionMenu = SelectionMenu;
  71. var SelectionBox = (function (_super) {
  72. __extends(SelectionBox, _super);
  73. function SelectionBox(title, signature, style, grid) {
  74. if (style === void 0) { style = "none"; }
  75. if (grid === void 0) { grid = "vertical"; }
  76. var _this = _super.call(this, title, null, signature) || this;
  77. _this.style = style;
  78. _this.grid = grid;
  79. _this._selections = [];
  80. _this.prefix = 'ctxt-selection';
  81. _this._balanced = true;
  82. return _this;
  83. }
  84. SelectionBox.fromJson = function (factory, _a, ctxt) {
  85. var title = _a.title, signature = _a.signature, selections = _a.selections, order = _a.order, grid = _a.grid;
  86. var sb = new this(title, signature, order, grid);
  87. sb.attachMenu(ctxt);
  88. var sels = selections.map(function (x) { return factory.get('selectionMenu')(factory, x, sb); });
  89. sb.selections = sels;
  90. return sb;
  91. };
  92. SelectionBox.prototype.attachMenu = function (menu) {
  93. this.menu = menu;
  94. };
  95. Object.defineProperty(SelectionBox.prototype, "selections", {
  96. get: function () {
  97. return this._selections;
  98. },
  99. set: function (selections) {
  100. var _this = this;
  101. this._selections = [];
  102. selections.forEach(function (x) { return _this.addSelection(x); });
  103. },
  104. enumerable: false,
  105. configurable: true
  106. });
  107. SelectionBox.prototype.addSelection = function (selection) {
  108. selection.anchor = this;
  109. this._selections.push(selection);
  110. };
  111. SelectionBox.prototype.rowDiv = function (sels) {
  112. var _this = this;
  113. var div = document.createElement('div');
  114. this.contentDiv.appendChild(div);
  115. var rects = sels.map(function (sel) {
  116. div.appendChild(sel.html);
  117. if (!sel.html.id) {
  118. sel.html.id = _this.prefix + menu_util_js_1.MenuUtil.counter();
  119. }
  120. return sel.html.getBoundingClientRect();
  121. });
  122. var column = rects.map(function (x) { return x.width; });
  123. var width = column.reduce(function (x, y) { return x + y; }, 0);
  124. var height = rects.reduce(function (x, y) { return Math.max(x, y.height); }, 0);
  125. div.classList.add(html_classes_js_1.HtmlClasses['SELECTIONDIVIDER']);
  126. div.setAttribute('style', 'height: ' + height + 'px;');
  127. return [div, width, height, column];
  128. };
  129. SelectionBox.prototype.display = function () {
  130. _super.prototype.display.call(this);
  131. this.order();
  132. if (!this.selections.length) {
  133. return;
  134. }
  135. var outerDivs = [];
  136. var maxWidth = 0;
  137. var balancedColumn = [];
  138. var chunks = this.getChunkSize(this.selections.length);
  139. var _loop_1 = function (i) {
  140. var sels = this_1.selections.slice(i, i + chunks);
  141. var _a = __read(this_1.rowDiv(sels), 4), div = _a[0], width = _a[1], height = _a[2], column = _a[3];
  142. outerDivs.push(div);
  143. maxWidth = Math.max(maxWidth, width);
  144. sels.forEach(function (sel) { return sel.html.style.height = height + 'px'; });
  145. balancedColumn = this_1.combineColumn(balancedColumn, column);
  146. };
  147. var this_1 = this;
  148. for (var i = 0; i < this.selections.length; i += chunks) {
  149. _loop_1(i);
  150. }
  151. if (this._balanced) {
  152. this.balanceColumn(outerDivs, balancedColumn);
  153. maxWidth = balancedColumn.reduce(function (x, y) { return x + y; }, 20);
  154. }
  155. outerDivs.forEach(function (div) { return div.style.width = maxWidth + 'px'; });
  156. };
  157. SelectionBox.prototype.getChunkSize = function (size) {
  158. switch (this.grid) {
  159. case "square":
  160. return Math.floor(Math.sqrt(size));
  161. case "horizontal":
  162. return Math.floor(size / SelectionBox.chunkSize);
  163. case "vertical":
  164. default:
  165. return SelectionBox.chunkSize;
  166. }
  167. };
  168. SelectionBox.prototype.balanceColumn = function (divs, column) {
  169. divs.forEach(function (div) {
  170. var children = Array.from(div.children);
  171. for (var i = 0, child = void 0; child = children[i]; i++) {
  172. child.style.width = column[i] + 'px';
  173. }
  174. });
  175. };
  176. SelectionBox.prototype.combineColumn = function (col1, col2) {
  177. var result = [];
  178. var i = 0;
  179. while (col1[i] || col2[i]) {
  180. if (!col1[i]) {
  181. result = result.concat(col2.slice(i));
  182. break;
  183. }
  184. if (!col2[i]) {
  185. result = result.concat(col1.slice(i));
  186. break;
  187. }
  188. result.push(Math.max(col1[i], col2[i]));
  189. i++;
  190. }
  191. ;
  192. return result;
  193. };
  194. SelectionBox.prototype.left = function (event) {
  195. var _this = this;
  196. this.move(event, function (index) {
  197. return (index === 0 ? _this.selections.length : index) - 1;
  198. });
  199. };
  200. SelectionBox.prototype.right = function (event) {
  201. var _this = this;
  202. this.move(event, function (index) {
  203. return index === _this.selections.length - 1 ? 0 : index + 1;
  204. });
  205. };
  206. SelectionBox.prototype.generateHtml = function () {
  207. _super.prototype.generateHtml.call(this);
  208. this.html.classList.add(html_classes_js_1.HtmlClasses['SELECTION']);
  209. };
  210. SelectionBox.prototype.generateContent = function () {
  211. var div = _super.prototype.generateContent.call(this);
  212. div.classList.add(html_classes_js_1.HtmlClasses['SELECTIONBOX']);
  213. div.removeAttribute('tabindex');
  214. return div;
  215. };
  216. SelectionBox.prototype.findSelection = function (event) {
  217. var target = event.target;
  218. var selection = null;
  219. if (target.id) {
  220. selection = this.selections.find(function (x) { return x.html.id === target.id; });
  221. }
  222. if (!selection) {
  223. var id_1 = target.parentElement.id;
  224. selection = this.selections.find(function (x) { return x.html.id === id_1; });
  225. }
  226. return selection;
  227. };
  228. SelectionBox.prototype.move = function (event, isNext) {
  229. var selection = this.findSelection(event);
  230. if (selection.focused) {
  231. selection.focused.unfocus();
  232. }
  233. var index = this.selections.indexOf(selection);
  234. var next = isNext(index);
  235. this.selections[next].focus();
  236. };
  237. SelectionBox.prototype.order = function () {
  238. this.selections.sort(SelectionBox.orderMethod.get(this.style));
  239. };
  240. SelectionBox.prototype.toJson = function () {
  241. return { type: ''
  242. };
  243. };
  244. SelectionBox.chunkSize = 4;
  245. SelectionBox.orderMethod = new Map([
  246. ["alphabetical", function (x, y) { return x.items[0].content.localeCompare(y.items[0].content); }],
  247. ["none", function (_x, _y) { return 1; }],
  248. ["decreasing", function (x, y) {
  249. var xl = x.items.length;
  250. var yl = y.items.length;
  251. return (xl < yl) ? 1 : ((yl < xl) ? -1 : 0);
  252. }],
  253. ["increasing", function (x, y) {
  254. var xl = x.items.length;
  255. var yl = y.items.length;
  256. return (xl < yl) ? -1 : ((yl < xl) ? 1 : 0);
  257. }],
  258. ]);
  259. return SelectionBox;
  260. }(info_js_1.Info));
  261. exports.SelectionBox = SelectionBox;
  262. //# sourceMappingURL=selection_box.js.map