changeset 87:fb5049fc5dd7 extractapp_dev

New:(1)UI(2)generate simple regex by examples(3)coordinates in book metadata
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Tue, 02 Jun 2015 16:52:05 +0200
parents 0141df465205
children 61593b047289
files config/config.php controllers/extractapp.php css/taggingtext.css js/taggingtext.js models/extractapp.php views/Extractapp/TaggingText.php views/maintemplate.php views/maintemplate_local.php
diffstat 8 files changed, 522 insertions(+), 153 deletions(-) [+]
line wrap: on
line diff
--- a/config/config.php	Thu Apr 30 10:52:05 2015 +0200
+++ b/config/config.php	Tue Jun 02 16:52:05 2015 +0200
@@ -22,17 +22,17 @@
 			$mysql_server = "localhost";
 			$mysql_user = "root";
 			$mysql_password = "admin";
-			$server_host = "http://localgazetteers-dev/";
+			$server_host = "http://localgazetteers/";
 			$lgserver_url = $server_host."LGServices/";
 
 		} else {
 			// localhost
-			$mysql_database = "Gazetteers";
+			$mysql_database = "Gazetteer";
 			$mysql_server = "localhost";
 			$mysql_user = "root";
 			$mysql_password = "root";
 			$server_host = "http://localhost:1080/localgazetteers-dev/";
-			$lgserver_url = "http://localgazetteers-dev/LGServices/";
+			$lgserver_url = "http://localgazetteers/LGServices/";
 		}
 
 		$systemNAME = "interface";
--- a/controllers/extractapp.php	Thu Apr 30 10:52:05 2015 +0200
+++ b/controllers/extractapp.php	Tue Jun 02 16:52:05 2015 +0200
@@ -59,6 +59,8 @@
         
         if (isset($this->postdata['func'])) {
             $func = $this->postdata['func'];
+        } else {
+            $func = "";
         }
         switch ($func) {
             case 'SaveFullText':
--- a/css/taggingtext.css	Thu Apr 30 10:52:05 2015 +0200
+++ b/css/taggingtext.css	Tue Jun 02 16:52:05 2015 +0200
@@ -1,18 +1,29 @@
 .bg-danger{
 	background-color: #FFD1D1;
+	margin: 5 5 5 5;
 }
 
 .bg-warning{
 	background-color: #FFFF99;
+	margin: 5 5 5 5;
 }
-
+.bg-success{
+	background-color: #ADFAAA;
+	margin: 5 5 5 5;
+}
 .bg-info{
 	background-color: #E7F3FF;
-
+	margin: 5 5 5 5;
 }
 .bg-default{
 	background-color: #F4F4F4;
+	margin: 5 5 5 5;
 }
+.bg-white{
+	background-color: white;
+	margin: 5 5 5 5;
+}
+/*
 .btn-xs{
 	height: 25px;
 	width: 60px;
@@ -30,6 +41,16 @@
 	width: 220px;
 }
 
+*/
+.tool-frame .btn{
+	background-color: #F4F4F4;
+}
+
+.btn:hover {
+	border: 2px solid #A1A1A1;
+    border-radius: 4px;
+}
+
 
 .info-board{
 	margin:10 30 10 30;
@@ -38,18 +59,32 @@
 	min-width: 680px;
 	border: 2px solid #A1A1A1;
     border-radius: 4px;
-	height:70px;
+	max-height:70px;
 	overflow:scroll;
 	overflow-x:hidden;
 }
+
+
 #follow-scroll {
 	position:absolute; 
 	height:680px; 
 	overflow:scroll; 
 	margin:0 5 15 5; 
 	max-width:280px; 
-	border: 1px solid #A1A1A1;
-	border-radius: 4px;
+}
+
+.tool-frame{
+	background-color: white;
+	border: 2px solid #A1A1A1;
+	border-radius: 3px;
+	padding: 5px;
+}
+.tool-topic {
+	background-color: #E7F3FF;
+	border-radius: 3px;
+}
+.tool-topic:hover{
+	border: 2px solid #E7F3FF;
 }
 
 .popup-frame {
@@ -62,26 +97,44 @@
 }
 
 .popup-regex {
-	left: -400px;
-	width: 400px;
+	left: -450px;
+	width: 450px;
 	height: 300px;
-	top: 100px;
+	top: 120px;
 	z-index: 2;
 }
 
 .popup-loadregex{
 	left: -300px;
 	width: 300px;
-	height: 70px;
+	height: 85px;
 	top: 100px;
-	z-index: 2;
+	z-index: 3;
 
 }
 
 .popup-topic {
-	left: -200px;
-	width: 200px;
-	height: 50px;
+	left: -250px;
+	width: 250px;
+	height: 80px;
 	top: 20px;
 	z-index: 2;
 }
+
+.tagItemDivClass {
+	position:absolute; 
+	background-color: white; 
+	border: 2px solid #A1A1A1;
+	border-radius: 4px;	
+	padding: 5px; 
+	z-index:5;
+}
+
+.questionMarkClass {
+	position:absolute; 
+	background-color: white; 
+	border: 2px solid #A1A1A1;
+	border-radius: 4px;	
+	padding: 5px; 
+	z-index:4;
+}
--- a/js/taggingtext.js	Thu Apr 30 10:52:05 2015 +0200
+++ b/js/taggingtext.js	Tue Jun 02 16:52:05 2015 +0200
@@ -238,16 +238,19 @@
 
 
 function removeTagNewDiv( eventObject, tagName, tagObject ) {
+    // TODO: nesting tag representaion
+
     saveUndoText();
     var newdiv = document.createElement("div");
-    newdiv.id = "questionMarkId";
-    newdiv.setAttribute("class", "questionMarkClass");
+    $(newdiv).id = "questionMarkId";
+    $(newdiv).attr("class", "questionMarkClass");
 
     // set z-index to 3 to bring popup tag windwo to front
-    newdiv.style.cssText = 'top:'+eventObject.pageY+'; left:'+eventObject.pageX+'; position:absolute; background-color: white; border:1px solid black; padding: 5px; z-index:3';
+    newdiv.style.cssText = 'top:'+eventObject.pageY+'; left:'+eventObject.pageX+';';
+    
     newdiv.innerHTML = "Tag: "+tagName+"<br>Value: "+tagObject.text()+"<br>";
     
-    var newbutton = $('<button>Remove this</button>').mouseup(function (e2) {
+    var newbutton = $('<button class="btn btn-default">Remove this</button>').mouseup(function (e2) {
         var textKeep = $(this).parent().parent().html();
         var regexText=/<div(.*?)<\/div>/g;
         var replaceText="";
@@ -257,7 +260,7 @@
     });
     newbutton.appendTo(newdiv);
     
-    var newbutton = $('<button>Remove this(with newline)</button>').mouseup(function (e2) {
+    var newbutton = $('<button class="btn btn-default">Remove this(with newline)</button>').mouseup(function (e2) {
         var textKeep = $(this).parent().parent().html();
         var regexText=/<div(.*?)<\/div>/g;
         var replaceText="";
@@ -271,7 +274,7 @@
     });
     newbutton.appendTo(newdiv);
     
-    var newbutton = $('<button>Remove all</button>').mouseup(function (e2) {
+    var newbutton = $('<button class="btn btn-default">Remove all</button>').mouseup(function (e2) {
         var textKeep = $(this).parent().parent().html();
         var regexText=/<div(.*?)<\/div>/g;
         var replaceText="";
@@ -293,7 +296,7 @@
     });
     newbutton.appendTo(newdiv);
     
-    var newbutton = $('<button>Remove all(with newline)</button>').mouseup(function (e2) {
+    var newbutton = $('<button class="btn btn-default">Remove all(with newline)</button>').mouseup(function (e2) {
         var textKeep = $(this).parent().parent().html();
         var regexText=/<div(.*?)<\/div>/g;
         var replaceText="";
@@ -464,7 +467,14 @@
 }
 
 function smartRegexNew() {
-    $('#smartRegexPopUpDiv').css("display", "block");
+
+    var popup_status = $('#smartRegexPopUpDiv').css("display");
+    if (popup_status == "block") {
+        $('#smartRegexPopUpDiv').css("display", "none");
+    } else {
+        $('#smartRegexPopUpDiv').css("display", "block");
+    }
+
 
     $('#smartRegexPopUpAdd').attr("disabled", false);
     $('#smartRegexPopUpEdit').attr("disabled", "disabled");
@@ -476,8 +486,6 @@
 
 function replaceSmartClose() {
     $('#smartRegexShowDiv > span').css("border","1px solid black");
-
-
     $('#smartRegexPopUpDiv').css("display", "none");
     $("#smartRegexPopUpSelectWord").val("NULL");
     $("#smartRegexPopUpText").val("");
@@ -545,6 +553,7 @@
     replaceSmartClose();
 }
 
+
 $(document).on("click", '#smartRegexShowDiv > span', function (e) {
     
     
@@ -566,6 +575,247 @@
     $('#smartRegexPopUpFor').attr("disabled", false);
 });
 
+function genRegexWindowOpen(){
+    var btn_state = $('#regex_generator').css('display');
+    if (btn_state == "block") {
+        $("#regex_generator").css("display", "none");
+        $("#gen_regex_window_open_id").text("Open Gen Regex");
+    } else { 
+        $('#regex_generator').css("display", "block");
+        $("#gen_regex_window_open_id").text("Close Gen Regex");
+    }
+}
+function genRegexWindowClose(){
+    $('#regex_generator').css("display", "none");
+}
+
+function sharedStart_(array){
+    var A= array.concat().sort(), 
+    a1= A[0], a2= A[A.length-1], L= a1.length, i= 0;
+    while(i<L && a1.charAt(i)=== a2.charAt(i)) i++;
+    return a1.substring(0, i);
+}
+
+function longestCommonSubstring_(string1, string2){
+    // init max value
+    var longestCommonSubstring = 0;
+    // init 2D array with 0
+    var table = [],
+        len1 = string1.length,
+        len2 = string2.length,
+        row, col;
+    
+    for(row = 0; row <= len1; row++){
+        table[row] = [];
+        for(col = 0; col <= len2; col++){
+            table[row][col] = 0;
+        }
+    }
+    // fill table
+    var i, j;
+    for(i = 0; i < len1; i++){
+        for(j = 0; j < len2; j++){
+            if(string1[i]==string2[j]){
+                if(table[i][j] == 0){
+                    table[i+1][j+1] = 1;
+                } else {
+                    table[i+1][j+1] = table[i][j] + 1;
+                }
+                if(table[i+1][j+1] > longestCommonSubstring){
+                    longestCommonSubstring = table[i+1][j+1];
+                }
+            } else {
+                table[i+1][j+1] = 0;
+            }
+        }
+    }
+    return longestCommonSubstring;
+}
+
+function longestCommonSubstring(s1, s2) {
+   
+    var start_idx = 0;
+    var max_len = 0;
+    for (var i = 0; i < s1.length; i++)
+    {
+        for (var j = 0; j < s2.length; j++)
+        {
+            var x = 0;
+            while (s1.charAt(i + x) == s2.charAt(j + x))
+            {
+                x++;
+                if ((i + x) >= s1.length || ((j + x) >= s2.length))
+                    break;
+            }
+            if (x > max_len)
+            {
+                max_len = x;
+                start_idx = i;
+            }
+         }
+    }
+    return s1.substring(start_idx, (start_idx + max_len));
+
+}
+
+function getRegex(_pattern) {
+    console.log(_pattern[0]);
+    console.log(_pattern[1]);
+    var p0 = _pattern[0];
+    var p1 = _pattern[1];
+
+    // TODO: find common pattern
+    var reg_str = "";
+    // _p1 = 測試
+    // _p2 = 測<tag_name>試</tag_name>一下
+    var combined = [];
+    if (p0.length > p1.length) {
+        combined = p0;
+    } else if(p0.length < p1.length) {
+        combined = p1;
+    } else {    // equal length
+        // find matching string
+        var cnt = p0.length;
+        for (var i = 0;  i < cnt; i++) {
+            if (p1[i].tag != null) {
+                combined.push({tag:p1[i].tag, txt:"[^○如即而之有<>〈〉【】]{1,"+p1[i].txt.length+"}"});
+            } else if (p0[i].tag != null) {
+                combined.push({tag:p0[i].tag, txt:"[^○如即而之有<>〈〉【】]{1,"+p0[i].txt.length+"}"});
+            } else {
+                // find matching for text in each corresponding position
+                var texts = [p0[i].txt, p1[i].txt];
+                var common = longestCommonSubstring(p0[i].txt, p1[i].txt);
+                
+                /*
+                var reg_for_common = "[";
+                for (var i = 0; i < common.length; i++) {
+                    common[i];
+                    reg_for_common += common[i]+"|";
+                };
+                reg_for_common += "]";
+                */
+                combined.push({tag:null, txt:common});
+            }
+        };
+    }
+
+    for (var i = 0; i < combined.length; i++) {
+       reg_str += combined[i].txt;
+    };
+
+    return reg_str;
+}
+
+var pattern_obj = [];  // record pattern array for regex generator. only contain pattern1 and pattern2
+
+function genRegexBySelection(tag_item_div, _selection) {
+    var add_gen_regex_button = document.createElement("button");
+    $(add_gen_regex_button).id = "addToGenRegex";
+    $(add_gen_regex_button).addClass("btn btn-md");
+    $(add_gen_regex_button).click( function(){
+        // popup for selected words regex gen
+        console.log("Debug: ");
+        console.log(_selection);
+
+        if (_selection.type == "Range") {
+            // select words, not just click on text
+            var anchor_node = _selection.anchorNode;
+            var focus_node = _selection.focusNode;
+            var sibling_node = anchor_node.nextElementSibling;
+            
+
+            if (anchor_node && sibling_node && focus_node && container.innerHTML.indexOf( "br" ) == -1) {
+                // Chrome can work on this.
+                // Safari does not support some of the member in selection object
+                // container.innerHTML.indexOf( "br" ) == -1: selection does not contain br.
+
+                var seleted_div = document.createElement("div");
+                var seleted_obj = [];   // array for selected text as well as its tag if it has any
+
+                if (anchor_node == focus_node ) { 
+                    // selected text in plain text
+                    var text_all = anchor_node.textContent;
+                    var text_ = text_all.substring(_selection.anchorOffset, _selection.focusOffset);
+                    $(seleted_div).text(text_);
+                    seleted_obj.push({tag:null, txt:text_});    // push object into array
+
+                } else {
+                    // selected text contain tags
+                    var text_before = anchor_node.textContent.substring(_selection.anchorOffset, anchor_node.length);
+                    var tag_name = sibling_node.nodeName.toLowerCase();
+                    var tagged_text = sibling_node.textContent;
+
+                    var text_after = _selection.focusNode.textContent.substring(0, _selection.focusOffset);
+
+                    $(seleted_div).text(text_before+tagged_text+text_after);
+                    seleted_obj.push({tag:null, txt:text_before});
+                    seleted_obj.push({tag:tag_name, txt:tagged_text});
+                    seleted_obj.push({tag:null, txt:text_after});
+
+
+                    console.log(text_before);
+                    console.log(tag_name);
+                    console.log(tagged_text);
+                    console.log(text_after);
+                }
+                
+                
+                var generated_regex = "";
+                // show generate regex window
+                $('#regex_generator').css("display", "block");
+                $("#gen_regex_window_open_id").text("Close Gen Regex");
+
+                //var seleted_text = String(_selection).replace(/^\s+|\s+$/g,'');
+                var pattern1 = $('#regex_pattern1');
+                var pattern2 = $('#regex_pattern2');
+                if (pattern1.children().length == 0) {
+                    pattern1.append(seleted_div);
+                    pattern_obj.push(seleted_obj);
+                    // pattern1.text(seleted_div.text());
+                } else if (pattern2.children().length == 0) {
+                    pattern2.append(seleted_div);
+                    pattern_obj.push(seleted_obj);
+                    //pattern2.text(seleted_div.text());
+                    generated_regex = getRegex(pattern_obj);
+
+                } else {
+                    // pattern1 and pattern2 are already having text
+                    pattern1.children().remove();
+                    pattern1.append(pattern2.children());
+
+                    pattern2.children().remove();
+                    pattern2.append(seleted_div);
+
+                    pattern_obj.shift();
+                    pattern_obj.push(seleted_obj);
+
+                    //pattern1.text(pattern2.text());
+                    //pattern2.text(seleted_div);
+                    generated_regex = getRegex(pattern_obj);
+
+                }
+                $('#generated_regex').text(generated_regex);
+                // ---
+
+                $('#regex_generator_error_msg').text("");
+            } else {
+                $('#regex_generator_error_msg').text("Note: Not a valid selection for regex generator.");
+            }
+
+            $('.tagItemDivClass').remove(); // close the tag window
+
+        } else if (_selection.type == "Caret") {
+            // TODO: click on tagged text case rather than select
+            // If do this process, also need to consider between browers since not all of them support
+            // and also need to modify pop_remove_tag_window
+        }       
+    });
+
+    $(add_gen_regex_button).text("Add to Gen Regex");
+    tag_item_div.appendChild(add_gen_regex_button);    
+}
+
+
 function smartRegexEmpty() {
     $('#smartRegexShowDiv').html("");
     regex_element_index = 0;
@@ -789,7 +1039,12 @@
 
 function smartRegexLoad(topic_id) {
     $('#load_regex_div').html("");
-    $('#load_regex_div').css("display", "block");
+    var popup_status = $('#load_regex_div').css("display");
+    if (popup_status == "block") {
+        $('#load_regex_div').css("display", "none");
+    } else {
+        $('#load_regex_div').css("display", "block");
+    }
 
     var newselect = document.createElement("select");
     newselect.id = "loadRegexSelect";
--- a/models/extractapp.php	Thu Apr 30 10:52:05 2015 +0200
+++ b/models/extractapp.php	Tue Jun 02 16:52:05 2015 +0200
@@ -27,7 +27,9 @@
 class ExtractappModel extends BaseModel{
     
     protected $section_id = 0, $data_path, $file_id = 0, $current_fileId=0, 
-        $branch_id = 0, $user_id = 0, $lg_text = "", $topic, $taglist_infile = "", $book_meta = "",$book_dynasty="";
+        $branch_id = 0, $user_id = 0, $lg_text = "", $topic, $taglist_infile = "", 
+        $book_meta = "", $book_dynasty = "";
+
     public $messages = array('info'=>"",'error'=>"",'warning'=>"",'debug'=>"");
 
 
@@ -107,6 +109,12 @@
         $book_meta->in_jibengujiku = $b['in_jibengujiku'];
         $book_meta->admin_type = $b['admin_type'];
 
+        $coordinates = $section_meta['section']['coordinates_books'];
+        $book_meta->x = $coordinates['x'];
+        $book_meta->y = $coordinates['y'];
+        $book_meta->place_name = $coordinates['place_name'];
+
+
 
         $this->book_meta = $book_meta;
 
@@ -385,23 +393,26 @@
         $book = $this->book_meta;
        
         $text .= "<book>\n";
-        $text .= "<id>".$book->id."</id>\n";
-        $text .= "<name>".$book->name."</name>\n";
-        $text .= "<level1>".$book->level1."</level1>\n";
-        $text .= "<level2>".$book->level2."</level2>\n";
-        $text .= "<period>".$book->period."</period>\n";
-        $text .= "<dynasty>".$book->dynasty."</dynasty>\n";
-        $text .= "<start_year>".$book->start_year."</start_year>\n";
-        $text .= "<end_year>".$book->end_year."</end_year>\n";
-        $text .= "<line>".$book->line."</line>\n";
-        $text .= "<volume>".$book->volume."</volume>\n";
-        $text .= "<author>".$book->author."</author>\n";
-        $text .= "<edition>".$book->edition."</edition>\n";
-        $text .= "<in_jibengujiku>".$book->in_jibengujiku."</in_jibengujiku>\n";
-        $text .= "<admin_type>".$book->admin_type."</admin_type>\n";
+            $text .= "<id>".$book->id."</id>\n";
+            $text .= "<name>".$book->name."</name>\n";
+            $text .= "<level1>".$book->level1."</level1>\n";
+            $text .= "<level2>".$book->level2."</level2>\n";
+            $text .= "<period>".$book->period."</period>\n";
+            $text .= "<dynasty>".$book->dynasty."</dynasty>\n";
+            $text .= "<start_year>".$book->start_year."</start_year>\n";
+            $text .= "<end_year>".$book->end_year."</end_year>\n";
+            $text .= "<line>".$book->line."</line>\n";
+            $text .= "<volume>".$book->volume."</volume>\n";
+            $text .= "<author>".$book->author."</author>\n";
+            $text .= "<edition>".$book->edition."</edition>\n";
+            $text .= "<in_jibengujiku>".$book->in_jibengujiku."</in_jibengujiku>\n";
+            $text .= "<admin_type>".$book->admin_type."</admin_type>\n";
+       
+            $text .= "<place_name>".$book->place_name."</place_name>\n";
+            $text .= "<x>".$book->x."</x>\n";
+            $text .= "<y>".$book->y."</y>\n";
         $text .= "</book>\n";
         
-   
         /*
         foreach ($book as $b) {
             $text .= "<book>\n";
--- a/views/Extractapp/TaggingText.php	Thu Apr 30 10:52:05 2015 +0200
+++ b/views/Extractapp/TaggingText.php	Tue Jun 02 16:52:05 2015 +0200
@@ -216,6 +216,7 @@
     });
     // ---
 
+
     //--- for popups ---
     //run once
     var el=$('#popups');
@@ -252,7 +253,7 @@
     var info = JSON.parse('<?php echo json_encode($info) ?>');
     // the first time to this section. without branch_id and file_id
     if (info['file_id'] == 0) {  // or info['branch_id'] == 0, means new file
-        $( "#saveTextToLGService_id").text( "Create(Save) Branch to LGService" );
+        $( "#save_text_to_LGService_id").text( "Create(Save) Branch to LGService" );
 
     }
     handleFileVersionConflict();
@@ -271,7 +272,7 @@
 
         // TOOD: ask user to modify/decide tags
         // showing taglist_infile and taglistArray
-        alert("Warning: taglist is deprecated in the current file.");
+        //alert("Warning: taglist is deprecated in the current file.");
 
     }
 
@@ -290,15 +291,20 @@
     container = document.createElement("div");
     container.appendChild(selection.getRangeAt(0).cloneContents());
 
-    if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
+    if(selection && (selection_plain = new String(selection).replace(/^\s+|\s+$/g,''))) {
         try{
         	var newdiv = document.createElement("div");
-            newdiv.id = "tagItemDivId";
-            newdiv.setAttribute("class", "tagItemDivClass");
-            newdiv.style.cssText = 'top:'+e.pageY+'; left:'+e.pageX+'; position:absolute; background-color: white; border:1px solid black; padding: 5px; z-index:4';
-            
+            //newdiv.id = "tagItemDivId";
+            //newdiv.setAttribute("class", "tagItemDivClass");
+            $(newdiv).id = "tagItemDivId";
+            $(newdiv).addClass("tagItemDivClass");
+            newdiv.style.cssText = 'top:'+e.pageY+'; left:'+e.pageX+';';
             //console.log(selection.getRangeAt(0).cloneContents());
             if ( container.innerHTML.indexOf( "br" ) != -1 ) {
+                // when selected words containing tags (i.e. has 'br' in the selected string), 
+                // which means user want to apply Title / or tags all in once at each line
+                // This may not be the best checking solution, since 'br' also appears between not-tagged words
+
                 var newselect = document.createElement("select");
                 newselect.id = "TitletagType";
                 //console.log(JSON.parse('<?php echo json_encode($taglistArray) ?>'));
@@ -319,8 +325,6 @@
                 } else {
                     newdiv.innerHTML += "<input id=\"TitletagName\" value=\"\"></br>";
                 }
-
-
                 newdiv.innerHTML += "<button onclick=\"addTagTitle( range, container )\">Add Title Tag To Each Line</button></br>";
                 
                 /*
@@ -340,57 +344,52 @@
                 newdiv.innerHTML += "<button onclick=\"removeTagTitle( range, container )\">Remove</button></br>";
             } else {
      			// for pop up window on edit-area for tag list
-                newdiv.innerHTML = "Tagging word: "+String(selection)+"<br>";
-                
+                // newdiv.innerHTML = "Tagging word: "+String(selection)+"<br>";
+                var tagging_words = document.createElement("div");
+                $(tagging_words).addClass("bg-info");
+                $(tagging_words).text("Tagging words: " + String(selection_plain));
+
+                newdiv.appendChild(tagging_words);
+
+                var tagging_tags = document.createElement("div");
+                $(tagging_tags).addClass("bg-white");
+                $(tagging_tags).text("Tag as: ");
+
+                tagging_tags.innerHTML += "<button accesskey=\"2\" onclick=\"tagwithtitle( range, '"+String(selection_plain)+"' )\">Title</button></br>";
+               
                 <?php 
                 foreach ( $taglistArray as $taglistValue ) {
-
                     if ($taglistValue[2] == $topic_tag) {
-                    	echo "newdiv.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."2' )\\\">Tag as:".$taglistValue[1]."</button>\";\n";
-                        echo "newdiv.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(without new line)</button>\";\n";
-                        echo "newdiv.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n";
-                        
-                        /*
-                        echo "newdiv.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(No BR)</button>\";\n";
-                        echo "newdiv.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."2' )\\\">Tag as:".$taglistValue[1]."</button>\";\n";
-                        echo "newdiv.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n";
-                		*/
+                    	echo "tagging_tags.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection_plain)+\"', '".$taglistValue[2]."2' )\\\">".$taglistValue[1]."</button>\";\n";
+                        echo "tagging_tags.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection_plain)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(without new line)</button>\";\n";
+                        echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection_plain)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n";
                         break;
                 	}
                 }
+			    foreach ( $taglistArray as $taglistValue ) {
+                    if ($taglistValue[2] == $topic_tag) { 
+                		// pass
+                	} else if ($taglistValue[2] == "post_time") {
+                        echo "tagging_tags.innerHTML += \"<button accesskey=\\\"3\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."</button>\";\n";
+                        echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n";
+                    } else if ($taglistValue[2] == "office") {
+                        echo "tagging_tags.innerHTML += \"<button accesskey=\\\"4\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."</button>\";\n";
+                        echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n";
+                    } else {
+                        echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."</button>\";\n";
+                        echo "tagging_tags.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">".$taglistValue[1]."(ALL)</button></br>\";\n";
+                    } 
+                }
                 ?>
 
-                newdiv.innerHTML += "<button accesskey=\"2\" onclick=\"tagwithtitle( range, '"+String(selection)+"' )\">Tag As Title</button></br>";
-                
-                <?php 
-                	
-    			    foreach ( $taglistArray as $taglistValue ) {
-                    	/*
-                    	if ( $taglistValue[2] == "person") {
-                            echo "newdiv.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(No BR)</button>\";\n";
-                            echo "newdiv.innerHTML += \"<button accesskey=\\\"1\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."2' )\\\">Tag as:".$taglistValue[1]."</button>\";\n";
-                            echo "newdiv.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n";
-                    	} else */
-                        if ($taglistValue[2] == $topic_tag) { 
-                    		// pass
-                    	} else if ($taglistValue[2] == "post_time") {
-                            echo "newdiv.innerHTML += \"<button accesskey=\\\"3\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."</button>\";\n";
-                            echo "newdiv.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n";
-                        } else if ($taglistValue[2] == "office") {
-                            echo "newdiv.innerHTML += \"<button accesskey=\\\"4\\\" onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."</button>\";\n";
-                            echo "newdiv.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n";
-                        } else {
-                            echo "newdiv.innerHTML += \"<button onclick=\\\"tagwithOnlytag( range, '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."</button>\";\n";
-                            echo "newdiv.innerHTML += \"<button onclick=\\\"tagStringWithTag( '\"+String(selection)+\"', '".$taglistValue[2]."' )\\\">Tag as:".$taglistValue[1]."(ALL)</button></br>\";\n";
-                        } 
+                newdiv.appendChild(tagging_tags);
+                // add selected words to generator regex
+                genRegexBySelection(newdiv, selection); // append button to newdiv and handle the selection words
 
-                    }
-                ?>
             }
 
             //$('#tagItemDivId').addClass();  // tagItemDivId is newdiv's id
             $('body').append(newdiv);
-            
             $('#TitletagType').val(lastAddTag);
 
             // selected some words, so hide popping up remove-tag-window
@@ -405,15 +404,9 @@
         pop_remove_tag_window = true;
     }
 
-
-
     //e.stopPropagation();
-
-
-
 });
 
-
 // === for exporttable.php ===
 function exportTable( range, container ) {
     //MsgBox("enter function");
@@ -708,9 +701,13 @@
     var t = JSON.parse( '<?php echo json_encode($topiclistArray) ?>' );
     var info = JSON.parse('<?php echo json_encode($info) ?>');
 
-
     $('#load_topic_div').html("");
-    $('#load_topic_div').css("display", "block");
+    var popup_status = $('#load_topic_div').css("display");
+    if (popup_status == "block") {
+        $('#load_topic_div').css("display", "none");
+    } else {
+        $('#load_topic_div').css("display", "block");
+    }
     /*
     $('#load_topic_div').css("border", "1px solid black");
     $('#load_topic_div').css("background-color", "White");
@@ -903,9 +900,22 @@
 
 
 </script>
- 
-<div id="dialog-form" title="config tags for topic" style="position:fixed; top: 30px; ">
-</div>
+
+
+<!--
+<fieldset class="info-board">
+    <legend><h5 class="tool-topic" data-toggle="collapse" data-target="#info_board">Information <span class="caret"></span></h5></legend>
+    <div id="info_board" class="collapse in">
+        <div class="bg-danger"><strong><?php echo $messages['error']; ?> </strong></div>
+        <div class="bg-warning"> <?php echo $messages['warning']; ?> 
+            <button id="reloadTextButton" onclick="reloadText(<?php echo $default_topic_id; ?>)" style="display:none;">Latest Version</button>
+        </div>
+        <div class="bg-info"> <?php echo $messages['info']; ?> </div>
+        <div class="bg-default"> <?php echo $messages['debug']; ?> </div>
+    </div>
+</fieldset>
+
+-->
 
 <div class="info-board">
     <div class="bg-danger"><strong><?php echo $messages['error']; ?> </strong></div>
@@ -916,6 +926,7 @@
     <div class="bg-default"> <?php echo $messages['debug']; ?> </div>
 </div>
 
+
 <table border="1" style="margin:10 30 30 30; width:100%; max-width:1270px; min-width:680px">
     <tr>
         <td style="padding:0 5 0 5; margin: 0 5 0 5;">
@@ -925,6 +936,20 @@
             <div id="popups" style="position:absolute; width:300px; ">
                 <div id="load_topic_div" class="popup-topic popup-frame"></div>
                 <div id="load_regex_div" class="popup-loadregex popup-frame"></div>
+                <div id="regex_generator" class="popup-regex popup-frame">
+                    <h3>Gen Regex</h3>
+                    <div>Pattern 1: 
+                        <div id="regex_pattern1" class="bg-info"></div>
+                    </div>
+                    <div>Pattern 2:
+                        <div id="regex_pattern2" class="bg-info"></div>
+                    </div>
+                    <div>Suggested Regex: 
+                        <div id="generated_regex" class="bg-success"></div>
+                    </div>
+                    <button onclick="genRegexWindowClose()" class="btn btn-default btn-xs">Close</button>
+                    <div id="regex_generator_error_msg" class="bg-white"></div>
+                </div>
                 <div id="smartRegexPopUpDiv" class="popup-regex popup-frame">
                     Name: <input id="smartRegexPopUpName"></input><br><br>
                     Word List: 
@@ -960,75 +985,94 @@
 
             <div id="follow-scroll">
                 <form action="javascript:void(0);">   
-                    <fieldset>
-                        <legend><h5 class="text-success"> Topic: </h5></legend>
-                        <div> Current topic is: <br> 
-                            <?php echo $topic_name['name_en']; ?> 
-                            (<?php echo $topic_name['name_ch']; ?>, <?php echo $topic_name['name_pinyin']; ?>)
-                            <button onclick="chooseTopic(<?php echo $default_topic_id;?>)" class="btn-xs">Change</button>
+                    <fieldset class="tool-frame">
+                        <legend><h5 class="tool-topic" data-toggle="collapse" data-target="#tool_topic"> Topic: <span class="caret"></span></h5></legend>
+                        <div id="tool_topic" class="collapse in">
+                            <div> Current topic is: <br> 
+                                <?php echo $topic_name['name_en']; ?> 
+                                (<?php echo $topic_name['name_ch']; ?>, <?php echo $topic_name['name_pinyin']; ?>)
+                                <button onclick="chooseTopic(<?php echo $default_topic_id;?>)" class="btn btn-default btn-xs">Change</button>
+                            </div>
+                            <div>The topic tag is: <?php echo $topic_tag_ch;?>(<?php echo $topic_tag;?>)</div>
+                            <button onclick="editTaglist(<?php echo $default_topic_id; ?>)" class="btn btn-default btn-block">Edit Tag List</button>
+                            <button onclick="configTagsInTopic(<?php echo $default_topic_id;?>)" class="btn btn-default btn-block">Select Existing Tags</button>
                         </div>
-                        <div>The topic tag is: <?php echo $topic_tag_ch;?>(<?php echo $topic_tag;?>)</div>
                     </fieldset>
 
-                    <fieldset>
-                        <legend><h5 class="text-success"> Replace By <i><b>Smart Regex</b></i>©: </h5></legend>
-                        <div id="smartRegexShowDiv"></div><br>
-                        <!-- <button onclick="smartRegexNew()" class="btn-lg">Add Regex Group</button></br>
-                        -->
-                        <button onclick="smartRegexNew()" class="btn-lg">Search by Patterns (Smart Regex)</button></br>
-                        Range: <input type="text" size="5" id="regexPageStart">to<input type="text" size="5" id="regexPageEnd"><br>
-                        <button onclick="replaceSmartRun()" class="btn-sm">Run</button>
-                        <button onclick="replaceSmartRunWithBr()" class="btn-md">Run(in new line)</button></br>
-                        <!-- <button onclick="replaceSmartRunSpace()" class="btn-lg">Run(Allow space between Group)</button></br>
-                        -->
-                        <button onclick="replaceSmartRunSpace()" class="btn-lg">Run(Allow space between block)</button></br>
-                        <button onclick="smartRegexSave(<?php echo $default_topic_id; ?>)" class="btn-sm">Save</button>
-                        <button onclick="smartRegexLoad(<?php echo $default_topic_id; ?>)" class="btn-sm">Load</button>
-                        <button onclick="smartRegexEmpty()" class="btn-sm">Clear</button></br>
+                    <fieldset class="tool-frame">
+                        <legend><h5 class="tool-topic" data-toggle="collapse" data-target="#tool_regex"> Replace By <i><b>Smart Regex</b></i>©: <span class="caret"></span></h5></legend>
+                        
+                        <div id="tool_regex" class="collapse in">
+                            <div id="smartRegexShowDiv"></div><br>
+                            <!-- <button onclick="smartRegexNew()" class="btn-lg">Add Regex Group</button></br>
+                            -->
+                            <button onclick="smartRegexNew()" id="smart_regex_new_id" class="btn btn-default btn-block">Search by Patterns (Smart Regex)</button>
+
+                            <button onclick="smartRegexLoad(<?php echo $default_topic_id; ?>)" class="btn btn-default">Load</button>
+                            <button onclick="smartRegexSave(<?php echo $default_topic_id; ?>)" class="btn btn-default">Save</button>
+                            <button onclick="smartRegexEmpty()" class="btn btn-default">Clear</button></br>
+
+
+                            Range: <input type="text" size="5" id="regexPageStart">to<input type="text" size="5" id="regexPageEnd"><br>
+                            <button onclick="replaceSmartRun()" class="btn btn-default">Run</button>
+                            <button onclick="replaceSmartRunWithBr()" class="btn btn-default">Run(in new line)</button></br>
+                            <!-- <button onclick="replaceSmartRunSpace()" class="btn-lg">Run(Allow space between Group)</button></br>
+                            -->
+                            <button onclick="replaceSmartRunSpace()" class="btn btn-default">Run(Allow space between block)</button></br>
+                            <button onclick="Undo()" class="btn btn-default btn-block" id="buttonUndo" disabled="true">Undo</button>
+
+                            <!-- developing...-->
+                            <button onclick="genRegexWindowOpen()" id="gen_regex_window_open_id" class="btn btn-default">Open GenRegex(test)</button></br>
+                        </div>
                     </fieldset>
                     
-                    <fieldset>
-                        <legend><h5 class="text-success"> Tag by rule:</h5></legend>
-                        <button onclick="tagNameWithLastName()" class="btn-lg">Tag Word Begin With Surname</button></br>
-                        <button onclick="tagNameWithLastName2()" style="height: 35px; width: 170px">Tag Word Begin With</button><input type="text" size="2" id="surname"></br>
+
+                    <?php
+                    if ($default_topic_id == 1) {
+                    ?>
+                    <fieldset class="tool-frame">
+                        <legend><h5 class="tool-topic" data-toggle="collapse" data-target="#tool_rule"> Tag by rule: <span class="caret"></span></h5></legend>
+                        <div id="tool_rule" class="collapse out">
+                            <button onclick="tagNameWithLastName()" class="btn btn-default">Tag Word Begin With Surname</button></br>
+                            <button onclick="tagNameWithLastName2()" class="btn btn-default btn-sm">Tag Word Begin With</button> <input type="text" size="2" id="surname">
+                        </div>
                         <!--<button onclick="tagTime()" style="height: 30px; width: 220px">Tag Time</button></br>-->
                         <!--<button onclick="tagBiogAddr()" style="height: 30px; width: 220px">Tag BiogAddr</button></br>-->
-                        <button onclick="Undo()" class="btn-lg" id="buttonUndo" disabled="true">Undo</button>
                     </fieldset>
-                                    
-                    <fieldset>
-                        <legend><h5 class="text-success"> Save:</h5></legend>
-                        <button onclick="saveTextToLGService()" id="saveTextToLGService_id" class="btn-lg">Save to LGService</button></br>
-                    </fieldset>
-                                    
-                    <fieldset>
-                        <legend><h5 class="text-success"> Edit:</h5></legend>
+
+                    <?php }?>
+                                     
+                    <fieldset class="tool-frame">
+                        <legend><h5 class="tool-topic" data-toggle="collapse" data-target="#tool_edit"> Edit: <span class="caret"></span></h5></legend>
                         <!--<button onclick="cleanUpTextArea()" style="height: 30px; width: 220px">Reform the text</button></br>-->
                         <!-- save text in local. replaced by saveTextToLGService, which is also saving on copy in local in development stage -->
                         <!-- 
                         <button onclick="saveText(<?php echo $section_id; ?>)" style="height: 30px; width: 220px">Save the text</button></br>
                         -->
-
-                        <button onclick="editText()" id="editTextId" class="btn-lg">Edit the text</button></br>
-                        </br>
-                        <button onclick="window.open('./EditWordlist')" class="btn-lg">Edit Word List</button></br>
-                        <button onclick="editTaglist(<?php echo $default_topic_id; ?>)" class="btn-lg">Edit Tag List</button></br>
-                        <button onclick="configTagsInTopic(<?php echo $default_topic_id;?>)" class="btn-lg">Select Existing Tags</button></br>
+                        <div class="collapse out" id="tool_edit">
+                            <button onclick="editText()" id="editTextId" class="btn btn-default btn-block">Edit the text</button>
+                            <button onclick="window.open('./EditWordlist')" class="btn btn-default btn-block">Edit WordList</button>
+                        </div>
                     </fieldset>
                     
-                    <fieldset>
-                        <legend><h5 class="text-success"> Export:</h5></legend>
-                        Page: <input type="text" size="5" id="exportPageStart">to<input type="text" size="5" id="exportPageEnd">
-                        <button onclick="exportPage(<?php echo $default_topic_id;?>)" class="btn-sm">Export</button></br>
-                        <button onclick="exportAll(<?php echo $default_topic_id;?>)" class="btn-lg">Export All</button></br>
+                    <fieldset class="tool-frame">
+                        <legend><h5 class="tool-topic" data-toggle="collapse" data-target="#tool_save"> Save and Export: <span class="caret"></span></h5></legend>
+                        <div class="collapse in" id="tool_save">
+                            <button onclick="saveTextToLGService()" id="save_text_to_LGService_id" class="btn btn-default btn-block">Save to LGService</button>
+                            <button onclick="exportAll(<?php echo $default_topic_id;?>)" class="btn btn-default btn-block">Export All</button>
+                            Page: <input type="text" size="5" id="exportPageStart">to<input type="text" size="5" id="exportPageEnd">
+                            <button onclick="exportPage(<?php echo $default_topic_id;?>)" class="btn btn-default btn-sm">Export</button></br>
+                        </div>
                     </fieldset>
                 
-                    <fieldset>
-                        <legend><h5 class="text-success"> Replace By <i><b>Regex</b></i>: </h5></legend>
-                        Range: <input type="text" size="5" id="regexPageStart2">to<input type="text" size="5" id="regexPageEnd2"><br>
-                        Regex: <input type="text" size="23" id="regexText"></br>
-                        Replace: <input type="text" size="23" id="replaceText"><br>
-                        <button onclick="replaceSmartRegex()">Replace!</button>
+                    <fieldset class="tool-frame">
+                        <legend><h5 class="tool-topic" data-toggle="collapse" data-target="#tool_replace_by"> Replace By <i><b>Regex</b></i>: <span class="caret"></span></h5></legend>
+                        <div class="collapse out" id="tool_replace_by">
+                            Range: <input type="text" size="5" id="regexPageStart2">to<input type="text" size="5" id="regexPageEnd2"><br>
+                            Regex: <input type="text" size="23" id="regexText"></br>
+                            Replace: <input type="text" size="23" id="replaceText"><br>
+                            <button onclick="replaceSmartRegex()" class="btn btn-default btn-sm">Replace!</button>
+                        </div>
                     </fieldset>
                 </form>
             </div>
--- a/views/maintemplate.php	Thu Apr 30 10:52:05 2015 +0200
+++ b/views/maintemplate.php	Tue Jun 02 16:52:05 2015 +0200
@@ -5,7 +5,9 @@
 <script src="../js/jquery-ui.js"></script>
 <script src="../js/taggingtext.js"></script>
 <script src="../js/cjst.js"></script>
+<script src="../js/bootstrap.min.js"></script>
 <link href="../css/taggingtext.css" rel="stylesheet">
+<link href="../css/bootstrap.min.css" rel="stylesheet">
 
 </head>
 <body>
--- a/views/maintemplate_local.php	Thu Apr 30 10:52:05 2015 +0200
+++ b/views/maintemplate_local.php	Tue Jun 02 16:52:05 2015 +0200
@@ -5,7 +5,9 @@
 <script src="../../js/jquery-ui.js"></script>
 <script src="../../js/taggingtext.js"></script>
 <script src="../../js/cjst.js"></script>
+<script src="../../js/bootstrap.min.js"></script>
 <link href="../../css/taggingtext.css" rel="stylesheet">
+<link href="../../css/bootstrap.min.css" rel="stylesheet">
 
 </head>
 <body>