-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
225 lines (191 loc) · 10.6 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html>
<head>
<!-- This title is not used. -->
<title>Rename & Redirect</title>
<!-- Use Bootstrap to match the look of OU Campus. -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous" />
<link rel="stylesheet" href="css/app.css" />
<!-- jQuery IS REQUIRED for gadgetlib.js to work. -->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<!-- gadgetlib.js is a library of basic functions that gadgets can use. -->
<script type="text/javascript" src="js/gadgetlib.min.js"></script>
<!-- moment.js for simple date/time manipulation -->
<script type="text/javascript" src="js/moment.min.js"></script>
</head>
<body>
<div id="main">
<form id="rename-redirect-gadget">
<fieldset>
<label for="new-name">New Page Name</label>
<input id="new-name" class="form-control" type="text" />
</fieldset>
<button type="button" id="rename-btn" class="btn btn-success" style="width:100%">Rename</button>
</form>
</div>
<!--
The following hidden div is only needed if you'll be editing your gadget in
OU Campus's source code editor. OU Campus automatically adds a DirectEdit link
to HTML files that you edit in OU Campus. This div hides the DirectEdit link.
-->
<div style="display:none">
<!-- ouc:ob --><!-- /ouc:ob -->
</div>
<script type="text/javascript">
$(document).ready(function () {
gadget.ready().then(function () {
const apiHost = gadget.get("apihost");
const authToken = gadget.get("token");
const site = gadget.get("site");
var isRenameFromGadget = false;
var newName;
var currentName;
var currentPath;
var currentDirectory;
var currentUrl;
resetGadget();
$("#rename-btn").on("click", function () {
isRenameFromGadget = true;
newName = $("#new-name").val();
// Step 1: validate filename
// get site settings for filename regex
var data = {
"authorization_token" : authToken,
"site" : site
};
$.get(apiHost + "/sites/view", data)
.done( function(siteInfo) {
//console.log("Site info: " + siteInfo);
var regex = new RegExp(siteInfo.file_regex);
if (!regex.test(newName)) {
// TODO: improve presentation of error message
alert(siteInfo.file_regex_desc);
return;
}
// Step 2: rename file
var data = {
"authorization_token" : authToken,
"site" : site,
"path" : currentPath,
"filename" : newName + ".pcf",
"remote" : false
}
$.post(apiHost + "/files/rename", data)
.done( function(results) {
//console.log("Renamed page successfully!", results);
// NOTE: .done() does not actually fire when the rename has finished, only that the operation has kicked off.
// Instead, we have to listen for the event $(gadget).on("rename") below for the file rename to be completed.
})
.fail( function(error) {
console.log("Error renaming file: ", error);
return;
});
})
.fail( function(error) {
console.log("Error getting site info", error);
return;
});
}); // $("#rename-btn").on("click")
$(gadget).on("rename", function (evt, data) {
if (!isRenameFromGadget)
return;
//console.log("File renamed", data);
// Step 3: create new file with old name
var data = {
"authorization_token" : authToken,
"site" : site,
"tcf_value_0": currentName, // TODO: this is supposed to be the page name, not filename
"tcf_value_1" : "",
"tcf_value_2" : false,
"tcf_value_3" : currentName,
"path" : currentDirectory,
"template" : "interior.tcf",
"submit" : "Create"
}
$.post(apiHost + "/templates/new", data)
.done( function(results) {
//console.log("New page created", results);
var redirectUrl = currentUrl.replace(currentName, newName);
var redirectText = "<script>window.location = '" + redirectUrl + "';\x3C/script>";
// Step 4: add redirect on old page
var data = {
"authorization_token" : authToken,
"site" : site,
"path" : currentPath,
"text" : redirectText,
"label" : "maincontent",
"wysiwyg" : true
}
$.post(apiHost + "/files/save", data)
.done( function(results) {
//console.log("Redirect saved", results);
// Step 5: first publish the new page (we can't do it after the expiration is set)
var data = {
"authorization_token" : authToken,
"site" : site,
"path" : currentPath,
"log" : "",
"target" : site,
"tweet" : "",
"wall_post" : ""
}
$.post(apiHost + "/files/publish", data)
.done( function(results) {
//console.log("Page published", results);
// Step 6: set old page to expire in 3 months
var expiration = moment().add(3, "months").format("YYYY-MM-DD[T]00:00:00.000[Z]");
var data = {
"authorization_token" : authToken,
"site" : site,
"date" : expiration,
"recipient" : "",
"subject" : "",
"content" : "",
"send_email" : false,
"sched_exp" : "",
"exp_type" : "delete",
"path" : currentPath
}
$.post(apiHost + "/scheduledexpirations/new", data)
.done( function(results) {
//console.log("Page expiration saved", results);
gadget.oucSetCurrentLocation("/previewedit" + currentDirectory + "/" + newName + ".pcf");
})
.fail( function(error) {
console.log("Error saving page expiration: ", error);
});
})
.fail( function(error) {
console.log("Error publishing page: ", error);
});
})
.fail( function(error) {
console.log("Error saving redirect: ", error);
});
})
.fail( function(error) {
console.log("Error creating new page: ", error);
});
}); // $(gadget).on("rename")
$(gadget).on("view_changed", function (evt) {
//console.log("view changed");
resetGadget();
});
function resetGadget() {
gadget.oucGetCurrentFileInfo().then( function(fileInfo) {
if (fileInfo == null)
return;
//console.log("Current file info: ", fileInfo);
currentName = fileInfo.filename.replace(".pcf", "");
currentPath = fileInfo.stagingPath;
currentDirectory = fileInfo.stagingPath.substring(0, fileInfo.stagingPath.lastIndexOf("/"));
currentUrl = fileInfo.productionUrl;
$("#new-name").val(currentName);
isRenameFromGadget = false;
});
}
}); // gadget.ready()
}); // document.ready()
</script>
</body>
</html>