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

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

More document maker changes

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