Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / webapp / jsp / hashloading.jsp
1 <%--
2 /*******************************************************************************
3  * Copyright (c) 2012-2013 University of Stuttgart.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and the Apache License 2.0 which both accompany this distribution,
7  * and are available at http://www.eclipse.org/legal/epl-v10.html
8  * and http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Contributors:
11  *    Oliver Kopp - initial API and implementation and/or initial documentation
12  *******************************************************************************/
13 --%>
14 <script>
15         function switchStyledTabSelection(element, onSuccess) {
16                 target = element.attr("href");
17                 target = target.replace('#','');
18                 target += "/";
19                 $(".styledTabMenuButton2ndlevel").removeClass("selected");
20                 element.addClass("selected");
21                 $("#ccontainer").html('<div id="loading" style="display:none;">loading...</div>');
22                 $("#loading").fadeIn(3000);
23                 $("#ccontainer").load(target, function(response, status, xhr) {
24                         if (status == "error") {
25                                 vShowError("Could not load tab content: " + xhr.status + " " + xhr.statusText);
26                         } else {
27                                 if (onSuccess) {
28                                         onSuccess();
29                                 }
30                         }
31                 });
32         }
33
34         /**
35          * @param onSuccess called if subTabs are available and the super tab could be loaded
36          */
37         function doTheTabSelection(onSuccess) {
38                 var hash = window.location.hash;
39
40                 // get rid of ";" additions
41                 var posColon = hash.indexOf(";");
42                 var subTab;
43                 if (posColon >= 0) {
44                         subTab = hash.substr(posColon + 1);
45                         hash = hash.substr(0, posColon);
46                 }
47
48                 if ($.inArray(hash, ${param.validpages}) == -1) {
49                         hash = "${param.defaultpage}";
50                 }
51
52                 var callBack = onSuccess;
53                 if (subTab) {
54                         var posColon = subTab.indexOf(";");
55                         if (posColon > 0) {
56                                 // more information added, for instance xmltree data
57                                 // the first part is the subtab
58                                 subTab = subTab.substr(0, posColon);
59                         }
60                         callBack = function () {
61                                 $('#myTab a[href="#' + subTab + '"]').tab('show');
62                                 if (onSuccess) {
63                                         onSuccess();
64                                 }
65                         };
66                 }
67
68                 switchStyledTabSelection($(".styledTabMenuButton2ndlevel[href='" + hash + "']"), callBack);
69         }
70
71         // Firefox does not fire the "popstate" event at the first load,
72         // but Chrome does
73         // Because of Firefox, we have to call the refresh here
74         doTheTabSelection();
75
76         // see http://blog.mgm-tp.com/2011/10/must-know-url-hashtechniques-for-ajax-applications/ for a broad discussion
77         $(window).on("hashchange", function(e) {
78                 if (internalHashChange) {
79                         // we do nothing
80                         internalHashChange = false;
81                 } else {
82                         // we have to check whether only additional data changed and thus no real change is required
83                         var oldURL = e.originalEvent.oldURL;
84                         var idx = oldURL.indexOf("#");
85                         if (idx != -1) {
86                                 var oldHash = oldURL.substr(idx);
87                                 var newURL = e.originalEvent.newURL;
88                                 // get the hash value only
89                                 idx = newURL.indexOf("#");
90                                 if (idx != -1) {
91                                         var newHash = newURL.substr(idx);
92                                         // search for the first ";"
93                                         idx = oldHash.indexOf(";");
94                                         if (idx != -1) {
95                                                 // search for the second ";"
96                                                 idx = oldHash.indexOf(";", idx+1);
97                                                 if (idx == -1) {
98                                                         // The new hash is only a refinement of the old hash:
99                                                         // Only a new ";" has been added
100                                                         // We do not need to reload
101                                                         return;
102                                                 } else {
103                                                         if (oldHash.substr(0, idx) == newHash.substr(0, idx)) {
104                                                                 // the two hashes equal until the second ";"
105                                                                 // we don't have to do any reload
106                                                                 return;
107                                                         }
108                                                 }
109                                         }
110                                 }
111                         }
112
113                         // switch to the new tab
114                         doTheTabSelection();
115                 }
116         });
117
118         var internalHashChange = false;
119
120         $(document).on('shown.bs.tab', "ul.nav-tabs > li > a", function (e) {
121                 var id = $(e.target).attr("href").substr(1);
122                 var hash = window.location.hash;
123                 var additionalData = "";
124                 var posColon = hash.indexOf(";");
125                 if (posColon >= 0) {
126                         // search for additionalData
127                         secondColon = hash.indexOf(";", posColon+1);
128                         if (secondColon > 0) {
129                                 // include the ";" in the additional data
130                                 additionalData = hash.substr(secondColon);
131                         }
132
133                         // wipe everything after the colon
134                         hash = hash.substr(0, posColon);
135                 }
136                 hash = hash + ";" + id + additionalData;
137                 internalHashChange = true;
138                 window.location.hash = hash;
139         });
140 </script>