source: main/trunk/greenstone3/web/interfaces/default/js/documentedit_scripts.js@ 38766

Last change on this file since 38766 was 38351, checked in by anupama, 7 months ago

Only deleting user comments through the editor works, I can't get editing to work yet. So I've put a copy of the code back to supporting just the deletions and continue to work on editing.

File size: 14.1 KB
Line 
1/** Javascript file for editing a single document's content - metadata and text */
2/** uses other functions in documentedit_scripts_util.js */
3
4/* some vars for document editing */
5/* if true, will look through all the metadata for the document, and add each namespace into the list of metadata sets. If set to false, will only add in the ones defined in setStaticMetadataSets function (defined below) - override this function to make a custom list of sets */
6var dynamic_metadata_set_list = true;
7/* if false, will hide the metadata list selector. So the user will only get to see the default metadata set. */
8var display_metadata_set_selector = true;
9/* if true, will make the editing controls stay visible even on page scrolling */
10var keep_editing_controls_visible = true;
11/* Here you can choose which save buttons you like. Choose from 'save', 'rebuild', 'saveandrebuild' */
12var save_and_rebuild_buttons = ["saveandrebuild"];
13//var save_and_rebuild_buttons = ["save", "rebuild", "saveandrebuild"];
14
15/* What kind of metadata element selection do we provide?
16plain: just a text input box
17fixedlist: a drop down menu with a fixed list of options (provided by the availableMetadataElements list)
18autocomplete: a text input box with a list of suggestions to choose from (provided by the availableMetadataElements list). Allows additional input other than the fixed list
19 */
20var new_metadata_field_input_type = "plain";
21/* add all metadata button? only valid with fixedlist or autocomplete metadata element selection */
22var enable_add_all_metadata_button = true;
23
24/* Metadata elements to be used in the fixedlist/autocomplete options above */
25var availableMetadataElements = ["dc.Title", "dc.Subject"];
26/* metadata elements that have a list of values/suggestions */
27var autocompleteMetadata = new Array();
28/* for each metadata element specified here, one should provide an array of values. The name is the meta_name + "_values", but you must strip . and _ from the name.
29for example
30var autocompleteMetadata = ["dc.Subject"];
31var dcSubject_values = ["Kings", "Queens", "others"];
32 */
33
34/* The metadata specified in multiValuedMetadata array will be treated as a delimited list, using mvm_delimiter. On saving, the values will be separated and saved individually */
35
36var multiValuedMetadata = new Array(); // eg ["xx.Person", "xx.Location"];
37var mvm_delimiter = ";";
38
39/************************
40 * METADATA EDIT SCRIPTS *
41 ************************/
42
43function addEditMetadataLink(cell) {
44 cell = $(cell);
45 var id = cell.attr("id").substring(6);
46 var metaTable = gs.jqGet("meta" + id);
47 var row = cell.parent();
48 var newCell = $("<td>", {
49 "style": "font-size:0.7em; padding:0px 10px",
50 "class": "editMetadataButton"
51 });
52 var linkSpan = $("<span>", {
53 "class": "ui-state-default ui-corner-all",
54 "style": "padding: 2px; float:left;"
55 });
56
57 var linkLabel = $("<span>" + gs.text.de.edit_metadata + "</span>");
58 var linkIcon = $("<span>", {
59 "class": "ui-icon ui-icon-folder-collapsed"
60 });
61 newCell.linkIcon = linkIcon;
62 newCell.linkLabel = linkLabel;
63
64 var uList = $("<ul>", {
65 "style": "outline: 0 none; margin:0px; padding:0px;"
66 });
67 var labelItem = $("<li>", {
68 "style": "float:left; list-style:none outside none;"
69 });
70 var iconItem = $("<li>", {
71 "style": "float:left; list-style:none outside none;"
72 });
73
74 uList.append(iconItem);
75 uList.append(labelItem);
76 labelItem.append(linkLabel);
77 iconItem.append(linkIcon);
78
79 var newLink = $("<a>", {
80 "href": "javascript:;"
81 });
82 newLink.on("click", function () {
83 if (metaTable.css("display") == "none") {
84 linkLabel.html(gs.text.de.hide_metadata);
85 linkIcon.attr("class", "ui-icon ui-icon-folder-open");
86 metaTable.css("display", "block");
87 metaTable.metaNameField.css("display", "inline");
88 metaTable.addRowButton.css("display", "inline");
89 if (enable_add_all_metadata_button == true) {
90 metaTable.addAllButton.css("display", "inline");
91 }
92 } else {
93 linkLabel.html(gs.text.de.edit_metadata);
94 linkIcon.attr("class", "ui-icon ui-icon-folder-collapsed");
95 metaTable.css("display", "none");
96 metaTable.metaNameField.css("display", "none");
97 metaTable.addRowButton.css("display", "none");
98 if (enable_add_all_metadata_button == true) {
99 metaTable.addAllButton.css("display", "none");
100 }
101 }
102 });
103
104 newLink.append(uList);
105 linkSpan.append(newLink);
106 newCell.append(linkSpan);
107 row.append(newCell);
108
109 addFunctionalityToTable(metaTable);
110 metaTable.metaNameField.css("display", "none");
111 metaTable.addRowButton.css("display", "none");
112 if (enable_add_all_metadata_button == true) {
113 metaTable.addAllButton.css("display", "none");
114 }
115}
116
117
118
119function setEditingFeaturesVisible(visible) {
120 if (visible) {
121 $("#editContentButton").html(gs.text.de.hide_editor);
122 $("#editContentButtonDiv").attr("class", "ui-state-default ui-corner-all");
123 } else {
124 $("#editContentButton").html(gs.text.de.edit_content);
125 $("#editContentButtonDiv").attr("class", "");
126 }
127
128 var visibility = (visible ? "" : "none");
129 if (display_metadata_set_selector == true) {
130 $("#metadataListLabel, #metadataSetList").css("display", visibility);
131 }
132
133 $(".editMetadataButton").each(function () {
134 $(this).css("display", visibility);
135 $(this.linkLabel).html(gs.text.de.edit_metadata);
136 $(this.linkIcon).attr("class", "ui-icon ui-icon-folder-collapsed");
137 });
138 /*
139 $(".editMapGPS").each(function(){
140 $(this).css("display", visibility);
141 $(this.linkLabel).html(gs.text.de.edit_map_gps);
142 $(this.linkIcon).attr("class", "ui-icon ui-icon-folder-collapsed");
143 });
144 */
145
146 $("table").each(function () {
147 if ($(this).attr("id") && $(this).attr("id").search(/^meta/) != -1) {
148 $(this).css("display", "none");
149 $(this.metaNameField).css("display", "none");
150 $(this.addRowButton).css("display", "none");
151 if (enable_add_all_metadata_button == true) {
152 $(this.addAllButton).css("display", "none");
153 }
154 }
155 });
156}
157
158/* override this function in other interface/site/collection if you want
159a different set of metadata sets
160Use in conjunction with the dynamic_metadata_set_list variable. */
161function setStaticMetadataSets(list) {
162 addOptionToList(list, "All", gs.text.de.all_metadata);
163}
164
165function readyPageForEditing() {
166
167 if ($("#metadataSetList").length) {
168 var setList = $("#metadataSetList");
169 if (!setList.css("display") || setList.css("display") == "") {
170 setEditingFeaturesVisible(false);
171 } else {
172 setEditingFeaturesVisible(true);
173 }
174 return;
175 }
176
177 $("#editContentButton").html(gs.text.de.hide_editor);
178 //wait for 0.5 sec to let ckeditor up
179
180 // Initialising editableInitStates for CKEDITOR instances now happens in the CKEDITOR.on('instanceReady') handler, which is added upon docReady, see documentedit_scripts_util::$( document ).ready(...)
181 // Attempting CKEDITOR.on('instanceReady') at the start of this method or anywhere in this method didn't work because it was probably too late in page load phase to add the event handler then
182 // (the instanceReady() event would have been triggered before this method finally got called).
183
184 var editBar = $("#editBarLeft");
185
186 var visibleMetadataList = $("<select>", {
187 "id": "metadataSetList",
188 "class": "ui-state-default"
189 });
190 setStaticMetadataSets(visibleMetadataList);
191
192 if (display_metadata_set_selector == true) {
193 var metadataListLabel = $("<span>", {
194 "id": "metadataListLabel",
195 "style": "margin-left:20px;"
196 });
197 metadataListLabel.html(gs.text.de.visible_metadata);
198 editBar.append(metadataListLabel);
199 } else {
200 visibleMetadataList.css("display", "none");
201 }
202 editBar.append(visibleMetadataList);
203 visibleMetadataList.change(onVisibleMetadataSetChange);
204 editBar.append("<br>");
205
206 for (var i = 0; i < save_and_rebuild_buttons.length; i++) {
207 var button_type = save_and_rebuild_buttons[i];
208 if (button_type == "save") {
209 var saveButton = $("<button>", {
210 "id": "saveButton",
211 "class": "ui-state-default ui-corner-all"
212 });
213 saveButton.on("click", save);
214 saveButton.html(gs.text.de.save);
215 editBar.append(saveButton);
216 } else if (button_type == "rebuild") {
217 var rebuildButton = $("<button>", {
218 "id": "rebuildButton",
219 "class": "ui-state-default ui-corner-all"
220 });
221 rebuildButton.on("click", rebuildCurrentCollection);
222 rebuildButton.html(gs.text.de.rebuild);
223 editBar.append(rebuildButton);
224 } else if (button_type == "saveandrebuild") {
225 var saveAndRebuildButton = $("<button>", {
226 "id": "saveAndRebuildButton",
227 "class": "ui-state-default ui-corner-all"
228 });
229 saveAndRebuildButton.on("click", saveAndRebuild);
230 saveAndRebuildButton.html(gs.text.de.saverebuild);
231 editBar.append(saveAndRebuildButton);
232
233 }
234 }
235 var statusBarDiv = $("<div>");
236 editBar.append(statusBarDiv);
237 _statusBar = new StatusBar(statusBarDiv[0]);
238
239 var titleDivs = $(".sectionTitle");
240 for (var i = 0; i < titleDivs.length; i++) {
241 addEditMetadataLink(titleDivs[i]);
242
243 if(i == 0) { // root doc id, has no period-mark in it
244 // user comments are attached to document root
245 addEditUserCommentsLink(titleDivs[i]);
246 }
247
248 if (gs.variables.isMapGPSEditingAllowed == "1") {
249 addEditMapGPSLink(titleDivs[i]);
250 }
251 }
252
253 // We need to keep track of editableElementsInitialisationProgress: the number of editable elements that need to be initialised/need to finish initialising
254 // As CKEditors will be added, meaning more editable elements, must increment our counter editableElementsInitialisationProgress
255 //var $num_editable_textareas = $(".sectionText"); // consider searching for 'contenteditable="true"' as this is what CKEDITOR is looking for (we think!)
256 // I think for us it's always a <div> that has contenteditable="true", but to get all elements with attr contenteditable set to true,
257 // see https://stackoverflow.com/questions/4958081/find-all-elements-with-a-certain-attribute-value-in-jquery
258 // which has inefficient and slightly more efficient ways of doing that
259 var $num_editable_textareas = $('div[contenteditable="true"]');
260 editableElementsInitialisationProgress += $num_editable_textareas.length;
261
262 _baseURL = gs.xsltParams.library_name;
263 onVisibleMetadataSetChange(); // make sure that the selected item in the list is active
264
265 // If the user is attempting to leave the page, check if there are unsaved changes
266 // and if so, display an "Are you sure you want to leave" message.
267 // https://stackoverflow.com/questions/7080269/javascript-before-leaving-the-page
268 // Newer versions of Firefox/Chrome don't display custom message (security feature):
269 // https://stackoverflow.com/questions/22776544/why-is-jquery-onbeforeunload-not-working-in-chrome-and-firefox
270 // and http://jsfiddle.net/XZAWS/
271 // jquery bind() is deprecated: https://stackoverflow.com/questions/33654716/is-jquery-bind-deprecated
272
273 $(window).on("beforeunload", function(event) {
274
275 if(gs.cgiParams.docEdit == "1") { // like document.xsl, which checks the same var upon onload
276 // shouldn't check for whether changes are saved unless on Doc Editing page (DocEdit=1)
277 // else the following pop up always ends up appearing when attempting
278 // to leave a doc view page in Doc Editing Mode (when not yet actually Doc Editing)
279
280 // Because we've done extra work now in maintaining "editableElementsInitialisationProgress", which is
281 // the number of editable elements that still need to finish initialising, we can now be confident that
282 // the call to changesToUpdate() below won't return the wrong answers if a page with docEdit turned on
283 // is asked to unload (e.g. by pressing Reload) before the page has finished loading.
284 var changes = changesToUpdate();
285
286 //console.log("#### CHANGES before page reload: ", changes);
287
288 if(changes.length > 0) {
289 console.log("The collection hasn't yet been saved after editing. Are you sure you want to leave?");
290 return "The collection hasn't yet been saved after editing. Are you sure you want to leave?";
291 }
292 }
293 });
294
295
296}
297
298// override the one in documentmaker_scripts_util
299// currently not used if other one is present. need to get the js include order right
300function enableSaveButtons(enabled) {
301 if (enabled) {
302 $("#saveButton, #rebuildButton, #saveAndRebuildButton").prop("disabled", false);
303 } else {
304 $("#saveButton, #rebuildButton, #saveAndRebuildButton").prop("disabled", true);
305 }
306}
307
308/* this is a cut down version of save() from documentmaker_scripts_util.js
309going back to using save, will delete this once everything working*/
310function saveMetadataChangesOld() {
311
312 console.log("Saving metadata changes");
313
314 // get collection name
315 var collection = gs.cgiParams.c;
316
317 // get document id
318 var docID = gs.cgiParams.d;
319
320 var metadataChanges = new Array();
321 if (_deletedMetadata.length > 0) {
322
323 for (var i = 0; i < _deletedMetadata.length; i++) {
324
325 var currentRow = _deletedMetadata[i];
326
327 //Get metadata name
328 var cells = currentRow.getElementsByTagName("TD");
329 var nameCell = cells[0];
330 var name = nameCell.innerHTML;
331 var valueCell = cells[1];
332 var value = valueCell.innerHTML;
333 metadataChanges.push({
334 type: 'delete',
335 docID: docID,
336 name: name,
337 value: value
338 });
339 removeFromParent(currentRow);
340 }
341 }
342
343 if (metadataChanges.length == 0) {
344 console.log(gs.text.de.no_changes);
345 return;
346 }
347
348 var processChangesLoop = function (index) {
349 var change = metadataChanges[index];
350
351 var callbackFunction;
352 if (index + 1 == metadataChanges.length) {
353 callbackFunction = function () {
354 console.log("Completed saving metadata changes. You must rebuild the collection for the changes to take effect.");
355 };
356 } else {
357 callbackFunction = function () {
358 processChangesLoop(index + 1)
359 };
360 }
361 if (change.type == "delete") {
362 gs.functions.removeArchivesMetadata(collection, gs.xsltParams.site_name, change.docID, change.name, null, change.value, function () {
363 callbackFunction();
364 });
365 } else {
366 if (change.orig) {
367 gs.functions.setArchivesMetadata(collection, gs.xsltParams.site_name, docID, change.name, null, change.value, change.orig, "override", function () {
368 callbackFunction();
369 });
370 } else {
371 gs.functions.setArchivesMetadata(collection, gs.xsltParams.site_name, docID, change.name, null, change.value, null, "accumulate", function () {
372 callbackFunction();
373 });
374 }
375 }
376 }
377 processChangesLoop(0);
378 /* need to clear the changes from the page */
379 while (_deletedMetadata.length > 0) {
380 _deletedMetadata.pop();
381 }
382
383}
Note: See TracBrowser for help on using the repository browser.