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

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

More fixes to the format manager

File size: 44.9 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.plugin.add("draggable", "connectToSortable", {
429 start: function(event, ui) {
430 //console.log("FUNCTION start draggable connectToSortable");
431 var inst = $(this).data("draggable"), o = inst.options,
432 uiSortable = $.extend({}, ui, { item: inst.element });
433 inst.sortables = [];
434 $(o.connectToSortable).each(function() {
435 var sortable = $.data(this, 'sortable');
436 if (sortable && !sortable.options.disabled) {
437 inst.sortables.push({
438 instance: sortable,
439 shouldRevert: sortable.options.revert
440 });
441 sortable._refreshItems(); //Do a one-time refresh at start to refresh the containerCache
442 sortable._trigger("activate", event, uiSortable);
443 }
444 });
445
446 },
447 stop: function(event, ui) {
448
449 //console.log("FUNCTION stop draggable connectToSortable");
450 //If we are still over the sortable, we fake the stop event of the sortable, but also remove helper
451 var inst = $(this).data("draggable"),
452 uiSortable = $.extend({}, ui, { item: inst.element });
453
454 $.each(inst.sortables, function() {
455 if(this.instance.isOver) {
456
457 this.instance.isOver = 0;
458
459 inst.cancelHelperRemoval = true; //Don't remove the helper in the draggable instance
460 this.instance.cancelHelperRemoval = false; //Remove it in the sortable instance (so sortable plugins like revert still work)
461
462 //The sortable revert is supported, and we have to set a temporary dropped variable on the draggable to support revert: 'valid/invalid'
463 if(this.shouldRevert) this.instance.options.revert = true;
464
465 //Trigger the stop of the sortable
466 //console.log("Draggable tells sortable to stop");
467 this.instance._mouseStop(event);
468
469 this.instance.options.helper = this.instance.options._helper;
470
471 //If the helper has been the original item, restore properties in the sortable
472 if(inst.options.helper == 'original')
473 this.instance.currentItem.css({ top: 'auto', left: 'auto' });
474
475 } else {
476 this.instance.cancelHelperRemoval = false; //Remove the helper in the sortable instance
477 this.instance._trigger("deactivate", event, uiSortable);
478 }
479
480 });
481
482 },
483 drag: function(event, ui) {
484 //console.log("FUNCTION drag draggable connectToSortable");
485
486 var inst = $(this).data("draggable"), self = this;
487
488 var checkPos = function(o) {
489 var dyClick = this.offset.click.top, dxClick = this.offset.click.left;
490 var helperTop = this.positionAbs.top, helperLeft = this.positionAbs.left;
491 var itemHeight = o.height, itemWidth = o.width;
492 var itemTop = o.top, itemLeft = o.left;
493
494 return $.ui.isOver(helperTop + dyClick, helperLeft + dxClick, itemTop, itemLeft, itemHeight, itemWidth);
495 };
496
497 var intersecting_items = new Array();
498
499 $.each(inst.sortables, function(i) {
500
501 //Copy over some variables to allow calling the sortable's native _intersectsWith
502 this.instance.positionAbs = inst.positionAbs;
503 this.instance.helperProportions = inst.helperProportions;
504 this.instance.offset.click = inst.offset.click;
505
506 if(this.instance._intersectsWith(this.instance.containerCache)) {
507
508 //If it intersects, we use a little isOver variable and set it once, so our move-in stuff gets fired only once
509 //if(!this.instance.isOver) {
510
511 //console.log('Line 1113');
512
513 // this.instance.isOver = 1;
514
515 intersecting_items.push(this.instance); // sam
516 //} //sam
517
518 //Now we fake the start of dragging for the sortable instance,
519 //by cloning the list group item, appending it to the sortable and using it as inst.currentItem
520 //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)
521 //sam this.instance.currentItem = $(self).clone().appendTo(this.instance.element).data("sortable-item", true);
522 //sam this.instance.options._helper = this.instance.options.helper; //Store helper option to later restore it
523 //sam this.instance.options.helper = function() { return ui.helper[0]; };
524
525 //sam event.target = this.instance.currentItem[0];
526 //sam this.instance._mouseCapture(event, true);
527 //sam this.instance._mouseStart(event, true, true);
528
529 //Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
530 //sam this.instance.offset.click.top = inst.offset.click.top;
531 //sam this.instance.offset.click.left = inst.offset.click.left;
532 //sam this.instance.offset.parent.left -= inst.offset.parent.left - this.instance.offset.parent.left;
533 //sam this.instance.offset.parent.top -= inst.offset.parent.top - this.instance.offset.parent.top;
534
535 //sam inst._trigger("toSortable", event);
536 //sam inst.dropped = this.instance.element; //draggable revert needs that
537 //hack so receive/update callbacks work (mostly)
538 //sam inst.currentItem = inst.element;
539 //sam this.instance.fromOutside = inst;
540
541 //sam brace
542
543 //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
544 //sam if(this.instance.currentItem) this.instance._mouseDrag(event);
545
546 } else {
547
548 //If it doesn't intersect with the sortable, and it intersected before,
549 //we fake the drag stop of the sortable, but make sure it doesn't remove the helper by using cancelHelperRemoval
550 if(this.instance.isOver) {
551
552 console.log("UNSETTING ISOVER");
553 console.log("ON ITEM="+this.instance.currentItem[0].getAttribute('class'))
554 this.instance.isOver = 0;
555 this.instance.cancelHelperRemoval = true;
556
557 //Prevent reverting on this forced stop
558 this.instance.options.revert = false;
559
560 // The out event needs to be triggered independently
561 this.instance._trigger('out', event, this.instance._uiHash(this.instance));
562
563 this.instance._mouseStop(event, true);
564 this.instance.options.helper = this.instance.options._helper;
565
566 //Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
567 //console.log("DO WE GET HERE?");
568 this.instance.currentItem.remove();
569 if(this.instance.placeholder) this.instance.placeholder.remove();
570
571 inst._trigger("fromSortable", event);
572 inst.dropped = false; //draggable revert needs that
573
574 }
575
576 }
577 });
578
579 //sam
580 //console.log("Contents of intersecting_items");
581 var innermostContainer = null, innermostIndex = null;
582 for (i=0;i<intersecting_items.length;i++)
583 {
584 //console.log('ITEM: '+intersecting_items[i].element[0].getAttribute('class'));
585
586 if(innermostContainer && $.ui.contains(intersecting_items[i].element[0], innermostContainer.element[0]))
587 continue;
588
589 innermostContainer = intersecting_items[i];
590 innermostIndex = i;
591
592 }
593
594 for (i=0;i<intersecting_items.length;i++)
595 {
596 if(intersecting_items[i] != innermostContainer)
597 if(intersecting_items[i].isOver) {
598
599 console.log("UNSETTING ISOVER");
600 console.log("ON ITEM="+intersecting_items[i].currentItem[0].getAttribute('class'))
601 intersecting_items[i].isOver = 0;
602 intersecting_items[i].cancelHelperRemoval = true;
603
604 //Prevent reverting on this forced stop
605 intersecting_items[i].options.revert = false;
606
607 // The out event needs to be triggered independently
608 intersecting_items[i]._trigger('out', event, intersecting_items[i]._uiHash(intersecting_items[i]));
609
610 intersecting_items[i]._mouseStop(event, true);
611 intersecting_items[i].options.helper = intersecting_items[i].options._helper;
612
613 //Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
614 //console.log("DO WE GET HERE?");
615 if(intersecting_items[i].currentItem) intersecting_items[i].currentItem.remove();
616 if(intersecting_items[i].placeholder) intersecting_items[i].placeholder.remove();
617
618 inst._trigger("fromSortable", event);
619 inst.dropped = false; //draggable revert needs that
620 }
621
622 intersecting_items[i].isOver = 0;
623
624 }
625
626 if(innermostContainer && !innermostContainer.isOver)
627 {
628 console.log("INNER="+innermostContainer.element[0].getAttribute('class'));
629 console.log("SETTING ISOVER");
630 innermostContainer.isOver = 1;
631
632 //Now we fake the start of dragging for the sortable instance,
633 //by cloning the list group item, appending it to the sortable and using it as inst.currentItem
634 //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)
635
636 if(innermostContainer.currentItem) innermostContainer.currentItem.remove();
637 if(innermostContainer.placeholder) innermostContainer.placeholder.remove();
638
639 innermostContainer.currentItem = $(self).clone().appendTo(innermostContainer.element).data("sortable-item", true);
640
641 innermostContainer.options._helper = innermostContainer.options.helper; //Store helper option to later restore it
642 innermostContainer.options.helper = function() { return ui.helper[0]; };
643
644 console.log("EVENT TARGET="+innermostContainer.currentItem[0].getAttribute('class'));
645 event.target = innermostContainer.currentItem[0];
646 innermostContainer._mouseCapture(event, true);
647 innermostContainer._mouseStart(event, true, true);
648
649 //Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
650 innermostContainer.offset.click.top = inst.offset.click.top;
651 innermostContainer.offset.click.left = inst.offset.click.left;
652 innermostContainer.offset.parent.left -= inst.offset.parent.left - innermostContainer.offset.parent.left;
653 innermostContainer.offset.parent.top -= inst.offset.parent.top - innermostContainer.offset.parent.top;
654
655 inst._trigger("toSortable", event);
656 inst.dropped = innermostContainer.element; //draggable revert needs that
657 //hack so receive/update callbacks work (mostly)
658 inst.currentItem = inst.element;
659 innermostContainer.fromOutside = inst;
660
661 //sam brace
662 }
663
664 if(innermostContainer)
665 {
666 //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
667 if(innermostContainer.currentItem) innermostContainer._mouseDrag(event);
668 }
669
670 }
671});
672
673 /*
674 var iframe = document.getElementById('iframe');
675 var iframe_document = iframe.document;
676
677 if(iframe.contentDocument)
678 {
679 iframe_document = iframe.contentDocument; // For NS6
680 console.log("Chose content document");
681 }
682 else if(iframe.contentWindow)
683 {
684 iframe_document = iframe.contentWindow.document; // For IE5.5 and IE6
685 console.log("Chose content window");
686 }
687
688 // Edit the hrefs in preview_html - search for <a href="format?a=b&rt=r&s=ClassifierBrowse&c=simpleht&cl=CL1.2">
689 var start_index = 0;
690 var end_index = 0;
691 while(start_index != -1)
692 {
693 start_index = preview_html.indexOf("href=\"format", start_index);
694 console.log("start index = " + start_index);
695 if(start_index != -1)
696 {
697 end_index = preview_html.indexOf("\">", start_index);
698 console.log("end_index = " + end_index);
699 a = preview_html.substring(0,end_index);
700 b = preview_html.substring(end_index);
701 preview_html = a.concat("&excerptid=results", b);
702 console.log(preview_html);
703 start_index = end_index + "&excerptid=results\">".length;
704 }
705 }
706
707 // Split the html code in to three parts
708 var first_index = preview_html.indexOf("<ul id=\"results\"");
709 console.log("First index is "+first_index);
710 var second_index = preview_html.indexOf("</ul>", first_index) + "</ul>".length;
711 console.log("Second index is "+second_index);
712
713 var first_half = preview_html.substring(0, first_index);
714 var iframe_code = preview_html.substring(first_index, second_index);
715 var second_half = preview_html.substring(second_index);
716
717 //$('#my_categories').innerHTML = first_half.concat(second_half);
718 document.getElementById("my_categories").innerHTML = first_half.concat(second_half);
719
720 // Put the content in the iframe
721 if(initialised_iframe == "false")
722 {
723 console.log("Initialised iframe with preview html");
724 console.log(preview_html);
725 iframe_document.open();
726 iframe_document.writeln(iframe_code); //.concat("&excerptid=gs_content"));
727 iframe_document.close();
728 initialised_iframe = "true";
729 }
730 */
731
732 /*
733 $(iframe_document.documentElement.innerHTML).find('a').each(function() {
734 console.log("data "+$(this).data('href'));
735 console.log("getAttribute "+this.getAttribute('href'));
736 console.log("attr "+$(this).attr('href'));
737 console.log("this.href "+this.href);
738 var original = this.href; //$(this).attr('href');
739 // check if greenstone link ie. starts with format
740 //var original = $(this).data('href');
741 var modified = original.toString().concat("&excerptid=gs_content");
742 console.log("* *");
743 console.log(modified);
744 console.log("* *");
745 //this.href = modified;
746 //$(this).attr('href',modified);
747 //$(this).data('href', modified);
748 $(this).attr({ 'href': modified });
749 //console.log($(this).attr('href'));
750 console.log("data "+$(this).data('href'));
751 console.log("getAttribute "+this.getAttribute('href'));
752 console.log("attr "+$(this).attr('href'));
753 console.log("this.href "+this.href);
754 console.log("***********************");
755 });
756 */
757
758 /******************************************/
759 /* DRAGGABLES */
760 /******************************************/
761
762 $(".draggable_gsf_template").draggable({
763 cursor: 'crosshair',
764 connectToSortable: '#formatStatement',
765 helper: 'clone',
766 revert: 'invalid'
767 });
768
769 $(".draggable_table").draggable({
770 cursor: 'crosshair',
771 connectToSortable: '.gsf_template',
772 helper: 'clone',
773 revert: 'invalid'
774 });
775
776 $(".draggable_tr").draggable({
777 cursor: 'crosshair',
778 connectToSortable: '.gsf_table',
779 helper: 'clone',
780 revert: 'invalid'
781 })
782
783 $(".draggable_td").draggable({
784 cursor: 'crosshair',
785 //connectToSortable: '.td',
786 helper: 'clone',
787 revert: 'invalid'
788 })
789
790 $(".draggable_gsf_text").draggable({
791 cursor: 'crosshair',
792 connectToSortable: '.td-div, .gsf_when, .gsf_otherwise, .gsf_link, .gsf_choose_metadata, .gsf_default',
793 helper: 'clone',
794 revert: 'invalid'
795 });
796
797 $(".draggable_gsf_choose_metadata").draggable({
798 cursor: 'crosshair',
799 connectToSortable: '.td-div, .gsf_link, .gsf_when, .gsf_otherwise',
800 helper: 'clone',
801 revert: 'invalid'
802 });
803
804 //$(".element_type_gsf_metadata").draggable({
805 $(".draggable_gsf_metadata").draggable({
806 cursor: 'crosshair',
807 connectToSortable: '.gsf_choose_metadata, .gsf_when, .gsf_otherwise, .gsf_link, .td-div',
808 helper: 'clone',
809 revert: 'invalid'
810 });
811
812 $(".draggable_gsf_link").draggable({
813 cursor: 'crosshair',
814 connectToSortable: '.td-div, .gsf_when, .gsf_otherwise, .gsf_link',
815 helper: 'clone',
816 revert: 'invalid'
817 });
818
819 // switch, when, otherwise, icon
820 $(".draggable_gsf_switch").draggable({
821 cursor: 'crosshair',
822 connectToSortable: '.td-div, .gsf_link',
823 helper: 'clone',
824 revert: 'invalid'
825 });
826
827 $(".draggable_gsf_when").draggable({
828 cursor: 'crosshair',
829 connectToSortable: '.gsf_switch',
830 helper: 'clone',
831 revert: 'invalid'
832 });
833
834 $(".draggable_gsf_otherwise").draggable({
835 cursor: 'crosshair',
836 connectToSortable: '.gsf_switch',
837 helper: 'clone',
838 revert: 'invalid'
839 });
840
841 $(".draggable_gsf_icon").draggable({
842 cursor: 'crosshair',
843 connectToSortable: '.td-div, .gsf_link, .gsf_choose, .gsf_when, .gsf_otherwise',
844 helper: 'clone',
845 revert: 'invalid'
846 });
847
848
849 /******************************************/
850 /* SORTABLES */
851 /******************************************/
852
853 bind_td_sortable();
854 bind_all_sortables();
855
856 $('#formatStatement').sortable({
857 cursor: 'pointer',
858 tolerance: 'pointer',
859 items: '.gsf_template',
860 placeholder:'placeholder',
861 //'nested':'div'
862 stop: function(event, ui) {
863 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('css_gsf_template')) { replace_with(ui.item,gsf_template_element); }
864 }
865 });
866
867 $('div.gsf_template').children(".block,.table").slideUp(300);
868});
869
870/*******************************************************************************/
871
872function bind_all_sortables()
873{
874 console.log('function bind_all_sortables()');
875 bind_template_sortable();
876 bind_choose_metadata_sortable();
877 bind_link_sortable();
878 bind_switch_sortable();
879 bind_when_sortable();
880 bind_otherwise_sortable();
881
882 bind_tables();
883
884 bind_block_mouseover();
885 bind_minmax_remove();
886}
887
888function bind_tables()
889{
890 console.log('function bind_tables()');
891
892 $('#sectionHeader').click(function () {
893 console.log('section Header click *');
894 return true;
895 });
896
897 $('#sectionContainer').click(function () {
898 console.log('section Container click *');
899 return true;
900 });
901
902 $(".td-div").resizable({
903 alsoResize: 'parent',
904 handles: 'w,e',
905 stop: function(event, ui) {
906 console.log('Resize table on stop');
907 resize_tables($(this));
908 }, });
909
910 $(".droppable").droppable({
911 accept: '.element_type_td',
912 tolerance: 'pointer',
913 activate: function(event, ui) { $(this).addClass("droppable_hl");},
914 deactivate: function(event, ui) { $(this).removeClass("droppable_hl"); },
915 drop: function(event, ui) {
916 var neverempty = document.createElement("div");
917 neverempty.setAttribute("class","neverempty block");
918 neverempty.setAttribute("style","height:50px");
919 neverempty.setAttribute("style","display:block");
920 var text = document.createTextNode('NEVER EMPTY');
921 neverempty.appendChild(text);
922 var td = document.createElement("td");
923 var div = document.createElement("div");
924 div.setAttribute("title","td-div");
925 div.setAttribute("class","td-div block");
926 div.setAttribute("style","margin-left:0px");
927 div.setAttribute("width","25px");
928 td.appendChild(div);
929 div.appendChild(neverempty);
930 var sep = document.createElement("td");
931 sep.setAttribute("class","droppable");
932 sep.setAttribute("width","10px");
933 $(this).after(sep);
934 $(this).after(td);
935 bind_tables();
936 resize_tables($(this));
937 bind_td_sortable();
938 bind_block_mouseover();
939 }
940 });
941
942}
943
944function replace_with(item, me)
945{
946 // Search me for select
947 if(me.search("select") != -1)
948 {
949 // If select exists, then find CURRENT_SELECT_VALUE
950 var index = me.search(CURRENT_SELECT_VALUE);
951 if(index == -1)
952 console.log("Did not find " + CURRENT_SELECT_VALUE);
953 else
954 console.log("Found " + CURRENT_SELECT_VALUE + " at index " + index);
955 index = index + CURRENT_SELECT_VALUE.length + 1;
956 console.log("Attempt inserting select at new index "+index);
957 a = me.substring(0,index);
958 b = me.substring(index);
959 me = a.concat("selected",b);
960 }
961
962 item.replaceWith(me); //'<div class="element element-txt">This text box has been added!</div>');
963
964 resize_tables(item);
965
966 bind_all_sortables();
967}
968
969function resize_tables(item)
970{
971 var max_height = 0;
972 (item.parents('.table')).each(function(index) {
973 $(this).children().children().children().each(function() {
974 var sum_one = 0;
975 var sum_two = 0;
976 $(this).children('.block').each(function() { sum_one = sum_one + $(this).height();
977 $(this).children('.block').each(function() { sum_two = sum_two + $(this).height(); } );
978 console.log("My height is " + $(this).height() + ", sum height 2 is " + sum_two);
979 });
980 console.log("My height is " + $(this).height() + ", sum height 1 is " + sum_one);
981 if (sum_two > max_height)
982 max_height = sum_two;
983 });
984 });
985 equalHeights(item,max_height);
986}
987
988function bind_template_sortable()
989{
990 //console.log('function bind_template_sortable()');
991 $('.gsf_template').sortable({
992 'cursor':'pointer',
993 'tolerance': 'pointer',
994 'items':'.table', //.gsf_choose_metadata, .gsf_metadata',
995 'placeholder':'placeholder',
996 //'nested':'.gsf:metadata'
997 stop: function(event, ui) {
998 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_table')) { replace_with(ui.item, "<table class=\"table\" border=\"2\"></table>"); }
999 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose_metadata')) { replace_with(ui.item, gsf_choose_metadata_element); }
1000 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1001 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1002 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_switch')) { replace_with(ui.item, gsf_switch_element); }
1003 }
1004 });
1005
1006 $('.gsf_template').click(function () {
1007 console.log('gsf_template class click');
1008 return false;
1009 });
1010
1011}
1012
1013function bind_td_sortable()
1014{
1015
1016 $('tr').sortable({
1017 'cursor':'pointer',
1018 'tolerance': 'intersect',
1019 'items':'.column',
1020 'placeholder':'placeholder_td',
1021 'connectWith':'column'});
1022
1023 $('.td-div').sortable({
1024 'cursor':'pointer',
1025 'tolerance': 'pointer',
1026 'items':'.gsf_metadata, .gsf_choose_metadata, .gsf_link, .gsf_switch',
1027 'placeholder':'placeholder',
1028 receive: function(event, ui) { alert("Attempted to receive"); },
1029 stop: function(event, ui) {
1030 // gsf metadata
1031 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1032 // gsf choose metadata
1033 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose_metadata')) { replace_with(ui.item, gsf_choose_metadata_element); }
1034 // gsf link
1035 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1036 // gsf switch
1037 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_switch')) { replace_with(ui.item, gsf_switch_element); }
1038
1039 }
1040
1041 });
1042
1043 $('.td-div').click(function () {
1044 //console.log('td class click');
1045 return true;
1046 });
1047
1048
1049
1050}
1051
1052
1053function bind_choose_metadata_sortable()
1054{
1055 //console.log('function bind_choose_metadata_sortable()');
1056 $('.gsf_choose_metadata').sortable({
1057 'cursor':'pointer',
1058 'tolerance': 'pointer',
1059 'items':'.gsf_metadata, .gsf_text, .gsf_default',
1060 'placeholder':'placeholder',
1061 'connectWith':'.gsf_choose_metadata',
1062 //'nested':'.gsf:metadata'
1063 stop: function(event, ui) {
1064 // gsf metadata
1065 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1066 // gsf text
1067 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_text')) { replace_with(ui.item, gsf_text_element); }
1068 // gsf default
1069 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_default')) { replace_with(ui.item, gsf_default_element); }
1070
1071 bind_all_sortables();
1072 }
1073 });
1074}
1075
1076function bind_link_sortable()
1077{
1078 //console.log('function bind_link_sortable()');
1079 $('.gsf_link').sortable({
1080 'cursor':'pointer',
1081 'tolerance': 'pointer',
1082 'items':'.leaf, .gsf_link, .gsf_switch, .gsf_choose',
1083 'placeholder':'placeholder',
1084 'connectWith':'.gsf_link',
1085 //'nested':'.gsf:metadata'
1086 stop: function(event, ui) {
1087 // gsf icon
1088 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_icon')) { replace_with(ui.item, gsf_icon_element); }
1089 // gsf text
1090 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_text')) { replace_with(ui.item, "<input type=\"text\" name=\"rawtextinput\" size=\"10\"/>"); }
1091 // gsf metadata
1092 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1093 // gsf link
1094 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1095 // gsf switch
1096 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_switch')) { replace_with(ui.item, gsf_switch_element); }
1097 // gsf choose
1098 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose_metadata')) { replace_with(ui.item, gsf_choose_metadata_element); }
1099 }
1100 });
1101}
1102
1103function bind_switch_sortable()
1104{
1105 //console.log('function bind_switch_sortable()');
1106 $('.gsf_switch').sortable({
1107 'cursor':'pointer',
1108 'tolerance': 'pointer',
1109 'items':'.gsf_metadata, .gsf_when, .gsf_otherwise, .gsf_text',
1110 'placeholder':'placeholder',
1111 'connectWith':'.gsf_switch',
1112 //'nested':'.gsf:metadata'
1113 stop: function(event, ui) {
1114 // gsf when
1115 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_when')) { replace_with(ui.item, gsf_when_element); }
1116 // gsf otherwise
1117 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_otherwise')) { replace_with(ui.item, gsf_otherwise_element); }
1118 // gsf metadata
1119 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1120 // gsf text
1121 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_text')) { replace_with(ui.item, gsf_text_element); }
1122 }
1123 });
1124}
1125
1126function bind_when_sortable()
1127{
1128 //console.log('function bind_when_sortable()');
1129 $('.gsf_when').sortable({
1130 'cursor':'pointer',
1131 'tolerance': 'pointer',
1132 'items':'.leaf, .gsf_link, .gsf_choose',
1133 'placeholder':'placeholder',
1134 //'nested':'.gsf:metadata'
1135 stop: function(event, ui) {
1136 // gsf metadata
1137 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1138 // gsf icon
1139 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_icon')) { replace_with(ui.item, gsf_icon_element); }
1140 // gsf text
1141 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_text')) { replace_with(ui.item, gsf_text_element); }
1142 // gsf link
1143 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1144 // gsf choose
1145 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose')) { replace_with(ui.item, gsf_choose_element); }
1146 }
1147 });
1148}
1149
1150function bind_otherwise_sortable()
1151{
1152 //console.log('function bind_otherwise_sortable()');
1153 $('.gsf_otherwise').sortable({
1154 'cursor':'pointer',
1155 'tolerance': 'pointer',
1156 'items':'.leaf, .gsf_link, .gsf_choose',
1157 'placeholder':'placeholder',
1158 //'nested':'.gsf:metadata'
1159 stop: function(event, ui) {
1160 // gsf metadata
1161 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_metadata')) { replace_with(ui.item, gsf_metadata_element); }
1162 // gsf text
1163 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_text')) { replace_with(ui.item, "<input type=\"text\" name=\"rawtextinput\" size=\"10\"/>"); }
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 link
1167 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_link')) { replace_with(ui.item, gsf_link_element); }
1168 // gsf choose
1169 if (ui.item.hasClass("ui-draggable") && ui.item.hasClass('draggable_gsf_choose')) { replace_with(ui.item, gsf_choose_element); }
1170 }
1171 });
1172}
1173
1174function bind_block_mouseover()
1175{
1176 //console.log('function bind_block_mouseover()');
1177 $(".block").mouseover(function()
1178 {
1179 $(this).parents().css("border", "");
1180 $(this).css("border", "2px solid blue");
1181 return false;
1182 }).mouseout(function(){
1183 $(this).css("border", "");
1184 return false;
1185 });
1186}
1187
1188function bind_minmax_remove()
1189{
1190 console.log('function bind_minmax_remove()');
1191 $('a.minmax').bind('click', toggleContent);
1192 $('a.remove').bind('click', removeContent);
1193};
1194
1195var removeContent = (function () {
1196 //console.log('var removeContent = (function ()');
1197 // this -> a -> td -> tr -> tbody -> table -> div
1198 //$(this).parent().parent().parent().parent().parent().remove();
1199 $(this).closest(".block").remove();
1200 });
1201
1202
1203var toggleContent = function(e)
1204{
1205 console.log('parent: ' + $(this).html());
1206 if ($(this).html() == '[+]'){ //targetContent.css('display') == 'none') {
1207 $(this).closest(".block").children(".table, .block").slideDown(300);
1208 $(this).html('[-]');
1209 $(this).removeClass("ui-icon-plusthick");
1210 $(this).addClass("ui-icon-minusthick");
1211 } else {
1212 $(this).closest(".block").children(".table, .block").slideUp(300);
1213 $(this).html('[+]');
1214 $(this).removeClass("ui-icon-minusthick");
1215 $(this).addClass("ui-icon-plusthick");
1216 }
1217 return false;
1218};
1219
1220function serialize(s)
1221{
1222 serial = $.SortSerialize(s);
1223 alert(serial.hash);
1224};
1225
1226function equalHeights(item, height) {
1227 //console.log('function equalHeights(item, height)');
1228 (item.parents('.table')).each(function(index) {
1229 $(this).children().children().children().each(function() {
1230 $(this).height(height);
1231 $(this).children().height(height);
1232 });
1233 });
1234};
1235
Note: See TracBrowser for help on using the repository browser.