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

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

Fixed document creation

  • Property svn:executable set to *
File size: 20.3 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 initDocumentMaker()
14{
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") && (gs.functions.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 = gs.text.dse.no_docs;
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 {
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", "topLevelItem");
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 topLevelItem" : "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 var createButton = document.getElementById("createNewDocumentButton");
225 createButton.disabled = true;
226
227 var saveButton = document.getElementById("saveButton");
228 saveButton.disabled = true;
229
230 var statusID = _statusBar.addStatus("Creating document...");
231
232 var newID = "HASH" + gs.functions.hashString("" + (new Date()).getTime());
233
234 var ajax = new gs.functions.ajaxRequest();
235 ajax.open("POST", gs.xsltParams.library_name, true);
236 ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
237 ajax.onreadystatechange = function()
238 {
239 if(ajax.readyState == 4 && ajax.status == 200)
240 {
241 buildCollections([gs.cgiParams.p_c], [newID], function()
242 {
243 //Create the necessary elements
244 var topLevelUL = document.createElement("UL");
245 var topLevelLI = document.createElement("LI");
246 var contentUL = document.createElement("UL");
247
248 //Append the top-level list item to the top-level list
249 topLevelUL.appendChild(topLevelLI);
250 topLevelUL.setAttribute("class", "topLevelItem");
251
252 //Set up the top-level item
253 topLevelLI.setAttribute("class", "dragItem topLevelItem");
254 topLevelLI.childList = contentUL;
255 topLevelLI.collection = gs.cgiParams.p_c;
256 topLevelLI.nodeID = newID;
257 contentUL.parentItem = topLevelLI;
258
259 //Add a textDiv to the top-level item
260 var textDiv = createSectionTextDiv(null);
261 textDiv.parentItem = topLevelLI;
262 topLevelLI.textDiv = textDiv;
263 topLevelUL.appendChild(textDiv);
264
265 //Create a blank metadata table
266 var metaTable = document.createElement("TABLE");
267 metaTable.setAttribute("id", "meta" + newID);
268 textDiv.insertBefore(metaTable, textDiv.firstChild);
269 var titleMetaRow = document.createElement("TR");
270 var titleMetaNameCell = document.createElement("TD");
271 titleMetaNameCell.innerHTML = "dc.Title";
272 titleMetaNameCell.setAttribute("class", "metaTableCellName");
273 var titleMetaValueCell = document.createElement("TD");
274 titleMetaValueCell.setAttribute("class", "metaTableCell editable");
275 titleMetaValueCell.innerHTML = "UNTITLED DOCUMENT";
276 titleMetaRow.appendChild(titleMetaNameCell);
277 titleMetaRow.appendChild(titleMetaValueCell);
278 metaTable.appendChild(titleMetaRow);
279 addFunctionalityToTable(metaTable);
280
281 //Add a title to the top-level item
282 var title = createSectionTitle(gs.text.dse.untitled);
283 topLevelLI.appendChild(title);
284 topLevelLI.sectionTitle = title;
285
286 createSectionMenu(topLevelLI);
287 setMouseOverAndOutFunctions(topLevelLI);
288
289 //Set up the placeholder for the first section
290 contentUL.setAttribute("class", "dragList");
291 new YAHOO.util.DDTarget(contentUL);
292
293 //Create a placeholder and add it to first section
294 var placeHolder = createPlaceholder(null, contentUL, false);
295 contentUL.appendChild(placeHolder);
296
297 //Add elements to the page
298 var dbDiv = document.getElementById("dbDiv");
299 if(dbDiv.firstChild)
300 {
301 dbDiv.insertBefore(topLevelUL, dbDiv.firstChild);
302 }
303 else
304 {
305 dbDiv.appendChild(topLevelUL);
306 }
307 insertAfter(contentUL, topLevelLI.textDiv);
308
309 //Correct any issues
310 updateFromTop();
311 });
312 createButton.disabled = false;
313 saveButton.disabled = false;
314 _statusBar.removeStatus(statusID);
315 }
316 else if (ajax.readyState == 4)
317 {
318 createButton.disabled = false;
319 saveButton.disabled = false;
320 _statusBar.removeStatus(statusID);
321 }
322 }
323 console.log('[{"operation":"createDocument", "oid":"' + newID + '", "collection":"' + gs.cgiParams.p_c + '"}]');
324 ajax.send('a=g&rt=r&s=DocumentExecuteTransaction&s1.transactions=[{"operation":"createDocument", "oid":"' + newID + '", "collection":"' + gs.cgiParams.p_c + '"}]');
325}
326
327function createPlaceholder(parent, parentList, mouseEvents)
328{
329 //Create the place holder and assign its class
330 var placeHolder = document.createElement("LI");
331 placeHolder.setAttribute("class", "placeHolder");
332
333 //If a parent was given then we can assign the collection and nodeID
334 if(parent)
335 {
336 placeHolder.collection = parent.collection;
337 placeHolder.nodeID = parent.nodeID;
338 }
339
340 //If this is to be a plain placeholder then we don't want it to react to mouse events
341 if(mouseEvents)
342 {
343 placeHolder.isEmptyList = true;
344
345 //Create the delete section link
346 var deleteSectionLink = document.createElement("A");
347 deleteSectionLink.innerHTML = gs.text.dse.delete_section;
348 deleteSectionLink.setAttribute("href", "javascript:;");
349 deleteSectionLink.setAttribute("class", "menuLink");
350 deleteSectionLink.style.display = "none";
351
352 //Set the onclick behaviour of the delete link
353 deleteSectionLink.onclick = function()
354 {
355 //Delete the place holder
356 removeFromParent(placeHolder);
357
358 //If this is in a list then delete the list (as this will be the only thing in the list)
359 if(parentList)
360 {
361 var undo = new Array();
362
363 undo.op = "mva";
364 undo.srcElem = parentList;
365 undo.refElem = parent;
366 undo.removeTransaction = false;
367 _undoOperations.push(undo);
368
369 removeFromParent(parentList);
370 }
371
372 //Enable the "add sub-section" menu option in the parent
373 if(parent)
374 {
375 parent.menu.newSectionLink.style.display = "inline";
376 parent.childList = null;
377 }
378 }
379 placeHolder.appendChild(deleteSectionLink);
380
381 //Colour the list item and display the menu on mouse over
382 placeHolder.onmouseover = function(e)
383 {
384 deleteSectionLink.style.display = "inline";
385 this.style.background = "rgb(255, 200, 0)";
386 };
387 //Uncolour the list item and hide the menu on mouse out
388 placeHolder.onmouseout = function(e)
389 {
390 deleteSectionLink.style.display = "none";
391 this.style.background = "none";
392 };
393 }
394
395 var dragItem = new YAHOO.example.DDList(placeHolder);
396 dragItem.addInvalidHandleClass("placeHolder");
397 return placeHolder;
398}
399
400function duplicateSection(section)
401{
402 var newLI = document.createElement("LI");
403 newLI.setAttribute("class", "dragItem");
404 new YAHOO.example.DDList(newLI);
405
406 if(section.textDiv)
407 {
408 var textDiv = createSectionTextDiv(section.textDiv.innerHTML);
409 newLI.textDiv = textDiv;
410 }
411 newLI.collection = section.collectionName;
412
413 if(section.sectionTitle)
414 {
415 var title = createSectionTitle(section.sectionTitle.innerHTML);
416 newLI.sectionTitle = title;
417 newLI.appendChild(title);
418 }
419
420 createSectionMenu(newLI);
421 setMouseOverAndOutFunctions(newLI);
422 newLI.onmouseout();
423
424 if(section.childList)
425 {
426 insertAfter(newLI, section.childList);
427 }
428 else if(section.textDiv)
429 {
430 insertAfter(newLI, section.textDiv);
431 }
432 else
433 {
434 insertAfter(newLI, section);
435 }
436
437 if(newLI.textDiv)
438 {
439 insertAfter(newLI.textDiv, newLI);
440 }
441 return newLI;
442}
443
444function duplicateSectionChildrenRecursive(duplicate, original)
445{
446 if(!original.childList)
447 {
448 return;
449 }
450
451 var newUL = document.createElement("UL");
452 newUL.setAttribute("class", "dragList");
453 new YAHOO.util.DDTarget(newUL);
454 insertAfter(newUL, duplicate.textDiv);
455
456 var children = new Array();
457 var current = original.childList.firstChild;
458 while(current != null)
459 {
460 children.push(current);
461 current = current.nextSibling;
462 }
463
464 for(var i = 0; i < children.length; i++)
465 {
466 current = children[i];
467 if(current.nodeName.toLowerCase() == "li" && !gs.functions.hasClass(current, "placeHolder"))
468 {
469 var newSection = duplicateSection(current);
470 newUL.appendChild(newSection);
471 if(current.childList)
472 {
473 duplicateSectionChildrenRecursive(newSection, current);
474 }
475 }
476 }
477
478 duplicate.childList = newUL;
479 newUL.parentItem = duplicate.childList;
480
481 if(duplicate.menu)
482 {
483 duplicate.menu.newSectionLink.style.display = "none";
484 }
485}
486
487function deleteSection(section)
488{
489 var undo = new Array();
490 var prev = getPrevSiblingOfType(section, "li");
491 var next = getNextSiblingOfType(section, "li");
492 var parent = section.parentList;
493 if(prev)
494 {
495 undo.op = "mva";
496 undo.refElem = prev;
497 }
498 else if(next)
499 {
500 undo.op = "mvb";
501 undo.refElem = next;
502 }
503 else
504 {
505 undo.op = "mvi";
506 undo.refElem = parent;
507 }
508 undo.srcElem = section;
509 undo.removeTransaction = true;
510 _undoOperations.push(undo);
511
512 saveTransaction('{"operation":"delete", "collection":"' + section.collection + '", "oid":"' + section.nodeID + '"}');
513 addCollectionToBuild(section.collection);
514
515 _deletedSections.push(section);
516 if(section.textDiv)
517 {
518 removeFromParent(section.textDiv);
519 }
520 if(section.childList)
521 {
522 removeFromParent(section.childList);
523 }
524 removeFromParent(section);
525 updateFromTop();
526}
527
528function createBlankSection(parent)
529{
530 if(parent.childList)
531 {
532 return;
533 }
534
535 var newUL = document.createElement("UL");
536
537 newUL.setAttribute("class", "dragList emptyList");
538 new YAHOO.util.DDTarget(newUL);
539
540 insertAfter(newUL, parent.textDiv);
541 parent.childList = newUL;
542 newUL.parentItem = parent;
543
544 var menu = parent.menu;
545 menu.newSectionLink.style.display = "none";
546
547 var undo = new Array();
548 undo.op = "del";
549 undo.srcElem = newUL;
550 undo.removeTransaction = false;
551 _undoOperations.push(undo);
552}
553
554function createSectionMenu(section)
555{
556 var menuBar = document.createElement("SPAN");
557
558 //Separator
559 menuBar.appendChild(document.createTextNode(" "));
560
561 //"Edit" link
562 var toggleLink = document.createElement("A");
563 toggleLink.innerHTML = gs.text.dse.edit;
564 toggleLink.setAttribute("class", "menuLink");
565 toggleLink.setAttribute("href", "javascript:;");
566 toggleLink.onclick = function(){toggleTextDiv(section);};
567 menuBar.appendChild(toggleLink);
568 menuBar.editTextLink = toggleLink;
569
570 //Separator
571 menuBar.appendChild(document.createTextNode(" "));
572
573 var newSectionLink = document.createElement("A");
574 newSectionLink.innerHTML = gs.text.dse.add_sub_section.replace(/ /g, "&nbsp;");
575 newSectionLink.setAttribute("class", "menuLink");
576 newSectionLink.setAttribute("href", "javascript:;");
577 newSectionLink.onclick = function()
578 {
579 createBlankSection(section);
580 updateFromTop();
581 };
582 menuBar.appendChild(newSectionLink);
583 menuBar.newSectionLink = newSectionLink;
584
585 //"New Section" link
586 if(section.childList)
587 {
588 newSectionLink.style.display = "none";
589 }
590
591 //Separator
592 menuBar.appendChild(document.createTextNode(" "));
593
594 //"Duplicate" link
595 var duplicateLink = document.createElement("A");
596 duplicateLink.innerHTML = gs.text.dse.duplicate;
597 duplicateLink.setAttribute("class", "menuLink");
598 duplicateLink.setAttribute("href", "javascript:;");
599 duplicateLink.onclick = function()
600 {
601 var newSection = duplicateSection(section);
602 if(section.childList)
603 {
604 duplicateSectionChildrenRecursive(newSection, section);
605 }
606
607 var newNodeID = section.nodeID;
608 var lastDigit = parseInt(newNodeID.substring(newNodeID.lastIndexOf(".") + 1));
609 newNodeID = newNodeID.replace(/\.[^\.]*$/, "." + ++lastDigit);
610
611 var undo = new Array();
612 undo.op = "del";
613 undo.srcElem = section;
614 undo.removeTransaction = true;
615 _undoOperations.push(undo);
616
617 saveTransaction('{"operation":"duplicate", "subOperation":"insertBefore", "collection":"' + section.collection + '", "oid":"' + section.nodeID + '", "newCollection":"' + section.collection + '", "newOID":"' + newNodeID + '"}');
618 addCollectionToBuild(section.collection);
619
620 updateFromTop();
621 };
622 menuBar.appendChild(duplicateLink);
623 menuBar.duplicateLink = duplicateLink;
624
625 //Separator
626 menuBar.appendChild(document.createTextNode(" "));
627
628 //"Delete" link
629 var deleteLink = document.createElement("A");
630 deleteLink.innerHTML = "[X]";
631 deleteLink.setAttribute("class", "menuLink deleteLink");
632 deleteLink.setAttribute("href", "javascript:;");
633 deleteLink.onclick = function(){deleteSection(section)};
634 menuBar.appendChild(deleteLink);
635 menuBar.deleteLink = deleteLink;
636
637 menuBar.style.display = "none";
638 section.appendChild(menuBar);
639 section.menu = menuBar;
640}
641
642function updateRecursive(parent, currentDocument, currentPosition, level)
643{
644 if(level == 0)
645 {
646 _indexCounter = 0;
647 }
648
649 level++;
650 var current = parent.firstChild;
651 var posCount = 1;
652 var lastItem;
653 var liCount = 0;
654 while(current != null)
655 {
656 if(current.nodeName.toLowerCase() == "ul")
657 {
658 var pos = null;
659 if(level > 2)
660 {
661 if(!currentPosition)
662 {
663 pos = (posCount - 1);
664 }
665 else
666 {
667 pos = currentPosition + "." + (posCount - 1);
668 }
669 }
670
671 updateRecursive(current, currentDocument, pos, level);
672 }
673 else if (current.nodeName.toLowerCase() == "li" && gs.functions.hasClass(current, "dragItem") && !gs.functions.hasClass(current, "placeHolder"))
674 {
675 if(currentDocument == null && current.nodeID)
676 {
677 currentDocument = current.nodeID;
678 }
679
680 var pos;
681 if(!currentPosition)
682 {
683 pos = posCount;
684 }
685 else
686 {
687 pos = currentPosition + "." + posCount;
688 }
689
690 if(!gs.functions.hasClass(current, "topLevelItem"))
691 {
692 current.nodeID = currentDocument + "." + pos;
693 current.position = pos;
694 current.documentID = currentDocument;
695 }
696 posCount++;
697
698 current.index = _indexCounter++;
699 }
700 else if (gs.functions.hasClass(current, "placeHolder") && !current.isEmptyList)
701 {
702 var pos;
703 if(!currentPosition)
704 {
705 pos = posCount - 1;
706 }
707 else
708 {
709 pos = currentPosition + "." + posCount - 1;
710 }
711 current.nodeID = currentDocument + "." + pos;
712 }
713
714 if(current.nodeName.toLowerCase() == "li")
715 {
716 liCount++;
717 lastItem = current;
718 }
719
720 current = current.nextSibling;
721 }
722
723 if(level > 2)
724 {
725 //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
726 if(lastItem && lastItem.childList)
727 {
728 var placeHolder = createPlaceholder(lastItem, parent, false);
729 parent.appendChild(placeHolder);
730 }
731
732 //If this list is empty or has 1 placeholder child
733 if(liCount == 0 || (liCount == 1 && gs.functions.hasClass(lastItem, "placeHolder")))
734 {
735 //Give it the emptyList css class (if it does not already have it)
736 if(!parent.getAttribute("class") || parent.getAttribute("class").search("emptyList") == -1)
737 {
738 var newClass = parent.getAttribute("class") ? parent.getAttribute("class") + " emptyList" : "emptyList";
739 parent.setAttribute("class", newClass);
740 }
741
742 //If the list is empty then add a placeholder
743 if(liCount == 0)
744 {
745 var placeHolder = createPlaceholder(parent.previousSibling.previousSibling, parent, true); //Find a smarter way of doing this
746 parent.appendChild(placeHolder);
747 }
748 }
749 //Remove the empty list class if the list is no longer empty
750 else if(gs.functions.hasClass(parent, "emptyList"))
751 {
752 parent.setAttribute("class", parent.getAttribute("class").replace(/emptyList/g, ""));
753 }
754 }
755}
Note: See TracBrowser for help on using the repository browser.