-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
135 lines (126 loc) · 5.07 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Locale Publisher</title>
<link rel="stylesheet" href="https://unpkg.com/contentful-ui-extensions-sdk@3/dist/cf-extension.css" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" />
<style>
body {
font-family: "Avenir Next W01", Helvetica, sans-serif;
font-size: 14px;
margin: 0;
color: #8091a5 !important;
overflow: hidden;
background-color: #f7f9fa;
}
p {
margin-top: 0;
margin-bottom: 1em;
}
.button {
display: block;
position: relative;
border-right: none;
overflow: hidden;
width: 100%;
height: 40px;
white-space: nowrap;
outline: 0;
padding: 0.625rem 1.5rem;
cursor: pointer;
text-align: center;
font-weight: 500;
border: 1px solid transparent;
border-color: #0eb87f;
border-radius: 2px;
line-height: 18px;
color: #fff;
background: linear-gradient(to top, #0eb87f, #14d997);
background-size: 100% 200%;
}
label {
font-weight: normal;
}
</style>
</head>
<body>
<div id="content"></div>
<script id="template" type="x-tmpl-mustache">
<p>Please select the locales you wish to publish:</p>
{{#locales}}
<div>
<input type="checkbox" id="{{.}}" name="{{.}}" value="{{.}}">
<label for="{{.}}">{{.}}</label>
</div>
{{/locales}}
<div>
<button id='locale-publisher-button' class="button cf-block">Publish</button>
</div>
</script>
<script src="https://unpkg.com/contentful-ui-extensions-sdk@3"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.3.0/mustache.min.js"></script>
<script>
window.contentfulExtension.init(function(extension) {
window.extension = extension
console.log('starting locale publisher');
var rendered = Mustache.render(document.getElementById('template').innerHTML, { locales: extension.locales.available });
document.getElementById('content').innerHTML = rendered;
document.getElementById('locale-publisher-button').addEventListener('click', publishSelectedLocales)
document.getElementById('locale-publisher-button').extension = extension
extension.window.startAutoResizer();
});
function publishSelectedLocales(event) {
let extension = event.target.extension
let localized_fields = extension.contentType.fields.filter(v => v.localized === true).map(v => v.id)
console.log("localized_fields", localized_fields)
let available_locales = extension.locales.available
let selected_locales = $(':checked').toArray().map(v => v.value)
let unselected_locales = available_locales.filter((e) => !selected_locales.includes(e))
console.log("available_locales", available_locales)
console.log("selected_locales", selected_locales)
console.log("unselected_locales", unselected_locales)
var backup_content = {}
localized_fields.forEach(field => {
backup_content[field] = {}
unselected_locales.forEach(locale => {
if (extension.entry.fields[field].getValue(locale)) {
console.log(`Backing up "${locale}" locale of "${field}" field to "${extension.entry.fields[field].getValue(locale)}" and clearing stored value`)
backup_content[field][locale] = extension.entry.fields[field].getValue(locale)
extension.entry.fields[field].setValue('', locale)
console.log("field value", `"${extension.entry.fields[field].getValue(locale)}"`)
}
})
})
console.log("backup_content", backup_content)
extension.space.getEntry(extension.entry.getSys().id).then(entry1 => {
console.log("pre publish entry", entry1.fields.title)
extension.space.publishEntry(entry1).then(entry2 => {
console.log("post publish entry", entry2.fields.title)
localized_fields.forEach(field => {
unselected_locales.forEach(locale => {
if (backup_content[field][locale]) {
console.log(`Setting "${locale}" locale of "${field}" field to "${backup_content[field][locale]}"`)
extension.entry.fields[field].setValue(backup_content[field][locale], locale)
console.log("field value", `"${extension.entry.fields[field].getValue(locale)}"`)
}
})
})
extension.space.getEntry(extension.entry.getSys().id).then(entry3 => {
console.log("post restore entry", entry3.fields.title)
})
$('#locale-publisher-button').text('Done')
$(':checked').each((i, el) => {
console.log(`unchecking ${el.value} locale box`)
el.checked = false
})
window.setTimeout(() => {
$('#locale-publisher-button').text('Publish')
}, 3000)
})
})
}
</script>
</body>
</html>