esri-loader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (factory((global.esriLoader = global.esriLoader || {})));
  5. }(this, (function (exports) { 'use strict';
  6. /* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
  7. * Apache-2.0 */
  8. var isBrowser = typeof window !== 'undefined';
  9. // allow consuming libraries to provide their own Promise implementations
  10. var utils = {
  11. Promise: isBrowser ? window['Promise'] : undefined
  12. };
  13. /* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
  14. * Apache-2.0 */
  15. var DEFAULT_VERSION = '4.23';
  16. var NEXT = 'next';
  17. function parseVersion(version) {
  18. if (version.toLowerCase() === NEXT) {
  19. return NEXT;
  20. }
  21. var match = version && version.match(/^(\d)\.(\d+)/);
  22. return match && {
  23. major: parseInt(match[1], 10),
  24. minor: parseInt(match[2], 10)
  25. };
  26. }
  27. /**
  28. * Get the CDN url for a given version
  29. *
  30. * @param version Ex: '4.23' or '3.40'. Defaults to the latest 4.x version.
  31. */
  32. function getCdnUrl(version) {
  33. if (version === void 0) { version = DEFAULT_VERSION; }
  34. return "https://js.arcgis.com/" + version + "/";
  35. }
  36. /**
  37. * Get the CDN url for a the CSS for a given version and/or theme
  38. *
  39. * @param version Ex: '4.23', '3.40', or 'next'. Defaults to the latest 4.x version.
  40. */
  41. function getCdnCssUrl(version) {
  42. if (version === void 0) { version = DEFAULT_VERSION; }
  43. var baseUrl = getCdnUrl(version);
  44. var parsedVersion = parseVersion(version);
  45. if (parsedVersion !== NEXT && parsedVersion.major === 3) {
  46. // NOTE: at 3.11 the CSS moved from the /js folder to the root
  47. var path = parsedVersion.minor <= 10 ? 'js/' : '';
  48. return "" + baseUrl + path + "esri/css/esri.css";
  49. }
  50. else {
  51. // assume 4.x
  52. return baseUrl + "esri/themes/light/main.css";
  53. }
  54. }
  55. /* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
  56. * Apache-2.0 */
  57. function createStylesheetLink(href) {
  58. var link = document.createElement('link');
  59. link.rel = 'stylesheet';
  60. link.href = href;
  61. return link;
  62. }
  63. function insertLink(link, before) {
  64. if (before) {
  65. // the link should be inserted before a specific node
  66. var beforeNode = document.querySelector(before);
  67. beforeNode.parentNode.insertBefore(link, beforeNode);
  68. }
  69. else {
  70. // append the link to then end of the head tag
  71. document.head.appendChild(link);
  72. }
  73. }
  74. // check if the css url has been injected or added manually
  75. function getCss(url) {
  76. return document.querySelector("link[href*=\"" + url + "\"]");
  77. }
  78. function getCssUrl(urlOrVersion) {
  79. return !urlOrVersion || parseVersion(urlOrVersion)
  80. // if it's a valid version string return the CDN URL
  81. ? getCdnCssUrl(urlOrVersion)
  82. // otherwise assume it's a URL and return that
  83. : urlOrVersion;
  84. }
  85. // lazy load the CSS needed for the ArcGIS API
  86. function loadCss(urlOrVersion, before) {
  87. var url = getCssUrl(urlOrVersion);
  88. var link = getCss(url);
  89. if (!link) {
  90. // create & load the css link
  91. link = createStylesheetLink(url);
  92. insertLink(link, before);
  93. }
  94. return link;
  95. }
  96. /* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
  97. * Apache-2.0 */
  98. var defaultOptions = {};
  99. function createScript(url) {
  100. var script = document.createElement('script');
  101. script.type = 'text/javascript';
  102. script.src = url;
  103. script.setAttribute('data-esri-loader', 'loading');
  104. return script;
  105. }
  106. // add a one-time load handler to script
  107. // and optionally add a one time error handler as well
  108. function handleScriptLoad(script, callback, errback) {
  109. var onScriptError;
  110. if (errback) {
  111. // set up an error handler as well
  112. onScriptError = handleScriptError(script, errback);
  113. }
  114. var onScriptLoad = function () {
  115. // pass the script to the callback
  116. callback(script);
  117. // remove this event listener
  118. script.removeEventListener('load', onScriptLoad, false);
  119. if (onScriptError) {
  120. // remove the error listener as well
  121. script.removeEventListener('error', onScriptError, false);
  122. }
  123. };
  124. script.addEventListener('load', onScriptLoad, false);
  125. }
  126. // add a one-time error handler to the script
  127. function handleScriptError(script, callback) {
  128. var onScriptError = function (e) {
  129. // reject the promise and remove this event listener
  130. callback(e.error || new Error("There was an error attempting to load " + script.src));
  131. // remove this event listener
  132. script.removeEventListener('error', onScriptError, false);
  133. };
  134. script.addEventListener('error', onScriptError, false);
  135. return onScriptError;
  136. }
  137. // allow the user to configure default script options rather than passing options to `loadModules` each time
  138. function setDefaultOptions(options) {
  139. if (options === void 0) { options = {}; }
  140. defaultOptions = options;
  141. }
  142. // get the script injected by this library
  143. function getScript() {
  144. return document.querySelector('script[data-esri-loader]');
  145. }
  146. // has ArcGIS API been loaded on the page yet?
  147. function isLoaded() {
  148. var globalRequire = window['require'];
  149. // .on() ensures that it's Dojo's AMD loader
  150. return globalRequire && globalRequire.on;
  151. }
  152. // load the ArcGIS API on the page
  153. function loadScript(options) {
  154. if (options === void 0) { options = {}; }
  155. // we would have liked to use spread like { ...defaultOptions, ...options }
  156. // but TS would inject a polyfill that would require use to configure rollup w content: 'window'
  157. // if we have another occasion to use spread, let's do that and replace this for...in
  158. var opts = {};
  159. [defaultOptions, options].forEach(function (obj) {
  160. for (var prop in obj) {
  161. if (Object.prototype.hasOwnProperty.call(obj, prop)) {
  162. opts[prop] = obj[prop];
  163. }
  164. }
  165. });
  166. // URL to load
  167. var version = opts.version;
  168. var url = opts.url || getCdnUrl(version);
  169. return new utils.Promise(function (resolve, reject) {
  170. var script = getScript();
  171. if (script) {
  172. // the API is already loaded or in the process of loading...
  173. // NOTE: have to test against scr attribute value, not script.src
  174. // b/c the latter will return the full url for relative paths
  175. var src = script.getAttribute('src');
  176. if (src !== url) {
  177. // potentially trying to load a different version of the API
  178. reject(new Error("The ArcGIS API for JavaScript is already loaded (" + src + ")."));
  179. }
  180. else {
  181. if (isLoaded()) {
  182. // the script has already successfully loaded
  183. resolve(script);
  184. }
  185. else {
  186. // wait for the script to load and then resolve
  187. handleScriptLoad(script, resolve, reject);
  188. }
  189. }
  190. }
  191. else {
  192. if (isLoaded()) {
  193. // the API has been loaded by some other means
  194. // potentially trying to load a different version of the API
  195. reject(new Error("The ArcGIS API for JavaScript is already loaded."));
  196. }
  197. else {
  198. // this is the first time attempting to load the API
  199. var css = opts.css;
  200. if (css) {
  201. var useVersion = css === true;
  202. // load the css before loading the script
  203. loadCss(useVersion ? version : css, opts.insertCssBefore);
  204. }
  205. // create a script object whose source points to the API
  206. script = createScript(url);
  207. // _currentUrl = url;
  208. // once the script is loaded...
  209. handleScriptLoad(script, function () {
  210. // update the status of the script
  211. script.setAttribute('data-esri-loader', 'loaded');
  212. // return the script
  213. resolve(script);
  214. }, reject);
  215. // load the script
  216. document.body.appendChild(script);
  217. }
  218. }
  219. });
  220. }
  221. /* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
  222. * Apache-2.0 */
  223. // wrap Dojo's require() in a promise
  224. function requireModules(modules) {
  225. return new utils.Promise(function (resolve, reject) {
  226. // If something goes wrong loading the esri/dojo scripts, reject with the error.
  227. var errorHandler = window['require'].on('error', reject);
  228. window['require'](modules, function () {
  229. var args = [];
  230. for (var _i = 0; _i < arguments.length; _i++) {
  231. args[_i] = arguments[_i];
  232. }
  233. // remove error handler
  234. errorHandler.remove();
  235. // Resolve with the parameters from dojo require as an array.
  236. resolve(args);
  237. });
  238. });
  239. }
  240. // returns a promise that resolves with an array of the required modules
  241. // also will attempt to lazy load the ArcGIS API if it has not already been loaded
  242. function loadModules(modules, loadScriptOptions) {
  243. if (loadScriptOptions === void 0) { loadScriptOptions = {}; }
  244. if (!isLoaded()) {
  245. // script is not yet loaded, is it in the process of loading?
  246. var script = getScript();
  247. var src = script && script.getAttribute('src');
  248. if (!loadScriptOptions.url && src) {
  249. // script is still loading and user did not specify a URL
  250. // in this case we want to default to the URL that's being loaded
  251. // instead of defaulting to the latest 4.x URL
  252. loadScriptOptions.url = src;
  253. }
  254. // attempt to load the script then load the modules
  255. return loadScript(loadScriptOptions).then(function () { return requireModules(modules); });
  256. }
  257. else {
  258. // script is already loaded, just load the modules
  259. return requireModules(modules);
  260. }
  261. }
  262. /*
  263. Copyright (c) 2017 Esri
  264. Licensed under the Apache License, Version 2.0 (the "License");
  265. you may not use this file except in compliance with the License.
  266. You may obtain a copy of the License at
  267. http://www.apache.org/licenses/LICENSE-2.0
  268. Unless required by applicable law or agreed to in writing, software
  269. distributed under the License is distributed on an "AS IS" BASIS,
  270. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  271. See the License for the specific language governing permissions and
  272. limitations under the License.
  273. */
  274. // re-export the functions that are part of the public API
  275. exports.utils = utils;
  276. exports.loadModules = loadModules;
  277. exports.getScript = getScript;
  278. exports.isLoaded = isLoaded;
  279. exports.loadScript = loadScript;
  280. exports.setDefaultOptions = setDefaultOptions;
  281. exports.loadCss = loadCss;
  282. Object.defineProperty(exports, '__esModule', { value: true });
  283. })));
  284. //# sourceMappingURL=esri-loader.js.map