env.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Copyright © 2014 - 2018 Esri. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. ///////////////////////////////////////////////////////////////////////////
  16. //****************************************************************
  17. //The main function of the env.js is to initialize apiUrl and path
  18. //****************************************************************
  19. /* jshint unused: false */
  20. /********************************
  21. Global variables:
  22. isXT: XT builder or not. This variable is set by builder node server.
  23. isBuilder: is builder or app. This flag is used to distinguish the build and app in jimu.js
  24. isRunInPortal: whether the app/builder is in portal or not.
  25. builderNls: the builder nls bundle
  26. portalUrl: the portal url
  27. configWindow: in builder only, the config app window object
  28. previewWindow: in builder only, the preview app window object
  29. setConfigViewerTopic: in builder only, used to communicate between builder and config window
  30. setPreviewViewerTopic: in builder only, used to communicate between builder and preview window
  31. appId: in builder only, the current opened app id.
  32. apiUrl: the URL of the ArcGIS API for JavaScript
  33. allCookies: all cookies
  34. path: the builder/app path section in window.location
  35. appPath: the app's path. In app, it's the same with path; In builder, it's not.
  36. queryObject: the query parameters
  37. jimuNls: the jimu nls bundle
  38. isRTL: the language is right to left,
  39. wabVersion: the wab version, like 1.1, 1.2
  40. productVersion: the product version, like portal10.3, online3.5, developer edition1.0
  41. dojoConfig: the dojo config object
  42. jimuConfig: defined in jimu/main
  43. weinreUrl: for mobile debug
  44. debug: boolean. If it's debug mode, the app will load weinre file
  45. Global functions:
  46. loadResource: load JS or CSS
  47. loadResources: load array of JS or CSS
  48. testLoad: load JS/CSS by condition
  49. loadingCallback: the resources loaded callback
  50. *******************************/
  51. /*global testLoad, ActiveXObject */
  52. var
  53. // the URL of the ArcGIS API for JavaScript, you can change it to point to your own API.
  54. apiUrl = null,
  55. //weinreUrl: String
  56. // weinre is a tool which can help debug the app on mobile devices.
  57. // Please see: http://people.apache.org/~pmuellr/weinre/docs/latest/Home.html
  58. weinreUrl = '//launch.chn.esri.com:8081/target/target-script-min.js',
  59. //debug: Boolean
  60. // If it's debug mode, the app will load weinre file
  61. debug = false,
  62. path = null,
  63. isXT = false,
  64. allCookies,
  65. //This version number will be appended to URL to avoid cache.
  66. //The reason we do not use wabVersion is to avoid force user to change wabVersion when they are customizing app.
  67. deployVersion = '2.11';
  68. // console.time('before map');
  69. /////Builder server will remove this line's comment, so set isXT flag to true
  70. //isXT = true;
  71. /////////////////////////////////////
  72. (function(global){
  73. //init API URL
  74. var queryObject = getQueryObject();
  75. var apiVersion = '4.11';
  76. ////////uncomment the following line when downloading the app
  77. apiUrl = 'https://js.arcgis.com/4.11';
  78. //////////////////////////////////////////////////////////////
  79. allCookies = getAllCookies();
  80. window.appInfo = {isRunInPortal: !isXT};
  81. if (queryObject.apiurl3d) {
  82. if(!checkApiUrl(queryObject.apiurl3d)){
  83. console.error('?apiurl must point to an ULR that is in the app or in esri.com/arcgis.com domain.');
  84. return;
  85. }
  86. apiUrl = queryObject.apiurl3d;
  87. }
  88. if (!apiUrl) {
  89. if (isXT) {
  90. apiUrl = 'https://js.arcgis.com/' + apiVersion;
  91. } else {
  92. var portalUrl = getPortalUrlFromLocation();
  93. if (portalUrl.indexOf('arcgis.com') > -1) {
  94. if(portalUrl.indexOf('devext.arcgis.com') > -1){
  95. apiUrl = '//jsdev.arcgis.com/' + apiVersion;
  96. }else if(portalUrl.indexOf('qa.arcgis.com') > -1){
  97. apiUrl = '//jsqa.arcgis.com/' + apiVersion;
  98. }else{
  99. apiUrl = '//js.arcgis.com/' + apiVersion;
  100. }
  101. // apiUrl = 'https://js.arcgis.com/' + apiVersion;
  102. } else {
  103. apiUrl = portalUrl + 'jsapi/jsapi4/';
  104. }
  105. }
  106. }
  107. if (apiUrl.substr(apiUrl.length - 1, apiUrl.length) !== '/') {
  108. apiUrl = apiUrl + '/';
  109. }
  110. path = getPath();
  111. function getAllCookies(){
  112. var strAllCookie = document.cookie;
  113. var cookies = {};
  114. if (strAllCookie) {
  115. var strCookies = strAllCookie.split(';');
  116. for(var i = 0; i < strCookies.length; i++){
  117. var splits = strCookies[i].split('=');
  118. if(splits && splits.length > 1){
  119. cookies[splits[0].replace(/^\s+|\s+$/gm, '')] = splits[1];
  120. }
  121. }
  122. }
  123. return cookies;
  124. }
  125. function checkApiUrl(url){
  126. if(/^\/\//.test(url) || /^https?:\/\//.test(url)){
  127. return /(?:[\w\-\_]+\.)+(?:esri|arcgis)\.com/.test(url); //api url must be in esri.com or arcgis.com
  128. }else{
  129. return true;
  130. }
  131. }
  132. function getPortalUrlFromLocation(){
  133. var portalUrl = getPortalServerFromLocation() + getDeployContextFromLocation();
  134. return portalUrl;
  135. }
  136. function getPortalServerFromLocation(){
  137. var server = window.location.protocol + '//' + window.location.host;
  138. return server;
  139. }
  140. function getDeployContextFromLocation (){
  141. var keyIndex = window.location.href.indexOf("/home");
  142. if(keyIndex < 0){
  143. keyIndex = window.location.href.indexOf("/apps");
  144. }
  145. var context = window.location.href.substring(window.location.href.indexOf(
  146. window.location.host) + window.location.host.length + 1, keyIndex);
  147. if (context !== "/") {
  148. context = "/" + context + "/";
  149. }
  150. return context;
  151. }
  152. function getPath() {
  153. var fullPath, path;
  154. fullPath = window.location.pathname;
  155. if (fullPath === "/GISBIM/map") {
  156. fullPath = "/GISBIM/";
  157. }
  158. if (fullPath === '/' || fullPath.substr(fullPath.length - 1) === '/') {
  159. path = fullPath;
  160. }else{
  161. var sections = fullPath.split('/');
  162. var lastSection = sections.pop();
  163. if (/\.html$/.test(lastSection) || /\.aspx$/.test(lastSection) ||
  164. /\.jsp$/.test(lastSection) || /\.php$/.test(lastSection)) {
  165. //index.html may be renamed to index.jsp, etc.
  166. path = sections.join('/') + '/';
  167. } else {
  168. return false;
  169. }
  170. }
  171. return path;
  172. }
  173. function getQueryObject(){
  174. var query = window.location.search;
  175. if (query.indexOf('?') > -1) {
  176. query = query.substr(1);
  177. }
  178. var pairs = query.split('&');
  179. var queryObject = {};
  180. for(var i = 0; i < pairs.length; i++){
  181. var splits = decodeURIComponent(pairs[i]).split('=');
  182. queryObject[splits[0]] = splits[1];
  183. }
  184. return queryObject;
  185. }
  186. function _loadPolyfills(prePath, cb) {
  187. prePath = prePath || "";
  188. var ap = Array.prototype,
  189. fp = Function.prototype,
  190. sp = String.prototype,
  191. loaded = 0,
  192. completeCb = function() {
  193. loaded++;
  194. if (loaded === tests.length) {
  195. cb();
  196. }
  197. },
  198. tests = [{
  199. test: window.console,
  200. failure: prePath + "libs/polyfills/console.js",
  201. callback: completeCb
  202. }, {
  203. test: ap.indexOf && ap.lastIndexOf && ap.forEach && ap.every && ap.some &&
  204. ap.filter && ap.map && ap.reduce && ap.reduceRight,
  205. failure: prePath + "libs/polyfills/array.generics.js",
  206. callback: completeCb
  207. }, {
  208. test: fp.bind,
  209. failure: prePath + "libs/polyfills/bind.js",
  210. callback: completeCb
  211. }, {
  212. test: Date.now,
  213. failure: prePath + "libs/polyfills/now.js",
  214. callback: completeCb
  215. }, {
  216. test: sp.trim,
  217. failure: prePath + "libs/polyfills/trim.js",
  218. callback: completeCb
  219. }, {
  220. test: false,
  221. failure: prePath + "libs/polyfills/FileSaver.js",
  222. callback: completeCb
  223. }, {
  224. test: typeof Blob !== 'undefined',
  225. failure: prePath + "libs/polyfills/FileSaver.ie9.js",
  226. callback: completeCb
  227. }];
  228. for(var i = 0; i < tests.length; i++){
  229. testLoad(tests[i]);
  230. }
  231. }
  232. function localeIsSame(locale1, locale2){
  233. return locale1.split('-')[0] === locale2.split('-')[0];
  234. }
  235. function _setRTL(locale){
  236. var rtlLocales = ["ar", "he"];
  237. var dirNode = document.getElementsByTagName("html")[0];
  238. var isRTLLocale = false;
  239. for (var i = 0; i < rtlLocales.length; i++) {
  240. if (localeIsSame(rtlLocales[i], locale)) {
  241. isRTLLocale = true;
  242. }
  243. }
  244. if (isRTLLocale) {
  245. dirNode.setAttribute("dir", "rtl");
  246. dirNode.className += " esriRtl jimu-rtl";
  247. dirNode.className += " " + locale + " " +
  248. (locale.indexOf("-") !== -1 ? locale.split("-")[0] : "");
  249. }else {
  250. dirNode.setAttribute("dir", "ltr");
  251. dirNode.className += " esriLtr jimu-ltr";
  252. dirNode.className += " " + locale + " " +
  253. (locale.indexOf("-") !== -1 ? locale.split("-")[0] : "");
  254. }
  255. window.isRTL = isRTLLocale;
  256. }
  257. global._loadPolyfills = _loadPolyfills;
  258. global.queryObject = queryObject;
  259. global._setRTL = _setRTL;
  260. })(window);