criterias-item.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template lang="pug">
  2. .criterias-item
  3. //- Type
  4. v-select(
  5. solo
  6. :items='filteredCriteriaTypes'
  7. v-model='item.type'
  8. placeholder='Rule Type'
  9. ref='typeSelect'
  10. hide-details
  11. )
  12. template(slot='item', slot-scope='data')
  13. v-list-tile-avatar
  14. v-avatar(:color='data.item.color', size='40', tile): v-icon(color='white') {{ data.item.icon }}
  15. v-list-tile-content
  16. v-list-tile-title(v-html='data.item.text')
  17. v-list-tile-sub-title.caption(v-html='data.item.description')
  18. //- Operator
  19. v-select(
  20. solo
  21. :items='filteredCriteriaOperators'
  22. v-model='item.operator'
  23. placeholder='Operator'
  24. :disabled='!item.type'
  25. :class='!item.type ? "blue-grey lighten-4" : ""'
  26. hide-details
  27. )
  28. template(slot='item', slot-scope='data')
  29. v-list-tile-avatar
  30. v-avatar.white--text(color='blue', size='30', tile) {{ data.item.icon }}
  31. v-list-tile-content
  32. v-list-tile-title(v-html='data.item.text')
  33. //- Value
  34. v-select(
  35. v-if='item.type === "country"'
  36. solo
  37. :items='countries'
  38. v-model='item.value'
  39. placeholder='Countries...'
  40. multiple
  41. item-text='name'
  42. item-value='code'
  43. hide-details
  44. )
  45. v-text-field(
  46. v-else-if='item.type === "path"'
  47. solo
  48. v-model='item.value'
  49. label='Path (e.g. /section)'
  50. hide-details
  51. )
  52. v-text-field(
  53. v-else-if='item.type === "date"'
  54. solo
  55. @click.native.stop='dateActivator = true'
  56. v-model='item.value'
  57. label='YYYY-MM-DD'
  58. readonly
  59. hide-details
  60. )
  61. v-text-field(
  62. v-else-if='item.type === "time"'
  63. solo
  64. @click.native.stop='timeActivator = true'
  65. v-model='item.value'
  66. label='HH:MM'
  67. readonly
  68. hide-details
  69. )
  70. v-select(
  71. v-else-if='item.type === "group"'
  72. solo
  73. :items='groups'
  74. v-model='item.value'
  75. placeholder='Group...'
  76. item-text='name'
  77. item-value='id'
  78. hide-details
  79. )
  80. v-text-field.blue-grey.lighten-4(
  81. v-else
  82. solo
  83. disabled
  84. hide-details
  85. )
  86. v-dialog(lazy, v-model='dateActivator', width='290px', ref='dateDialog')
  87. v-date-picker(v-model='item.value', scrollable, color='primary')
  88. v-btn(flat, color='primary' @click='$refs.dateDialog.save(date)', block) ok
  89. v-dialog(lazy, v-model='timeActivator', width='300px', ref='timeDialog')
  90. v-time-picker(v-model='item.value', scrollable, color='primary')
  91. v-btn(flat, color='primary' @click='$refs.timeDialog.save(time)', block) ok
  92. v-btn(icon, @click='remove'): v-icon(color='blue-grey') clear
  93. </template>
  94. <script>
  95. import _ from 'lodash'
  96. // import countriesQuery from 'gql/upsells-query-countries.gql'
  97. export default {
  98. inject: ['allowedCriteriaTypes'],
  99. props: {
  100. value: {
  101. type: Object,
  102. default() { return {} }
  103. },
  104. groupIndex: {
  105. type: Number,
  106. default() { return 0 }
  107. },
  108. itemIndex: {
  109. type: Number,
  110. default() { return 0 }
  111. }
  112. },
  113. data() {
  114. return {
  115. item: {
  116. operator: '',
  117. type: '',
  118. value: ''
  119. },
  120. dateActivator: false,
  121. dateDialog: false,
  122. timeActivator: false,
  123. timeDialog: false,
  124. countries: [],
  125. groups: [],
  126. criteriaTypes: [
  127. { text: 'Path', value: 'path', icon: 'space_bar', color: 'blue', description: 'Match the path of the document being viewed.' },
  128. { text: 'Date', value: 'date', icon: 'date_range', color: 'blue', description: 'Match the current calendar day.' },
  129. { text: 'Time', value: 'time', icon: 'access_time', color: 'blue', description: 'Match the current time of day.' },
  130. { text: 'User Country', value: 'country', icon: 'public', color: 'red', description: `Match the user's country.` },
  131. { text: 'User Group', value: 'group', icon: 'group', color: 'orange', description: 'Match the user group assignments.' }
  132. ],
  133. criteriaOperators: {
  134. country: [
  135. { text: 'In', value: 'in', icon: '[...]' },
  136. { text: 'Not In', value: 'notIn', icon: '[ x ]' }
  137. ],
  138. path: [
  139. { text: 'Matches Exactly', value: 'eq', icon: '=' },
  140. { text: 'NOT Matches Exactly', value: 'ne', icon: '!=' },
  141. { text: 'Starts With', value: 'sw', icon: 'x...' },
  142. { text: 'NOT Starts With', value: 'nsw', icon: '!x...' },
  143. { text: 'Ends With', value: 'ew', icon: '...x' },
  144. { text: 'NOT Ends With', value: 'new', icon: '!...x' },
  145. { text: 'Matches Regex', value: 'regexp', icon: '^x$' }
  146. ],
  147. date: [
  148. { text: 'On or After', value: 'gte', icon: '>=' },
  149. { text: 'On or Before', value: 'lte', icon: '<=' }
  150. ],
  151. time: [
  152. { text: 'At or Later Than', value: 'gte', icon: '>=' },
  153. { text: 'At or Before', value: 'lte', icon: '<=' }
  154. ],
  155. group: [
  156. { text: 'Is Part Of', value: 'in', icon: '[...]' },
  157. { text: 'Is Not Part Of', value: 'notIn', icon: '[ x ]' }
  158. ]
  159. }
  160. }
  161. },
  162. computed: {
  163. filteredCriteriaOperators() {
  164. return _.get(this.criteriaOperators, this.item.type, [])
  165. },
  166. filteredCriteriaTypes() {
  167. console.info(this.allowedCriteriaTypes)
  168. return _.filter(this.criteriaTypes, c => _.includes(this.allowedCriteriaTypes, c.value))
  169. },
  170. itemType() {
  171. return this.item.type
  172. }
  173. },
  174. watch: {
  175. itemType(newValue, oldValue) {
  176. this.item.operator = _.head(this.criteriaOperators[newValue]).value
  177. this.item.value = ''
  178. },
  179. item: {
  180. handler(newValue, oldValue) {
  181. this.$emit('update', this.groupIndex, this.itemIndex, this.item)
  182. },
  183. deep: true
  184. }
  185. },
  186. mounted() {
  187. if (!this.item.type) {
  188. this.$refs.typeSelect.showMenu()
  189. }
  190. },
  191. methods: {
  192. remove() {
  193. this.$emit('remove', this.groupIndex, this.itemIndex)
  194. }
  195. }
  196. // apollo: {
  197. // countries: {
  198. // query: countriesQuery,
  199. // update: (data) => data.location.countries
  200. // }
  201. // }
  202. }
  203. </script>