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

Last change on this file since 32850 was 32850, checked in by ak19, 5 years ago
  1. Zeddy introduces suppressUndo and cleaned up lots of CloneShape sectionst that were commented out. 2. Zeddy and Dr Bainbridge solved the issue whereby shapes loaded from archives_metadata were not selectable and needed a deleteAllShapes followed by Undo to make them selectable. These steps are no longer needed now, as it's solved more efficiently.
File size: 15.2 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.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
117var gsmap_store = {};
118var gps_metadata_name = "GPS.mapOverlay";
119
120// Called by documentedit_scripts_util.js when saving and rebuilding.
121// This function should return all the doc sections' map overlay data so that
122// setArchivesMetadata can be called for each and the entire collection rebuilt with the changes
123function getDocMapsEditDataForSaving(collName) {
124 var map_editors_array = Object.values(gsmap_store);
125 var modifiedMaps = []; // the array that is the return value: an array of only all the modified maps
126
127
128 for(var i = 0; i < map_editors_array.length; i++) {
129 var map_editor = map_editors_array[i];
130 var oldMapData = map_editor.savedOverlays; // stringified JSON shape
131 var newMapData = JSON.stringify(ShapesUtil.overlayToJSON(map_editor.overlays)); // stringified JSON shape too
132
133 // We only consider a map editor's map data to have been modified in the following cases:
134 // - if oldMapData is null, new mapData should not be empty array
135 // - OR oldMapData had some value and it's not the same as newMapData
136 if(!oldMapData && newMapData !== "[]" || oldMapData && oldMapData !== newMapData) {
137 var nodeID = map_editors_array[i].id;
138 //console.log("old vs new mapdata for nodeID " + nodeID);
139 //console.log("OLD: " + oldMapData);
140 //console.log("NEW: " + newMapData);
141
142 modifiedMaps.push({
143 collection: collName,
144 docID: nodeID,
145 name:gps_metadata_name,
146 metapos: 0,
147 value:newMapData
148 });
149
150 // Save the new overlay values as the old ones for the state after saving and rebuilding is done.
151 // Ideally this should be do after saveAndRebuild has completed successfully, since if saveAndRebuild goes wrong
152 // we'd not have saved newMapData AND because savedOverlays will contain the newMapData, we won't detect these unsaved
153 // values in future attempts to save.
154 // But for now, we're doing this here *because* this is the procedure with regular metadata (works out changes,
155 // then saves those changes as initStates BEFORE saveAndRebuild is called)
156 map_editor.savedOverlays = newMapData;
157 }
158
159 }
160
161 return modifiedMaps;
162}
163
164
165function addEditMapGPSLink(cell) {
166 cell = $(cell);
167 var id = cell.attr("id").substring(6);
168 //console.log(id);
169 var mapGPScontainer = gs.jqGet("map-and-controls-" + id);
170 var row = cell.parent();
171 var newCell = $("<td>", {
172 "style": "font-size:0.7em; padding:0px 10px",
173 "class": "editMapGPSButton"
174 });
175 var linkSpan = $("<span>", {
176 "class": "ui-state-default ui-corner-all",
177 "style": "padding: 2px; float:left;"
178 });
179
180 var linkLabel = $("<span>" + gs.text.de.edit_map_gps + "</span>");
181 var linkIcon = $("<span>", {
182 "class": "ui-icon ui-icon-folder-collapsed"
183 });
184 newCell.linkIcon = linkIcon;
185 newCell.linkLabel = linkLabel;
186
187 var uList = $("<ul>", {
188 "style": "outline: 0 none; margin:0px; padding:0px;"
189 });
190 var labelItem = $("<li>", {
191 "style": "float:left; list-style:none outside none;"
192 });
193 var iconItem = $("<li>", {
194 "style": "float:left; list-style:none outside none;"
195 });
196
197 uList.append(iconItem);
198 uList.append(labelItem);
199 labelItem.append(linkLabel);
200 iconItem.append(linkIcon);
201
202 var mapEditor = new MapEditor(id);
203 gsmap_store["map-" + id] = mapEditor;
204
205 var newLink = $("<a>", {
206 "href": "javascript:;"
207 });
208 newLink.click(function () {
209 //console.log(" Show/Hide Map Editor ");
210 var clicked_mapEditor = gsmap_store["map-" + id];
211
212 if (clicked_mapEditor.map == null) {
213 clicked_mapEditor.initMapEditorControls();
214 clicked_mapEditor.initMapEditor();
215 }
216 if (mapGPScontainer.css("display") == "none") {
217 linkLabel.html(gs.text.de.hide_map_gps);
218 linkIcon.attr("class", "ui-icon ui-icon-folder-open");
219 mapGPScontainer.css("display", "block");
220 } else {
221 linkLabel.html(gs.text.de.edit_map_gps);
222 linkIcon.attr("class", "ui-icon ui-icon-folder-collapsed");
223 mapGPScontainer.css("display", "none");
224
225 }
226 });
227
228 newLink.append(uList);
229 linkSpan.append(newLink);
230 newCell.append(linkSpan);
231 row.append(newCell);
232
233 mapGPScontainer.css("display", "none");
234
235 addFunctionalityToTable(mapGPScontainer);
236 mapGPScontainer.metaNameField.css("display", "none");
237 mapGPScontainer.addRowButton.css("display", "none");
238 if (enable_add_all_metadata_button == true) {
239 mapGPScontainer.addAllButton.css("display", "none");
240 }
241
242}
243
244function setEditingFeaturesVisible(visible) {
245 if (visible) {
246 $("#editContentButton").html(gs.text.de.hide_editor);
247 $("#editContentButtonDiv").attr("class", "ui-state-default ui-corner-all");
248 } else {
249 $("#editContentButton").html(gs.text.de.edit_content);
250 $("#editContentButtonDiv").attr("class", "");
251 }
252
253 var visibility = (visible ? "" : "none");
254 if (display_metadata_set_selector == true) {
255 $("#metadataListLabel, #metadataSetList").css("display", visibility);
256 }
257
258 $(".editMetadataButton").each(function () {
259 $(this).css("display", visibility);
260 $(this.linkLabel).html(gs.text.de.edit_metadata);
261 $(this.linkIcon).attr("class", "ui-icon ui-icon-folder-collapsed");
262 });
263 /*
264 $(".editMapGPS").each(function(){
265 $(this).css("display", visibility);
266 $(this.linkLabel).html(gs.text.de.edit_map_gps);
267 $(this.linkIcon).attr("class", "ui-icon ui-icon-folder-collapsed");
268 });
269 */
270
271 $("table").each(function () {
272 if ($(this).attr("id") && $(this).attr("id").search(/^meta/) != -1) {
273 $(this).css("display", "none");
274 $(this.metaNameField).css("display", "none");
275 $(this.addRowButton).css("display", "none");
276 if (enable_add_all_metadata_button == true) {
277 $(this.addAllButton).css("display", "none");
278 }
279 }
280 });
281}
282
283/* override this function in other interface/site/collection if you want
284a different set of metadata sets
285Use in conjunction with the dynamic_metadata_set_list variable. */
286function setStaticMetadataSets(list) {
287 addOptionToList(list, "All", gs.text.de.all_metadata);
288}
289
290function readyPageForEditing() {
291
292 // CKEDITOR.on('instanceReady', function(evt) {
293 // addCKEEditableState(evt,editableInitStates);
294 // });
295
296 if ($("#metadataSetList").length) {
297 var setList = $("#metadataSetList");
298 if (!setList.css("display") || setList.css("display") == "") {
299 setEditingFeaturesVisible(false);
300 } else {
301 setEditingFeaturesVisible(true);
302 }
303 return;
304 }
305
306 $("#editContentButton").html(gs.text.de.hide_editor);
307 //wait for 0.5 sec to let ckeditor up
308 setTimeout(function () {
309 $(".sectionText").each(function () {
310 addEditableState(this, editableInitStates);
311 });
312 }, 500);
313 var editBar = $("#editBarLeft");
314
315 var visibleMetadataList = $("<select>", {
316 "id": "metadataSetList",
317 "class": "ui-state-default"
318 });
319 setStaticMetadataSets(visibleMetadataList);
320
321 if (display_metadata_set_selector == true) {
322 var metadataListLabel = $("<span>", {
323 "id": "metadataListLabel",
324 "style": "margin-left:20px;"
325 });
326 metadataListLabel.html(gs.text.de.visible_metadata);
327 editBar.append(metadataListLabel);
328 } else {
329 visibleMetadataList.css("display", "none");
330 }
331 editBar.append(visibleMetadataList);
332 visibleMetadataList.change(onVisibleMetadataSetChange);
333 editBar.append("<br>");
334
335 for (var i = 0; i < save_and_rebuild_buttons.length; i++) {
336 var button_type = save_and_rebuild_buttons[i];
337 if (button_type == "save") {
338 var saveButton = $("<button>", {
339 "id": "saveButton",
340 "class": "ui-state-default ui-corner-all"
341 });
342 saveButton.click(save);
343 saveButton.html(gs.text.de.save);
344 editBar.append(saveButton);
345 } else if (button_type == "rebuild") {
346 var rebuildButton = $("<button>", {
347 "id": "rebuildButton",
348 "class": "ui-state-default ui-corner-all"
349 });
350 rebuildButton.click(rebuildCurrentCollection);
351 rebuildButton.html(gs.text.de.rebuild);
352 editBar.append(rebuildButton);
353 } else if (button_type == "saveandrebuild") {
354 var saveAndRebuildButton = $("<button>", {
355 "id": "saveAndRebuildButton",
356 "class": "ui-state-default ui-corner-all"
357 });
358 saveAndRebuildButton.click(saveAndRebuild);
359 saveAndRebuildButton.html(gs.text.de.saverebuild);
360 editBar.append(saveAndRebuildButton);
361
362 }
363 }
364 var statusBarDiv = $("<div>");
365 editBar.append(statusBarDiv);
366 _statusBar = new StatusBar(statusBarDiv[0]);
367
368 var titleDivs = $(".sectionTitle");
369 for (var i = 0; i < titleDivs.length; i++) {
370 addEditMetadataLink(titleDivs[i]);
371 addEditMapGPSLink(titleDivs[i]);
372 }
373
374 _baseURL = gs.xsltParams.library_name;
375 onVisibleMetadataSetChange(); // make sure that the selected item in the list is active
376}
377
378// override the one in documentmaker_scripts_util
379// currently not used if other one is present. need to get the js include order right
380function enableSaveButtons(enabled) {
381 if (enabled) {
382 $("#saveButton, #rebuildButton, #saveAndRebuildButton").removeAttr("disabled");
383 } else {
384 $("#saveButton, #rebuildButton, #saveAndRebuildButton").attr("disabled", "disabled");
385 }
386}
387
388/* this is a cut down version of save() from documentmaker_scripts_util.js
389going back to using save, will delete this once everything working*/
390function saveMetadataChangesOld() {
391
392 console.log("Saving metadata changes");
393
394 // get collection name
395 var collection = gs.cgiParams.c;
396
397 // get document id
398 var docID = gs.cgiParams.d;
399
400 var metadataChanges = new Array();
401 if (_deletedMetadata.length > 0) {
402
403 for (var i = 0; i < _deletedMetadata.length; i++) {
404
405 var currentRow = _deletedMetadata[i];
406
407 //Get metadata name
408 var cells = currentRow.getElementsByTagName("TD");
409 var nameCell = cells[0];
410 var name = nameCell.innerHTML;
411 var valueCell = cells[1];
412 var value = valueCell.innerHTML;
413 metadataChanges.push({
414 type: 'delete',
415 docID: docID,
416 name: name,
417 value: value
418 });
419 removeFromParent(currentRow);
420 }
421 }
422
423 if (metadataChanges.length == 0) {
424 console.log(gs.text.de.no_changes);
425 return;
426 }
427
428 var processChangesLoop = function (index) {
429 var change = metadataChanges[index];
430
431 var callbackFunction;
432 if (index + 1 == metadataChanges.length) {
433 callbackFunction = function () {
434 console.log("Completed saving metadata changes. You must rebuild the collection for the changes to take effect.");
435 };
436 } else {
437 callbackFunction = function () {
438 processChangesLoop(index + 1)
439 };
440 }
441 if (change.type == "delete") {
442 gs.functions.removeArchivesMetadata(collection, gs.xsltParams.site_name, change.docID, change.name, null, change.value, function () {
443 callbackFunction();
444 });
445 } else {
446 if (change.orig) {
447 gs.functions.setArchivesMetadata(collection, gs.xsltParams.site_name, docID, change.name, null, change.value, change.orig, "override", function () {
448 callbackFunction();
449 });
450 } else {
451 gs.functions.setArchivesMetadata(collection, gs.xsltParams.site_name, docID, change.name, null, change.value, null, "accumulate", function () {
452 callbackFunction();
453 });
454 }
455 }
456 }
457 processChangesLoop(0);
458 /* need to clear the changes from the page */
459 while (_deletedMetadata.length > 0) {
460 _deletedMetadata.pop();
461 }
462
463}
Note: See TracBrowser for help on using the repository browser.