forked from plamzi/gtm-tools-sheets-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushChangesJavaScript.html
135 lines (106 loc) · 5.06 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
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
<script>
(function() {
var push = document.querySelector('#push');
var info = document.querySelector('#info');
var close = document.querySelector('#close');
var selectWorkspaces = document.querySelector('#workspaces');
var wsname = '';
var wsid = '';
var aid = '';
var cid = '';
var count = 0;
var length = 0;
var items = [];
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 + ' was successfully created. <a href="#" id="rebuild">Quick Rebuild</a> to see the latest release.';
document.querySelector('#rebuild').addEventListener('click', function(e) {
e.preventDefault();
push.disabled = false;
push.innerHTML = 'Rebuilding...';
close.innerHTML = 'Cancel';
google.script.run.withSuccessHandler(onDone).withFailureHandler(onError).quickRebuild();
});
};
var updateSingle = function() {
if (items.length) {
count++;
var item = items.shift();
var which = items.notes ? 'notes' : 'folder';
aid = item.accountId;
cid = item.containerId;
info.innerHTML = 'Updating ' + which + ' for ' + item.type.slice(0, -1) + ' <b>' + (item.name || item.id) + '</b> (' + count + '/' + length + ').<br><br><i>* Batch update throttled to fit under the 25 hits / 100 sec. GTM API quota.</i>';
google.script.run.withSuccessHandler(updateSingle).withFailureHandler(onError).updateSingle(item, wsid, items);
}
else {
info.innerHTML = '<strong>Done updating ' + count + ' items!</strong> You can now close this modal.\
<br/><br/>Don\'t forget that you still need to <strong>publish</strong> / <strong>create a version from</strong> the workspace "' + wsname + '" in order to see the changes when you build the documentation again.<br/>\
<br/>You can also click <a href="#" id="createVersion">here</a> to create a version and publish the workspace at your own risk.';
close.innerHTML = 'Close';
push.innerHTML = 'Finished';
push.disabled = false;
push.addEventListener('click', onDone);
document.querySelector('#createVersion').addEventListener('click', function(e) {
e.preventDefault();
push.innerHTML = 'Publishing...';
push.disabled = true;
close.innerHTML = 'Cancel';
info.innerHTML = 'Creating new published version of this workspace...';
google.script.run.withSuccessHandler(versionCreated).withFailureHandler(onError).createVersion(aid, cid, wsid);
});
}
};
var updateItems = function(toUpdate) {
wsid = selectWorkspaces.options[selectWorkspaces.selectedIndex].value;
wsname = selectWorkspaces.options[selectWorkspaces.selectedIndex].innerHTML;
length = toUpdate.length;
count = 0;
items = toUpdate;
if (length) {
updateSingle();
}
else {
onError({message: 'There were no changed folder or notes fields to update in the sheets for the selected container.'});
}
};
var onDone = function() {
google.script.host.close();
}
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(updateItems).withFailureHandler(onError).processChanges('push');
});
google.script.run.withSuccessHandler(addWorkspaces).withFailureHandler(onError).getWorkspaces();
})();
</script>