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

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

Adding a new function that will be called on saveAndRebuild to save mapdata. So far it is only syntax error free, untested.

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