forked from contentful/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (77 loc) · 2.26 KB
/
app.js
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
// import { init } from '../../lib/api'
const cfExt = window.contentfulExtension || window.contentfulWidget
cfExt.init(api => {
const slugField = api.field
const titleField = api.entry.fields.title
const _ = window._
const getSlug = window.getSlug
const debouncedUpdateStatus = _.debounce(updateStatus, 500)
const input = document.getElementById('slug')
const statusElements = {
error: document.getElementById('error'),
ok: document.getElementById('ok'),
loading: document.getElementById('loading')
}
api.window.updateHeight()
titleField.onValueChanged(handleSlugChange)
input.addEventListener('input', () => handleSlugChange(input.value))
input.addEventListener('change', () => handleSlugChange(input.value))
updateStatus(slugField.getValue())
/**
* Handle change of slug value caused by either changing slug field
* or changing the title of the entry
*/
function handleSlugChange (value) {
setSlug(getSlug(value || ''))
}
/**
* Set the input value to 'slug' and update the status by checking for
* duplicates.
*/
function setSlug (slug) {
input.value = slug
slugField.setValue(slug)
setStatus('loading')
debouncedUpdateStatus(slug)
}
/**
* Show inline status icon based on current status
*/
function updateStatus (slug) {
getDuplicates(slug).then(function (hasDuplicates) {
if (hasDuplicates) {
setStatus('error')
} else {
setStatus('ok')
}
})
}
/**
* Show icon for given status
*/
function setStatus (status) {
_.each(statusElements, function (el, name) {
if (name === status) {
el.style.display = 'inline'
} else {
el.style.display = 'none'
}
})
}
/**
* Check if slug is already in use.
* Resolves to 'true' if there are entries of the given content type that have
* the same 'slug' value.
*/
function getDuplicates (slug) {
if (!slug) {
return Promise.resolve(false)
}
let query = {}
query['content_type'] = api.entry.getSys().contentType.sys.id
query['fields.' + slugField.id] = slug
query['sys.id[ne]'] = api.entry.getSys().id
query['sys.publishedAt[exists]'] = true
return api.space.getEntries(query).then(result => result.total > 0)
}
})