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

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

Save and rebuild works with map, but as with save shortcut key on map editor, the metamode ends up as accumulate still in doc.xml. Some other issues (not related to saveandrebuild) need fixing up like a popup about unsaved changes appearing even after save and rebuild has finished and possibly more things).

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