SelectByPropertyTool.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SelectByPropertyTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { Controls } from '../ui/Controls.js'
  8. import { PropertySelectorDialog } from '../ui/PropertySelectorDialog.js'
  9. import { ObjectUtils } from '../utils/ObjectUtils.js'
  10. import { Toast } from '../ui/Toast.js'
  11. import { MessageDialog } from '../ui/MessageDialog.js'
  12. class SelectByPropertyTool extends Tool {
  13. constructor(application, options) {
  14. super(application)
  15. this.name = 'select_by_property'
  16. this.label = 'tool.select_by_property.label'
  17. this.help = 'tool.select_by_property.help'
  18. this.className = 'select_by_property'
  19. this.setOptions(options)
  20. this.immediate = true
  21. this.dialog = new PropertySelectorDialog(this.application, { title: 'title.select_by_property', selectValues: true, findPropertiesOnSelection: false })
  22. const dialog = this.dialog
  23. this.addButton = dialog.addContextButton('add_prop', 'button.add', () => this.addProperty())
  24. this.eqButton = dialog.addContextButton('add_expr_eq', '=', () => this.addExpression('=='))
  25. this.neqButton = dialog.addContextButton('add_expr_neq', '!=', () => this.addExpression('!='))
  26. this.ltButton = dialog.addContextButton('add_expr_lt', '<', () => this.addExpression('<'))
  27. this.gtButton = dialog.addContextButton('add_expr_gt', '>', () => this.addExpression('>'))
  28. this.andButton = dialog.addContextButton('add_op_and', 'And', () => this.addOperator('&&'))
  29. this.orButton = dialog.addContextButton('add_op_or', 'Or', () => this.addOperator('||'))
  30. this.clearButton = dialog.addContextButton('clear_prop', 'button.clear', () => this.clearExpression())
  31. dialog.onAccept = () => this.selectObjects()
  32. dialog.updateContextButtons = () => {
  33. let path = dialog.getSelectedNodePath()
  34. let pathLength = path.length
  35. this.addButton.disabled = pathLength === 0 || pathLength >= 3
  36. this.eqButton.disabled = pathLength !== 3
  37. this.neqButton.disabled = pathLength !== 3
  38. this.ltButton.disabled = pathLength !== 3
  39. this.gtButton.disabled = pathLength !== 3
  40. }
  41. }
  42. execute() {
  43. this.dialog.show()
  44. }
  45. addProperty() {
  46. const dialog = this.dialog
  47. if (dialog.isValue) return
  48. let path = dialog.getSelectedNodePath()
  49. let line = '$('
  50. for (let i = 0; i < path.length; i++) {
  51. let part = path[i]
  52. if (i > 0) line += ', '
  53. line += '"' + part + '"'
  54. }
  55. line += ') '
  56. dialog.appendCode(line)
  57. }
  58. addExpression(operator) {
  59. const dialog = this.dialog
  60. if (!dialog.isValue) return
  61. let path = dialog.getSelectedNodePath()
  62. let value = path[path.length - 1]
  63. let line = '$('
  64. for (let i = 0; i < path.length - 1; i++) {
  65. let part = path[i]
  66. if (i > 0) line += ', '
  67. line += '"' + part + '"'
  68. }
  69. line += ') ' + operator + ' '
  70. if (typeof value === 'string') {
  71. line += '"' + value + '"'
  72. } else {
  73. line += value
  74. }
  75. dialog.appendCode(line + '\n')
  76. }
  77. addOperator(operator) {
  78. const dialog = this.dialog
  79. dialog.appendCode(operator + '\n')
  80. }
  81. clearExpression() {
  82. const dialog = this.dialog
  83. dialog.setCode('')
  84. }
  85. selectObjects() {
  86. const application = this.application
  87. const selection = application.selection
  88. const dialog = this.dialog
  89. try {
  90. let objectExpression = dialog.getCode()
  91. let selectedObjects = application.findObjects(objectExpression, application.baseObject, true)
  92. if (selectedObjects.length > 0) {
  93. selection.add(...selectedObjects)
  94. dialog.hide()
  95. }
  96. Toast.create('message.objects_selected_by_prop', selectedObjects.length)
  97. .setI18N(application.i18n)
  98. .show()
  99. } catch (ex) {
  100. MessageDialog.create('ERROR', ex)
  101. .setClassName('error')
  102. .setI18N(this.application.i18n)
  103. .show()
  104. }
  105. }
  106. }
  107. export { SelectByPropertyTool }