-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathPushChangesJavaScript.html
99 lines (87 loc) · 4.26 KB
/
PushChangesJavaScript.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
<script>
(function() {
var push = document.querySelector('#push');
var info = document.querySelector('#info');
var selectWorkspaces = document.querySelector('#workspaces');
var wsname = '';
var wsid = '';
var aid = '';
var cid = '';
var count = 0;
var length = 0;
var notes = [];
var arraySortByName = function(array) {
return array.sort(function(a, b) {
var aName = a.name.toUpperCase();
var bName = b.name.toUpperCase();
return (aName < bName) ? -1 : (aName > bName) ? 1 : 0;
});
};
var versionCreated = function(version) {
info.innerHTML = 'Version ' + version.containerVersionId + ' successfully created. <a href="#" id="build">Build documentation</a> to see the changes.';
document.querySelector('#build').addEventListener('click', function(e) {
e.preventDefault();
google.script.run.withSuccessHandler(function() { google.script.host.close(); }).withFailureHandler(onError).startProcess(version.accountId, version.containerId);
});
};
var updateSingleNote = function() {
if (notes.length) {
count++;
var obj = notes.shift();
aid = obj.json.accountId;
cid = obj.json.containerId;
info.innerHTML = 'Updating notes for ' + obj.json.name + ' (' + count + '/' + length + ').';
google.script.run.withSuccessHandler(updateSingleNote).withFailureHandler(onError).updateSingleNote(obj, wsid);
} else {
info.innerHTML = '<strong>Done updating ' + length + ' notes!</strong> You can now close this modal.<br/><br/>Don\'t forget that you still need to <strong>publish</strong> or <strong>create a version from</strong> the workspace "' + wsname + '" in order to see the notes when you build the documentation again.<br/><br/>You can also click <a href="#" id="createVersion">here</a> to create a version of the workspace at your own risk.';
document.querySelector('#createVersion').addEventListener('click', function(e) {
e.preventDefault();
info.innerHTML = 'Creating version...';
google.script.run.withSuccessHandler(versionCreated).withFailureHandler(onError).createVersion(aid, cid, wsid);
});
}
};
var updateNotes = function(notesToUpdate) {
wsid = selectWorkspaces.options[selectWorkspaces.selectedIndex].value;
wsname = selectWorkspaces.options[selectWorkspaces.selectedIndex].innerHTML;
length = notesToUpdate.length;
count = 0;
notes = notesToUpdate;
if (length) {
updateSingleNote();
} else {
onError({message: 'There were no changed notes fields to update in the sheets for the selected container.'});
}
};
var onError = function(error) {
push.disabled = true;
info.innerHTML = '<span class="error">' + error.message + '</span>';
};
var invalidSheet = function() {
info.innerHTML = 'For <strong>Push changes</strong> to work, you need to first generate the documentation using the <strong>Build documentation</strong> menu option.<br/><br/>If you have already generated the documentation, make sure you have one of these documentation sheets active in your Google Sheets before selecting the <strong>Push changes</strong> menu option.';
};
var addWorkspaces = function(workspaces) {
info.style.display = 'block';
push.disabled = false;
workspaces = arraySortByName(workspaces);
workspaces.forEach(function(workspace) {
var option = document.createElement('option');
option.value = workspace.workspaceId;
option.innerHTML = workspace.name;
selectWorkspaces.appendChild(option);
});
selectWorkspaces.disabled = false;
};
document.querySelector('#close').addEventListener('click', function() {
google.script.host.close();
});
push.addEventListener('click', function() {
push.disabled = true;
push.innerText = 'Working...';
push.className = 'share';
selectWorkspaces.disabled = true;
google.script.run.withSuccessHandler(updateNotes).withFailureHandler(onError).processNotes('push');
});
google.script.run.withSuccessHandler(addWorkspaces).withFailureHandler(onError).getWorkspaces();
})();
</script>