forked from area17/twill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepeater.vue
executable file
·177 lines (164 loc) · 4.78 KB
/
Repeater.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<template>
<div class="content">
<draggable class="content__content" v-model="blocks" :options="dragOptions">
<transition-group name="draggable_list" tag='div'>
<div class="content__item" v-for="(block, index) in blocks" :key="block.id">
<a17-block :block="block" :index="index" :size="blockSize" :opened="opened" @open="setOpened">
<a17-button slot="block-actions" variant="icon" data-action @click="duplicateBlock(index)" v-if="hasRemainingBlocks"><span v-svg symbol="add"></span></a17-button>
<div slot="dropdown-action">
<button type="button" @click="collapseAllBlocks()">Collapse All</button>
<button type="button" @click="deleteBlock(index)">Delete</button>
<button type="button" @click="duplicateBlock(index)" v-if="hasRemainingBlocks">Duplicate</button>
</div>
</a17-block>
</div>
</transition-group>
</draggable>
<div class="content__trigger">
<a17-button :class="triggerClass" :variant="triggerVariant" :size="triggerSize" @click="addBlock()" v-if="hasRemainingBlocks && blockType.trigger">{{ blockType.trigger }}</a17-button>
<div class="content__note f--note f--small"><slot></slot></div>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
import { FORM } from '@/store/mutations'
import draggable from 'vuedraggable'
import draggableMixin from '@/mixins/draggable'
import Block from '@/components/blocks/Block.vue'
export default {
name: 'A17Repeater',
components: {
'a17-block': Block,
draggable
},
mixins: [draggableMixin],
props: {
type: {
type: String,
required: true
},
name: {
type: String,
required: true
}
},
data: function () {
return {
opened: true,
handle: '.block__handle' // drag handle
}
},
computed: {
triggerVariant: function () {
return this.inContentEditor ? 'aslink' : 'action'
},
triggerSize: function () {
return this.inContentEditor ? 'small' : ''
},
triggerClass: function () {
return this.inContentEditor ? 'content__button' : ''
},
blockSize: function () {
return this.inContentEditor ? 'small' : ''
},
inContentEditor: function () {
return typeof this.$parent.repeaterName !== 'undefined'
},
hasRemainingBlocks: function () {
return !this.blockType.hasOwnProperty('max') || (this.blockType.max > this.blocks.length)
},
blockType: function () {
return this.availableBlocks[this.type] ? this.availableBlocks[this.type] : {}
},
blocks: {
get () {
if (this.savedBlocks.hasOwnProperty(this.name)) {
return this.savedBlocks[this.name] || []
} else {
return []
}
},
set (value) {
this.$store.commit(FORM.REORDER_FORM_BLOCKS, {
type: this.type,
name: this.name,
blocks: value
})
}
},
...mapState({
savedBlocks: state => state.repeaters.repeaters,
availableBlocks: state => state.repeaters.availableRepeaters
})
},
methods: {
setOpened: function (value) {
this.opened = value
},
addBlock: function () {
this.opened = true
this.$store.commit(FORM.ADD_FORM_BLOCK, { type: this.type, name: this.name })
},
duplicateBlock: function (index) {
this.opened = true
this.$store.commit(FORM.DUPLICATE_FORM_BLOCK, {
type: this.type,
name: this.name,
index: index
})
},
deleteBlock: function (index) {
this.$store.commit(FORM.DELETE_FORM_BLOCK, {
type: this.type,
name: this.name,
index: index
})
},
collapseAllBlocks: function () {
this.opened = false
}
},
mounted: function () {
const self = this
// if there are blocks, these should be all collapse by default
this.$nextTick(function () {
if (self.savedBlocks.length > 0) self.collapseAllBlocks()
})
}
}
</script>
<style lang="scss" scoped>
.content {
margin-top:20px; // margin-top:35px;
}
.content__content {
margin-bottom:20px;
+ .dropdown {
display:inline-block;
}
}
.content__item {
border:1px solid $color__border;
border-top:0 none;
&.sortable-ghost {
opacity:0.5;
}
}
.content__item:first-child {
border-top:1px solid $color__border;
}
.content__trigger {
display:flex;
}
.content__button {
display:block;
width:100%;
text-align:center;
margin-top:-5px;
}
.content__note {
flex-grow:1;
text-align:right;
}
</style>