You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rule.vue 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <div v-if="operation" class="section rule" :style="{ borderLeftColor: operation.color || '' }">
  3. <div class="trigger">
  4. <p>
  5. <span>{{ t('workflowengine', 'When') }}</span>
  6. <Event :rule="rule" @update="updateRule" />
  7. </p>
  8. <p v-for="(check, index) in rule.checks" :key="index">
  9. <span>{{ t('workflowengine', 'and') }}</span>
  10. <Check :check="check"
  11. :rule="rule"
  12. @update="updateRule"
  13. @validate="validate"
  14. @remove="removeCheck(check)" />
  15. </p>
  16. <p>
  17. <span />
  18. <input v-if="lastCheckComplete"
  19. type="button"
  20. class="check--add"
  21. value="Add a new filter"
  22. @click="onAddFilter">
  23. </p>
  24. </div>
  25. <div class="flow-icon icon-confirm" />
  26. <div class="action">
  27. <Operation :operation="operation" :colored="false">
  28. <component :is="operation.options"
  29. v-if="operation.options"
  30. v-model="rule.operation"
  31. @input="updateOperation" />
  32. </Operation>
  33. <div class="buttons">
  34. <button class="status-button icon"
  35. :class="ruleStatus.class"
  36. @click="saveRule">
  37. {{ ruleStatus.title }}
  38. </button>
  39. <button v-if="rule.id < -1 || dirty" @click="cancelRule">
  40. {{ t('workflowengine', 'Cancel') }}
  41. </button>
  42. <button v-else-if="!dirty" @click="deleteRule">
  43. {{ t('workflowengine', 'Delete') }}
  44. </button>
  45. </div>
  46. <p v-if="error" class="error-message">
  47. {{ error }}
  48. </p>
  49. </div>
  50. </div>
  51. </template>
  52. <script>
  53. import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
  54. import Actions from '@nextcloud/vue/dist/Components/Actions'
  55. import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
  56. import Event from './Event'
  57. import Check from './Check'
  58. import Operation from './Operation'
  59. export default {
  60. name: 'Rule',
  61. components: {
  62. Operation, Check, Event, Actions, ActionButton,
  63. },
  64. directives: {
  65. Tooltip,
  66. },
  67. props: {
  68. rule: {
  69. type: Object,
  70. required: true,
  71. },
  72. },
  73. data() {
  74. return {
  75. editing: false,
  76. checks: [],
  77. error: null,
  78. dirty: this.rule.id < 0,
  79. originalRule: null,
  80. }
  81. },
  82. computed: {
  83. operation() {
  84. return this.$store.getters.getOperationForRule(this.rule)
  85. },
  86. ruleStatus() {
  87. if (this.error || !this.rule.valid || this.rule.checks.length === 0 || this.rule.checks.some((check) => check.invalid === true)) {
  88. return {
  89. title: t('workflowengine', 'The configuration is invalid'),
  90. class: 'icon-close-white invalid',
  91. tooltip: { placement: 'bottom', show: true, content: this.error },
  92. }
  93. }
  94. if (!this.dirty) {
  95. return { title: t('workflowengine', 'Active'), class: 'icon icon-checkmark' }
  96. }
  97. return { title: t('workflowengine', 'Save'), class: 'icon-confirm-white primary' }
  98. },
  99. lastCheckComplete() {
  100. const lastCheck = this.rule.checks[this.rule.checks.length - 1]
  101. return typeof lastCheck === 'undefined' || lastCheck.class !== null
  102. },
  103. },
  104. mounted() {
  105. this.originalRule = JSON.parse(JSON.stringify(this.rule))
  106. },
  107. methods: {
  108. async updateOperation(operation) {
  109. this.$set(this.rule, 'operation', operation)
  110. await this.updateRule()
  111. },
  112. validate(state) {
  113. this.error = null
  114. this.$store.dispatch('updateRule', this.rule)
  115. },
  116. updateRule() {
  117. if (!this.dirty) {
  118. this.dirty = true
  119. }
  120. this.error = null
  121. this.$store.dispatch('updateRule', this.rule)
  122. },
  123. async saveRule() {
  124. try {
  125. await this.$store.dispatch('pushUpdateRule', this.rule)
  126. this.dirty = false
  127. this.error = null
  128. this.originalRule = JSON.parse(JSON.stringify(this.rule))
  129. } catch (e) {
  130. console.error('Failed to save operation')
  131. this.error = e.response.data.ocs.meta.message
  132. }
  133. },
  134. async deleteRule() {
  135. try {
  136. await this.$store.dispatch('deleteRule', this.rule)
  137. } catch (e) {
  138. console.error('Failed to delete operation')
  139. this.error = e.response.data.ocs.meta.message
  140. }
  141. },
  142. cancelRule() {
  143. if (this.rule.id < 0) {
  144. this.$store.dispatch('removeRule', this.rule)
  145. } else {
  146. this.$store.dispatch('updateRule', this.originalRule)
  147. this.originalRule = JSON.parse(JSON.stringify(this.rule))
  148. this.dirty = false
  149. }
  150. },
  151. async removeCheck(check) {
  152. const index = this.rule.checks.findIndex(item => item === check)
  153. if (index > -1) {
  154. this.$delete(this.rule.checks, index)
  155. }
  156. this.$store.dispatch('updateRule', this.rule)
  157. },
  158. onAddFilter() {
  159. // eslint-disable-next-line vue/no-mutating-props
  160. this.rule.checks.push({ class: null, operator: null, value: '' })
  161. },
  162. },
  163. }
  164. </script>
  165. <style scoped lang="scss">
  166. button.icon {
  167. padding-left: 32px;
  168. background-position: 10px center;
  169. }
  170. .buttons {
  171. display: block;
  172. overflow: hidden;
  173. button {
  174. float: right;
  175. height: 34px;
  176. }
  177. }
  178. .error-message {
  179. float: right;
  180. margin-right: 10px;
  181. }
  182. .status-button {
  183. transition: 0.5s ease all;
  184. display: block;
  185. margin: 3px 10px 3px auto;
  186. }
  187. .status-button.primary {
  188. padding-left: 32px;
  189. background-position: 10px center;
  190. }
  191. .status-button:not(.primary) {
  192. background-color: var(--color-main-background);
  193. }
  194. .status-button.invalid {
  195. background-color: var(--color-warning);
  196. color: #fff;
  197. border: none;
  198. }
  199. .status-button.icon-checkmark {
  200. border: 1px solid var(--color-success);
  201. }
  202. .flow-icon {
  203. width: 44px;
  204. }
  205. .rule {
  206. display: flex;
  207. flex-wrap: wrap;
  208. border-left: 5px solid var(--color-primary-element);
  209. .trigger, .action {
  210. flex-grow: 1;
  211. min-height: 100px;
  212. max-width: 700px;
  213. }
  214. .action {
  215. max-width: 400px;
  216. position: relative;
  217. }
  218. .icon-confirm {
  219. background-position: right 27px;
  220. padding-right: 20px;
  221. margin-right: 20px;
  222. }
  223. }
  224. .trigger p, .action p {
  225. min-height: 34px;
  226. display: flex;
  227. & > span {
  228. min-width: 50px;
  229. text-align: right;
  230. color: var(--color-text-maxcontrast);
  231. padding-right: 10px;
  232. padding-top: 6px;
  233. }
  234. .multiselect {
  235. flex-grow: 1;
  236. max-width: 300px;
  237. }
  238. }
  239. .trigger p:first-child span {
  240. padding-top: 3px;
  241. }
  242. .check--add {
  243. background-position: 7px center;
  244. background-color: transparent;
  245. padding-left: 6px;
  246. margin: 0;
  247. width: 180px;
  248. border-radius: var(--border-radius);
  249. color: var(--color-text-maxcontrast);
  250. font-weight: normal;
  251. text-align: left;
  252. font-size: 1em;
  253. }
  254. @media (max-width:1400px) {
  255. .rule {
  256. &, .trigger, .action {
  257. width: 100%;
  258. max-width: 100%;
  259. }
  260. .flow-icon {
  261. display: none;
  262. }
  263. }
  264. }
  265. </style>