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

Last change on this file since 32840 was 32840, checked in by ak19, 5 years ago

After we're ready to save and rebuild, new overlays get saved as the old ones (as savedOverlays) for each mapeditor whose data gets saved, parallelling what the regular metadata editor does. Now we no longer see the popup about unsaved changes after saveAndRebuild.

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