JAL-3210 New site-resources dir too
[jalview.git] / utils / jalviewjs / site-resources / 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 
17  * the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
18  * The Jalview Authors are detailed in the 'AUTHORS' file.
19  */
20
21
22 $(document).ready(function() {
23         processAllAppletElements();
24 });
25
26
27     
28     var currentPage = "applets";
29
30     function processAllAppletElements() {
31         var apps = document.getElementsByTagName("applet");
32         for (var i = apps.length; --i >= 0;) {
33                 processAppletElement(apps[i]);
34         }
35     }
36        
37     var jvid = 0;
38
39     function testBtn(e) {
40         var element = e.target.appletElement;
41         var text = element.outerHTML;
42         
43         //var
44         var Info = self.JalviewInfo || {
45   code: null,
46   main: "jalview.bin.Jalview",
47   core: "NONE",
48         
49         
50         width: 850,
51         height: 550,
52   readyFunction: null,
53         serverURL: 'https://chemapps.stolaf.edu/jmol/jsmol/php/jsmol.php',
54         j2sPath: 'swingjs/j2s',
55 //      console:'sysoutdiv',
56         allowjavascript: true
57 }
58         
59 var id = "JVApplet" + jvid++;
60 var args = text.replace(/[\n\t]/g, " ").split("<param ");
61 Info.j2sAppletID = id;
62 Info.jalview_SCREEN_WIDTH= 100, // desktop width -- 0 to hide
63 Info.jalview_SCREEN_HEIGHT= 70,  // desktop height -- 0 to hide
64 Info.jalview_SCREEN_X= 10,
65 Info.jalview_SCREEN_Y= 10,
66 Info.jalview_EMBEDDED= true;
67
68
69 for (var i in Info) {
70         var v = ("" + Info[i] || "null").replace(/\"/g,"'");
71         args.push("name=\"Info." + i + "\" value=\"" + v + "\""); 
72 }
73 Info.args = args;
74 SwingJS.getApplet(id, Info);
75         document.title = id;
76         e.target.style.visibility="hidden";
77
78     }
79     
80     function processAppletElement(element) {
81         var code = element.getAttribute("code");
82         var parent = element.parentElement;
83         if (code == "jalview.bin.JalviewLite") {
84                 var text = element.outerHTML;
85                 console.log(text);
86                 var btn = document.createElement("button");
87                 btn.innerHTML = "Start Jalview";
88                 var a = element.getAttribute("width");
89                 btn.style.width = (a || 140) + "px";
90                 a = element.getAttribute("height");
91                 btn.style.height = (a || 25) + "px";
92                 btn.appletElement = element;
93                 parent.replaceChild(btn, element);        
94                 $(btn).click(testBtn)
95         } else {
96                 parent.removeElement(element);
97         }
98     }
99        
100
101     /** 
102      * Generate an applet tag
103      * 
104      * @param code
105      * @param name
106      * @param archive
107      * @param width
108      * @param height
109      * @param params
110      * @returns  a DOM APPLET element
111      */
112     function createAppletTag(code, name, archive, width, height, params){
113         var app = document.createElement('applet');
114         app.code= code;
115         app.width = width;
116         app.height = height;
117         app.archive = archive;
118
119         var arrayLength = params.length;
120         for (var i = 0; i < arrayLength; i++) {
121             //console.log('name : '+ params[i][0] + ' code : '+ params[i][1]);              
122             var param = document.createElement('param');
123             param.name = params[i][0];
124             param.value = params[i][1];
125             app.appendChild(param);
126         }
127         return app;
128     }
129
130     function readCookie(name) {
131         var nameEQ = name + "=";
132         var ca = document.cookie.split(';');
133         for(var i=0;i < ca.length;i++) {
134             var c = ca[i];
135             while (c.charAt(0)==' ') c = c.substring(1,c.length);
136             if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
137         }
138         return null;
139     }
140
141     function setOrUpdateCookie(name, value, days) {
142         var expires;
143         if (days) {
144             var date = new Date();
145             date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
146             expires = "; expires=" + date.toGMTString();
147         }
148         else {
149             expires = "";
150         }
151         document.cookie = name + "=" + value + expires + "; path=/";
152     }
153
154    $(function(){        
155         var url = window.location.href;
156         var end = url.length;
157         var start = url.lastIndexOf("#");
158         var newPage = url.substring(start + 1, end);
159         var page = ((start === -1) ? currentPage : newPage);
160         //alert("page:" + page); 
161        if(page === "embeddedWJmol"){
162          // do nothing embeddedWJmol page already include
163
164             $('#header').load("includes/header_jv.html");
165             //$('#content').load(page + ".html");
166             $('#nav').load("includes/nav_jv.html");
167             $('#footer').load("includes/footer_jv.html"); 
168             $('#'+ currentPage).addClass('active-trail active'); 
169         }else{
170             $('#header').load("includes/header_jv.html");
171             $('#content').load(page + ".html");
172             $('#nav').load("includes/nav_jv.html");
173             $('#footer').load("includes/footer_jv.html"); 
174             $('#'+ currentPage).addClass('active-trail active');  
175
176             var e = document.getElementById("view_decorated");
177             e.style.display = 'none';
178         }     
179    });
180    
181 function doSubmit(target){
182    var currentPage = target+'.html';
183    //alert("page:" + target); 
184    if(target == "embeddedWJmol"){
185       //loadJMolPage();
186       window.location.href = 'embeddedWJmol.html#' + target;
187       $('#content').load(currentPage);
188    }else{
189       window.location.href = 'index.html#' + target;
190       $('#content').load(currentPage);
191    }
192    updateLinks(target);
193 }
194
195
196
197 function updateLinks(target) {
198     var ul = document.getElementById("menu");
199     var items = ul.getElementsByTagName("li");
200     for (var i = 0; i < items.length; ++i) {
201         removeClass(items[i], "active-trail active");
202     }
203    $('#'+ target).addClass('active-trail active');
204 }
205
206 function hasClass(ele,cls) {
207   return !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
208 }
209
210 function addClass(ele,cls) {
211   if (!hasClass(ele,cls)) ele.className += " "+cls;
212 }
213
214 function removeClass(ele,cls) {
215   if (hasClass(ele,cls)) {
216     var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
217     ele.className=ele.className.replace(reg,' ');
218   }
219 }
220