JAL-857 modified javascipt to read browser url on page load and redirect accordingly
[jalview.git] / examples / javascript / jvcontroller.js
1 /**
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19
20  
21     //var currentPage = (readCookie("current_page")) ? readCookie("current_page") : "applets";
22     var currentPage = "applets";
23     var signedJars = "jalviewApplet.jar,JmolApplet-12.2.4.jar,java-json.jar,json_simple-1.1.jar";
24     var unsignedJars = "u_jalviewApplet.jar,u_JmolApplet-12.2.4.jar,u_java-json.jar,u_json_simple-1.1.jar";
25     setOrUpdateCookie('jv_applet_mode', 'false', '1');
26     //var isSignedAppletMode = readCookie("jv_applet_mode");
27     //var appletJars = (isSignedAppletMode === 'true') ? signedJars : unsignedJars;
28     //alert(unsigned);
29
30
31     function createAppletTag(code, name, archive, width, height, params){
32         var app = document.createElement('applet');
33         app.code= code;
34         app.width = width;
35         app.height = height;
36         app.archive = archive;
37
38         var arrayLength = params.length;
39         for (var i = 0; i < arrayLength; i++) {
40             //console.log('name : '+ params[i][0] + ' code : '+ params[i][1]);              
41             var param = document.createElement('param');
42             param.name = params[i][0];
43             param.value = params[i][1];
44             app.appendChild(param);
45         }
46         return app;
47     }
48
49     function readCookie(name) {
50         var nameEQ = name + "=";
51         var ca = document.cookie.split(';');
52         for(var i=0;i < ca.length;i++) {
53             var c = ca[i];
54             while (c.charAt(0)==' ') c = c.substring(1,c.length);
55             if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
56         }
57         return null;
58     }
59
60     function setOrUpdateCookie(name, value, days) {
61         var expires;
62         if (days) {
63             var date = new Date();
64             date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
65             expires = "; expires=" + date.toGMTString();
66         }
67         else {
68             expires = "";
69         }
70         document.cookie = name + "=" + value + expires + "; path=/";
71     }
72
73    $(function(){        
74         var url = window.location.href;
75         var end = url.length;
76         var start = url.lastIndexOf("#");
77         var newPage = url.substring(start + 1, end);
78         var page = ((start === -1) ? currentPage : newPage);
79         alert("page:" + page); 
80        $('#header').load("includes/header_jv.html");
81        $('#content').load(page+".html");
82        $('#nav').load("includes/nav_jv.html");
83        $('#footer').load("includes/footer_jv.html"); 
84        $('#'+ currentPage).addClass('active-trail active');       
85    });
86    
87   $(document).ready(function(){
88     //var currentPage = (readCookie("current_page")) ? readCookie("current_page") : "applets.html";
89     //updateLinks(currentPage);
90     var params = [["file","uniref50.fa"],
91                   ["treeFile","ferredoxin.nw"],
92                   ["userDefinedColour","C=yellow; R,K,H=FF5555; D,E=5555F"],
93                   ["sortByTree","True"],
94                   ["showSequenceLogo","true"],
95                   ["showGroupConsensus","true"],
96                   ["showFullId","false"],
97                   ["linkLabel_1","Uniprot"],
98                   ["linkUrl_1","http://www.uniprot.org/uniprot/$SEQUENCE_ID$"],
99                   ["linkLabel_2","EMBL-EBI Search"],
100                   ["linkUrl_2","http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$"],
101                   ["APPLICATION_URL","http://www.jalview.org/services/launchApp"]];
102     var app = createAppletTag('jalview.bin.JalviewLite','',appletJars,'140','35', params);
103     console.log('—————————> app ———————>  ' + app);
104     //var appDiv = document.getElementById("applet_div");
105     //var appDiv = document.getElementById("applet_div");
106     //appDiv.appendChild(app);
107     $("<div class='newbox'>I'm new box by appendTo</div>").appendTo('.applet_div');
108     //$(‘.applet_div').append(app);
109   });
110
111 function doSubmit(target){
112    //setOrUpdateCookie('current_page', target, '1');
113    //alert('current page : ' + readCookie("current_page"));
114    var currentPage = target+'.html';
115    $('#content').load(currentPage);
116    updateLinks(target);
117 }
118
119 function updateLinks(target) {
120     var ul = document.getElementById("menu");
121     var items = ul.getElementsByTagName("li");
122     for (var i = 0; i < items.length; ++i) {
123         removeClass(items[i], "active-trail active");
124     }
125    $('#'+ target).addClass('active-trail active');
126 }
127
128 function hasClass(ele,cls) {
129   return !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
130 }
131
132 function addClass(ele,cls) {
133   if (!hasClass(ele,cls)) ele.className += " "+cls;
134 }
135
136 function removeClass(ele,cls) {
137   if (hasClass(ele,cls)) {
138     var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
139     ele.className=ele.className.replace(reg,' ');
140   }
141 }