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

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

made hasClass a global function

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