73f6670e4183b20b4e15d7de94d96d1ffa7e2b8a
[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     
22     var currentPage = "applets";
23
24     function createAppletTag(code, name, archive, width, height, params){
25         var app = document.createElement('applet');
26         app.code= code;
27         app.width = width;
28         app.height = height;
29         app.archive = archive;
30
31         var arrayLength = params.length;
32         for (var i = 0; i < arrayLength; i++) {
33             //console.log('name : '+ params[i][0] + ' code : '+ params[i][1]);              
34             var param = document.createElement('param');
35             param.name = params[i][0];
36             param.value = params[i][1];
37             app.appendChild(param);
38         }
39         return app;
40     }
41
42     function readCookie(name) {
43         var nameEQ = name + "=";
44         var ca = document.cookie.split(';');
45         for(var i=0;i < ca.length;i++) {
46             var c = ca[i];
47             while (c.charAt(0)==' ') c = c.substring(1,c.length);
48             if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
49         }
50         return null;
51     }
52
53     function setOrUpdateCookie(name, value, days) {
54         var expires;
55         if (days) {
56             var date = new Date();
57             date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
58             expires = "; expires=" + date.toGMTString();
59         }
60         else {
61             expires = "";
62         }
63         document.cookie = name + "=" + value + expires + "; path=/";
64     }
65
66    $(function(){        
67         var url = window.location.href;
68         var end = url.length;
69         var start = url.lastIndexOf("#");
70         var newPage = url.substring(start + 1, end);
71         var page = ((start === -1) ? currentPage : newPage);
72         //alert("page:" + page); 
73        if(page === "embeddedWJmol"){
74          // do nothing embeddedWJmol page already include
75
76             $('#header').load("includes/header_jv.html");
77             //$('#content').load(page + ".html");
78             $('#nav').load("includes/nav_jv.html");
79             $('#footer').load("includes/footer_jv.html"); 
80             $('#'+ currentPage).addClass('active-trail active'); 
81         }else{
82             $('#header').load("includes/header_jv.html");
83             $('#content').load(page + ".html");
84             $('#nav').load("includes/nav_jv.html");
85             $('#footer').load("includes/footer_jv.html"); 
86             $('#'+ currentPage).addClass('active-trail active');  
87
88             var e = document.getElementById("view_decorated");
89             e.style.display = 'none';
90         }     
91    });
92    
93
94 function doSubmit(target){
95    var currentPage = target+'.html';
96    //alert("page:" + target); 
97    if(target == "embeddedWJmol"){
98       //loadJMolPage();
99       window.location.href = 'embeddedWJmol.html#' + target;
100       $('#content').load(currentPage);
101    }else{
102       window.location.href = 'index.html#' + target;
103       $('#content').load(currentPage);
104    }
105    updateLinks(target);
106 }
107
108
109
110 function updateLinks(target) {
111     var ul = document.getElementById("menu");
112     var items = ul.getElementsByTagName("li");
113     for (var i = 0; i < items.length; ++i) {
114         removeClass(items[i], "active-trail active");
115     }
116    $('#'+ target).addClass('active-trail active');
117 }
118
119 function hasClass(ele,cls) {
120   return !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
121 }
122
123 function addClass(ele,cls) {
124   if (!hasClass(ele,cls)) ele.className += " "+cls;
125 }
126
127 function removeClass(ele,cls) {
128   if (hasClass(ele,cls)) {
129     var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
130     ele.className=ele.className.replace(reg,' ');
131   }
132 }
133