diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 18:44:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-05-19 18:44:42 +0300 |
commit | 4555e1b21c365ed8303ffb7a3325d773c9b8bf31 (patch) | |
tree | 5423a1c7516cffe36384133ade12572cf709398d /vendor/assets/javascripts/vue-virtual-scroller/src/mixins/IdState.js | |
parent | e570267f2f6b326480d284e0164a6464ba4081bc (diff) |
Add latest changes from gitlab-org/gitlab@13-12-stable-eev13.12.0-rc42
Diffstat (limited to 'vendor/assets/javascripts/vue-virtual-scroller/src/mixins/IdState.js')
-rw-r--r-- | vendor/assets/javascripts/vue-virtual-scroller/src/mixins/IdState.js | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/vendor/assets/javascripts/vue-virtual-scroller/src/mixins/IdState.js b/vendor/assets/javascripts/vue-virtual-scroller/src/mixins/IdState.js new file mode 100644 index 00000000000..9b5bc57ab92 --- /dev/null +++ b/vendor/assets/javascripts/vue-virtual-scroller/src/mixins/IdState.js @@ -0,0 +1,79 @@ +import Vue from 'vue' + +export default function ({ + idProp = vm => vm.item.id, +} = {}) { + const store = {} + const vm = new Vue({ + data () { + return { + store, + } + }, + }) + + // @vue/component + return { + data () { + return { + idState: null, + } + }, + + created () { + this.$_id = null + if (typeof idProp === 'function') { + this.$_getId = () => idProp.call(this, this) + } else { + this.$_getId = () => this[idProp] + } + this.$watch(this.$_getId, { + handler (value) { + this.$nextTick(() => { + this.$_id = value + }) + }, + immediate: true, + }) + this.$_updateIdState() + }, + + beforeUpdate () { + this.$_updateIdState() + }, + + methods: { + /** + * Initialize an idState + * @param {number|string} id Unique id for the data + */ + $_idStateInit (id) { + const factory = this.$options.idState + if (typeof factory === 'function') { + const data = factory.call(this, this) + vm.$set(store, id, data) + this.$_id = id + return data + } else { + throw new Error('[mixin IdState] Missing `idState` function on component definition.') + } + }, + + /** + * Ensure idState is created and up-to-date + */ + $_updateIdState () { + const id = this.$_getId() + if (id == null) { + console.warn(`No id found for IdState with idProp: '${idProp}'.`) + } + if (id !== this.$_id) { + if (!store[id]) { + this.$_idStateInit(id) + } + this.idState = store[id] + } + }, + }, + } +} |