source: main/trunk/greenstone3/web/interfaces/oran/js/documentmaker_scripts.js@ 25105

Last change on this file since 25105 was 25104, checked in by sjm84, 12 years ago

Added the ability to remove metadata

  • Property svn:executable set to *
File size: 18.0 KB
Line 
1var _transactions = new Array();
2var _collectionsToBuild = new Array();
3var _allContents = new Array();
4var _idCounter = 0;
5var _indexCounter = 0;
6var _deletedSections = new Array();
7var _deletedMetadata = new Array();
8var _undoOperations = new Array();
9var _baseURL;
10var _statusBar;
11var _metadataSetList = new Array();
12
13function init()
14{
15 de.init();
16 //Get all of the links on the page
17 var allLinks = document.getElementsByTagName("a");
18
19 //Work out which links are the actual document links
20 var docLinks = new Array();
21 for(var i = 0; i < allLinks.length; i++)
22 {
23 if(allLinks[i].getAttribute("class") && (hasClass(allLinks[i], "dbdoc")))
24 {
25 docLinks.push(allLinks[i]);
26 }
27 }
28
29 if(gs.cgiParams.docToEdit)
30 {
31 var content = document.getElementById("gs_content");
32 var newLink = document.createElement("A");
33 newLink.setAttribute("href", gs.xsltParams.library_name + "?a=d&c=" + gs.cgiParams.p_c + "&dt=hierarchy&ed=1&d=" + gs.cgiParams.docToEdit);
34 content.appendChild(newLink);
35 docLinks.push(newLink);
36 }
37
38 if(docLinks.length == 0)
39 {
40 document.getElementById("gs_content").innerHTML = "No documents in the Document Basket";
41 return;
42 }
43
44 //Create the top menu bar
45 var menuBar = createTopMenuBar();
46
47 //Add the menu bar to the page
48 var mainContentDiv = document.getElementById("gs_content");
49 mainContentDiv.appendChild(menuBar);
50
51 var dbDiv = document.createElement("DIV");
52 dbDiv.setAttribute("id", "dbDiv");
53 insertAfter(dbDiv, menuBar);
54
55 var statusDiv = document.createElement("DIV");
56 statusDiv.setAttribute("class", "statusBar");
57 insertAfter(statusDiv, menuBar);
58 _statusBar = new StatusBar(statusDiv);
59
60 //Request the html for each link's page
61 for(var i = 0; i < docLinks.length; i++)
62 {
63 var callback = {
64 success: addDocumentStructureToPage,
65 failure: function(data){/*alert("FAILED");*/}
66 }
67 callback.currentLink = docLinks[i];
68 YAHOO.util.Connect.asyncRequest("GET", docLinks[i].getAttribute("href").concat('&dmd=true&excerptid=gs-document-text&hhf=[{"name":"Cache-Control", "value":"no-cache"}]'), callback);
69 }
70
71 _baseURL = gs.xsltParams.library_name;
72}
73
74function addDocumentStructureToPage(data)
75{
76 //Get the HTML
77 var page = data.responseText;
78
79 //Add the HTML to the page inside an invisible div
80 var tempDiv = document.createElement("DIV");
81 tempDiv.innerHTML = page;
82 tempDiv.style.display = "none";
83 insertAfter(tempDiv, this.currentLink);
84
85 //Get the collection that this document belongs to
86 var collection = document.getElementById("gs-document-text").getAttribute("collection");
87
88 //Get the Document Basket div
89 var dbDiv = document.getElementById("dbDiv");
90
91 //Create the container list and add it to the Document Basket div
92 var containerUL = document.createElement("UL");
93 containerUL.setAttribute("class", "topLevel");
94 dbDiv.appendChild(containerUL);
95
96 //Get all of the headers in the page
97 var headers = getElementsByClassName("sectionTitle", tempDiv);
98
99 //Some necessary loop variables
100 var prevItem = null;
101 var prevDepth = 0;
102 var levelContainers = new Array();
103 levelContainers[0] = containerUL;
104
105 //Loop through all of the headers
106 for(var i = 0; i < headers.length; i++)
107 {
108 var currentHeader = headers[i];
109
110 //If the currentHeader is not a <td> element then we are not interested
111 if(currentHeader.nodeName.toLowerCase() != "td")
112 {
113 continue;
114 }
115
116 //Split the section ID on . to get its position in the document
117 var posArray = currentHeader.getAttribute("id").split(".");
118
119 //Save the document ID
120 var docID = posArray[0].substring(6);
121
122 //Save the depth of the section (top level is 0)
123 var depth = posArray.length - 1;
124
125 //Turn the position array into a string
126 var position = "";
127 for(var j = 1; j < posArray.length; j++)
128 {
129 if(j != 1)
130 {
131 position += ".";
132 }
133 position += posArray[j];
134 }
135
136 //Save the section number
137 var secID = currentHeader.getAttribute("id").substring("6");
138
139 //Get the text of the section
140 var currentText = document.getElementById("text" + secID);
141 var renderedDiv = createSectionTextDiv(currentText.innerHTML);
142
143 var newItem = document.createElement("LI");
144 new YAHOO.example.DDList(newItem);
145
146 var title = createSectionTitle(currentHeader.innerHTML);
147 newItem.sectionTitle = title;
148 newItem.appendChild(title);
149 newItem.setAttribute("class", depth == 0 ? "dragItem topLevel" : "dragItem");
150 newItem.textDiv = renderedDiv;
151 renderedDiv.parentItem = newItem;
152
153 var metadataTable = document.getElementById("meta" + secID);
154 renderedDiv.insertBefore(metadataTable, renderedDiv.firstChild);
155 addFunctionalityToTable(metadataTable);
156
157 if(depth > prevDepth)
158 {
159 var newContainer = document.createElement("UL");
160 new YAHOO.util.DDTarget(newContainer);
161 newContainer.setAttribute("class", "dragList");
162
163 prevItem.childList = newContainer;
164 prevItem.menu.newSectionLink.style.display = "none";
165 newContainer.parentItem = prevItem;
166 levelContainers[depth - 1].appendChild(newContainer);
167 levelContainers[depth] = newContainer;
168 }
169 prevDepth = depth;
170
171 levelContainers[depth].appendChild(newItem);
172 levelContainers[depth].appendChild(renderedDiv);
173
174 createSectionMenu(newItem);
175 setMouseOverAndOutFunctions(newItem);
176
177 //Set various section properties
178 //newItem.collection = collectionName;
179 newItem.documentID = docID;
180 newItem.position = position;
181 newItem.nodeID = secID;
182 newItem.dbID = _idCounter++;
183 newItem.index = newItem.dbID;
184 newItem.collection = collection;
185 newItem.parentList = levelContainers[depth];
186
187 prevItem = newItem;
188
189 //Insert the section into the list of sections
190 _allContents.push(newItem);
191 }
192
193 removeFromParent(this.currentLink);
194 updateFromTop();
195}
196
197function createSectionTextDiv(text)
198{
199 var renderedDiv = document.createElement("DIV");
200 renderedDiv.setAttribute("style", "display:none;");
201
202 var textDiv = document.createElement("DIV");
203 if(text && text.length > 0)
204 {
205 textDiv.innerHTML = text;
206 }
207 else
208 {
209 textDiv.innerHTML = "&nbsp;";
210 }
211 textDiv.setAttribute("class", "renderedText editable");
212
213 //This registering can cause a sizeable delay so we'll thread it (effectively) so the browser is not paused
214 setTimeout(function(){de.doc.registerEditSection(textDiv)}, 0);
215
216 renderedDiv.appendChild(textDiv);
217 textDiv.parentDiv = renderedDiv;
218
219 return renderedDiv;
220}
221
222function createNewDocumentArea()
223{
224 //Create the necessary elements
225 var topLevelUL = document.createElement("UL");
226 var topLevelLI = document.createElement("LI");
227 var contentUL = document.createElement("UL");
228
229 //Append the top-level list item to the top-level list
230 topLevelUL.appendChild(topLevelLI);
231 topLevelUL.setAttribute("class", "topLevel");
232
233 //Set up the top-level item
234 topLevelLI.setAttribute("class", "dragItem topLevel");
235 topLevelLI.childList = contentUL;
236 contentUL.parentItem = topLevelLI;
237
238 //Add a textDiv to the top-level item
239 var textDiv = createSectionTextDiv(null);
240 topLevelLI.textDiv = textDiv;
241 topLevelUL.appendChild(textDiv);
242
243 //Add a title to the top-level item
244 var title = createSectionTitle("UNTITLED DOCUMENT");
245 topLevelLI.appendChild(title);
246 topLevelLI.sectionTitle = title;
247
248 createSectionMenu(topLevelLI);
249 setMouseOverAndOutFunctions(topLevelLI);
250
251 //Set up the placeholder for the first section
252 contentUL.setAttribute("class", "dragList");
253 new YAHOO.util.DDTarget(contentUL);
254
255 //Create a placeholder and add it to first section
256 var placeHolder = createPlaceholder(null, contentUL, false);
257 contentUL.appendChild(placeHolder);
258
259 var dbDiv = document.getElementById("dbDiv");
260
261 //Add elements to the page
262 if(dbDiv.firstChild)
263 {
264 dbDiv.insertBefore(topLevelUL, dbDiv.firstChild);
265 }
266 else
267 {
268 dbDiv.appendChild(topLevelUL);
269 }
270 insertAfter(contentUL, topLevelLI.textDiv);
271
272 //Correct any issues
273 updateFromTop();
274}
275
276function createPlaceholder(parent, parentList, mouseEvents)
277{
278 //Create the place holder and assign its class
279 var placeHolder = document.createElement("LI");
280 placeHolder.setAttribute("class", "placeHolder");
281
282 //If a parent was given then we can assign the collection and nodeID
283 if(parent)
284 {
285 placeHolder.collection = parent.collection;
286 placeHolder.nodeID = parent.nodeID;
287 }
288
289 //If this is to be a plain placeholder then we don't want it to react to mouse events
290 if(mouseEvents)
291 {
292 placeHolder.isEmptyList = true;
293
294 //Create the delete section link
295 var deleteSectionLink = document.createElement("A");
296 deleteSectionLink.innerHTML = "delete section";
297 deleteSectionLink.setAttribute("href", "javascript:;");
298 deleteSectionLink.setAttribute("class", "menuLink");
299 deleteSectionLink.style.display = "none";
300
301 //Set the onclick behaviour of the delete link
302 deleteSectionLink.onclick = function()
303 {
304 //Delete the place holder
305 removeFromParent(placeHolder);
306
307 //If this is in a list then delete the list (as this will be the only thing in the list)
308 if(parentList)
309 {
310 var undo = new Array();
311
312 undo.op = "mva";
313 undo.srcElem = parentList;
314 undo.refElem = parent;
315 undo.removeTransaction = false;
316 _undoOperations.push(undo);
317
318 removeFromParent(parentList);
319 }
320
321 //Enable the "add sub-section" menu option in the parent
322 if(parent)
323 {
324 parent.menu.newSectionLink.style.display = "inline";
325 parent.childList = null;
326 }
327 }
328 placeHolder.appendChild(deleteSectionLink);
329
330 //Colour the list item and display the menu on mouse over
331 placeHolder.onmouseover = function(e)
332 {
333 deleteSectionLink.style.display = "inline";
334 this.style.background = "rgb(255, 200, 0)";
335 };
336 //Uncolour the list item and hide the menu on mouse out
337 placeHolder.onmouseout = function(e)
338 {
339 deleteSectionLink.style.display = "none";
340 this.style.background = "none";
341 };
342 }
343
344 var dragItem = new YAHOO.example.DDList(placeHolder);
345 dragItem.addInvalidHandleClass("placeHolder");
346 return placeHolder;
347}
348
349function duplicateSection(section)
350{
351 var newLI = document.createElement("LI");
352 newLI.setAttribute("class", "dragItem");
353 new YAHOO.example.DDList(newLI);
354
355 if(section.textDiv)
356 {
357 var textDiv = createSectionTextDiv(section.textDiv.innerHTML);
358 newLI.textDiv = textDiv;
359 }
360 newLI.collection = section.collectionName;
361
362 if(section.sectionTitle)
363 {
364 var title = createSectionTitle(section.sectionTitle.innerHTML);
365 newLI.sectionTitle = title;
366 newLI.appendChild(title);
367 }
368
369 createSectionMenu(newLI);
370 setMouseOverAndOutFunctions(newLI);
371 newLI.onmouseout();
372
373 if(section.childList)
374 {
375 insertAfter(newLI, section.childList);
376 }
377 else if(section.textDiv)
378 {
379 insertAfter(newLI, section.textDiv);
380 }
381 else
382 {
383 insertAfter(newLI, section);
384 }
385
386 if(newLI.textDiv)
387 {
388 insertAfter(newLI.textDiv, newLI);
389 }
390 return newLI;
391}
392
393function duplicateSectionChildrenRecursive(duplicate, original)
394{
395 if(!original.childList)
396 {
397 return;
398 }
399
400 var newUL = document.createElement("UL");
401 newUL.setAttribute("class", "dragList");
402 new YAHOO.util.DDTarget(newUL);
403 insertAfter(newUL, duplicate.textDiv);
404
405 var children = new Array();
406 var current = original.childList.firstChild;
407 while(current != null)
408 {
409 children.push(current);
410 current = current.nextSibling;
411 }
412
413 for(var i = 0; i < children.length; i++)
414 {
415 current = children[i];
416 if(current.nodeName.toLowerCase() == "li" && !hasClass(current, "placeHolder"))
417 {
418 var newSection = duplicateSection(current);
419 newUL.appendChild(newSection);
420 if(current.childList)
421 {
422 duplicateSectionChildrenRecursive(newSection, current);
423 }
424 }
425 }
426
427 duplicate.childList = newUL;
428 newUL.parentItem = duplicate.childList;
429
430 if(duplicate.menu)
431 {
432 duplicate.menu.newSectionLink.style.display = "none";
433 }
434}
435
436function deleteSection(section)
437{
438 var undo = new Array();
439 var prev = getPrevSiblingOfType(section, "li");
440 var next = getNextSiblingOfType(section, "li");
441 var parent = section.parentList;
442 if(prev)
443 {
444 undo.op = "mva";
445 undo.refElem = prev;
446 }
447 else if(next)
448 {
449 undo.op = "mvb";
450 undo.refElem = next;
451 }
452 else
453 {
454 undo.op = "mvi";
455 undo.refElem = parent;
456 }
457 undo.srcElem = section;
458 undo.removeTransaction = true;
459 _undoOperations.push(undo);
460
461 saveTransaction('{"operation":"delete", "collection":"' + section.collection + '", "oid":"' + section.nodeID + '"}');
462 addCollectionToBuild(section.collection);
463
464 _deletedSections.push(section);
465 if(section.textDiv)
466 {
467 removeFromParent(section.textDiv);
468 }
469 if(section.childList)
470 {
471 removeFromParent(section.childList);
472 }
473 removeFromParent(section);
474 updateFromTop();
475}
476
477function createBlankSection(parent)
478{
479 if(parent.childList)
480 {
481 return;
482 }
483
484 var newUL = document.createElement("UL");
485
486 newUL.setAttribute("class", "dragList emptyList");
487 new YAHOO.util.DDTarget(newUL);
488
489 insertAfter(newUL, parent.textDiv);
490 parent.childList = newUL;
491 newUL.parentItem = parent;
492
493 var menu = parent.menu;
494 menu.newSectionLink.style.display = "none";
495
496 var undo = new Array();
497 undo.op = "del";
498 undo.srcElem = newUL;
499 undo.removeTransaction = false;
500 _undoOperations.push(undo);
501}
502
503function createSectionMenu(section)
504{
505 var menuBar = document.createElement("SPAN");
506
507 //Separator
508 menuBar.appendChild(document.createTextNode(" "));
509
510 //"Edit" link
511 var toggleLink = document.createElement("A");
512 toggleLink.innerHTML = "edit";
513 toggleLink.setAttribute("class", "menuLink");
514 toggleLink.setAttribute("href", "javascript:;");
515 toggleLink.onclick = function(){toggleTextDiv(section);};
516 menuBar.appendChild(toggleLink);
517 menuBar.editTextLink = toggleLink;
518
519 //Separator
520 menuBar.appendChild(document.createTextNode(" "));
521
522 var newSectionLink = document.createElement("A");
523 newSectionLink.innerHTML = "add&nbsp;sub-section";
524 newSectionLink.setAttribute("class", "menuLink");
525 newSectionLink.setAttribute("href", "javascript:;");
526 newSectionLink.onclick = function()
527 {
528 createBlankSection(section);
529 updateFromTop();
530 };
531 menuBar.appendChild(newSectionLink);
532 menuBar.newSectionLink = newSectionLink;
533
534 //"New Section" link
535 if(section.childList)
536 {
537 newSectionLink.style.display = "none";
538 }
539
540 //Separator
541 menuBar.appendChild(document.createTextNode(" "));
542
543 //"Duplicate" link
544 var duplicateLink = document.createElement("A");
545 duplicateLink.innerHTML = "duplicate";
546 duplicateLink.setAttribute("class", "menuLink");
547 duplicateLink.setAttribute("href", "javascript:;");
548 duplicateLink.onclick = function()
549 {
550 var newSection = duplicateSection(section);
551 if(section.childList)
552 {
553 duplicateSectionChildrenRecursive(newSection, section);
554 }
555
556 var newNodeID = section.nodeID;
557 var lastDigit = parseInt(newNodeID.substring(newNodeID.lastIndexOf(".") + 1));
558 newNodeID = newNodeID.replace(/\.[^\.]*$/, "." + ++lastDigit);
559
560 var undo = new Array();
561 undo.op = "del";
562 undo.srcElem = section;
563 undo.removeTransaction = true;
564 _undoOperations.push(undo);
565
566 saveTransaction('{"operation":"duplicate", "subOperation":"insertBefore", "collection":"' + section.collection + '", "oid":"' + section.nodeID + '", "newCollection":"' + section.collection + '", "newOID":"' + newNodeID + '"}');
567 addCollectionToBuild(section.collection);
568
569 updateFromTop();
570 };
571 menuBar.appendChild(duplicateLink);
572 menuBar.duplicateLink = duplicateLink;
573
574 //Separator
575 menuBar.appendChild(document.createTextNode(" "));
576
577 //"Delete" link
578 var deleteLink = document.createElement("A");
579 deleteLink.innerHTML = "[X]";
580 deleteLink.setAttribute("class", "menuLink deleteLink");
581 deleteLink.setAttribute("href", "javascript:;");
582 deleteLink.onclick = function(){deleteSection(section)};
583 menuBar.appendChild(deleteLink);
584 menuBar.deleteLink = deleteLink;
585
586 menuBar.style.display = "none";
587 section.appendChild(menuBar);
588 section.menu = menuBar;
589}
590
591function updateRecursive(parent, currentDocument, currentPosition, level)
592{
593 if(level == 0)
594 {
595 _indexCounter = 0;
596 }
597
598 level++;
599 var current = parent.firstChild;
600 var posCount = 1;
601 var lastItem;
602 var liCount = 0;
603 while(current != null)
604 {
605 if(current.nodeName.toLowerCase() == "ul")
606 {
607 var pos = null;
608 if(level > 2)
609 {
610 if(!currentPosition)
611 {
612 pos = (posCount - 1);
613 }
614 else
615 {
616 pos = currentPosition + "." + (posCount - 1);
617 }
618 }
619
620 updateRecursive(current, currentDocument, pos, level);
621 }
622 else if (current.nodeName.toLowerCase() == "li" && hasClass(current, "dragItem") && !hasClass(current, "placeHolder"))
623 {
624 if(currentDocument == null && current.nodeID)
625 {
626 currentDocument = current.nodeID;
627 }
628
629 var pos;
630 if(!currentPosition)
631 {
632 pos = posCount;
633 }
634 else
635 {
636 pos = currentPosition + "." + posCount;
637 }
638
639 if(!hasClass(current, "topLevel"))
640 {
641 current.nodeID = currentDocument + "." + pos;
642 current.position = pos;
643 current.documentID = currentDocument;
644 }
645 posCount++;
646
647 current.index = _indexCounter++;
648 }
649 else if (hasClass(current, "placeHolder") && !current.isEmptyList)
650 {
651 var pos;
652 if(!currentPosition)
653 {
654 pos = posCount - 1;
655 }
656 else
657 {
658 pos = currentPosition + "." + posCount - 1;
659 }
660 current.nodeID = currentDocument + "." + pos;
661 }
662
663 if(current.nodeName.toLowerCase() == "li")
664 {
665 liCount++;
666 lastItem = current;
667 }
668
669 current = current.nextSibling;
670 }
671
672 if(level > 2)
673 {
674 //If the last section a this level has a child list then insert a blank placeholder after it so we can insert sections after the list
675 if(lastItem && lastItem.childList)
676 {
677 var placeHolder = createPlaceholder(lastItem, parent, false);
678 parent.appendChild(placeHolder);
679 }
680
681 //If this list is empty or has 1 placeholder child
682 if(liCount == 0 || (liCount == 1 && hasClass(lastItem, "placeHolder")))
683 {
684 //Give it the emptyList css class (if it does not already have it)
685 if(!parent.getAttribute("class") || parent.getAttribute("class").search("emptyList") == -1)
686 {
687 var newClass = parent.getAttribute("class") ? parent.getAttribute("class") + " emptyList" : "emptyList";
688 parent.setAttribute("class", newClass);
689 }
690
691 //If the list is empty then add a placeholder
692 if(liCount == 0)
693 {
694 var placeHolder = createPlaceholder(parent.previousSibling.previousSibling, parent, true); //Find a smarter way of doing this
695 parent.appendChild(placeHolder);
696 }
697 }
698 //Remove the empty list class if the list is no longer empty
699 else if(hasClass(parent, "emptyList"))
700 {
701 parent.setAttribute("class", parent.getAttribute("class").replace(/emptyList/g, ""));
702 }
703 }
704}
705
706YAHOO.util.Event.onDOMReady(init);
Note: See TracBrowser for help on using the repository browser.