You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vaadinBootstrap.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. (function() {
  2. var apps = {};
  3. var themesLoaded = {};
  4. var widgetsets = {};
  5. var log;
  6. if (typeof console === "undefined" || !window.location.search.match(/[&?]debug(&|$)/)) {
  7. //If no console.log present, just use a no-op
  8. log = function() {};
  9. } else if (typeof console.log === "function") {
  10. //If it's a function, use it with apply
  11. log = function() {
  12. console.log.apply(console, arguments);
  13. };
  14. } else {
  15. //In IE, its a native function for which apply is not defined, but it works without a proper 'this' reference
  16. log = console.log;
  17. }
  18. var loadTheme = function(url, version) {
  19. if(!themesLoaded[url]) {
  20. log("loadTheme", url, version);
  21. var href = url + '/styles.css';
  22. if (version) {
  23. href += '?v=' + version;
  24. }
  25. var stylesheet = document.createElement('link');
  26. stylesheet.setAttribute('rel', 'stylesheet');
  27. stylesheet.setAttribute('type', 'text/css');
  28. stylesheet.setAttribute('href', href);
  29. document.getElementsByTagName('head')[0].appendChild(stylesheet);
  30. themesLoaded[url] = true;
  31. }
  32. };
  33. var isWidgetsetLoaded = function(widgetset) {
  34. var className = widgetset.replace(/\./g, "_");
  35. return (typeof window[className]) != "undefined";
  36. };
  37. var loadWidgetset = function(url, widgetset) {
  38. if (widgetsets[widgetset]) {
  39. return;
  40. }
  41. log("load widgetset", url, widgetset);
  42. setTimeout(function() {
  43. if (!isWidgetsetLoaded(widgetset)) {
  44. alert("Failed to load the widgetset: " + url);
  45. }
  46. }, 15000);
  47. var scriptTag = document.createElement('script');
  48. scriptTag.setAttribute('type', 'text/javascript');
  49. scriptTag.setAttribute('src', url);
  50. document.getElementsByTagName('head')[0].appendChild(scriptTag);
  51. widgetsets[widgetset] = {
  52. pendingApps: []
  53. };
  54. };
  55. var isInitializedInDom = function(appId) {
  56. var appDiv = document.getElementById(appId);
  57. if (!appDiv) {
  58. return false;
  59. }
  60. for ( var i = 0; i < appDiv.childElementCount; i++) {
  61. var className = appDiv.childNodes[i].className;
  62. // If the app div contains a child with the class
  63. // "v-app-loading" we have only received the HTML
  64. // but not yet started the widget set
  65. // (UIConnector removes the v-app-loading div).
  66. if (className && className.indexOf("v-app-loading") != -1) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. };
  72. window.vaadin = window.vaadin || {
  73. initApplication: function(appId, config) {
  74. var testbenchId = appId.replace(/-\d+$/, '');
  75. if (apps[appId]) {
  76. if (window.vaadin && window.vaadin.clients && window.vaadin.clients[testbenchId] && window.vaadin.clients[testbenchId].initializing) {
  77. throw "Application " + appId + " is already being initialized";
  78. }
  79. if (isInitializedInDom(appId)) {
  80. throw "Application " + appId + " already initialized";
  81. }
  82. }
  83. log("init application", appId, config);
  84. window.vaadin.clients[testbenchId] = {
  85. isActive: function() {
  86. return true;
  87. },
  88. initializing: true
  89. };
  90. var getConfig = function(name) {
  91. var value = config[name];
  92. return value;
  93. };
  94. var fetchRootConfig = function(callback) {
  95. log('Fetching root config');
  96. var url = getConfig('browserDetailsUrl');
  97. if (!url) {
  98. // No special url defined, use the same URL that loaded this page (without the fragment)
  99. url = window.location.href.replace(/#.*/,'');
  100. }
  101. // Timestamp to avoid caching
  102. url += ((/\?/).test(url) ? "&" : "?") + "v-" + (new Date()).getTime();
  103. var params = "v-browserDetails=1";
  104. var rootId = getConfig("v-rootId");
  105. if (rootId !== undefined) {
  106. params += "&v-rootId=" + rootId;
  107. }
  108. // Tell the UI what theme it is configured to use
  109. var theme = getConfig('theme');
  110. if (theme !== undefined) {
  111. params += '&theme=' + encodeURIComponent(theme);
  112. }
  113. params += "&v-appId=" + appId;
  114. var extraParams = getConfig('extraParams')
  115. if (extraParams !== undefined) {
  116. params += extraParams;
  117. }
  118. params += '&' + vaadin.getBrowserDetailsParameters(appId, getConfig('sendUrlsAsParameters'));
  119. var r;
  120. try {
  121. r = new XMLHttpRequest();
  122. } catch (e) {
  123. r = new ActiveXObject("MSXML2.XMLHTTP.3.0");
  124. }
  125. r.open('POST', url, true);
  126. r.onreadystatechange = function (aEvt) {
  127. if (r.readyState == 4) {
  128. // Save responseStatus so as Offline Applications know what happened
  129. // when loading root configuration from server, and depending on the
  130. // error status display an error message or the offline UI.
  131. config.rootResponseStatus = r.status;
  132. config.rootResponseText = r.responseText;
  133. var text = r.responseText;
  134. if (r.status == 200){
  135. log("Got root config response", text);
  136. var updatedConfig = JSON.parse(text);
  137. // Copy new properties to the config object
  138. for (var property in updatedConfig) {
  139. if (updatedConfig.hasOwnProperty(property)) {
  140. config[property] = updatedConfig[property];
  141. }
  142. }
  143. // Try bootstrapping again, this time without fetching missing info
  144. bootstrapApp(false);
  145. } else {
  146. log('Error', r.statusText, text);
  147. //Let TB waitForVaadin work again
  148. delete window.vaadin.clients[testbenchId];
  149. // Show the error in the app's div
  150. var appDiv = document.getElementById(appId);
  151. appDiv.innerHTML = text;
  152. appDiv.style['overflow'] = 'auto';
  153. }
  154. // Run the fetchRootConfig callback if present.
  155. callback && callback(r);
  156. }
  157. };
  158. // send parameters as POST data
  159. r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  160. r.send(params);
  161. log('sending request to ', url);
  162. };
  163. //Export public data
  164. var app = {
  165. getConfig: getConfig,
  166. // Used when the app was started in offline, so as it is possible
  167. // to defer root configuration loading until network is available.
  168. fetchRootConfig: fetchRootConfig
  169. };
  170. apps[appId] = app;
  171. if (!window.name) {
  172. window.name = appId + '-' + Math.random();
  173. }
  174. var bootstrapApp = function(mayDefer) {
  175. var vaadinDir = getConfig('vaadinDir');
  176. var versionInfo = getConfig('versionInfo');
  177. var themeUri = vaadinDir + 'themes/' + getConfig('theme');
  178. loadTheme(themeUri, versionInfo && versionInfo['vaadinVersion']);
  179. var widgetset = getConfig('widgetset');
  180. var widgetsetUrl = getConfig('widgetsetUrl');
  181. if (!widgetsetUrl) {
  182. widgetsetUrl = vaadinDir + 'widgetsets/' + widgetset + "/" + widgetset + ".nocache.js?" + new Date().getTime();
  183. }
  184. loadWidgetset(widgetsetUrl, widgetset);
  185. if (getConfig('uidl') === undefined) {
  186. if (mayDefer) {
  187. fetchRootConfig();
  188. } else {
  189. throw "May not defer bootstrap any more";
  190. }
  191. } else {
  192. if (widgetsets[widgetset].callback) {
  193. log("Starting from bootstrap", appId);
  194. widgetsets[widgetset].callback(appId);
  195. } else {
  196. log("Setting pending startup", appId);
  197. widgetsets[widgetset].pendingApps.push(appId);
  198. }
  199. }
  200. };
  201. bootstrapApp(true);
  202. if (getConfig("debug")) {
  203. // TODO debug state is now global for the entire page, but should somehow only be set for the current application
  204. window.vaadin.debug = true;
  205. }
  206. return app;
  207. },
  208. clients: {},
  209. getAppIds: function() {
  210. var ids = [ ];
  211. for (var id in apps) {
  212. if (apps.hasOwnProperty(id)) {
  213. ids.push(id);
  214. }
  215. }
  216. return ids;
  217. },
  218. getApp: function(appId) {
  219. return apps[appId];
  220. },
  221. loadTheme: loadTheme,
  222. registerWidgetset: function(widgetset, callback) {
  223. log("Widgetset registered", widgetset);
  224. var ws = widgetsets[widgetset];
  225. if (ws && ws.pendingApps) {
  226. ws.callback = callback;
  227. for(var i = 0; i < ws.pendingApps.length; i++) {
  228. var appId = ws.pendingApps[i];
  229. log("Starting from register widgetset", appId);
  230. callback(appId);
  231. }
  232. ws.pendingApps = null;
  233. }
  234. },
  235. getBrowserDetailsParameters: function(parentElementId, sendUrlsAsParameters) {
  236. // Screen height and width
  237. var params = 'v-sh=' + window.screen.height;
  238. params += '&v-sw=' + window.screen.width;
  239. // Window height and width
  240. var cw = 0;
  241. var ch = 0;
  242. if(typeof(window.innerWidth) == 'number') {
  243. // Modern browsers
  244. cw = window.innerWidth;
  245. ch = window.innerHeight;
  246. } else {
  247. // IE 8
  248. cw = document.documentElement.clientWidth;
  249. ch = document.documentElement.clientHeight;
  250. }
  251. params += '&v-cw=' + cw + '&v-ch=' + ch;
  252. var d = new Date();
  253. params += '&v-curdate=' + d.getTime();
  254. var tzo1 = d.getTimezoneOffset(); // current offset
  255. var dstDiff = 0;
  256. var rtzo = tzo1;
  257. for (var m=12;m>0;m--) {
  258. d.setUTCMonth(m);
  259. var tzo2 = d.getTimezoneOffset();
  260. if (tzo1 != tzo2) {
  261. dstDiff = (tzo1 > tzo2 ? tzo1-tzo2 : tzo2-tzo1); // offset w/o DST
  262. rtzo = (tzo1 > tzo2 ? tzo1 : tzo2); // offset w/o DST
  263. break;
  264. }
  265. }
  266. // Time zone offset
  267. params += '&v-tzo=' + tzo1;
  268. // DST difference
  269. params += '&v-dstd=' + dstDiff;
  270. // Raw time zone offset
  271. params += '&v-rtzo=' + rtzo;
  272. // DST in effect?
  273. params += '&v-dston=' + (tzo1 != rtzo);
  274. var pe = document.getElementById(parentElementId);
  275. if (pe) {
  276. params += '&v-vw=' + pe.offsetWidth;
  277. params += '&v-vh=' + pe.offsetHeight;
  278. }
  279. // Location
  280. if (sendUrlsAsParameters !== false) {
  281. params += '&v-loc=' + encodeURIComponent(location.href);
  282. }
  283. // Window name
  284. if (window.name) {
  285. params += '&v-wn=' + encodeURIComponent(window.name);
  286. }
  287. // Detect touch device support
  288. var supportsTouch = false;
  289. try {
  290. document.createEvent("TouchEvent");
  291. supportsTouch = true;
  292. } catch (e) {
  293. // Chrome and IE10 touch detection
  294. supportsTouch = 'ontouchstart' in window
  295. || navigator.msMaxTouchPoints;
  296. }
  297. if (supportsTouch) {
  298. params += "&v-td=1";
  299. }
  300. return params;
  301. }
  302. };
  303. log('Vaadin bootstrap loaded');
  304. })();