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 14KB

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