source: main/trunk/greenstone3/web/interfaces/oran/js/gui_div.js@ 24442

Last change on this file since 24442 was 24442, checked in by sjm84, 13 years ago

Moving another modification made to the JQuery UI to gui_div.js

File size: 45.5 KB
Line 
1
2console.log("Loading gui_div.js\n");
3
4/* DOCUMENT SPECIFIC FUNCTIONS */
5
6function displayTOC(checkbox)
7{
8 if (checkbox.checked == true)
9 {
10 console.log("Show the TOC!");
11 displaySideBar(true);
12 $("#tableOfContents").css("display", "block");
13 }
14 else
15 {
16 console.log("Hide the TOC!");
17 $("#tableOfContents").css("display", "none");
18 if ($("#coverImage").css("display") == "none")
19 displaySideBar(false);
20 }
21
22 return;
23}
24
25function displayBookCover(checkbox)
26{
27 if (checkbox.checked == true)
28 {
29 console.log("Show the book cover!");
30 displaySideBar(true);
31 $("#coverImage").css("display", "block");
32 }
33 else
34 {
35 console.log("Hide the book cover!");
36 $("#coverImage").css("display", "none");
37 if ($("#tableOfContents").css("display") == "none")
38 displaySideBar(false);
39 }
40
41 return;
42}
43
44function displaySideBar(toggle)
45{
46 if (toggle == true)
47 {
48 console.log("Show the sidebar!");
49 $("#rightSidebar").css("display", "block");
50 }
51 else
52 {
53 console.log("Hide the sidebar!");
54 $("#rightSidebar").css("display", "none");
55 }
56
57 return;
58}
59
60function checkDocumentRadio()
61{
62 var selection = $('input[name="documentChanges"]'); //document.quiz.colour;
63
64 for (i=0; i<selection.length; i++)
65
66 if (selection[i].checked == true)
67 return selection[i].value;
68
69 return "this";
70}
71
72function saveDocumentChanges()
73{
74 console.log("Saving changes to "+checkDocumentRadio());
75 console.log("TOC="+$('input[name="TOC"]').attr('checked'));
76 console.log("Cover Image="+$('input[name="bookCover"]').attr('checked'));
77
78 var myurl = document.URL;
79
80 var collection_name = getSubstring(myurl, "&c", "&");
81 var document_id = getSubstring(myurl, "&d", "&");
82 var document_type = getSubstring(myurl, "&dt", "&");
83 var prev_action = getSubstring(myurl, "&p.a", "&");
84 var prev_service = getSubstring(myurl, "&p.s", "&");
85
86 var post_url = "http://localhost:8989/greenstone3/format?a=f&sa=saveDocument&c=" + collection_name + "&d=" + document_id + "&dt=" + document_type + "&p.a=" + prev_action + "&p.s=" + prev_service;
87
88 // XML will be automatically wrapped in <display><format> tags when saved to collection config
89 var xml = '<format><gsf:option name="TOC" value="'+$('input[name="TOC"]').attr('checked')+'"/><gsf:option name="coverImage" value="'+$('input[name="bookCover"]').attr('checked')+'"/></format>';
90
91 $.post(post_url, {data: xml}, function(data) {
92 console.log("Success, we have received data");
93 }, 'xml');
94}
95
96/* FUNCTIONS FOR FORMAT EDITING */
97
98function onTextChange(item, text)
99{
100 console.log("I have set "+item+".value to "+text);
101 item.setAttribute("value",text);
102}
103
104function onSelectChange(item)
105{
106 console.log("I have set "+item.value+".selected to selected");
107 for (var i=0; i<item.options.length; i++)
108 {
109 if(item.selectedIndex == i)
110 item.options[i].setAttribute("selected", "selected");
111 else
112 item.options[i].removeAttribute("selected");
113 }
114 //item.options[item.selectedIndex].selected = "selected";
115 //item.setAttribute("selected","selected");
116}
117
118function getSubstring(str, first, last)
119{
120 var first_index = str.indexOf(first)+first.length+1;
121 var last_index = str.indexOf(last, first_index);
122
123 if(last_index == -1)
124 last_index = str.length;
125
126 var substring = str.substring(first_index, last_index);
127
128 console.log(substring);
129
130 return substring;
131}
132
133function getFormatStatement()
134{
135 var formatDiv = document.getElementById('formatStatement');
136 var formatStatement = innerXHTML(formatDiv);
137 return formatStatement;
138}
139
140function checkClassifierRadio()
141{
142 var selection = $('input[name="classifiers"]'); //document.quiz.colour;
143
144 for (i=0; i<selection.length; i++)
145
146 if (selection[i].checked == true)
147 return selection[i].value;
148
149 return "this";
150
151}
152
153function updateFormatStatement()
154{
155 var formatStatement = getFormatStatement();
156
157 var thisOrAll = checkClassifierRadio();
158 console.log(thisOrAll);
159 var myurl = document.URL;
160
161 var collection_name = getSubstring(myurl, "&c", "&");
162 var service_name = getSubstring(myurl, "&s", "&");
163
164 if(thisOrAll == "all")
165 service_name = "AllClassifierBrowse";
166
167 var classifier_name = null;
168
169 if(service_name == "ClassifierBrowse")
170 classifier_name = getSubstring(myurl, "&cl", "&");
171
172 var post_url = "http://localhost:8989/greenstone3/format?a=f&sa=update&c=" + collection_name +"&s=" + service_name;
173
174 if(classifier_name != null)
175 post_url = post_url + "&cl=" + classifier_name;
176
177 $.post(post_url, {data: formatStatement}, function(data) {
178 //$('.result').innerHTML = data; //html(data);
179
180 // An error is returned because there is no valid XSLT for a format update action, there probably shouldn't be one so we ignore what the post returns.
181 console.log("Successfully updated");
182 //console.log(data);
183 }, 'html');
184}
185
186function saveFormatStatement()
187{
188 var formatStatement = getFormatStatement();
189 var thisOrAll = checkClassifierRadio();
190
191 var myurl = document.URL;
192
193 var collection_name = getSubstring(myurl, "&c", "&");
194 var service_name = getSubstring(myurl, "&s", "&");
195 var classifier_name = null;
196
197 if(thisOrAll == "all")
198 service_name = "AllClassifierBrowse";
199
200 if(service_name == "ClassifierBrowse")
201 classifier_name = getSubstring(myurl, "&cl", "&");
202
203 var post_url = "http://localhost:8989/greenstone3/format?a=f&sa=save&c=" + collection_name +"&s=" + service_name;
204
205 if(classifier_name != null)
206 post_url = post_url + "&cl=" + classifier_name;
207
208 $.post(post_url, {data: formatStatement}, function(data) {
209 // An error is returned because there is no valid XSLT for a format update action, there probably shouldn't be one so we ignore what the post returns.
210 console.log("Successfully saved");
211 }, 'html');
212}
213
214function getXSLT(classname)
215{
216 var myurl = document.URL;
217
218 var collection_name = getSubstring(myurl, "&c", "&");
219 var document_id = getSubstring(myurl, "&d", "&");
220 var document_type = getSubstring(myurl, "&dt", "&");
221 var prev_action = getSubstring(myurl, "&p.a", "&");
222 var prev_service = getSubstring(myurl, "&p.s", "&");
223
224 var post_url = "http://localhost:8989/greenstone3/format?a=d&c=" + collection_name + "&d=" + document_id + "&dt=" + document_type + "&p.a=" + prev_action + "&p.s=" + prev_service + "&o=skinandlib";
225
226 $.post(post_url, {data: classname}, function(data) {
227 console.log("Success, we have received data");
228 classname = "." + classname;
229 console.log(classname);
230 var content = $( data ).find(classname);
231 console.log(content.xml());
232 $("#XSLTcode").val(content.xml());
233 }, 'xml');
234}
235
236function traverse(node, formatstring)
237 {
238
239 if(node.nodeName=='DIV')
240 {
241 console.log("Found a div" +node.nodeName+","+node.nodeType);
242 formatstring = formatstring + find_class(node);
243 console.log(formatstring);
244 }
245
246 var children = $(node).children();
247 for(var i=0; i < children.length; i++)
248 formatstring = formatstring + traverse(children[i], formatstring);
249
250 return formatstring;
251 }
252
253function find_class(current)
254{
255 var classes = current.className.split(' ');
256 var none = "";
257 for(var i = 0; i < classes.length; i++)
258 {
259 switch(classes[i])
260 {
261 case 'gsf_template':
262 return create_gsf_template(current);
263 default:
264 {
265 console.log("Class not found");
266 return none;
267 }
268 }
269 }
270}
271
272function create_gsf_template(current)
273{
274 // find match text which is an input with class match
275 var match = $(current).find('.match')[0].value;
276 console.log(match);
277
278 // find mode text which is an input with class mode
279 var mode = $(current).find('.mode')[0].value;
280 console.log(mode);
281
282 // "<gsf:template match=\"classifierNode\" mode=\"horizontal\">"
283 var gsf = "<gsf:template match=\"" + match + "\"";
284 if(mode != "vertical")
285 gsf = gsf + " mode=\"" + mode + "\"";
286 gsf = gsf + ">";
287
288 return gsf;
289
290}
291
292/*
293$("#iframe").ready(function(){
294 console.log("iframe is ready ...");
295 var iframe = document.getElementById('iframe');
296 var iframe_document = iframe.document;
297
298 if(iframe.contentDocument)
299 {
300 iframe_document = iframe.contentDocument; // For NS6
301 console.log("Chose content document");
302 }
303 else if(iframe.contentWindow)
304 {
305 iframe_document = iframe.contentWindow.document; // For IE5.5 and IE6
306 console.log("Chose content window");
307 }
308
309 console.log(iframe_document.documentElement.innerHTML); //document.documentElement.outerHTML
310
311 $(iframe_document.documentElement.innerHTML).find('a').each(function() {
312 console.log("data "+$(this).data('href'));
313 console.log("getAttribute "+$(this).getAttribute('href'));
314 console.log("attr "+$(this).attr('href'));
315 console.log("this.href "+this.href);
316 var original = this.href; //$(this).attr('href');
317 // check if greenstone link ie. starts with format
318 //var original = $(this).data('href');
319 var modified = original.toString().concat("&excerptid=gs_content");
320 console.log(modified);
321 this.href = modified;
322 //$(this).attr('href',modified);
323 //$(this).data('href', modified);
324 //console.log($(this).attr('href'));
325 console.log("data "+$(this).data('href'));
326 console.log("getAttribute "+$(this).getAttribute('href'));
327 console.log("attr "+$(this).attr('href'));
328 console.log("this.href "+this.href);
329 console.log("**********");
330
331 });
332}); */
333/*
334function loadXMLDoc()
335{
336 if (window.XMLHttpRequest)
337 {// code for IE7+, Firefox, Chrome, Opera, Safari
338 xmlhttp=new XMLHttpRequest();
339 }
340 else
341 {// code for IE6, IE5
342 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
343 }
344
345 xmlhttp.onreadystatechange=function()
346 {
347 console.log("state changed to " + xmlhttp.readyState);
348 console.log("status is " + xmlhttp.status);
349 if (xmlhttp.readyState==4 && xmlhttp.status==200)
350 {
351 console.log("get code");
352 console.log(xmlhttp.responseText);
353 document.getElementById("gs_content").innerHTML=xmlhttp.responseText;
354 }
355 }
356
357 xmlhttp.open("GET","http://localhost:8080/greenstone3/format?a=b&rt=s&s=ClassifierBrowse&c=simpleht&cl=CL1&excerptid=gs_content",true);
358 //xmlhttp.open("GET","http://www.cs.waikato.ac.nz",true);
359 //xmlhttp.open("GET","http://wand.net.nz/~sjb48/index.html",true);
360 xmlhttp.send();
361}
362*/
363$(document).ready(function(){
364
365 console.log("Document ready function\n");
366
367 var CURRENT_SELECT_VALUE = "";
368
369 /* DOCUMENT SPECIFIC FUNCTIONS */
370
371 $('.sectionHeader').click(function () {
372 console.log('section Header click *');
373 getXSLT("sectionHeader");
374 return false; //don't event bubble
375 });
376
377 $('.sectionContainer').click(function () {
378 console.log('section Container click *');
379 getXSLT("sectionContainer");
380 return false; // don't event bubble
381 });
382
383 var collection = "";
384
385 var regex = new RegExp("[?&]c=");
386 var matches = regex.exec(document.URL);
387 if(matches != null)
388 {
389 var startIndex = matches.index;
390 var endIndex = document.URL.indexOf("&", startIndex + 1);
391
392 if(endIndex == -1)
393 {
394 endIndex = document.URL.length;
395 }
396
397 collection = document.URL.substring(startIndex, endIndex);
398 }
399
400 //Retrieve the collection metadataset using ajax
401 $.ajax
402 ({
403 type: "GET",
404 url: "?a=g&s=CoverageMetadataRetrieve&o=xml&rt=r&c=" + collection,
405 success: function(data)
406 {
407 var str = "<select name=\"meta_select\" onChange=\"onSelectChange(this)\">";
408
409 var selectorArea = document.getElementById("metadataSelector");
410 var metadataSets = data.getElementsByTagName("metadataSet");
411 for(var i = 0; i < metadataSets.length; i++)
412 {
413 var metadata = metadataSets[i].getElementsByTagName("metadata");
414 for(var j = 0; j < metadata.length; j++)
415 {
416 var metaValue = metadataSets[i].getAttribute("name") + "." + metadata[j].getAttribute("name");
417 str += "<option value=\"" + metaValue + "\">" + metaValue + "</option>";
418 }
419 }
420
421 str += "</select>";
422
423 selectorArea.innerHTML = str;
424 gsf_metadata_element = str;
425 }
426 });
427
428 $.ui.sortable.prototype._removeCurrentsFromItems = function() {
429 //console.log("IN _removeCurrentsFromItems FUNCTION");
430 //console.log("this = " + this.currentItem[0].getAttribute('class'));
431 var list = this.currentItem.find(":data(sortable-item)");
432
433 var i = 0;
434 while (i<this.items.length) {
435 var found_match = false;
436 for (var j=0; j<list.length; j++) {
437 if(this.items[i])
438 {
439 if(list[j] == this.items[i].item[0]) {
440 //console.log("Item to splice = " + this.items[i].item[0].getAttribute('class'));
441 this.items.splice(i,1);
442 found_match = true;
443 break;
444 }
445 }
446 };
447 if (!found_match)
448 i++;
449 else
450 break;
451 }
452 };
453
454 $.ui.plugin.add("draggable", "connectToSortable", {
455 start: function(event, ui) {
456 //console.log("FUNCTION start draggable connectToSortable");
457 var inst = $(this).data("draggable"), o = inst.options,
458 uiSortable = $.extend({}, ui, { item: inst.element });
459 inst.sortables = [];
460 $(o.connectToSortable).each(function() {
461 var sortable = $.data(this, 'sortable');
462 if (sortable && !sortable.options.disabled) {
463 inst.sortables.push({
464 instance: sortable,
465 shouldRevert: sortable.options.revert
466 });
467 sortable._refreshItems(); //Do a one-time refresh at start to refresh the containerCache
468 sortable._trigger("activate", event, uiSortable);
469 }
470 });
471
472 },
473 stop: function(event, ui) {
474
475 //console.log("FUNCTION stop draggable connectToSortable");
476 //If we are still over the sortable, we fake the stop event of the sortable, but also remove helper
477 var inst = $(this).data("draggable"),
478 uiSortable = $.extend({}, ui, { item: inst.element });
479
480 $.each(inst.sortables, function() {
481 if(this.instance.isOver) {
482
483 this.instance.isOver = 0;
484
485 inst.cancelHelperRemoval = true; //Don't remove the helper in the draggable instance
486 this.instance.cancelHelperRemoval = false; //Remove it in the sortable instance (so sortable plugins like revert still work)
487
488 //The sortable revert is supported, and we have to set a temporary dropped variable on the draggable to support revert: 'valid/invalid'
489 if(this.shouldRevert) this.instance.options.revert = true;
490
491 //Trigger the stop of the sortable
492 //console.log("Draggable tells sortable to stop");
493 this.instance._mouseStop(event);
494
495 this.instance.options.helper = this.instance.options._helper;
496
497 //If the helper has been the original item, restore properties in the sortable
498 if(inst.options.helper == 'original')
499 this.instance.currentItem.css({ top: 'auto', left: 'auto' });
500
501 } else {
502 this.instance.cancelHelperRemoval = false; //Remove the helper in the sortable instance
503 this.instance._trigger("deactivate", event, uiSortable);
504 }
505
506 });
507
508 },
509 drag: function(event, ui) {
510 //console.log("FUNCTION drag draggable connectToSortable");
511
512 var inst = $(this).data("draggable"), self = this;
513
514 var checkPos = function(o) {
515 var dyClick = this.offset.click.top, dxClick = this.offset.click.left;
516 var helperTop = this.positionAbs.top, helperLeft = this.positionAbs.left;
517 var itemHeight = o.height, itemWidth = o.width;
518 var itemTop = o.top, itemLeft = o.left;
519
520 return $.ui.isOver(helperTop + dyClick, helperLeft + dxClick, itemTop, itemLeft, itemHeight, itemWidth);
521 };
522
523 var intersecting_items = new Array();
524
525 $.each(inst.sortables, function(i) {
526
527 //Copy over some variables to allow calling the sortable's native _intersectsWith
528 this.instance.positionAbs = inst.positionAbs;
529 this.instance.helperProportions = inst.helperProportions;
530 this.instance.offset.click = inst.offset.click;
531
532 if(this.instance._intersectsWith(this.instance.containerCache)) {
533
534 //If it intersects, we use a little isOver variable and set it once, so our move-in stuff gets fired only once
535 //if(!this.instance.isOver) {
536
537 //console.log('Line 1113');
538
539 // this.instance.isOver = 1;
540
541 intersecting_items.push(this.instance); // sam
542 //} //sam
543
544 //Now we fake the start of dragging for the sortable instance,
545 //by cloning the list group item, appending it to the sortable and using it as inst.currentItem
546 //We can then fire the start event of the sortable with our passed browser event, and our own helper (so it doesn't create a new one)
547 //sam this.instance.currentItem = $(self).clone().appendTo(this.instance.element).data("sortable-item", true);
548 //sam this.instance.options._helper = this.instance.options.helper; //Store helper option to later restore it
549 //sam this.instance.options.helper = function() { return ui.helper[0]; };
550
551 //sam event.target = this.instance.currentItem[0];
552 //sam this.instance._mouseCapture(event, true);
553 //sam this.instance._mouseStart(event, true, true);
554
555 //Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
556 //sam this.instance.offset.click.top = inst.offset.click.top;
557 //sam this.instance.offset.click.left = inst.offset.click.left;
558 //sam this.instance.offset.parent.left -= inst.offset.parent.left - this.instance.offset.parent.left;
559 //sam this.instance.offset.parent.top -= inst.offset.parent.top - this.instance.offset.parent.top;
560
561 //sam inst._trigger("toSortable", event);
562 //sam inst.dropped = this.instance.element; //draggable revert needs that
563 //hack so receive/update callbacks work (mostly)
564 //sam inst.currentItem = inst.element;
565 //sam this.instance.fromOutside = inst;
566
567 //sam brace
568
569 //Provided we did all the previous steps, we can fire the drag event of the sortable on every draggable drag, when it intersects with the sortable
570 //sam if(this.instance.currentItem) this.instance._mouseDrag(event);
571
572 } else {
573
574 //If it doesn't intersect with the sortable, and it intersected before,
575 //we fake the drag stop of the sortable, but make sure it doesn't remove the helper by using cancelHelperRemoval
576 if(this.instance.isOver) {
577
578 console.log("UNSETTING ISOVER");
579 console.log("ON ITEM="+this.instance.currentItem[0].getAttribute('class'))
580 this.instance.isOver = 0;
581 this.instance.cancelHelperRemoval = true;
582
583 //Prevent reverting on this forced stop
584 this.instance.options.revert = false;
585
586 // The out event needs to be triggered independently
587 this.instance._trigger('out', event, this.instance._uiHash(this.instance));
588
589 this.instance._mouseStop(event, true);
590 this.instance.options.helper = this.instance.options._helper;
591
592 //Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
593 //console.log("DO WE GET HERE?");
594 this.instance.currentItem.remove();
595 if(this.instance.placeholder) this.instance.placeholder.remove();
596
597 inst._trigger("fromSortable", event);
598 inst.dropped = false; //draggable revert needs that
599
600 }
601
602 }
603 });
604
605 //sam
606 //console.log("Contents of intersecting_items");
607 var innermostContainer = null, innermostIndex = null;
608 for (i=0;i<intersecting_items.length;i++)
609 {
610 //console.log('ITEM: '+intersecting_items[i].element[0].getAttribute('class'));
611
612 if(innermostContainer && $.ui.contains(intersecting_items[i].element[0], innermostContainer.element[0]))
613 continue;
614
615 innermostContainer = intersecting_items[i];
616 innermostIndex = i;
617
618 }
619
620 for (i=0;i<intersecting_items.length;i++)
621 {
622 if(intersecting_items[i] != innermostContainer)
623 if(intersecting_items[i].isOver) {
624
625 console.log("UNSETTING ISOVER");
626 console.log("ON ITEM="+intersecting_items[i].currentItem[0].getAttribute('class'))
627 intersecting_items[i].isOver = 0;
628 intersecting_items[i].cancelHelperRemoval = true;
629
630 //Prevent reverting on this forced stop
631 intersecting_items[i].options.revert = false;
632
633 // The out event needs to be triggered independently
634 intersecting_items[i]._trigger('out', event, intersecting_items[i]._uiHash(intersecting_items[i]));
635
636 intersecting_items[i]._mouseStop(event, true);
637 intersecting_items[i].options.helper = intersecting_items[i].options._helper;
638
639 //Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
640 //console.log("DO WE GET HERE?");
641 if(intersecting_items[i].currentItem) intersecting_items[i].currentItem.remove();
642 if(intersecting_items[i].placeholder) intersecting_items[i].placeholder.remove();
643
644 inst._trigger("fromSortable", event);
645 inst.dropped = false; //draggable revert needs that
646 }
647
648 intersecting_items[i].isOver = 0;
649
650 }
651
652 if(innermostContainer && !innermostContainer.isOver)
653 {
654 console.log("INNER="+innermostContainer.element[0].getAttribute('class'));
655 console.log("SETTING ISOVER");
656 innermostContainer.isOver = 1;
657
658 //Now we fake the start of dragging for the sortable instance,
659 //by cloning the list group item, appending it to the sortable and using it as inst.currentItem
660 //We can then fire the start event of the sortable with our passed browser event, and our own helper (so it doesn't create a new one)
661
662 if(innermostContainer.currentItem) innermostContainer.currentItem.remove();
663 if(innermostContainer.placeholder) innermostContainer.placeholder.remove();
664
665 innermostContainer.currentItem = $(self).clone().appendTo(innermostContainer.element).data("sortable-item", true);
666
667 innermostContainer.options._helper = innermostContainer.options.helper; //Store helper option to later restore it
668 innermostContainer.options.helper = function() { return ui.helper[0]; };
669
670 console.log("EVENT TARGET="+innermostContainer.currentItem[0].getAttribute('class'));
671 event.target = innermostContainer.currentItem[0];
672 innermostContainer._mouseCapture(event, true);
673 innermostContainer._mouseStart(event, true, true);
674
675 //Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
676 innermostContainer.offset.click.top = inst.offset.click.top;
677 innermostContainer.offset.click.left = inst.offset.click.left;
678 innermostContainer.offset.parent.left -= inst.offset.parent.left - innermostContainer.offset.parent.left;
679 innermostContainer.offset.parent.top -= inst.offset.parent.top - innermostContainer.offset.parent.top;
680
681 inst._trigger("toSortable", event);
682 inst.dropped = innermostContainer.element; //draggable revert needs that
683 //hack so receive/update callbacks work (mostly)
684 inst.currentItem = inst.element;
685 innermostContainer.fromOutside = inst;
686
687 //sam brace
688 }
689
690 if(innermostContainer)
691 {
692 //Provided we did all the previous steps, we can fire the drag event of the sortable on every draggable drag, when it intersects with the sortable
693 if(innermostContainer.currentItem) innermostContainer._mouseDrag(event);
694 }
695
696 }
697});
698
699 /*
700 var iframe = document.getElementById('iframe');
701 var iframe_document = iframe.document;
702
703 if(iframe.contentDocument)
704 {
705 iframe_document = iframe.contentDocument; // For NS6
706 console.log("Chose content document");
707 }
708 else if(iframe.contentWindow)
709 {
710 iframe_document = iframe.contentWindow.document; // For IE5.5 and IE6
711 console.log("Chose content window");
712 }
713
714 // Edit the hrefs in preview_html - search for <a href="format?a=b&rt=r&s=ClassifierBrowse&c=simpleht&cl=CL1.2">
715 var start_index = 0;
716 var end_index = 0;
717 while(start_index != -1)
718 {
719 start_index = preview_html.indexOf("href=\"format", start_index);
720 console.log("start index = " + start_index);
721 if(start_index != -1)
722 {
723 end_index = preview_html.indexOf("\">", start_index);
724 console.log("end_index = " + end_index);
725 a = preview_html.substring(0,end_index);
726 b = preview_html.substring(end_index);
727 preview_html = a.concat("&excerptid=results", b);
728 console.log(preview_html);
729 start_index = end_index + "&excerptid=results\">".length;
730 }
731 }
732
733 // Split the html code in to three parts
734 var first_index = preview_html.indexOf("<ul id=\"results\"");
735 console.log("First index is "+first_index);
736 var second_index = preview_html.indexOf("</ul>", first_index) + "</ul>".length;
737 console.log("Second index is "+second_index);
738
739 var first_half = preview_html.substring(0, first_index);
740 var iframe_code = preview_html.substring(first_index, second_index);
741 var second_half = preview_html.substring(second_index);
742
743 //$('#my_categories').innerHTML = first_half.concat(second_half);
744 document.getElementById("my_categories").innerHTML = first_half.concat(second_half);
745
746 // Put the content in the iframe
747 if(initialised_iframe == "false")
748 {
749 console.log("Initialised iframe with preview html");
750 console.log(preview_html);
751 iframe_document.open();
752 iframe_document.writeln(iframe_code); //.concat("&excerptid=gs_content"));
753 iframe_document.close();
754 initialised_iframe = "true";
755 }
756 */
757
758 /*
759 $(iframe_document.documentElement.innerHTML).find('a').each(function() {
760 console.log("data "+$(this).data('href'));
761 console.log("getAttribute "+this.getAttribute('href'));
762 console.log("attr "+$(this).attr('href'));
763 console.log("this.href "+this.href);
764 var original = this.href; //$(this).attr('href');
765 // check if greenstone link ie. starts with format
766 //var original = $(this).data('href');
767 var modified = original.toString().concat("&excerptid=gs_content");
768 console.log("* *");
769 console.log(modified);
770 console.log("* *");
771 //this.href = modified;
772 //$(this).attr('href',modified);
773 //$(this).data('href', modified);
774 $(this).attr({ 'href': modified });
775 //console.log($(this).attr('href'));
776 console.log("data "+$(this).data('href'));
777 console.log("getAttribute "+this.getAttribute('href'));
778 console.log("attr "+$(this).attr('href'));
779 console.log("this.href "+this.href);
780 console.log("***********************");
781 });
782 */
783
784 /******************************************/
785 /* DRAGGABLES */
786 /******************************************/
787
788 $(".draggable_gsf_template").draggable({
789 cursor: 'crosshair',
790 connectToSortable: '#formatStatement',
791 helper: 'clone',
792 revert: 'invalid'
793 });
794
795 $(".draggable_table").draggable({
796 cursor: 'crosshair',
797 connectToSortable: '.gsf_template',
798 helper: 'clone',
799 revert: 'invalid'
800 });
801
802 $(".draggable_tr").draggable({
803 cursor: 'crosshair',
804 connectToSortable: '.gsf_table',
805 helper: 'clone',
806 revert: 'invalid'
807 })
808
809 $(".draggable_td").draggable({
810 cursor: 'crosshair',
811 //connectToSortable: '.td',
812 helper: 'clone',
813 revert: 'invalid'
814 })
815
816 $(".draggable_gsf_text").draggable({
817 cursor: 'crosshair',
818 connectToSortable: '.td-div, .gsf_when, .gsf_otherwise, .gsf_link, .gsf_choose_metadata, .gsf_default',
819 helper: 'clone',
820 revert: 'invalid'
821 });
822
823 $(".draggable_gsf_choose_metadata").draggable({
824 cursor: 'crosshair',
825 connectToSortable: '.td-div, .gsf_link, .gsf_when, .gsf_otherwise',
826 helper: 'clone',
827 revert: 'invalid'
828 });
829
830 //$(".element_type_gsf_metadata").draggable({
831 $(".draggable_gsf_metadata").draggable({
832 cursor: 'crosshair',
833 connectToSortable: '.gsf_choose_metadata, .gsf_when, .gsf_otherwise, .gsf_link, .td-div',
834 helper: 'clone',
835 revert: 'invalid'
836 });
837
838 $(".draggable_gsf_link").draggable({
839 cursor: 'crosshair',
840 connectToSortable: '.td-div, .gsf_when, .gsf_otherwise, .gsf_link',
841 helper: 'clone',
842 revert: 'invalid'
843 });
844
845 // switch, when, otherwise, icon
846 $(".draggable_gsf_switch").draggable({
847 cursor: 'crosshair',
848 connectToSortable: '.td-div, .gsf_link',
849 helper: 'clone',
850 revert: 'invalid'
851 });
852
853 $(".draggable_gsf_when").draggable({
854 cursor: 'crosshair',
855 connectToSortable: '.gsf_switch',
856 helper: 'clone',
857 revert: 'invalid'
858 });
859
860 $(".draggable_gsf_otherwise").draggable({
861 cursor: 'crosshair',
862 connectToSortable: '.gsf_switch',
863 helper: 'clone',
864 revert: 'invalid'
865 });
866
867 $(".draggable_gsf_icon").draggable({
868 cursor: 'crosshair',
869 connectToSortable: '.td-div, .gsf_link, .gsf_choose, .gsf_when, .gsf_otherwise',
870 helper: 'clone',
871 revert: 'invalid'
872 });
873
874
875 /******************************************/
876 /* SORTABLES */
877 /******************************************/
878
879 bind_td_sortable();
880 bind_all_sortables();
881
882 $('#formatStatement').sortable({
883 cursor: 'pointer',
884 tolerance: 'pointer',
885 items: '.gsf_template',
886 placeholder:'placeholder',
887 //'nested':'div'
888 stop: function(event, ui) {
889 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('css_gsf_template')) { replace_with(ui.item,gsf_template_element); }
890 }
891 });
892
893 $('div.gsf_template').children(".block,.table").slideUp(300);
894});
895
896/*******************************************************************************/
897
898function bind_all_sortables()
899{
900 console.log('function bind_all_sortables()');
901 bind_template_sortable();
902 bind_choose_metadata_sortable();
903 bind_link_sortable();
904 bind_switch_sortable();
905 bind_when_sortable();
906 bind_otherwise_sortable();
907
908 bind_tables();
909
910 bind_block_mouseover();
911 bind_minmax_remove();
912}
913
914function bind_tables()
915{
916 console.log('function bind_tables()');
917
918 $('#sectionHeader').click(function () {
919 console.log('section Header click *');
920 return true;
921 });
922
923 $('#sectionContainer').click(function () {
924 console.log('section Container click *');
925 return true;
926 });
927
928 $(".td-div").resizable({
929 alsoResize: 'parent',
930 handles: 'w,e',
931 stop: function(event, ui) {
932 console.log('Resize table on stop');
933 resize_tables($(this));
934 }, });
935
936 $(".droppable").droppable({
937 accept: '.element_type_td',
938 tolerance: 'pointer',
939 activate: function(event, ui) { $(this).addClass("droppable_hl");},
940 deactivate: function(event, ui) { $(this).removeClass("droppable_hl"); },
941 drop: function(event, ui) {
942 var neverempty = document.createElement("div");
943 neverempty.setAttribute("class","neverempty block");
944 neverempty.setAttribute("style","height:50px");
945 neverempty.setAttribute("style","display:block");
946 var text = document.createTextNode('NEVER EMPTY');
947 neverempty.appendChild(text);
948 var td = document.createElement("td");
949 var div = document.createElement("div");
950 div.setAttribute("title","td-div");
951 div.setAttribute("class","td-div block");
952 div.setAttribute("style","margin-left:0px");
953 div.setAttribute("width","25px");
954 td.appendChild(div);
955 div.appendChild(neverempty);
956 var sep = document.createElement("td");
957 sep.setAttribute("class","droppable");
958 sep.setAttribute("width","10px");
959 $(this).after(sep);
960 $(this).after(td);
961 bind_tables();
962 resize_tables($(this));
963 bind_td_sortable();
964 bind_block_mouseover();
965 }
966 });
967
968}
969
970function replace_with(item, me)
971{
972 // Search me for select
973 if(me.search("select") != -1)
974 {
975 // If select exists, then find CURRENT_SELECT_VALUE
976 var index = me.search(CURRENT_SELECT_VALUE);
977 if(index == -1)
978 console.log("Did not find " + CURRENT_SELECT_VALUE);
979 else
980 console.log("Found " + CURRENT_SELECT_VALUE + " at index " + index);
981 index = index + CURRENT_SELECT_VALUE.length + 1;
982 console.log("Attempt inserting select at new index "+index);
983 a = me.substring(0,index);
984 b = me.substring(index);
985 me = a.concat("selected",b);
986 }
987
988 item.replaceWith(me); //'<div class="element element-txt">This text box has been added!</div>');
989
990 resize_tables(item);
991
992 bind_all_sortables();
993}
994
995function resize_tables(item)
996{
997 var max_height = 0;
998 (item.parents('.table')).each(function(index) {
999 $(this).children().children().children().each(function() {
1000 var sum_one = 0;
1001 var sum_two = 0;
1002 $(this).children('.block').each(function() { sum_one = sum_one + $(this).height();
1003 $(this).children('.block').each(function() { sum_two = sum_two + $(this).height(); } );
1004 console.log("My height is " + $(this).height() + ", sum height 2 is " + sum_two);
1005 });
1006 console.log("My height is " + $(this).height() + ", sum height 1 is " + sum_one);
1007 if (sum_two > max_height)
1008 max_height = sum_two;
1009 });
1010 });
1011 equalHeights(item,max_height);
1012}
1013
1014function bind_template_sortable()
1015{
1016 //console.log('function bind_template_sortable()');
1017 $('.gsf_template').sortable({
1018 'cursor':'pointer',
1019 'tolerance': 'pointer',
1020 'items':'.table', //.gsf_choose_metadata, .gsf_metadata',
1021 'placeholder':'placeholder',
1022 //'nested':'.gsf:metadata'
1023 stop: function(event, ui) {
1024 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_table')) { replace_with(ui.item, "<table class=\"table\" border=\"2\"></table>"); }
1025 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose_metadata')) { replace_with(ui.item, gsf_choose_metadata_element); }
1026 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1027 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1028 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_switch')) { replace_with(ui.item, gsf_switch_element); }
1029 }
1030 });
1031
1032 $('.gsf_template').click(function () {
1033 console.log('gsf_template class click');
1034 return false;
1035 });
1036
1037}
1038
1039function bind_td_sortable()
1040{
1041
1042 $('tr').sortable({
1043 'cursor':'pointer',
1044 'tolerance': 'intersect',
1045 'items':'.column',
1046 'placeholder':'placeholder_td',
1047 'connectWith':'column'});
1048
1049 $('.td-div').sortable({
1050 'cursor':'pointer',
1051 'tolerance': 'pointer',
1052 'items':'.gsf_metadata, .gsf_choose_metadata, .gsf_link, .gsf_switch',
1053 'placeholder':'placeholder',
1054 receive: function(event, ui) { alert("Attempted to receive"); },
1055 stop: function(event, ui) {
1056 // gsf metadata
1057 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1058 // gsf choose metadata
1059 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose_metadata')) { replace_with(ui.item, gsf_choose_metadata_element); }
1060 // gsf link
1061 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1062 // gsf switch
1063 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_switch')) { replace_with(ui.item, gsf_switch_element); }
1064
1065 }
1066
1067 });
1068
1069 $('.td-div').click(function () {
1070 //console.log('td class click');
1071 return true;
1072 });
1073
1074
1075
1076}
1077
1078
1079function bind_choose_metadata_sortable()
1080{
1081 //console.log('function bind_choose_metadata_sortable()');
1082 $('.gsf_choose_metadata').sortable({
1083 'cursor':'pointer',
1084 'tolerance': 'pointer',
1085 'items':'.gsf_metadata, .gsf_text, .gsf_default',
1086 'placeholder':'placeholder',
1087 'connectWith':'.gsf_choose_metadata',
1088 //'nested':'.gsf:metadata'
1089 stop: function(event, ui) {
1090 // gsf metadata
1091 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1092 // gsf text
1093 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_text')) { replace_with(ui.item, gsf_text_element); }
1094 // gsf default
1095 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_default')) { replace_with(ui.item, gsf_default_element); }
1096
1097 bind_all_sortables();
1098 }
1099 });
1100}
1101
1102function bind_link_sortable()
1103{
1104 //console.log('function bind_link_sortable()');
1105 $('.gsf_link').sortable({
1106 'cursor':'pointer',
1107 'tolerance': 'pointer',
1108 'items':'.leaf, .gsf_link, .gsf_switch, .gsf_choose',
1109 'placeholder':'placeholder',
1110 'connectWith':'.gsf_link',
1111 //'nested':'.gsf:metadata'
1112 stop: function(event, ui) {
1113 // gsf icon
1114 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_icon')) { replace_with(ui.item, gsf_icon_element); }
1115 // gsf text
1116 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_text')) { replace_with(ui.item, "<input type=\"text\" name=\"rawtextinput\" size=\"10\"/>"); }
1117 // gsf metadata
1118 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1119 // gsf link
1120 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1121 // gsf switch
1122 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_switch')) { replace_with(ui.item, gsf_switch_element); }
1123 // gsf choose
1124 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose_metadata')) { replace_with(ui.item, gsf_choose_metadata_element); }
1125 }
1126 });
1127}
1128
1129function bind_switch_sortable()
1130{
1131 //console.log('function bind_switch_sortable()');
1132 $('.gsf_switch').sortable({
1133 'cursor':'pointer',
1134 'tolerance': 'pointer',
1135 'items':'.gsf_metadata, .gsf_when, .gsf_otherwise, .gsf_text',
1136 'placeholder':'placeholder',
1137 'connectWith':'.gsf_switch',
1138 //'nested':'.gsf:metadata'
1139 stop: function(event, ui) {
1140 // gsf when
1141 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_when')) { replace_with(ui.item, gsf_when_element); }
1142 // gsf otherwise
1143 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_otherwise')) { replace_with(ui.item, gsf_otherwise_element); }
1144 // gsf metadata
1145 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1146 // gsf text
1147 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_text')) { replace_with(ui.item, gsf_text_element); }
1148 }
1149 });
1150}
1151
1152function bind_when_sortable()
1153{
1154 //console.log('function bind_when_sortable()');
1155 $('.gsf_when').sortable({
1156 'cursor':'pointer',
1157 'tolerance': 'pointer',
1158 'items':'.leaf, .gsf_link, .gsf_choose',
1159 'placeholder':'placeholder',
1160 //'nested':'.gsf:metadata'
1161 stop: function(event, ui) {
1162 // gsf metadata
1163 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1164 // gsf icon
1165 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_icon')) { replace_with(ui.item, gsf_icon_element); }
1166 // gsf text
1167 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_text')) { replace_with(ui.item, gsf_text_element); }
1168 // gsf link
1169 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1170 // gsf choose
1171 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose')) { replace_with(ui.item, gsf_choose_element); }
1172 }
1173 });
1174}
1175
1176function bind_otherwise_sortable()
1177{
1178 //console.log('function bind_otherwise_sortable()');
1179 $('.gsf_otherwise').sortable({
1180 'cursor':'pointer',
1181 'tolerance': 'pointer',
1182 'items':'.leaf, .gsf_link, .gsf_choose',
1183 'placeholder':'placeholder',
1184 //'nested':'.gsf:metadata'
1185 stop: function(event, ui) {
1186 // gsf metadata
1187 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1188 // gsf text
1189 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_text')) { replace_with(ui.item, "<input type=\"text\" name=\"rawtextinput\" size=\"10\"/>"); }
1190 // gsf icon
1191 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_icon')) { replace_with(ui.item, gsf_icon_element); }
1192 // gsf link
1193 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1194 // gsf choose
1195 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose')) { replace_with(ui.item, gsf_choose_element); }
1196 }
1197 });
1198}
1199
1200function bind_block_mouseover()
1201{
1202 //console.log('function bind_block_mouseover()');
1203 $(".block").mouseover(function()
1204 {
1205 $(this).parents().css("border", "");
1206 $(this).css("border", "2px solid blue");
1207 return false;
1208 }).mouseout(function(){
1209 $(this).css("border", "");
1210 return false;
1211 });
1212}
1213
1214function bind_minmax_remove()
1215{
1216 console.log('function bind_minmax_remove()');
1217 $('a.minmax').bind('click', toggleContent);
1218 $('a.remove').bind('click', removeContent);
1219};
1220
1221var removeContent = (function () {
1222 //console.log('var removeContent = (function ()');
1223 // this -> a -> td -> tr -> tbody -> table -> div
1224 //$(this).parent().parent().parent().parent().parent().remove();
1225 $(this).closest(".block").remove();
1226 });
1227
1228
1229var toggleContent = function(e)
1230{
1231 console.log('parent: ' + $(this).html());
1232 if ($(this).html() == '[+]'){ //targetContent.css('display') == 'none') {
1233 $(this).closest(".block").children(".table, .block").slideDown(300);
1234 $(this).html('[-]');
1235 $(this).removeClass("ui-icon-plusthick");
1236 $(this).addClass("ui-icon-minusthick");
1237 } else {
1238 $(this).closest(".block").children(".table, .block").slideUp(300);
1239 $(this).html('[+]');
1240 $(this).removeClass("ui-icon-minusthick");
1241 $(this).addClass("ui-icon-plusthick");
1242 }
1243 return false;
1244};
1245
1246function serialize(s)
1247{
1248 serial = $.SortSerialize(s);
1249 alert(serial.hash);
1250};
1251
1252function equalHeights(item, height) {
1253 //console.log('function equalHeights(item, height)');
1254 (item.parents('.table')).each(function(index) {
1255 $(this).children().children().children().each(function() {
1256 $(this).height(height);
1257 $(this).children().height(height);
1258 });
1259 });
1260};
1261
Note: See TracBrowser for help on using the repository browser.