comparison views/Extractapp/TaggingText.php @ 98:5bea390e7ca9 extractapp

new: short tag in front of long tag
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 14 Mar 2016 14:43:42 +0100
parents c1bb174a22f3
children c3e149277b58
comparison
equal deleted inserted replaced
97:c1bb174a22f3 98:5bea390e7ca9
281 $(document).on("mouseup", '#editable-area', function (e) { 281 $(document).on("mouseup", '#editable-area', function (e) {
282 $('.questionMarkClass').remove(); 282 $('.questionMarkClass').remove();
283 $('.tagItemDivClass').remove(); 283 $('.tagItemDivClass').remove();
284 284
285 if ( $("#editTextId").html() != "Edit the text" ) return 0; 285 if ( $("#editTextId").html() != "Edit the text" ) return 0;
286 var selection = getSelected(); 286
287 287 //var selection = getSelected();
288 range = selection.getRangeAt(0); 288 selection = getSelected(); // selection is a global variable
289
290 range = selection.getRangeAt(0); // range is a global variable
289 291
290 container = document.createElement("div"); 292 container = document.createElement("div");
291 container.appendChild(selection.getRangeAt(0).cloneContents()); 293 container.appendChild(selection.getRangeAt(0).cloneContents());
294
292 295
293 if(selection && (selection_plain = new String(selection).replace(/^\s+|\s+$/g,''))) { 296 if(selection && (selection_plain = new String(selection).replace(/^\s+|\s+$/g,''))) {
294 try{ 297 try{
295 var newdiv = document.createElement("div"); 298 var newdiv = document.createElement("div");
296 //newdiv.id = "tagItemDivId"; 299 //newdiv.id = "tagItemDivId";
354 $(tagging_tags).addClass("bg-white"); 357 $(tagging_tags).addClass("bg-white");
355 $(tagging_tags).text("Tag as: "); 358 $(tagging_tags).text("Tag as: ");
356 359
357 tagging_tags.innerHTML += "<button accesskey=\"2\" onclick=\"tagwithtitle( range, '"+String(selection_plain)+"' )\">Title</button></br>"; 360 tagging_tags.innerHTML += "<button accesskey=\"2\" onclick=\"tagwithtitle( range, '"+String(selection_plain)+"' )\">Title</button></br>";
358 361
362
363 console.log(selection);
364
365
359 <?php 366 <?php
360 foreach ( $taglistArray as $taglistValue ) { 367 foreach ( $taglistArray as $taglistValue ) {
361 if ($taglistValue[2] == $topic_tag) { 368 if ($taglistValue[2] == $topic_tag) {
362 echo "tagging_tags.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection_plain)+\"', '".$taglistValue[2]."2' )\\\">".$taglistValue[1]."</button>\";\n"; 369 echo "tagging_tags.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, selection, '".$taglistValue[2]."2' )\\\">".$taglistValue[1]."</button>\";\n";
363 echo "tagging_tags.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection_plain)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(without new line)</button>\";\n"; 370 echo "tagging_tags.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, selection, '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(without new line)</button>\";\n";
364 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection_plain)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n"; 371 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection_plain)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n";
365 break; 372 break;
366 } 373 }
367 } 374 }
368 foreach ( $taglistArray as $taglistValue ) { 375 foreach ( $taglistArray as $taglistValue ) {
369 if ($taglistValue[2] == $topic_tag) { 376 if ($taglistValue[2] == $topic_tag) {
370 // pass 377 // pass
371 } else if ($taglistValue[2] == "post_time") { 378 } else if ($taglistValue[2] == "post_time") {
372 echo "tagging_tags.innerHTML += \"<button accesskey=\\\"3\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."</button>\";\n"; 379 // this case could be deprecated
380 echo "tagging_tags.innerHTML += \"<button accesskey=\\\"3\\\" onclick=\\\"tagwithOnlytag( range, selection, '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."</button>\";\n";
373 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n"; 381 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n";
374 } else if ($taglistValue[2] == "office") { 382 } else if ($taglistValue[2] == "office") {
375 echo "tagging_tags.innerHTML += \"<button accesskey=\\\"4\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."</button>\";\n"; 383 // this case could be deprecated
384 echo "tagging_tags.innerHTML += \"<button accesskey=\\\"4\\\" onclick=\\\"tagwithOnlytag( range, selection, '".$taglistValue[2]."' )\\\">".$taglistValue[1]."</button>\";\n";
376 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n"; 385 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n";
377 } else { 386 } else {
387 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagwithOnlytag(range, selection, '".$taglistValue[2]."')\\\">".$taglistValue[1]."</button>\";\n";
388 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n";
389
390 /* testing... backup the original one
391
378 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."</button>\";\n"; 392 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."</button>\";\n";
379 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n"; 393 echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n";
394 */
395
380 } 396 }
381 } 397 }
382 ?> 398 ?>
383 399
384 newdiv.appendChild(tagging_tags); 400 newdiv.appendChild(tagging_tags);
563 form.appendChild(hiddenField); 579 form.appendChild(hiddenField);
564 580
565 _postForContineTagging(form); 581 _postForContineTagging(form);
566 582
567 } 583 }
568 // =========== 584
569 585
570 function tagwithOnlytag( range, stringSelection, tag ) { 586 function tagTestX(range, selection, tag) {
587 console.log("selection string: ");
588 console.log(String(selection));
589
590 var stringSelection = String(selection);
591
592 saveUndoText();
593 var topic_tag = JSON.parse('<?php echo json_encode($topic_tag) ?>');
594 var topic_tag2 = topic_tag+"2";
595
596
597 // stringSelection here only contains plain text (string) without the tagging structure
598 // how to use the existing tagging structure
599
600 // ---
601 var documentFragment = range.extractContents();
602
603
604 var tmpStringSelection = ""
605 var childNodes = documentFragment.childNodes
606 for (var i = 0; i < childNodes.length; i++){
607
608 if (childNodes[i].outerHTML == undefined) {
609
610 tmpStringSelection += childNodes[i].textContent
611 } else {
612
613 tmpStringSelection += childNodes[i].outerHTML
614 }
615
616 }
617
618 stringSelection = tmpStringSelection
619
620 range.deleteContents();
621
622 if ( tag==topic_tag2) {
623 var newdiv = document.createElement(topic_tag);
624 newdiv.innerHTML = stringSelection;
625 range.insertNode(newdiv);
626 var newdiv = document.createElement("br");
627 range.insertNode(newdiv);
628
629 } else {
630 var newdiv = document.createElement(tag);
631 newdiv.innerHTML = stringSelection;
632 range.insertNode(newdiv);
633 }
634
635
636 $('.tagItemDivClass').remove();
637
638
639 return
640
641
642 // ---
643
644 console.log("the anchorNode: ");
645 console.log(selection.anchorNode);
646
647 console.log("the anchorNode's nextSibling");
648 console.log(next);
649
650
651 // TODO if no fragment... bug
652
653 if ( typeof selection !== 'string' ) {
654 var next = selection.anchorNode.nextSibling;
655
656
657
658 // TODO fragment at the beginning of selection: anchorNode
659 var tmpStringSelection = ""
660
661 var startInx = selection.anchorOffset
662 var endSelectionInx = startInx + stringSelection.length
663 var endInx = selection.anchorNode.length
664 console.log("startInx=" + startInx + " endInx= " + endInx + "; endSelectionInx= " + endSelectionInx)
665
666 var anchorText = selection.anchorNode.textContent
667
668
669 /*
670 console.log("anchorNode...")
671 if (selection.anchorNode.nodeType == 3 && next.nodeName != "BR" && next.nodeName != "br") {
672 // if (next != null && selection.anchorNode.nodeType == 3 && next.nodeName != "BR" && next.nodeName != "br") {
673 // here the anchor node is text
674
675 //tmpStringSelection += anchorText.substring(selection.anchorOffset, selection.anchorNode.length);
676 tmpStringSelection += anchorText.substring(startInx, endSelectionInx);
677
678 console.log("append anchorText to tmpStringSelection: " + tmpStringSelection)
679
680 endInx += endSelectionInx - startInx;
681
682 //endInx += (selection.anchorNode.length - selection.anchorOffset);
683
684
685 }
686 */
687
688 // TODO when anchorNode is not text, but a node
689
690
691 /*
692 if (next != null) {
693
694 if (selection.anchorNode.nodeType == 3 && next.nodeName != "BR" && next.nodeName != "br") {
695 var anchorText = selection.anchorNode.textContent
696 // never goes here?
697 tmpStringSelection += anchorText.substring(selection.anchorOffset, selection.anchorNode.length);
698
699 console.log("append anchorText to tmpStringSelection: " + tmpStringSelection)
700
701
702 }
703 }
704 */
705
706 // TODO parsing stringSelection object in string, with the tagging strucutre
707
708 var checkExtentNode = true
709
710 while(next != null && next != undefined && next.nodeName != "BR" && next.nodeName != "br" ) {
711
712 /*
713 if (endInx >= endSelectionInx) {
714 console.log("startInx=" + startInx + " endInx= " + endInx + "; endSelectionInx= " + endSelectionInx)
715
716 console.log("debug: end of traverse. next=")
717 console.log(next)
718
719 break;
720 }
721 */
722
723 startInx = endInx
724
725 if (next.nodeType == 3) { // text case
726 console.log("text case.")
727
728 endInx += next.textContent.length
729
730 if (endInx > endSelectionInx) {
731
732 /*
733 tmpStringSelection += next.textContent.substring(0, endSelectionInx-startInx)
734
735 console.log("startInx=" + startInx + " endInx= " + endInx + "; endSelectionInx= " + endSelectionInx)
736 console.log("appened next.textContent.substring tmpStringSelection (text case): " + tmpStringSelection)
737 */
738
739 //checkExtentNode = false
740
741 //break;
742 } else {
743
744
745 tmpStringSelection += next.textContent;
746 console.log("appened tmpStringSelection (text case): " + tmpStringSelection)
747
748
749 }
750
751
752
753 } else if (next.nodeType == 1) { // node case
754 console.log("node case")
755
756 endInx += next.innerHTML.length
757
758 if (endInx > endSelectionInx) {
759
760 // TODO do something about tmpStringSelection here?
761
762 console.log("startInx=" + startInx + " endInx= " + endInx + "; endSelectionInx= " + endSelectionInx)
763
764
765 //checkExtentNode = false
766
767 //break;
768 } else {
769
770
771 tmpStringSelection += next.outerHTML;
772 console.log("appened tmpStringSelection (node case): " + tmpStringSelection)
773 // endInx += next.outerHTML.length
774 }
775
776
777 } else {
778 console.log("[debug] case: nodeType=" + next.nodeType)
779 }
780
781
782 /*
783
784 } else if (next.innerHTML != undefined) {
785
786
787 endInx += next.innerHTML.length;
788 if (endInx >= endSelectionInx) {
789 break;
790 }
791
792
793 } else {
794 break
795 }
796 console.log("startInx= " + startInx + "; endInx= " + endInx)
797 if (next.outerHTML != undefined) {
798 tmpStringSelection += next.outerHTML;
799 console.log("appened tmpStringSelection: " + tmpStringSelection)
800
801 }
802 */
803
804 console.log("next: ");
805 console.log(next);
806 next = next.nextSibling;
807
808 }
809
810
811 // TODO
812 // fragment at the end of selection: extentNode
813
814 console.log("extentNode...")
815 console.log("startInx=" + startInx + " endInx= " + endInx + "; endSelectionInx= " + endSelectionInx)
816
817 var extentNode = selection.extentNode
818
819
820 if (checkExtentNode) {
821
822 if (endInx < endSelectionInx && startInx < endSelectionInx ) {
823 tmpStringSelection += anchorText.substring(endInx, endSelectionInx);
824 console.log("append anchorText.substring(endInx, endSelectionInx) to tmpStringSelection: " + tmpStringSelection)
825
826 } else if (extentNode != null) {
827 var extentText = extentNode.textContent
828
829 tmpStringSelection += extentText.substring(startInx, endSelectionInx);
830
831 console.log("append extentNode to tmpStringSelection: " + tmpStringSelection)
832 } else {
833 tmpStringSelection += anchorText.substring(startInx, endSelectionInx);
834 console.log("append anchorText.substring(startInx, endSelectionInx) to tmpStringSelection: " + tmpStringSelection)
835 }
836 }
837
838 /*
839 if (extentNode != null) {
840 if (extentNode.nodeType == 3 ) { // text case
841 var extentText = extentNode.textContent
842
843 if (endInx >= endSelectionInx) {
844
845 console.log("tmpStringSelection: " + tmpStringSelection)
846
847 console.log(extentText.substring(startInx, selection.extentOffset))
848 tmpStringSelection += extentText.substring(startInx, endSelectionInx);
849
850 console.log("append extentNode to tmpStringSelection: " + tmpStringSelection)
851
852 } else {
853 tmpStringSelection += anchorText.substring(endInx, endSelectionInx);
854
855 }
856 }
857 }
858 */
859
860 console.log("tmpStringSelection:")
861 console.log(tmpStringSelection)
862 stringSelection = tmpStringSelection;
863
864 }
865
866 range.deleteContents();
867
868 if ( tag==topic_tag2) {
869 var newdiv = document.createElement(topic_tag);
870 newdiv.innerHTML = stringSelection;
871 range.insertNode(newdiv);
872 var newdiv = document.createElement("br");
873 range.insertNode(newdiv);
874
875 } else {
876 var newdiv = document.createElement(tag);
877 newdiv.innerHTML = stringSelection;
878 range.insertNode(newdiv);
879 }
880
881
882 $('.tagItemDivClass').remove();
883
884
885 }
886
887 function tagwithOnlytag( range, selection, tag ) {
888
889 saveUndoText();
890 var topic_tag = JSON.parse('<?php echo json_encode($topic_tag) ?>');
891 var topic_tag2 = topic_tag+"2";
892
893 var documentFragment = range.extractContents();
894
895
896 var stringSelection = ""
897 var childNodes = documentFragment.childNodes
898 for (var i = 0; i < childNodes.length; i++){
899 if (childNodes[i].outerHTML == undefined) {
900 stringSelection += childNodes[i].textContent
901 } else {
902 stringSelection += childNodes[i].outerHTML
903 }
904
905 }
906 range.deleteContents();
907
908 if ( tag==topic_tag2) {
909 var newdiv = document.createElement(topic_tag);
910 newdiv.innerHTML = stringSelection;
911 range.insertNode(newdiv);
912 var newdiv = document.createElement("br");
913 range.insertNode(newdiv);
914
915 } else {
916 var newdiv = document.createElement(tag);
917 newdiv.innerHTML = stringSelection;
918 range.insertNode(newdiv);
919 }
920
921
922 $('.tagItemDivClass').remove();
923 }
924
925 function tagwithOnlytag_old( range, stringSelection, tag ) {
571 saveUndoText(); 926 saveUndoText();
572 var topic_tag = JSON.parse('<?php echo json_encode($topic_tag) ?>'); 927 var topic_tag = JSON.parse('<?php echo json_encode($topic_tag) ?>');
573 var topic_tag2 = topic_tag+"2"; 928 var topic_tag2 = topic_tag+"2";
574 929
575 range.deleteContents(); 930 range.deleteContents();
784 var topic_select = document.createElement("select"); 1139 var topic_select = document.createElement("select");
785 topic_select.id = "loadTopiclist"; 1140 topic_select.id = "loadTopiclist";
786 var selected_topic = t[0]; 1141 var selected_topic = t[0];
787 topic_select.onchange = function(){ 1142 topic_select.onchange = function(){
788 1143
789 selected_topic = topic_select.options[topic_select.selectedIndex]; 1144 var changeConfirm = confirm("You're going to change topic. \nUnsaved data will be lost.");
790 console.log(selected_topic.text); 1145
791 1146 if (changeConfirm) {
792 var topic_id = selected_topic.value; 1147
793 1148 selected_topic = topic_select.options[topic_select.selectedIndex];
794 var form = document.createElement("form"); 1149 console.log(selected_topic.text);
795 form.setAttribute("method", "post"); 1150
796 form.setAttribute("action", "./TaggingText"); // hand to controller 1151 var topic_id = selected_topic.value;
797 form.setAttribute("target", "_self"); 1152
798 1153 var form = document.createElement("form");
799 var hiddenField = document.createElement("input"); 1154 form.setAttribute("method", "post");
800 hiddenField.setAttribute("name", "topic"); 1155 form.setAttribute("action", "./TaggingText"); // hand to controller
801 hiddenField.setAttribute("value", topic_id); 1156 form.setAttribute("target", "_self");
802 form.appendChild(hiddenField); 1157
803 1158 var hiddenField = document.createElement("input");
804 var hiddenField = document.createElement("input"); 1159 hiddenField.setAttribute("name", "topic");
805 hiddenField.setAttribute("name", "func"); 1160 hiddenField.setAttribute("value", topic_id);
806 hiddenField.setAttribute("value", "ContinueTagging"); 1161 form.appendChild(hiddenField);
807 form.appendChild(hiddenField); 1162
808 1163 var hiddenField = document.createElement("input");
809 _postForContineTagging(form); 1164 hiddenField.setAttribute("name", "func");
810 1165 hiddenField.setAttribute("value", "ContinueTagging");
1166 form.appendChild(hiddenField);
1167
1168 _postForContineTagging(form);
1169
1170
1171 } else {
1172 return
1173 }
811 }; 1174 };
812 1175
813 1176
814 //Create and append the options 1177 //Create and append the options
815 for (var i = 0; i < t.length; i++) { 1178 for (var i = 0; i < t.length; i++) {