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

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

Adding in the files for the document maker

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