JAL-857 updated examples directory web pages to be dynamicaly decorated with jalview...
[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 signedJars = "jalviewApplet.jar,JmolApplet-12.2.4.jar,java-json.jar,json_simple-1.1.jar";
23     var unsignedJars = "u_jalviewApplet.jar,u_JmolApplet-12.2.4.jar,u_java-json.jar,u_json_simple-1.1.jar";
24     setOrUpdateCookie('jv_applet_mode', 'false', '1');
25     var isSignedAppletMode = readCookie("jv_applet_mode");
26     var appletJars = (isSignedAppletMode === 'true') ? signedJars : unsignedJars;
27     //alert(unsigned);
28
29
30     function createAppletTag(code, name, archive, width, height, params){
31         var app = document.createElement('applet');
32         app.code= code;
33         app.width = width;
34         app.height = height;
35         app.archive = archive;
36
37         var arrayLength = params.length;
38         for (var i = 0; i < arrayLength; i++) {
39             //console.log('name : '+ params[i][0] + ' code : '+ params[i][1]);              
40             var param = document.createElement('param');
41             param.name = params[i][0];
42             param.value = params[i][1];
43             app.appendChild(param);
44         }
45         return app;
46     }
47
48     function readCookie(name) {
49         var nameEQ = name + "=";
50         var ca = document.cookie.split(';');
51         for(var i=0;i < ca.length;i++) {
52             var c = ca[i];
53             while (c.charAt(0)==' ') c = c.substring(1,c.length);
54             if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
55         }
56         return null;
57     }
58
59     function setOrUpdateCookie(name, value, days) {
60         var expires;
61         if (days) {
62             var date = new Date();
63             date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
64             expires = "; expires=" + date.toGMTString();
65         }
66         else {
67             expires = "";
68         }
69         document.cookie = name + "=" + value + expires + "; path=/";
70     }
71
72    $(function(){
73        //var currentPage = (readCookie("current_page")) ? readCookie("current_page") : "applets.html";
74        //alert("cur page : " + currentPage); 
75        $('#header').load("includes/header_jv.html");
76        $('#content').load(currentPage+".html");
77        $('#nav').load("includes/nav_jv.html");
78        $('#footer').load("includes/footer_jv.html"); 
79        $('#'+ currentPage).addClass('active-trail active');       
80    });
81    
82   $(document).ready(function(){
83     //var currentPage = (readCookie("current_page")) ? readCookie("current_page") : "applets.html";
84     //updateLinks(currentPage);
85     var params = [["file","uniref50.fa"],
86                   ["treeFile","ferredoxin.nw"],
87                   ["userDefinedColour","C=yellow; R,K,H=FF5555; D,E=5555F"],
88                   ["sortByTree","True"],
89                   ["showSequenceLogo","true"],
90                   ["showGroupConsensus","true"],
91                   ["showFullId","false"],
92                   ["linkLabel_1","Uniprot"],
93                   ["linkUrl_1","http://www.uniprot.org/uniprot/$SEQUENCE_ID$"],
94                   ["linkLabel_2","EMBL-EBI Search"],
95                   ["linkUrl_2","http://www.ebi.ac.uk/ebisearch/search.ebi?db=allebi&query=$SEQUENCE_ID$"],
96                   ["APPLICATION_URL","http://www.jalview.org/services/launchApp"]];
97     var app = createAppletTag('jalview.bin.JalviewLite','',appletJars,'140','35', params);
98     console.log('—————————> app ———————>  ' + app);
99     //var appDiv = document.getElementById("applet_div");
100     //var appDiv = document.getElementById("applet_div");
101     //appDiv.appendChild(app);
102     $("<div class='newbox'>I'm new box by appendTo</div>").appendTo('.applet_div');
103     //$(‘.applet_div').append(app);
104   });
105
106 function doSubmit(target){
107    setOrUpdateCookie('current_page', target, '1');
108    //alert('current page : ' + readCookie("current_page"));
109    var currentPage = target+'.html';
110    $('#content').load(currentPage);
111    updateLinks(target);
112 }
113
114 function updateLinks(target) {
115     var ul = document.getElementById("menu");
116     var items = ul.getElementsByTagName("li");
117     for (var i = 0; i < items.length; ++i) {
118         removeClass(items[i], "active-trail active");
119     }
120    $('#'+ target).addClass('active-trail active');
121 }
122
123 function hasClass(ele,cls) {
124   return !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
125 }
126
127 function addClass(ele,cls) {
128   if (!hasClass(ele,cls)) ele.className += " "+cls;
129 }
130
131 function removeClass(ele,cls) {
132   if (hasClass(ele,cls)) {
133     var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
134     ele.className=ele.className.replace(reg,' ');
135   }
136 }